-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb2musicxml.js
541 lines (489 loc) · 20.1 KB
/
sb2musicxml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
sb2musicxml.js Convert scratch project to MusicXML for singing voice synthesis
MIT License
Copyright (c) 2018 Hiroaki Kawashima
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const divisions = 24;
const mapNoteDuration = {
'4' : divisions * 4,
'3' : divisions * 3,
'2' : divisions * 2,
'1+1\/2' : divisions * 3 / 2,
'1' : divisions,
'23\/24' : divisions * 23 / 24,
'11\/12' : divisions * 11 / 12,
'7\/8' : divisions * 7 / 8,
'5\/6' : divisions * 5 / 6,
'3\/4' : divisions * 3 / 4,
'2\/3' : divisions * 2 / 3,
'1\/2' : divisions / 2,
'3\/8' : divisions * 3 / 8,
'1\/3' : divisions / 3,
'1\/4' : divisions / 4,
'1\/6' : divisions / 6,
'1\/8' : divisions / 8,
'1\/12' : divisions / 12,
'1\/24' : divisions / 24
}
const chromaticStep = ['C', 'C', 'D', 'D', 'E', 'F', 'F', 'G', 'G', 'A', 'A', 'B'];
const chromaticAlter = [0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0];
const resultSuffix = 'song';
const restSymbolForLyric = '_'; // Symbol used for 'rest (pause)' in lyric
const mouth2soundMapJP = {
a: ['あ', 'か', 'さ', 'た', 'な', 'は', 'や', 'ら', 'が', 'ざ', 'だ', 'ゃ', 'ぁ'],
ma: ['ま', 'ば', 'ぱ'],
ua: ['わ'],
i: ['い', 'き', 'し', 'ち', 'に', 'ひ', 'り', 'ぎ', 'じ', 'ぢ', 'ぃ'],
mi: ['み', 'び', 'ぴ'],
u: ['う', 'く', 'す', 'つ', 'ぬ', 'ふ', 'ゆ', 'る', 'ぐ', 'ず', 'づ', 'ゅ', 'ぅ'],
mu: ['む', 'ぶ', 'ぷ'],
e: ['え', 'け', 'せ', 'て', 'ね', 'へ', 'れ', 'げ', 'ぜ', 'で', 'ぇ'],
me: ['め', 'べ', 'ぺ'],
o: ['お', 'こ', 'そ', 'と', 'の', 'ほ', 'よ', 'ろ', 'ご', 'ぞ', 'ど', 'ょ', 'ぉ'],
mo: ['も', 'ぼ', 'ぽ'],
uo: ['を'],
n: ['ん']
};
// Manage mapping from lyric to mouth shape symbol
// Constructor (arg: mouth2sound map)
Lyric2Mouth = function (m2sMap) {
this.mouth2sound = m2sMap;
// Create sound2mouth mapping
this.sound2mouth = {};
this.sound2mouth[restSymbolForLyric] = restSymbolForLyric;
Object.keys(this.mouth2sound).forEach(function (key) {
this.mouth2sound[key].forEach(function (val) {
this.sound2mouth[val] = key;
}.bind(this)); // or use var _this = this;
}.bind(this));
this.prevMouthChar = restSymbolForLyric;
}
// Convert lyric to mouth shape symbol
Lyric2Mouth.prototype.convert = function (lyric) {
var mouthChar = '';
var tmpArray = [];
lyric.split('').forEach(function (mchar) {
if (mchar == 'ー' || mchar == 'っ') {
// if prev is consonant + vowel (such as 'ma'), extract only the vowel ('a')
mouthChar = prevMouthChar.split('').slice(-1)[0];
} else {
if (this.sound2mouth[mchar] === undefined) {
mouthChar = restSymbolForLyric;
} else {
mouthChar = this.sound2mouth[mchar];
}
}
if (mchar == 'ゃ' || mchar == 'ゅ' || mchar == 'ょ'
|| mchar == 'ぁ' || mchar =='ぃ' || mchar == 'ぅ' || mchar == 'ぇ' || mchar == 'ぉ') {
// console.log('overwrite ' + tmpArray.slice(-1)[0]); // this is only a view
tmpArray[tmpArray.length - 1] = mouthChar; // overwrite the previousMouthChar
} else {
tmpArray.push(mouthChar);
}
prevMouthChar = mouthChar;
}.bind(this));
return tmpArray.join('-');
}
// -----------------------------------------------------------------------
function handleFileSelect(evt) {
var files = evt.target.files;
var f = files[0];
filenameElm = document.getElementById('filename');
if (filenameElm != null) {
if (f == null) {
filenameElm.value = 'File is not selected';
return;
} else {
filenameElm.value = f.name;
}
}
JSZip.loadAsync(f) // Load zip file and extract json file
.then(function (loadfile) {
console.log('Loaded ZIP file. Extract JSON...');
return loadfile.file('project.json').async('string');
}).then(function (json) {
console.log('Loaded JSON. Extract scratch script...');
var obj = JSON.parse(json);
scriptArray = extractScratchScript(obj); // Find script part
if (scriptArray.length > 1) {
return convertSongScript2XML(scriptArray); // Convert to MusicXML
} else if (scriptArray.length == 0) {
throw new Error('Cannot find valid sprite. Sprite name needs to be "song".');
} else {
throw new Error('Cannot find valid sprite. Sprite needs to start with "when Green Flag clicked" followed by song data.');
}
}).then(function (xml) { // Convert xml object and output results
// Show the result area
var resultElm = document.getElementById('result');
if (resultElm == null) {
throw new Error('Cannot find div#result');
}
resultElm.style.display = 'block';
// Prepare xml file for download
var xmlString = (new XMLSerializer()).serializeToString(xml);
console.log(xmlString); // for debug
var dlXmlElm = document.getElementById('dl-xml');
if (dlXmlElm == null) {
throw new Error('Cannot find a#dl');
}
createXMLDownloadLink(dlXmlElm, xmlString, resultSuffix + '.xml');
// Prepare text files for download (timing, lyrics, mouth shape)
var typeList = ['timing', 'lyric', 'mouth'];
var dlElm = {};
var retJson = {};
for (var i in typeList) {
type = typeList[i];
dlElm[type] = document.getElementById('dl-' + type);
if (dlElm[type] != null) {
console.log('list type: ' + type);
if (Object.keys(retJson).length==0) { // create a list when required first time
retJson = convertXML2TimingList(xml);
console.log(retJson);
}
createTextDownloadLink(dlElm[type], retJson[type].join('\n'), resultSuffix + '-' + type + '.txt');
}
}
}).catch(function (error) {
alert(error);
});
}
//-----------------------------------------------------------------
// Create download link for XML
function createXMLDownloadLink(dlXmlElm, xmlString, resultFilename) {
if (window.navigator && window.navigator.msSaveBlob) { // for IE and Edge
dlXmlElm.addEventListener('click', function(e) {
e.preventDefault();
navigator.msSaveBlob( new Blob(['<?xml version="1.0" encoding="UTF-8"?>' + xmlString], {type:'text/xml'}), resultFilename );
}, false);
} else {
dlXmlElm.href = 'data:text/xml;charset=utf-8,' + encodeURIComponent(xmlString);
dlXmlElm.download = resultFilename;
}
}
// Create download link for text
function createTextDownloadLink(dlTextElm, textString, resultFilename) {
if (window.navigator && window.navigator.msSaveBlob) { // for IE and Edge
dlTextElm.addEventListener('click', function(e) {
e.preventDefault();
navigator.msSaveBlob( new Blob([textString], {type:'text/plain'}), resultFilename );
}, false);
} else {
dlTextElm.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(textString);
dlTextElm.download = resultFilename;
}
}
// Extract scratch script array
function extractScratchScript(obj) {
var scriptArray = [];
// for (var child of obj.children) { // cannot use for-of in IE
for (var i in obj.children) { //
child = obj.children[i];
if (child.objName === undefined || child.objName !== 'song') {
continue;
}
console.log(child.objName);
scriptArray = ['invalid song data']; // invalid array data with length 1
// for (var script of child.scripts) { // cannot use for-of in IE
for (var j in child.scripts) {
script = child.scripts[j];
if (script[2][0][0] !== 'whenGreenFlag') {
continue;
}
console.log(script[2][0][0]);
scriptArray = script[2];
}
}
return scriptArray;
}
// convert song data to xml
function convertSongScript2XML(scriptArray) {
var xmlSource = '<?xml version="1.0" encoding="UTF-8"?>'
+ '<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN"'
+ ' "http://www.musicxml.org/dtds/partwise.dtd">'
+ '<score-partwise>'
+ ' <identification>'
+ ' <encoding>'
+ ' <software>Scratch - sb2musicxml</software>'
+ ' <encoding-date></encoding-date>'
+ ' </encoding>'
+ ' </identification>'
+ ' <part-list>'
+ ' <score-part id="P1">'
+ ' <part-name>MusicXML Part</part-name>'
+ ' </score-part>'
+ ' </part-list>'
+ ' <part id="P1">'
+ ' <measure number="1">'
+ ' <attributes>'
+ ' <divisions></divisions>'
+ ' <time>'
+ ' <beats></beats>'
+ ' <beat-type></beat-type>'
+ ' </time>'
+ ' </attributes>'
+ ' <sound/>'
+ ' </measure>'
+ ' </part>'
+ '</score-partwise>'
var xml = (new DOMParser()).parseFromString(xmlSource, 'text/xml');
// Insert date
var date = new Date();
//xml.getElementsByTagName('encoding-date')[0].textContent = date.toLocaleDateString().split('/').join('-');
xml.querySelector('encoding-date').textContent = [date.getFullYear(), ('0' + (date.getMonth() + 1)).slice(-2), ('0' + date.getDate()).slice(-2)].join('-');
// Default values (may be overwritten later)
var tempo = 110;
var beats = 2;
var beatType = 4;
var durationPerMeasure; // duration of a measure
xml.querySelector('divisions').textContent = divisions; // default divisions
xml.querySelector('sound').setAttribute('tempo', tempo); // default tempo
xml.querySelector('beats').textContent = beats; // default beats
xml.querySelector('beat-type').textContent = beatType; // default beat-type
// Start parsing
var measureNumber = 1;
var initialNoteFlag = true;
var cumSumDuration = 0 ; // cumulative duration to check whether a new measure needs to be created or not
var curMeasureElm = xml.querySelector('measure[number="1"]'); // current measure
var syllabicState = 'single'; // for English lyric
var reservedElmForNextMeasure = null;
// Create a note with sound
function _createSoundNote(step, alter, octave, duration, lyricText, syllabicState) {
var pitchElm = xml.createElement('pitch'); // pitch {step, alter, octave}
var stepElm = xml.createElement('step');
stepElm.textContent = step;
pitchElm.appendChild(stepElm);
if (alter != 0) {
var alterElm = xml.createElement('alter');
alterElm.textContent = alter;
pitchElm.appendChild(alterElm);
}
var octaveElm = xml.createElement('octave');
octaveElm.textContent = octave;
pitchElm.appendChild(octaveElm);
var durationElm = xml.createElement('duration'); // duration
durationElm.textContent = duration;
var lyricElm = xml.createElement('lyric'); // lyric {text, syllabic}
var textElm = xml.createElement('text');
textElm.textContent = lyricText;
lyricElm.appendChild(textElm);
var syllabicElm = xml.createElement('syllabic');
syllabicElm.textContent = syllabicState;
lyricElm.appendChild(syllabicElm);
var noteElm = xml.createElement('note'); // note {pitch, duration, lyric}
noteElm.appendChild(pitchElm);
noteElm.appendChild(durationElm);
noteElm.appendChild(lyricElm);
return noteElm;
}
// Create a new 'rest' note
function _createRestNote(duration) {
var durationElm = xml.createElement('duration'); // duration
durationElm.textContent = duration;
var noteElm = xml.createElement('note'); // note {rest, duration}
noteElm.appendChild(xml.createElement('rest'));
noteElm.appendChild(durationElm);
return noteElm;
}
// Create sound
function _createTempo(tempo) {
var soundElm = xml.createElement('sound');
soundElm.setAttribute('tempo', tempo);
return soundElm;
}
// Overwrite duration-related variables (this needs to be called when 'beatType' or 'beats' is updated)
function _updateDurationSettings() {
durationPerMeasure = divisions * (4 / beatType) * beats;
}
// Check the duration of current measure, and create new measure if necessary
// Also, add one empty measure with 'rest' if the initial note is NOT 'rest'
function _createNewMeasureIfNecessary(duration, restFlag) {
if (initialNoteFlag) {
if (restFlag) { // if initial note is 'rest'
cumSumDuration = 0; // reset cumSumDuration
} else { // if initial note is sound (not 'rest'), add one empty measure
curMeasureElm.appendChild(_createRestNote(durationPerMeasure));
cumSumDuration = durationPerMeasure; // enforce to create measure=2 immediately after this
}
initialNoteFlag = false;
}
cumSumDuration += duration;
if (cumSumDuration > durationPerMeasure) { // create new measure
curMeasureElm = xml.createElement('measure');
curMeasureElm.setAttribute('number', ++measureNumber); // increment number
if (reservedElmForNextMeasure !== null) {
curMeasureElm.appendChild(reservedElmForNextMeasure);
console.log('append reserved tempo')
reservedElmForNextMeasure = null;
}
xml.querySelector('part').appendChild(curMeasureElm);
cumSumDuration = duration;
}
}
// Find duration: variable or value
function _extractDuration(arrayOrValue) {
var duration = 0;
if (Array.isArray(arrayOrValue) && arrayOrValue.length > 0 && arrayOrValue[0] === 'readVariable') { // variable
duration = mapNoteDuration[arrayOrValue[1]];
} else if (arrayOrValue > 0) { // value
duration = arrayOrValue * divisions;
}
return duration;
}
_updateDurationSettings(); // update duration settings using default values
for (var i=0; i < scriptArray.length; i++) { // for (var i in scriptArray) {
if (i==0) continue; // skip
// Tempo and beat settings
if (scriptArray[i][0] === 'setTempoTo:') {
tempo = scriptArray[i][1];
console.log('tempo: ' + tempo);
if (cumSumDuration == durationPerMeasure) {
reservedElmForNextMeasure = _createTempo(tempo); // will add to the next measure
} else {
var soundElm = curMeasureElm.querySelector('sound');
if (soundElm) {
soundElm.setAttribute('tempo', tempo); // add or overwrite
} else {
curMeasureElm.appendChild(_createTempo(tempo)); // add immediately
}
console.log('add tempo immediately')
}
}
if (scriptArray[i][0] === 'setVar:to:' && scriptArray[i][1] === 'beats') {
beats = scriptArray[i][2];
console.log('beats: ' + beats);
xml.querySelector('beats').textContent = beats; // overwrite default beats
_updateDurationSettings();
}
if (scriptArray[i][0] === 'setVar:to:' && scriptArray[i][1] === 'beat-type') {
beatType = scriptArray[i][2];
console.log('beat-type: ' + beatType);
xml.querySelector('beat-type').textContent = beatType; // overwrite default beat-type
_updateDurationSettings();
}
// Add sound note
if (scriptArray[i][0] === 'noteOn:duration:elapsed:from:'){
var duration = _extractDuration(scriptArray[i][2]);
if (duration > 0) {
if (scriptArray[i-1][0] === 'say:') { // if lyric exists
try {
var midiPitch = scriptArray[i][1];
var step = chromaticStep[midiPitch % 12]; // chromatic step name ('C', etc.)
var alter = chromaticAlter[midiPitch % 12]; // -1, 0, 1
var octave = Math.floor(midiPitch / 12) - 1;
var lyricText = scriptArray[i-1][1];
if (lyricText.split('').slice(-1)[0] == '-') {
lyricText = lyricText.replace(/-$/, ''); // remove the last char '-'
if (syllabicState == 'single' || syllabicState == 'end') {
syllabicState = 'begin';
} else if (syllabicState == 'begin' || syllabicState == 'middle') {
syllabicState = 'middle';
}
} else {
if (syllabicState == 'single' || syllabicState == 'end') {
syllabicState = 'single';
} else if (syllabicState == 'begin' || syllabicState == 'middle') {
syllabicState = 'end';
}
}
console.log('midiPitch: ' + midiPitch + ', duration: ' + duration + ', ' + lyricText, ',' + syllabicState);
// --- Append node ---
var noteElm = _createSoundNote(step, alter, octave, duration, lyricText, syllabicState);
_createNewMeasureIfNecessary(duration, false);
curMeasureElm.appendChild(noteElm);
} catch (e) {
alert(e);
}
} else {
console.log('No "say" block at i= ' + i);
}
} else {
console.log('No variable at i= ' + i);
}
}
// Add rest
if (scriptArray[i][0] === 'rest:elapsed:from:') {
var duration = _extractDuration(scriptArray[i][1]);
if (duration > 0) {
try {
console.log('rest, duration: ' + duration);
// --- Append node ---
var noteElm = _createRestNote(duration);
_createNewMeasureIfNecessary(duration, true);
curMeasureElm.appendChild(noteElm);
} catch (e) {
alert(e);
}
} else {
console.log('No variable at i= ' + i);
}
}
}
return xml;
}
// Convert MusicXML to timing, lyric, and mouth shape data
function convertXML2TimingList(xml) {
var timingList = [];
var lyricList = [];
var mouthList = [];
var curTimeSec = 0; // physical time in sec
var tempo = 110; // default tempo
var secPerDivision;
var durationDiv; // duration of a note in division unit
var durationSec; // duration of a note in sec unit
console.log('lyric2mouth');
var lyric2mouth = new Lyric2Mouth(mouth2soundMapJP);
try {
// Parse measures in a document
var measureList = xml.getElementsByTagName('measure'); // querySelectorAll... which is faster?
for (var i=0; i < measureList.length; i++) {
var measureElm = measureList[i];
var soundElm = measureElm.querySelector('sound');
if (soundElm != null) {
var tempoAttr = soundElm.getAttribute('tempo');
if (tempoAttr != null) {
tempo = tempoAttr;
secPerDivision = 60 / tempo / divisions; // physical sec per tick (division)
console.log('tempo: ' + tempo + ', secPerDivision: ' + secPerDivision);
}
}
// Parse notes in a measure
var noteList = measureElm.getElementsByTagName('note');
for (var j=0; j < noteList.length; j++) {
var noteElm = noteList[j];
var durationElm = noteElm.querySelector('duration');
if (durationElm != null) {
// Duration
durationDiv = durationElm.textContent;
durationSec = secPerDivision * durationDiv;
timingList.push(curTimeSec); // start time of this note
// Lyrics
var textElm = noteElm.querySelector('lyric > text');
var lyric = (textElm != null ? textElm.textContent : restSymbolForLyric);
lyricList.push(lyric);
// Mouth shape
mouthList.push(lyric2mouth.convert(lyric));
curTimeSec += durationSec;
}
}
}
} catch (e){
alert(e);
}
return {timing: timingList, lyric: lyricList, mouth: mouthList};
}
document.getElementById('infile').addEventListener('change', handleFileSelect, false);