-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmusic.js
197 lines (170 loc) · 6.87 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// procedurally generated music object
function gen_music()
{
// Musical note digraphs by frequency
//
// C2 C2toC7 second
// to
// C7 first
this.tune_pop=[];
this.tune_weights=0;
this.tune_stats=[
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,8,,,,,,4,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,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
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,4,8,,,,,,,,],
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]
];
this.build_tune_pop=function()
{
// populate digraph table
for (var n1=0; n1<50; n1++)
{
for (var n2=0; n2<50; n2++)
{
var w=(this.tune_stats[n1][n2]||0);
this.tune_pop[(n1*50)+n2]={dig:n1+","+n2,score:w};
this.tune_weights+=w;
}
}
// sort by weight
this.tune_pop.sort(function(a,b) {return ((b.score>a.score)?1:(b.score==a.score)?0:-1)});
};
this.audioCtx=null;
this.gainNodeMusic=null;
this.gainNodeCollect=null;
this.panNode=null;
this.notelen=1/5;
this.notenum=0;
this.randoms=new randomizer();
this.play_collect=function(f)
{
if (this.audioCtx==null) return;
var e=this.audioCtx.currentTime;
var osc1=this.audioCtx.createOscillator();
var osc2=this.audioCtx.createOscillator();
osc1.connect(this.gainNodeCollect);
osc1.frequency.value=65.41*Math.pow(2, (f==0?37:45)/12);
osc1.start(e);
osc1.stop(e+(1/8));
osc2.connect(this.gainNodeCollect);
osc2.frequency.value=65.41*Math.pow(2, 42/12);
osc2.start(e+(1/8));
osc2.stop(e+(2/8));
};
this.play_note=function(type, note, start, len)
{
if ((note==0) || (this.audioCtx==null)) return;
var e=this.audioCtx.currentTime+start;
var osc=this.audioCtx.createOscillator();
// Set oscillator wave type
osc.type=type;
// Pan the note to where it would sound like if played on a piano
osc.connect(this.panNode);
if (note!=1) this.panNode.pan.setValueAtTime(((note/50)*2)-1, e);
// Convert the note from note value to frequency in Hz
osc.frequency.value=65.41*Math.pow(2, note/12);
// Set the start and stop times for the oscillator
osc.start(e);
osc.stop(e+len);
};
// Start playing the tune
this.play_tune=function()
{
// Build digraph table if not already done
if (this.tune_pop.length==0)
this.build_tune_pop();
// Create audio context if not done already
if (this.audioCtx==null)
{
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Add volume control for music
this.gainNodeMusic=this.audioCtx.createGain();
this.gainNodeMusic.connect(this.audioCtx.destination);
this.gainNodeMusic.gain.setValueAtTime(0.01, this.audioCtx.currentTime);
// Add volume control for collectables
this.gainNodeCollect=this.audioCtx.createGain();
this.gainNodeCollect.connect(this.audioCtx.destination);
this.gainNodeCollect.gain.setValueAtTime(0.05, this.audioCtx.currentTime);
// Add audio panning, so notes appear in 2d space like being sat at a piano
this.panNode=this.audioCtx.createStereoPanner();
this.panNode.connect(this.gainNodeMusic);
}
////////////////////////////////////
for (var i=0; i<50; i++)
{
var phrasestr="";
var dig_len=2;
// Build phrase up from note digraphs using weighted 2nd order probability
for (var pairs=0; pairs<dig_len; pairs++)
{
var w=this.randoms.rnd(this.tune_weights);
for (var j=0; j<this.tune_pop.length; j++)
{
if (w<this.tune_pop[j].score)
break;
w-=this.tune_pop[j].score;
}
phrasestr+=","+this.tune_pop[j].dig;
}
var phrase=phrasestr.split(',');
// Repeat each phrase twice
for (var rep=0; rep<2; rep++)
{
for (var k=0; k<phrase.length; k++)
{
this.play_note('square', phrase[k]||0, this.notenum*this.notelen, this.notelen/1.5); // Melody
this.play_note('sawtooth', phrase[1]/4, this.notenum*this.notelen, this.notelen/1.8); // Baseline
this.notenum++;
}
}
}
// Reset note count so next call doesn't have a delay
this.notenum=0;
};
}