-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetronome.js
264 lines (232 loc) · 9.18 KB
/
metronome.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
class Voice
{
constructor(tempo = 120)
{
this.audioContext = null;
this.notesInQueue = []; // notes that have been put into the web audio and may or may not have been played yet {note, time}
this.currentQuarterNote = 0;
this.tempo = tempo * 3;
this.lookahead = 25; // How frequently to call scheduling function (in milliseconds)
this.scheduleAheadTime = 0.1; // How far ahead to schedule audio (sec)
this.nextNoteTime = 0.0; // when the next note is due
this.isRunning = false;
this.intervalID = null;
this.beatsPerBar = 15;
this.botFloat = 5;
this.topFloat = 3;
this.osc3Volume = 0.5;
//automation
this.activeCycle = 0;
this.currentLoop = 0;
this.currentLoopMax = 2;
}
nextNote()
{
// Advance current note and time by a quarter note (crotchet if you're posh)
let secondsPerBeat = 60.0 / this.tempo; // Notice this picks up the CURRENT tempo value to calculate beat length.
this.nextNoteTime += secondsPerBeat; // Add beat length to last beat time
this.currentQuarterNote++; // Advance the beat number, wrap to zero
if (this.currentQuarterNote == this.beatsPerBar) {
this.currentQuarterNote = 0;
}
}
scheduleNote(beatNumber, time)
{
// push the note on the queue, even if we're not playing.
this.notesInQueue.push({ note: beatNumber, time: time });
// create 3 oscillators
const osc1 = this.audioContext.createOscillator();
const osc2 = this.audioContext.createOscillator();
const osc3 = this.audioContext.createOscillator();
const envelope1 = this.audioContext.createGain();
const envelope2 = this.audioContext.createGain();
const envelope3 = this.audioContext.createGain();
const stereo1 = this.audioContext.createStereoPanner();
const stereo2 = this.audioContext.createStereoPanner();
const stereo3 = this.audioContext.createStereoPanner();
//beatNumber is current subdivision(currentQuarterNote), beatsPerBar is number of subdivisions per ratio
//this is because tempo is bpm * top num
// if (beatNumber % this.beatsPerBar == 0) {
if (beatNumber == 0 ) {
//first sub 0 index
this.automation();
this.botFloat = botFloat;
this.topFloat = topFloat;
//if tempo change it happens here
if (tempo.value > 19 && tempo.value < 399) {
this.tempo = tempo.value * this.topFloat;
}
//update subdivision
this.beatsPerBar = this.botFloat * this.topFloat;
//osc1 is top number, osc 2 is bottom number, osc 3 is downbeat and subdivisions
osc1.frequency.value = 1200;
osc2.frequency.value = 800;
osc3.frequency.value = 450;
this.osc3Volume = 0.5
} else if (beatNumber % this.topFloat == 0){
osc1.frequency.value = 0;
osc2.frequency.value = 800;
let sub = playSub === true ? osc3.frequency.value = 1500 : osc3.frequency.value = 0;
this.osc3Volume = 0.2;
} else if (beatNumber % this.botFloat == 0) {
osc1.frequency.value = 1200;
osc2.frequency.value = 0;
let sub = playSub === true ? osc3.frequency.value = 1500 : osc3.frequency.value = 0;
this.osc3Volume = 0.2;
} else {
osc1.frequency.value = 0;
osc2.frequency.value = 0;
let sub = playSub === true ? osc3.frequency.value = 1500 : osc3.frequency.value = 0;
this.osc3Volume = 0.2;
}
if (beatNumber === this.beatsPerBar - 1) {
this.advance(); //automation
}
stereo1.pan.value = -1.0;
stereo2.pan.value = 1.0;
stereo3.pan.value = 0.0;
envelope1.gain.value = topVolume;
envelope2.gain.value = botVolume;
envelope3.gain.value = this.osc3Volume;
envelope1.gain.exponentialRampToValueAtTime(topVolume, time + 0.001);
envelope2.gain.exponentialRampToValueAtTime(botVolume, time + 0.001);
envelope3.gain.exponentialRampToValueAtTime(this.osc3Volume, time + 0.001);
envelope1.gain.exponentialRampToValueAtTime(0.001, time + 0.04);
envelope2.gain.exponentialRampToValueAtTime(0.001, time + 0.04);
envelope3.gain.exponentialRampToValueAtTime(0.001, time + 0.01);
if (!topNumMute){
osc1.connect(envelope1).connect(stereo1);
stereo1.connect(this.audioContext.destination);
}
if (!botNumMute) {
osc2.connect(envelope2).connect(stereo2);
stereo2.connect(this.audioContext.destination);
}
osc3.connect(envelope3).connect(stereo3);
stereo3.connect(this.audioContext.destination);
osc1.start(time);
osc1.stop(time + 0.05);
osc2.start(time);
osc2.stop(time + 0.05);
osc3.start(time);
osc3.stop(time + 0.05);
}
scheduler()
{
// while there are notes that will need to play before the next interval, schedule them and advance the pointer.
while (this.nextNoteTime < this.audioContext.currentTime + this.scheduleAheadTime ) {
this.scheduleNote(this.currentQuarterNote, this.nextNoteTime);
this.nextNote();
}
}
start()
{
if (this.isRunning) return;
if (this.audioContext == null)
{
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
this.isRunning = true;
this.currentQuarterNote = 0;
this.nextNoteTime = this.audioContext.currentTime + 0.05;
this.intervalID = setInterval(() => this.scheduler(), this.lookahead);
}
stop()
{
this.isRunning = false;
clearInterval(this.intervalID);
this.reset();
}
startStop()
{
if (this.isRunning) {
this.stop();
}
else {
this.start();
}
}
automation() {
if (!automation){
document.getElementById("automation-status").style.visibility = "hidden";
return;
} else {
this.cycles();
document.getElementById("automation-status").style.visibility = "visible";
document.getElementById("status").innerHTML = `cycle: ${this.activeCycle} loop: ${this.currentLoop}/${this.currentLoopMax}`;
}
}
advance() {
if(automation) {
if (this.currentLoopMax == this.currentLoop) {this.nextCycle()};
} else {
return;
}
}
cycles() {
if (this.activeCycle == 0) {
this.activeCycle = 1;
}
this.checkMax();
this.currentLoop++;
this.update();
}
nextCycle() {
switch(this.activeCycle) {
case 0:
console.log('Error: nextCycle()');
break;
case 1:
let next1 = automation ? this.step() : this.reset();
break;
case 2:
let next2 = automation3 ? this.step() : this.reset();
break;
case 3:
let next3 = automation4 ? this.step() : this.reset();
break;
case 4:
console.log('case 4');
let next4 = automation ? this.step() : this.reset();
break;
default:
console.log('nextCycle switch default error');
}
}
step() {
if (this.activeCycle == 4) {
this.activeCycle = 1;
this.currentLoop = 0;
} else {
this.activeCycle++;
this.currentLoop = 0;
}
}
reset() {
this.activeCycle = 0;
this.currentLoop = 0;
}
backCheck() {
let next = automation ? this.step() : this.reset();
}
checkMax() {
if (!automation) {
console.log('Error checkMax()');
return;
} else if (this.activeCycle == 0) {
return;
} else {
this.currentLoopMax = params['cycles'+this.activeCycle];
}
}
update() {
let a = this.activeCycle;
topNumInput.value = topFloat = params['step'+a+'TopNum'];
botNumInput.value = botFloat = params['step'+a+'BotNum'];
playSubInput.checked = playSub = params['step'+a+'playSub'];
topNumMuteInput.checked = topNumMute = params['step'+a+'TopNumMute']
botNumMuteInput.checked = botNumMute = params['step'+a+'BotNumMute']
inputMargin(topNumInput);
inputMargin(botNumInput);
}
}