forked from ChromeGaming/Physi-c-Tech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.js
51 lines (51 loc) · 1.14 KB
/
music.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
var tones = [130,138,146,155,165,175,185,196,208,221,234,248,263,279,295,313,332,352,373,395,419,444,471,499,529,123];
var song=[];
var A = new window.AudioContext;
copy=D=>{
for (var d = 0; d<D.length; d++){
song[song.length] = D[d];
}
}
n=(e,x,s)=>{
for(V = x,
b = (e, t, a, i) => Math.sin(e / t * 6.28 * a + i),
w=(e,t)=>Math.sin(e / 44100 * t * 6.28 + b(e, 44100, t, 0) ** 2 + .75 * b(e, 44100, t, .25) + .1 * b(e, 44100, t, .5)),
D = [],
i = 0;
i < 44100 * V;
i++
){
D[i] =
i < 88
? i / 88.2 * w(i, e)
: e ? (1 - (i - 88.2) / (44100 * (V * s))) ** ((.5 * Math.log(1e4 * e / 44100)) ** 2) * w(i, e) : 0;
}
copy(D);
};
clearSong=e=>{
song = [];
};
playSong=notes=>{
if (sound){
A.resume();
buildSong(notes);
m = A.createBuffer(1, 1e6, 44100),
m.getChannelData(0).set(song),
s = A.createBufferSource(),
s.buffer = m,
s.connect(A.destination),
s.onended=clearSong(),
s.start();
}
};
buildSong=notes=>{
for(var i=0; i<notes.length; i++){
if(notes[i]) {
note = notes[i].split('-');
var s = note[2] ? note[2] : 2;
n(tones[note[0]],note[1]/5,s);
} else {
n(0,.1);
}
}
};