-
Notifications
You must be signed in to change notification settings - Fork 3
/
play.js
96 lines (85 loc) · 1.99 KB
/
play.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
var mapBols={
"Na": [31],
"Ne": [33],
"Tin": [41],
"Tin2": [42],
"Tu": [39],
"Ti": [40],
"Re": [27],
"Ki": [30],
"Ke": [30],
"Ta": [32],
"Te": [37],
"TiTa": [40, 35],
"Kat": [22],
"Ge": [26],
"Ga": [25],
"Soft Ra": [27],
"Ghasee Ra": [29],
"Din": [38],
"Thun": [38],
"Tra": [35, 27],
"Ra": [27],
"-": [34],
"Dhi": [40, 36],
"Ghasee Dhi": [40, 29],
"Soft Dhi": [40, 27],
"Dha": [32, 25],
"Dha1": [32, 26],
"Dha2": [32, 27],
"Dha3": [32, 28],
"Dha4": [32, 29],
"Dha5": [32, 21],
"Dhin": [41, 25],
"Dhin1": [41, 26],
"Dhin2": [41, 27],
"Dhin3": [41, 28],
"Dhin4": [41, 29],
"Dhin5": [41, 21],
"Dhun": [38, 25],
"-": [34]
};
function playComposition(composition, tempo, volume){
var delay = 60.0/tempo;
var delta = delay;
ctxtime = MIDI.getContext().currentTime;
if (composition)
composition.split("|").forEach(function(beat, index) {
ctxtime = playBeat(beat.replace(/\s/g, ""), ctxtime, delta);
});
}
midiAudioBuffers = [];
function playBeat(beat, ctxtime, delta) {
//Check emphasis
var emph = 0;
var e_rgx = /\+/g;
if(e_rgx.test(beat)){
emph = beat.match(e_rgx).length;
beat = beat.replace(e_rgx, "");
}
//Check matra
var matra = 1;
var m_rgx = /\[\s*(\d+\.?\d*)\s*\]/;
if ((m = m_rgx.exec(beat)) !== null) {
matra = m[1];
beat = beat.replace(m_rgx, "");
}
var midi = mapBols[beat];
console.log(ctxtime, beat, emph, matra, midi);
if (beat != "-" && beat in mapBols) {
for (note of midi){
midiAudioBuffers.push(
MIDI.noteOn(window.CHANNEL, note, window.VOLUME + emph*5, ctxtime)
);
}
}
else {
MIDI.noteOff(CHANNEL, beat, ctxtime)
}
return ctxtime + delta*matra;
}
function stopAllBeats(){
midiAudioBuffers.forEach(function(ab){
ab.stop();
});
}