-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
253 lines (216 loc) · 7.68 KB
/
script.js
File metadata and controls
253 lines (216 loc) · 7.68 KB
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
const ac = new AudioContext();
const recorderNode = ac.createGain();
const rec = new Recorder(recorderNode);
// source: http://drum-machine.maryrosecook.com/
const BUTTON_SIZE = 26;
const screen = document.getElementById("screen").getContext("2d");
const green = "#00BA8C";
const orange = "#F15A2B";
const kick = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_bassdrum.wav?1550690555700');
const snare = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_snare.wav?1550690555419');
const snareSide = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_snare_sidestick.wav?1550690555484');
const conga = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_conga.wav?1550690555716');
const congaHigh = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_conga_high.wav?1550690555911');
const highHat = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_hihat.wav?1550690556072');
const openHat = new Audio('https://cdn.glitch.com/cc093c8e-9559-4f24-a71e-df60d5b1502c%2FMT52_openhat.wav?1550690556269');
const data = {
step: 0,
tracks: [createTrack(green, kick),
createTrack(green, snare),
createTrack(green, snareSide),
createTrack(green, conga),
createTrack(green, congaHigh),
createTrack(green, highHat),
createTrack(green, openHat)]
};
let midi = false;
let count = 0;
let interval = setInterval(function() {
data.step = (data.step + 1) % data.tracks[0].steps.length;
data.tracks
.filter(function(track) { return track.steps[data.step]; })
.forEach(function(track) {
let clone = track.playSound.cloneNode(true);
let buffer;
const request = new XMLHttpRequest();
request.open('GET', track.playSound.src, true);
request.responseType = 'arraybuffer';
request.onload = function() {
ac.decodeAudioData(request.response, function(buffer) {
buffer = buffer;
const gain = ac.createGain();
const playSound = ac.createBufferSource();
playSound.buffer = buffer;
console.log(playSound.buffer);
playSound.connect(gain);
gain.connect(recorderNode);
gain.connect(ac.destination);
playSound.start(0);
clone.remove();
});
}
request.send();
});
}, 100);
function createTrack(color, playSound) {
let steps = [];
for(let i = 0; i < 16; i++) {
steps.push(false);
}
return {steps: steps, color: color, playSound: playSound};
};
function buttonPosition(column, row) {
return {
x: BUTTON_SIZE / 2 + column * BUTTON_SIZE * 1.5,
y: BUTTON_SIZE / 2 + row * BUTTON_SIZE * 1.5
};
};
function drawButton(screen, column, row, color) {
let position = buttonPosition(column, row);
screen.fillStyle = color;
screen.fillRect(position.x, position.y, BUTTON_SIZE, BUTTON_SIZE);
};
function drawTracks(screen, data) {
data.tracks.forEach(function(track, row) {
track.steps.forEach(function(on, column) {
drawButton(screen, column, row, on ? track.color : "ghostwhite");
});
});
};
function isPointInButton(p, column, row) {
let b = buttonPosition(column, row);
return !(p.x < b.x ||
p.y < b.y ||
p.x > b.x + BUTTON_SIZE ||
p.y > b.y + BUTTON_SIZE);
};
function onMIDISuccess(midiAccess, midiOptions) {
const inputs = midiAccess.inputs;
const outputs = midiAccess.outputs;
for (var input of midiAccess.inputs.values()) {
input.onmidimessage = getMIDIMessage;
}
}
function getMIDIMessage(message) {
const command = message.data[0];
const note = message.data[1];
const velocity = (message.data.length > 2) ? message.data[2] : 0; // a velocity value might not be included with a noteOff command
switch (command) {
case 144: // noteOn
if (velocity > 0) {
if(count === 16) {
count = 1;
} else {
count = count + 1;
}
data.step = (data.step + 1) % data.tracks[0].steps.length;
data.tracks
.filter(function(track) { return track.steps[data.step]; })
.forEach(function(track) {
let clone = track.playSound.cloneNode(true);
let buffer;
const request = new XMLHttpRequest();
request.open('GET', track.playSound.src, true);
request.responseType = 'arraybuffer';
request.onload = function() {
ac.decodeAudioData(request.response, function(buffer) {
buffer = buffer;
const gain = ac.createGain();
const playSound = ac.createBufferSource();
playSound.buffer = buffer;
console.log(playSound.buffer);
playSound.connect(gain);
gain.connect(recorderNode);
gain.connect(ac.destination);
playSound.start(0);
clone.remove();
});
}
request.send();
});
} else {
}
break;
case 128: // noteOff
break;
// we could easily expand this switch statement to cover other types of commands such as controllers or sysex
}
}
function onMIDIFailure() {
console.log('Could not access your MIDI devices.');
}
// update
function update() {
if(midi === false) {
location.reload();
} else {
navigator.requestMIDIAccess()
.then(onMIDISuccess, onMIDIFailure);
}
}
// draw
(function draw() {
screen.clearRect(0, 0, screen.canvas.width, screen.canvas.height);
drawTracks(screen, data);
drawButton(screen, data.step, data.tracks.length, orange);
requestAnimationFrame(draw);
})();
// handle click events on tracks
(function setupButtonClicking() {
addEventListener("click", function(e) {
let p = { x: e.offsetX, y: e.offsetY };
data.tracks.forEach(function(track, row) {
track.steps.forEach(function(on, column) {
if(isPointInButton(p, column, row)) {
track.steps[column] = !on;
}
});
});
});
document.addEventListener("keypress", function (e) {
e = e || window.event;
if(e.keyCode === 99) {
data.tracks.forEach(function(track, row) {
track.steps.forEach(function(on, column) {
track.steps[column] = false;
});
});
}
});
// Record button click event
document.getElementById("record").addEventListener("click", function() {
rec.record();
console.log("I'm recording.");
document.getElementById("record").className = "hidden";
document.getElementById("stop").className = "";
});
document.getElementById("stop").addEventListener("click", function() {
rec.stop();
console.log("Stopped recording.");
document.getElementById("stop").className = "hidden";
document.getElementById("wav").className = "";
});
document.getElementById("wav").addEventListener("click", function() {
rec.exportWAV(function(blob) {
const audio = document.createElement("audio");
const url = URL.createObjectURL(blob);
audio.src = url;
audio.controls = "true";
const flex = document.createElement("div");
flex.className = "flex-container";
flex.append(audio);
document.querySelector(".container").append(flex);
document.getElementById("wav").className = "hidden";
document.getElementById("record").className = "";
});
});
document.getElementById("midi").addEventListener("click", function() {
midi = !midi;
clearInterval(interval);
data.step = 0;
update();
if(midi) {
document.getElementById("midi").innerHTML = "MIDI ON";
}
});
})();