forked from SuneBear/midi.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebaudio.js
executable file
·355 lines (331 loc) · 10.5 KB
/
webaudio.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* ----------------------------------------------------------
* Web Audio API - OGG or MPEG Soundbank
* ----------------------------------------------------------
* http://webaudio.github.io/web-audio-api/
* ----------------------------------------------------------
*/
import root from '../root';
// REF: http://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer
function base64ToArrayBuffer(base64) {
const binaryString = window.atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
window.AudioContext &&
(function () {
// var audioContext = null // new AudioContext()
const useStreamingBuffer = false; // !!audioContext.createMediaElementSource
const midi = (root.WebAudio = { api: 'webaudio' });
let ctx; // audio context
const sources = {};
const effects = {};
let masterVolume = 127;
const audioBuffers = {};
// /
midi.audioBuffers = audioBuffers;
midi.send = function (data, delay) {};
midi.setController = function (channelId, type, value, delay) {};
midi.setVolume = function (channelId, volume, delay) {
if (delay) {
setTimeout(function () {
masterVolume = volume;
}, delay * 1000);
} else {
masterVolume = volume;
}
};
midi.programChange = function (channelId, program, delay) {
/*
* if (delay) {
* return setTimeout(function() {
* var channel = root.channels[channelId]
* channel.instrument = program
* }, delay)
* } else {
*/
const channel = root.channels[channelId];
channel.instrument = program;
// }
};
midi.pitchBend = function (channelId, program, delay) {
/*
* if (delay) {
* setTimeout(function() {
* var channel = root.channels[channelId]
* channel.pitchBend = program
* }, delay)
* } else {
*/
const channel = root.channels[channelId];
channel.pitchBend = program;
// }
};
midi.noteOn = function (channelId, noteId, velocity, delay) {
delay = delay || 0;
// / check whether the note exists
const channel = root.channels[channelId];
const { instrument } = channel;
const bufferId = `${instrument}${noteId}`;
const buffer = audioBuffers[bufferId];
if (!buffer) {
// console.log(midi.GM.byId[instrument].id, instrument, channelId)
return;
}
// / convert relative delay to absolute delay
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}
// / create audio buffer
let source;
if (useStreamingBuffer) {
source = ctx.createMediaElementSource(buffer);
} else {
// XMLHTTP buffer
source = ctx.createBufferSource();
source.buffer = buffer;
}
// / add effects to buffer
if (effects) {
let chain = source;
for (const key in effects) {
chain.connect(effects[key].input);
chain = effects[key];
}
}
// / add gain + pitchShift
const gain = (velocity / 127) * (masterVolume / 127) * 2 - 1;
source.connect(ctx.destination);
source.playbackRate.value = 1; // pitch shift
source.gainNode = ctx.createGain(); // gain
source.gainNode.connect(ctx.destination);
source.gainNode.gain.value = Math.min(1.0, Math.max(-1.0, gain));
source.connect(source.gainNode);
// /
if (useStreamingBuffer) {
if (delay) {
return setTimeout(function () {
buffer.currentTime = 0;
buffer.play();
}, delay * 1000);
}
buffer.currentTime = 0;
buffer.play();
} else {
source.start(delay || 0);
}
// /
sources[`${channelId}${noteId}`] = source;
// /
return source;
};
midi.noteOff = function (channelId, noteId, delay) {
delay = delay || 0;
// / check whether the note exists
const channel = root.channels[channelId];
const { instrument } = channel;
const bufferId = `${instrument}${noteId}`;
const buffer = audioBuffers[bufferId];
if (buffer) {
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}
// /
const source = sources[`${channelId}${noteId}`];
if (source) {
if (source.gainNode) {
/*
* @Miranet: 'the values of 0.2 and 0.3 could of course be used as
* a 'release' parameter for ADSR like time settings.'
* add { 'metadata': { release: 0.3 } } to soundfont files
*/
const { gain } = source.gainNode;
gain.linearRampToValueAtTime(gain.value, delay);
gain.linearRampToValueAtTime(-1.0, delay + 0.3);
}
// /
if (useStreamingBuffer) {
if (delay) {
setTimeout(function () {
buffer.pause();
}, delay * 1000);
} else {
buffer.pause();
}
} else if (source.noteOff) {
source.noteOff(delay + 0.5);
} else {
source.stop(delay + 0.5);
}
// /
delete sources[`${channelId}${noteId}`];
// /
return source;
}
}
};
midi.chordOn = function (channel, chord, velocity, delay) {
const res = {};
for (var n = 0, note, len = chord.length; n < len; n++) {
res[(note = chord[n])] = midi.noteOn(channel, note, velocity, delay);
}
return res;
};
midi.chordOff = function (channel, chord, delay) {
const res = {};
for (var n = 0, note, len = chord.length; n < len; n++) {
res[(note = chord[n])] = midi.noteOff(channel, note, delay);
}
return res;
};
midi.stopAllNotes = function () {
for (const sid in sources) {
let delay = 0;
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}
const source = sources[sid];
source.gain.linearRampToValueAtTime(1, delay);
source.gain.linearRampToValueAtTime(0, delay + 0.3);
if (source.noteOff) {
// old api
source.noteOff(delay + 0.3);
} else {
// new api
source.stop(delay + 0.3);
}
delete sources[sid];
}
};
midi.setEffects = function (list) {
if (ctx.tunajs) {
for (let n = 0; n < list.length; n++) {
const data = list[n];
const effect = new ctx.tunajs[data.type](data);
effect.connect(ctx.destination);
effects[data.type] = effect;
}
} else {
return console.log('Effects module not installed.');
}
};
midi.connect = function (opts) {
root.setDefaultPlugin(midi);
midi.setContext(ctx || createAudioContext(), opts.onsuccess);
};
midi.getContext = function () {
return ctx;
};
midi.setContext = function (newCtx, onload, onprogress, onerror) {
ctx = newCtx;
// / tuna.js effects module - https://github.com/Dinahmoe/tuna
if (typeof window.Tuna !== 'undefined' && !ctx.tunajs) {
ctx.tunajs = new window.Tuna(ctx);
}
// / loading audio files
const urls = [];
const notes = root.keyToNote;
for (const key in notes) urls.push(key);
// /
const waitForEnd = function (instrument) {
for (const key in bufferPending) {
// has pending items
if (bufferPending[key]) return;
}
// /
if (onload) {
// run onload once
onload();
onload = null;
}
};
// /
const requestAudio = function (soundfont, instrumentId, index, key) {
const url = soundfont[key];
if (url) {
bufferPending[instrumentId]++;
loadAudio(
url,
function (buffer) {
buffer.id = key;
const noteId = root.keyToNote[key];
audioBuffers[`${instrumentId}${noteId}`] = buffer;
// /
if (--bufferPending[instrumentId] === 0) {
/*
* var percent = index / 87
* console.log(midi.GM.byId[instrumentId], 'processing: ', percent)
*/
soundfont.isLoaded = true;
waitForEnd(instrument);
}
},
function (err) {
console.error(err);
}
);
}
};
// /
var bufferPending = {};
for (var instrument in root.Soundfont) {
const soundfont = root.Soundfont[instrument];
if (soundfont.isLoaded) {
continue;
}
// /
const synth = root.GM.byName[instrument];
const instrumentId = synth.number;
// /
bufferPending[instrumentId] = 0;
// /
for (let index = 0; index < urls.length; index++) {
const key = urls[index];
requestAudio(soundfont, instrumentId, index, key);
}
}
// /
setTimeout(waitForEnd, 1);
};
/*
* Load audio file: streaming | base64 | arraybuffer
* ----------------------------------------------------------------------
*/
function loadAudio(url, onload, onerror) {
if (useStreamingBuffer) {
const audio = new window.Audio();
audio.src = url;
audio.controls = false;
audio.autoplay = false;
audio.preload = false;
audio.addEventListener('canplay', function () {
onload && onload(audio);
});
audio.addEventListener('error', function (err) {
onerror && onerror(err);
});
document.body.appendChild(audio);
} else if (url.indexOf('data:audio') === 0) {
// Base64 string
const base64 = url.split(',')[1];
const buffer = base64ToArrayBuffer(base64);
ctx.decodeAudioData(buffer, onload, onerror);
} else {
// XMLHTTP buffer
const request = new window.XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function () {
ctx.decodeAudioData(request.response, onload, onerror);
};
request.send();
}
}
function createAudioContext() {
return new (window.AudioContext || window.webkitAudioContext)();
}
})();