-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-4.js
More file actions
57 lines (52 loc) · 1.58 KB
/
2-4.js
File metadata and controls
57 lines (52 loc) · 1.58 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
var melody2_mus =
{ tag: 'seq',
left:
{ tag: 'seq',
left: { tag: 'note', pitch: 'a4', dur: 250 },
right: { tag: 'note', pitch: 'b4', dur: 250 } },
right:
{ tag: 'seq',
left: { tag: 'note', pitch: 'c4', dur: 500 },
right: { tag: 'note', pitch: 'd4', dur: 500 } } };
var compile = function (musexpr) {
// your code here
if (musexpr.tag == 'seq'){
if(typeof start === "undefined"){
start = 0;
}
var larr,rarr,prevItem,lastItem; // make it local!!!
larr = compile(musexpr.left);
rarr = compile(musexpr.right);
res = larr.concat(rarr);
return res;
}
else{
musexpr.start = start;
start += musexpr.dur;
return [musexpr];
}
};
//console.log(compile(melody2_mus));
var compile = function (musexpr) {
var rcompile = function (start,musexpr){
if (musexpr.tag == 'seq'){
var l,r;
l = rcompile(start,musexpr.left);
lastItem = l[l.length-1];
start = lastItem.start+lastItem.dur;
r = rcompile(start,musexpr.right);
//lastItem = r[r.length-1];
//start = lastItem.start+lastItem.dur;
return l.concat(r);
}
else if (musexpr.tag == 'note'){
musexpr.start = start;
//start += musexpr.dur;
//console.log("s+:"+start+"a")
//console.log(musexpr);
return [musexpr];
}
}; //rcompile
return rcompile(0,musexpr);
}
console.log(compile(melody2_mus));