-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvurm.js
More file actions
389 lines (359 loc) · 11.8 KB
/
vurm.js
File metadata and controls
389 lines (359 loc) · 11.8 KB
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
// This dis/plays the music in the window.location.hash, in abc format.
//
// Display doesn't care (much) about note count / bar, but might display warning wiggly lines.
//
// Interpret abc to:
// note = iterator.next(root) : iterator is either display or play - repeat sections.
// - pitch, basic C..GAB
// - accidentals +/- n
// - duration (p/q)
// - tied to next note
// bar = group of notes
// - repeat group (2 times default, specified, or deduced from nth repeat bars)
// - bar, perhaps for nth repeat
//
// But... some things straddle bars. Maybe bars are ephemeral?
// - tuple of notes
// - start of slur/group (to bar/repeat)
// - end of slur/group (from bar/repeat)
// - meta [ K|M|G|L|Q|T|X ]
function AbcPitchTokeniser () {
this.convert = function(abc) {
// Reads a single element of pitch
var r = /^(([zxZ])|([_=^]*)([a-gA-G])([',]*))/.exec(abc);
if (!r) {
return null;
}
var note = { chars: r[1].length };
if (!r[2]) {
var accidence = r[3];
if (accidence) {
var l = accidence.length;
note.sharps = ("_=^".indexOf(accidence.charAt(0)) - 1) * l;
}
var step = r[5];
var octave = 0;
if (step) {
var l = step.length;
for (var i = 0; i < l ; ++i) {
octave += step.charAt(i) == "'" ? 7 : -7;
}
}
note.ledger = octave + "CDEFGABcdefgab".indexOf(r[4]);
}
return note;
}
}
abcPitchTokeniser = new AbcPitchTokeniser();
function AbcDurationTokeniser() {
this.convert = function(abc) {
// Reads a single element of duration (for after some pitch)
var d = /^(([0-9]*)(\/+([0-9]*))?(<+|>+)?`*)/.exec(abc);
if (!d) {
return null;
}
var chunk = { chars: d[1].length, numer: d[2], denom: d[4], dots: d[5] };
var halves = d[3];
if (chunk.denom == undefined) { chunk.denom = 1; }
if (chunk.denom == '') {
chunk.denom = Math.pow(2, halves.length);
}
if (chunk.numer == '') { chunk.numer = 1; }
return chunk;
}
}
abcDurationTokeniser = new AbcDurationTokeniser();
function AbcPunctuationTokeniser() {
this.convert = function(abc) {
// Pre, inter, and post punctuation.
var d = /^((\()|(\))|(\|)|( +))/.exec(abc);
if (!d) {
return null;
}
var chunk = {
chars: d[1].length,
tieStart: d[2],
tieEnd: d[3],
bar: d[4],
space: d[5]
};
for (var key in chunk) {
if (!chunk[key]) {
delete chunk[key];
}
}
return chunk;
}
}
abcPunctuationTokeniser = new AbcPunctuationTokeniser();
function AbcChunkReader() {
this.convert = function(abc) {
// Read a smallest complete chunk into notes, like a>b.
// Later, handle recursive [abc]->[abc] and similar.
var p = abcPitchTokeniser.convert(abc);
if (!p) {
return abcPunctuationTokeniser.convert(abc);
}
var chunk = { chars: p.chars, notes: [] };
abc = abc.slice(p.chars);
delete p.chars;
var d = abcDurationTokeniser.convert(abc);
if (!d) {
return null;
}
abc = abc.slice(d.chars);
chunk.chars += d.chars;
p.duration = (1*d.numer) / (1*d.denom);
if (d.dots) {
var p2 = abcPitchTokeniser.convert(abc);
if (!p2) {
return null;
}
abc = abc.slice(p2.chars);
chunk.chars += p2.chars;
delete p2.chars;
var d2 = abcDurationTokeniser.convert(abc);
if (!d2) {
return null;
}
abc = abc.slice(d2.chars);
chunk.chars += d2.chars;
p2.duration = (1*d2.numer) / (1*d2.denom);
if (p2.dots) {
// Not going to handle a>b>c: unequal lengths are not advised anyway.
// Mmmaybe have a>b>c mean 'carry the dot' for a3/2 b c/2.
return null;
}
var dd = Math.pow(0.5, d.dots.length);
p.duration *= d.dots.match('>') ? 2-dd : dd;
p2.duration *= d.dots.match('<') ? 2-dd : dd;
chunk.notes.push(p, p2);
} else {
chunk.notes.push(p);
}
return chunk;
}
}
abcChunkReader = new AbcChunkReader();
function LineSource(abc) {
// Strips %% commands and post \ comments and trailing spaces.
this.lines = abc.split("\n");
this.line = 0;
for (var n = this.lines.length - 1; n >= 0; --n) {
var r = /^\s*((%%)?(([^\\%]|\\.)*?)(\s*?(\\))?)(\s*?(%.*)?)$/.exec(this.lines[n]);
this.lines[n] = (r[7] && !r[1]) ? '%' : r[1];
}
this.state = function() {
// Show the parsing status
var see = this.see();
return "[:"
+ this.lines.slice(0, this.line).join("><")
+ '-' + this.line + "=" + see + '-'
+ this.lines.slice(this.line).join("><")
+ ":]";
};
this.skipDels = function() {
// Skip any lines marked 'deleted'.
while ( this.lines.length > this.line && this.lines[this.line] == '%') {
++this.line;
};
};
this.see = function() {
// Look at the current line
this.skipDels();
return this.lines[this.line];
};
this.take = function() {
// Move on from the current line
this.skipDels();
var line = this.lines[this.line ++];
return line;
}
}
function HeaderLineParser(lineSource) {
// Cope with header lines having %% preprocessor comments
this.source = lineSource;
this.preparse = function (line) {
// Turn '%% preprocessor' directives into the I: form.
var prep = /^%%\s*(.*?)\s*$/.exec(line);
if (prep) {
return { head: [ 'I', prep[1] ] };
}
};
this.matchHeaderLine = function (line) {
// Recognise X: lkajlsjfasdf header lines
return /^(\w|\+):\s*(.*?)\s*(%.*)?$/.exec(line);
};
this.parse = function() {
var line = this.source.see();
var p = this.preparse(line);
if (p) { return p; }
var r = this.matchHeaderLine(line);
if (r) {
var headData = [ r[1], r[2] ];
this.source.take();
do {
line = this.source.see();
p = this.preparse(line);
if (p) {
headData.push(p);
this.source.take();
}
r = this.matchHeaderLine(line);
if (r) {
if (r[1] == '+') {
headData.push(' ' + r[2]);
this.source.take();
}
else {
r = undefined;
}
}
} while (p || r);
return { head: headData };
}
return null;
}
}
// Returns [ { head: [ 'x', 'values', 'of', 'the', 'header' ] }, ... ]
function HeaderBlockParser(lineSource) {
this.lineSource = lineSource;
this.source = new HeaderLineParser(lineSource);
this.parse = function() {
var header = this.source.parse();
if (!header) { return header; }
var heads = [];
do {
heads.push(header);
var backup = this.lineSource.line;
header = this.source.parse();
if (header) {
if (header.head[0] == 'X') {
header = '';
this.lineSource.line = backup;
}
else if (header.head[0] == 'K') {
heads.push(header);
header = '';
}
}
} while (header);
return heads;
}
}
function TuneLineParser(lineSource) {
// Returns a free header line, or a tune line with embedded headers
this.source = lineSource;
this.parse = function() {
tuneLine = [];
headerLineParser = new HeaderLineParser(lineSource);
lineHeader = headerLineParser.parse();
if (lineHeader != null) {
return [ lineHeader ];
}
abcChunkReader = new AbcChunkReader();
var line = lineSource.see();
do {
var chunk = abcChunkReader.convert(line);
if (chunk != null) {
tuneLine.push(chunk.notes);
line = line.slice(chunk.chars);
}
else {
if (line == '\\') {
lineSource.take();
do {
lineHeader = headerLineParser.parse();
if (lineHeader != null) {
tuneLine.push(lineHeader);
}
}
while (lineHeader != null)
line = lineSource.see();
}
else {
tuneLine.push({error: line});
}
}
} while (line.length > 0);
lineSource.take();
return tuneLine;
}
}
function AbcReader() {
this.convert = function(abc) {
var chunks = []
while (abc.length > 0) {
var chunk = abcChunkReader.convert(abc);
var old = abc;
if (chunk) {
abc = abc.slice(chunk.chars);
if (chunk.notes) {
for (var i = 0; i < chunk.notes.length; ++i ) {
var n = chunk.notes[i];
chunks.push(n);
}
} else {
chunks.push(chunk);
}
}
if (old == abc) {
chunks.push({parseFailAt: abc});
abc = '';
}
}
return { parts: { "": { "": chunks } } };
}
}
abcReader = new AbcReader();
function VurmToMidi() {
this.convert = function (tune, vurm) {
voice = [];
var s = tune.parts[""][""];
for (var i = 0; i < s.length; ++i) {
n = s[i];
if ('ledger' in n) {
var ledger = n.ledger;
var letter = (ledger % 7 + 7 ) % 7;
var octave = (ledger - letter)/7;
// c-d-ef-g-a-bc
// 0=0 1=2 2=4 3=5 4=7 5=9 6=11
var semitones = letter*2 - (letter > 2) + ( n.sharps ? n.sharps : 0);
// semitones += key(letter);
// FCGDAEB - 3 0 4 1 5 2 6 'Fat Charlie Got Don An Elephant Barnacle' :)
var midi = semitones + octave*12 + 60; // = MIDI.pianoKeyOffset;
voice.push({deltaTime: 0, type: "channel", subtype: 'noteOn', channel:1, noteNumber: midi, velocity:127});
var t = n.duration * 64;
voice.push({deltaTime: t, type: "channel", subtype: 'noteOff', channel:1, noteNumber: midi, velocity:0});
}
}
return {
header: {
formatType: 1,
trackCount: 2,
ticksPerBeat: 64 // per crotchet
},
tracks: [ // One per voice.
[
// Meta: title, key, etc.
],
voice
]
};
}
}
vurmToMidi = new VurmToMidi();
function onAbcChanged() {
// Auto-play first tune.
var abc = document.getElementById('abc').value;
var vurm = abcReader.convert(abc);
var firstTune = vurm.parts[""][""];
var midi = vurmToMidi.convert(vurm, firstTune);
MIDI.Player.setMidiData(midi);
MIDI.Player.resume();
}
function onceLoaded() {
MIDI.programChange(0, 0);
document.getElementById('abc').textContent = window.location.hash.slice(1);
onAbcChanged();
}