-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusCompilerPar.js
42 lines (40 loc) · 1.17 KB
/
MusCompilerPar.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
var max = function(left, right) {
if(left > right) return left;
return right;
};
var endTime = function(musexpr) {
if(musexpr.tag === 'note') {
return musexpr.dur;
}
if(musexpr.tag === 'par') {
var a = endTime(musexpr.left);
var b = endTime(musexpr.right);
return(max(a,b));
}
if(musexpr.tag === 'seq') {
return(endTime(musexpr.left) + endTime(musexpr.left));
}
};
var compileT = function(musexpr, time) {
if(musexpr.tag === 'note') {
var ret = { tag: musexpr.tag, pitch: musexpr.pitch, start: time, dur: musexpr.dur };
var note = [];
note.push(ret);
return note;
}
if(musexpr.tag === 'par') {
var notes2 =[];
notes2 = notes2.concat(compileT(musexpr.left, time));
notes2 = notes2.concat(compileT(musexpr.right, time));
return notes2;
}
if(musexpr.tag === 'seq') {
var notes =[];
notes = notes.concat(compileT(musexpr.left, time));
notes = notes.concat(compileT(musexpr.right, time + endTime(musexpr.left)));
return notes;
}
};
var compile = function (musexpr) {
return(compileT(musexpr,0));
};