-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
266 lines (159 loc) · 7.17 KB
/
app.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
265
266
$(function() {
"use strict"
//_________________________________________________________General variable declarations
var isPlaying = false,
tempo = 120.0, // tempo (in beats per minute)
current16thNote = 1,
futureTickTime = 0.0,
timerID = 0;
//_________________________________________________________END General variable declarations
//_________________________________________________________Load sounds
var kick = audioFileLoader("sounds/kick.mp3"),
snare = audioFileLoader("sounds/snare.mp3"),
hihat = audioFileLoader("sounds/hihat.mp3"),
shaker = audioFileLoader("sounds/shaker.mp3");
//_________________________________________________________END Load sounds
//_________________________________________________________Track que array
var track1Que = [],
track2Que = [],
track3Que = [],
track4Que = [];
//_________________________________________________________END Track que arrays
//_________________________________________________________Track arrays
var track1 = [],
track2 = [],
track3 = [],
track4 = [];
//_________________________________________________________END Track arrays
//_________________________________________________________Future tick
function futureTick() {
var secondsPerBeat = 60.0 / tempo;
futureTickTime += 0.25 * secondsPerBeat;
current16thNote++;
if (current16thNote > 16) {
current16thNote = 1
}
}
//_________________________________________________________END Future tick
//_________________________________________________________Set demo div colors
function setDemoDivColors(domElementGridNote, arr) {
for (var i = 0; i < arr.length; i += 1) {
$(domElementGridNote + arr[i]).css("background-color", "yellow");
}
}
setDemoDivColors('#gridBeatTrack1-Rhyth', track1);
setDemoDivColors('#gridBeatTrack2-Rhyth', track2);
setDemoDivColors('#gridBeatTrack3-Rhyth', track3);
setDemoDivColors('#gridBeatTrack4-Rhyth', track4);
//_________________________________________________________END Set demo div colors
//_________________________________________________________CheckRecordAndPlay
function checkIfRecordedAndPlay(trackArray, sndToPlay, gridBeat, timeVal) {
for (var i = 0; i < trackArray.length; i += 1) {
if (gridBeat === trackArray[i]) {
sndToPlay.play(timeVal)
}
}
};
//_________________________________________________________ END CheckRecordAndPlay
//__________________________________________________________Schedule note
function scheduleNote(beatDivisionNumber, time) {
$("#metro-ui-" + (beatDivisionNumber)).effect("pulsate", {
times: 1
}, 10);
checkIfRecordedAndPlay(track1, kick, beatDivisionNumber, time);
checkIfRecordedAndPlay(track2, snare, beatDivisionNumber, time);
checkIfRecordedAndPlay(track3, hihat, beatDivisionNumber, time);
checkIfRecordedAndPlay(track4, shaker, beatDivisionNumber, time);
removeDuplicates(track1);
removeDuplicates(track2);
removeDuplicates(track3);
removeDuplicates(track4);
track1.push(track1Que[0]);
track1Que[0] = undefined;
track2.push(track2Que[0]);
track2Que[0] = undefined;
track3.push(track3Que[0]);
track3Que[0] = undefined;
track4.push(track4Que[0]);
track4Que[0] = undefined;
// var osc = audioContext.createOscillator(); // Oscillator
// osc.connect(audioContext.destination)
// osc.start(time)
// osc.stop(time + 0.1)
}
//_________________________________________________________END schedule note
//_________________________________________________________Scheduler
function scheduler() {
while (futureTickTime < audioContext.currentTime + 0.1) {
scheduleNote(current16thNote, futureTickTime);
futureTick();
}
timerID = window.setTimeout(scheduler, 50.0)
}
//_________________________________________________________END Scheduler
//_________________________________________________________Transport controls
function play() {
isPlaying = !isPlaying;
if (isPlaying) {
current16thNote = 1;
futureTickTime = audioContext.currentTime;
console.log(futureTickTime)
scheduler();
return "stop";
} else {
window.clearTimeout(timerID);
return "play";
}
}
$("#play-button").on("click", function() {
play();
})
$("#tempo").on("change", function() {
tempo = this.value;
$("#showTempo").html(tempo);
})
//__________________________________________________________END Transport controls
//__________________________________________________________Grid toggle
function sequenceGridToggler(classDomEle, arr) {
$(classDomEle).on("mousedown", function() {
// console.log(classDomEle)
var rhythmicValue = parseInt(this.id.match(/(\d+)$/)[0], 10);
var index = arr.indexOf(rhythmicValue);
if (index > -1) {
arr.splice(index, 1);
$('#' + this.id).css("background-color", "");
} else {
arr.push(rhythmicValue);
$('#' + this.id).css("background-color", "yellow");
}
});
}
sequenceGridToggler(".grid-track1", track1);
sequenceGridToggler(".grid-track2", track2);
sequenceGridToggler(".grid-track3", track3);
sequenceGridToggler(".grid-track4", track4);
//__________________________________________________________END Grid toggle
//__________________________________________________________Remove duplicates
function removeDuplicates(arr) {
for (var i = 0; i < arr.length - 1; i += 1) {
for (var j = i + 1; j < arr.length; j += 1)
if (arr[i] === arr[j]) {
arr.splice(i, 1);
}
};
};
//___________________________________________________________END remove duplicates
//___________________________________________________________Drum Pad Events
function drumPadAction(domElementDrumPad, domElementGridNote, arrayTrack, sound) {
$(domElementDrumPad).on("mousedown", function() {
$(domElementGridNote + (current16thNote)).css("background-color", "yellow"); //______The sequencer note grid value is changed color
arrayTrack[0] = (current16thNote) //_______send to the appropriate array que
sound.play(audioContext.currentTime);
});
}
drumPadAction("#drumpad-track1", "#gridBeatTrack1-Rhyth", track1Que, kick)
drumPadAction("#drumpad-track2", "#gridBeatTrack2-Rhyth", track2Que, snare)
drumPadAction("#drumpad-track3", "#gridBeatTrack3-Rhyth", track3Que, hihat)
drumPadAction("#drumpad-track4", "#gridBeatTrack4-Rhyth", track4Que, shaker)
//____________________________________________________________END Drum Pad Events
});