-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.ts
146 lines (128 loc) · 3.38 KB
/
music.ts
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
type Tag = 'note' | 'seq' | 'par' | 'rest' | 'repeat'
interface IMusic {
tag: Tag
//seq|par
left?: IMusic
right?: IMusic
//note
pitch?: string
dur?: number
//rest:
duration?: number
//repeat
section?: IMusic
count?: number
}
// http://nathansuniversity.com/music.html
function prelude(expr: IMusic) : IMusic{
var data = {
tag: 'seq',
left: {
tag: 'note',
pitch: 'd4',
dur: 500
},
right: expr
}
return data;
}
// http://nathansuniversity.com/music1.html
function reverse(expr: IMusic): IMusic {
if(expr.tag === 'note'){
return expr;
}
var new_expr = {
tag: 'seq',
left: reverse(expr.right),
right: reverse(expr.left)
}
return new_expr;
}
// http://nathansuniversity.com/music2.html
function endTime(time: number, expr: IMusic): number {
// function innerSum(expr: IMusic) : number{
// if(expr.tag === 'note'){
// return expr.dur;
// }else{
// return innerSum(expr.left) + innerSum(expr.right);
// }
// }
// return time + innerSum(expr);
if(expr.tag === 'note')
return time + expr.dur;
return endTime(endTime(time, expr.left), expr.right);
}
interface ILinearMusic {
tag: string
pitch: string
start: number
dur: number
}
// http://nathansuniversity.com/music3.html
// http://nathansuniversity.com/music6.html
// http://nathansuniversity.com/music14.html
function compile(musxpr: IMusic): Array<ILinearMusic>{
function innerCompile(expr: IMusic, time: number, res: Array<ILinearMusic>) {
if(!expr){
return 0;
}
var tag = expr.tag, t1, t2;
if(tag === 'note'){
var new_expr = {
tag: 'note',
pitch: expr.pitch,
start: time,
dur: expr.dur
};
res.push(new_expr);
return expr.dur + time;
}
if(tag === 'seq'){
t1 = innerCompile(expr.left, time, res);
t2 = innerCompile(expr.right, t1, res);
return t2;
}
if(tag === 'par'){
t1 = innerCompile(expr.left, time, res);
t2 = innerCompile(expr.right, time, res);
return t1 > t2? t1 : t2;
}
if(tag === 'repeat'){
t1 = time;
for(var i = 0; i < expr.count; i++){
t1 = innerCompile(expr.section, t1, res);
}
return t1;
}
}
var res = []
innerCompile(musxpr, 0, res);
return res;
}
// http://nathansuniversity.com/music4.html
var playMUS = function(expr) {
return playNOTE(compile(expr));
};
// http://nathansuniversity.com/music13.html
function convertPitch(pitch: string): number {
//sample: c4 => 60
var map = {
C: 0,
D: 2,
B: 11
};
return 12 + 12 * parseInt(pitch[1]) + map[pitch[0]];
}
// ------------- test --------------
var melody_mus =
{ tag: 'seq',
left:
{ tag: 'par',
left: { tag: 'note', pitch: 'c3', dur: 250 },
right: { tag: 'note', pitch: 'g4', dur: 500 } },
right:
{ tag: 'par',
left: { tag: 'note', pitch: 'd3', dur: 500 },
right: { tag: 'note', pitch: 'f4', dur: 250 } } };
var res = compile(melody2_mus);
// console.log(res);