forked from cubbK/js13kGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonantx.js
436 lines (399 loc) · 13.6 KB
/
sonantx.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
// Sonant-X
//
// Copyright (c) 2014 Nicolas Vanhoren
//
// Sonant-X is a fork of js-sonant by Marcus Geelnard and Jake Taylor. It is
// still published using the same license (zlib license, see below).
//
// Copyright (c) 2011 Marcus Geelnard
// Copyright (c) 2008-2009 Jake Taylor
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
var sonantx;
(function() {
"use strict";
sonantx = {};
var WAVE_SPS = 44100; // Samples per second
var WAVE_CHAN = 2; // Channels
var MAX_TIME = 33; // maximum time, in millis, that the generator can use consecutively
var audioCtx = null;
// Oscillators
function osc_sin(value)
{
return Math.sin(value * 6.283184);
}
function osc_square(value)
{
if(osc_sin(value) < 0) return -1;
return 1;
}
function osc_saw(value)
{
return (value % 1) - 0.5;
}
function osc_tri(value)
{
var v2 = (value % 1) * 4;
if(v2 < 2) return v2 - 1;
return 3 - v2;
}
// Array of oscillator functions
var oscillators =
[
osc_sin,
osc_square,
osc_saw,
osc_tri
];
function getnotefreq(n)
{
return 0.00390625 * Math.pow(1.059463094, n - 128);
}
function genBuffer(waveSize, callBack) {
setTimeout(function() {
// Create the channel work buffer
var buf = new Uint8Array(waveSize * WAVE_CHAN * 2);
var b = buf.length - 2;
var iterate = function() {
var begin = new Date();
var count = 0;
while(b >= 0)
{
buf[b] = 0;
buf[b + 1] = 128;
b -= 2;
count += 1;
if (count % 1000 === 0 && (new Date() - begin) > MAX_TIME) {
setTimeout(iterate, 0);
return;
}
}
setTimeout(function() {callBack(buf);}, 0);
};
setTimeout(iterate, 0);
}, 0);
}
function applyDelay(chnBuf, waveSamples, instr, rowLen, callBack) {
var p1 = (instr.fx_delay_time * rowLen) >> 1;
var t1 = instr.fx_delay_amt / 255;
var n1 = 0;
var iterate = function() {
var beginning = new Date();
var count = 0;
while(n1 < waveSamples - p1)
{
var b1 = 4 * n1;
var l = 4 * (n1 + p1);
// Left channel = left + right[-p1] * t1
var x1 = chnBuf[l] + (chnBuf[l+1] << 8) +
(chnBuf[b1+2] + (chnBuf[b1+3] << 8) - 32768) * t1;
chnBuf[l] = x1 & 255;
chnBuf[l+1] = (x1 >> 8) & 255;
// Right channel = right + left[-p1] * t1
x1 = chnBuf[l+2] + (chnBuf[l+3] << 8) +
(chnBuf[b1] + (chnBuf[b1+1] << 8) - 32768) * t1;
chnBuf[l+2] = x1 & 255;
chnBuf[l+3] = (x1 >> 8) & 255;
++n1;
count += 1;
if (count % 1000 === 0 && (new Date() - beginning) > MAX_TIME) {
setTimeout(iterate, 0);
return;
}
}
setTimeout(callBack, 0);
};
setTimeout(iterate, 0);
}
sonantx.AudioGenerator = function(mixBuf) {
this.mixBuf = mixBuf;
this.waveSize = mixBuf.length / WAVE_CHAN / 2;
};
sonantx.AudioGenerator.prototype.getWave = function() {
var mixBuf = this.mixBuf;
var waveSize = this.waveSize;
// Local variables
var b, k, x, wave, l1, l2, s, y;
// Turn critical object properties into local variables (performance)
var waveBytes = waveSize * WAVE_CHAN * 2;
// Convert to a WAVE file (in a binary string)
l1 = waveBytes - 8;
l2 = l1 - 36;
wave = String.fromCharCode(82,73,70,70,
l1 & 255,(l1 >> 8) & 255,(l1 >> 16) & 255,(l1 >> 24) & 255,
87,65,86,69,102,109,116,32,16,0,0,0,1,0,2,0,
68,172,0,0,16,177,2,0,4,0,16,0,100,97,116,97,
l2 & 255,(l2 >> 8) & 255,(l2 >> 16) & 255,(l2 >> 24) & 255);
b = 0;
while(b < waveBytes)
{
// This is a GC & speed trick: don't add one char at a time - batch up
// larger partial strings
x = "";
for (k = 0; k < 256 && b < waveBytes; ++k, b += 2)
{
// Note: We amplify and clamp here
y = 4 * (mixBuf[b] + (mixBuf[b+1] << 8) - 32768);
y = y < -32768 ? -32768 : (y > 32767 ? 32767 : y);
x += String.fromCharCode(y & 255, (y >> 8) & 255);
}
wave += x;
}
return wave;
};
sonantx.AudioGenerator.prototype.getAudio = function() {
var wave = this.getWave();
var a = new Audio("data:audio/wav;base64," + btoa(wave));
a.preload = "none";
a.load();
return a;
};
sonantx.AudioGenerator.prototype.getAudioBuffer = function(callBack) {
if (audioCtx === null)
audioCtx = new AudioContext();
var mixBuf = this.mixBuf;
var waveSize = this.waveSize;
var buffer = audioCtx.createBuffer(WAVE_CHAN, this.waveSize, WAVE_SPS); // Create Mono Source Buffer from Raw Binary
var lchan = buffer.getChannelData(0);
var rchan = buffer.getChannelData(1);
var b = 0;
var iterate = function() {
var beginning = new Date();
var count = 0;
while (b < waveSize) {
var y = 4 * (mixBuf[b * 4] + (mixBuf[(b * 4) + 1] << 8) - 32768);
y = y < -32768 ? -32768 : (y > 32767 ? 32767 : y);
lchan[b] = y / 32768;
y = 4 * (mixBuf[(b * 4) + 2] + (mixBuf[(b * 4) + 3] << 8) - 32768);
y = y < -32768 ? -32768 : (y > 32767 ? 32767 : y);
rchan[b] = y / 32768;
b += 1;
count += 1;
if (count % 1000 === 0 && new Date() - beginning > MAX_TIME) {
setTimeout(iterate, 0);
return;
}
}
setTimeout(function() {callBack(buffer);}, 0);
};
setTimeout(iterate, 0);
};
sonantx.SoundGenerator = function(instr, rowLen) {
this.instr = instr;
this.rowLen = rowLen || 5605;
this.osc_lfo = oscillators[instr.lfo_waveform];
this.osc1 = oscillators[instr.osc1_waveform];
this.osc2 = oscillators[instr.osc2_waveform];
this.attack = instr.env_attack;
this.sustain = instr.env_sustain;
this.release = instr.env_release;
this.panFreq = Math.pow(2, instr.fx_pan_freq - 8) / this.rowLen;
this.lfoFreq = Math.pow(2, instr.lfo_freq - 8) / this.rowLen;
};
sonantx.SoundGenerator.prototype.genSound = function(n, chnBuf, currentpos) {
var marker = new Date();
var c1 = 0;
var c2 = 0;
// Precalculate frequencues
var o1t = getnotefreq(n + (this.instr.osc1_oct - 8) * 12 + this.instr.osc1_det) * (1 + 0.0008 * this.instr.osc1_detune);
var o2t = getnotefreq(n + (this.instr.osc2_oct - 8) * 12 + this.instr.osc2_det) * (1 + 0.0008 * this.instr.osc2_detune);
// State variable init
var q = this.instr.fx_resonance / 255;
var low = 0;
var band = 0;
for (var j = this.attack + this.sustain + this.release - 1; j >= 0; --j)
{
var k = j + currentpos;
// LFO
var lfor = this.osc_lfo(k * this.lfoFreq) * this.instr.lfo_amt / 512 + 0.5;
// Envelope
var e = 1;
if(j < this.attack)
e = j / this.attack;
else if(j >= this.attack + this.sustain)
e -= (j - this.attack - this.sustain) / this.release;
// Oscillator 1
var t = o1t;
if(this.instr.lfo_osc1_freq) t += lfor;
if(this.instr.osc1_xenv) t *= e * e;
c1 += t;
var rsample = this.osc1(c1) * this.instr.osc1_vol;
// Oscillator 2
t = o2t;
if(this.instr.osc2_xenv) t *= e * e;
c2 += t;
rsample += this.osc2(c2) * this.instr.osc2_vol;
// Noise oscillator
if(this.instr.noise_fader) rsample += (2*Math.random()-1) * this.instr.noise_fader * e;
rsample *= e / 255;
// State variable filter
var f = this.instr.fx_freq;
if(this.instr.lfo_fx_freq) f *= lfor;
f = 1.5 * Math.sin(f * 3.141592 / WAVE_SPS);
low += f * band;
var high = q * (rsample - band) - low;
band += f * high;
switch(this.instr.fx_filter)
{
case 1: // Hipass
rsample = high;
break;
case 2: // Lopass
rsample = low;
break;
case 3: // Bandpass
rsample = band;
break;
case 4: // Notch
rsample = low + high;
break;
default:
}
// Panning & master volume
t = osc_sin(k * this.panFreq) * this.instr.fx_pan_amt / 512 + 0.5;
rsample *= 39 * this.instr.env_master;
// Add to 16-bit channel buffer
k = k * 4;
if (k + 3 < chnBuf.length) {
var x = chnBuf[k] + (chnBuf[k+1] << 8) + rsample * (1 - t);
chnBuf[k] = x & 255;
chnBuf[k+1] = (x >> 8) & 255;
x = chnBuf[k+2] + (chnBuf[k+3] << 8) + rsample * t;
chnBuf[k+2] = x & 255;
chnBuf[k+3] = (x >> 8) & 255;
}
}
};
sonantx.SoundGenerator.prototype.getAudioGenerator = function(n, callBack) {
var bufferSize = (this.attack + this.sustain + this.release - 1) + (32 * this.rowLen);
var self = this;
genBuffer(bufferSize, function(buffer) {
self.genSound(n, buffer, 0);
applyDelay(buffer, bufferSize, self.instr, self.rowLen, function() {
callBack(new sonantx.AudioGenerator(buffer));
});
});
};
sonantx.SoundGenerator.prototype.createAudio = function(n, callBack) {
this.getAudioGenerator(n, function(ag) {
callBack(ag.getAudio());
});
};
sonantx.SoundGenerator.prototype.createAudioBuffer = function(n, callBack) {
this.getAudioGenerator(n, function(ag) {
ag.getAudioBuffer(callBack);
});
};
sonantx.MusicGenerator = function(song) {
this.song = song;
// Wave data configuration
this.waveSize = WAVE_SPS * song.songLen; // Total song size (in samples)
};
sonantx.MusicGenerator.prototype.generateTrack = function (instr, mixBuf, callBack) {
var self = this;
genBuffer(this.waveSize, function(chnBuf) {
// Preload/precalc some properties/expressions (for improved performance)
var waveSamples = self.waveSize,
waveBytes = self.waveSize * WAVE_CHAN * 2,
rowLen = self.song.rowLen,
endPattern = self.song.endPattern,
soundGen = new sonantx.SoundGenerator(instr, rowLen);
var currentpos = 0;
var p = 0;
var row = 0;
var recordSounds = function() {
var beginning = new Date();
while (true) {
if (row === 32) {
row = 0;
p += 1;
continue;
}
if (p === endPattern - 1) {
setTimeout(delay, 0);
return;
}
var cp = instr.p[p];
if (cp) {
var n = instr.c[cp - 1].n[row];
if (n) {
soundGen.genSound(n, chnBuf, currentpos);
}
}
currentpos += rowLen;
row += 1;
if (new Date() - beginning > MAX_TIME) {
setTimeout(recordSounds, 0);
return;
}
}
};
var delay = function() {
applyDelay(chnBuf, waveSamples, instr, rowLen, finalize);
};
var b2 = 0;
var finalize = function() {
var beginning = new Date();
var count = 0;
// Add to mix buffer
while(b2 < waveBytes)
{
var x2 = mixBuf[b2] + (mixBuf[b2+1] << 8) + chnBuf[b2] + (chnBuf[b2+1] << 8) - 32768;
mixBuf[b2] = x2 & 255;
mixBuf[b2+1] = (x2 >> 8) & 255;
b2 += 2;
count += 1;
if (count % 1000 === 0 && (new Date() - beginning) > MAX_TIME) {
setTimeout(finalize, 0);
return;
}
}
setTimeout(callBack, 0);
};
setTimeout(recordSounds, 0);
});
};
sonantx.MusicGenerator.prototype.getAudioGenerator = function(callBack) {
var self = this;
genBuffer(this.waveSize, function(mixBuf) {
var t = 0;
var recu = function() {
if (t < self.song.songData.length) {
t += 1;
self.generateTrack(self.song.songData[t - 1], mixBuf, recu);
} else {
callBack(new sonantx.AudioGenerator(mixBuf));
}
};
recu();
});
};
sonantx.MusicGenerator.prototype.createAudio = function(callBack) {
this.getAudioGenerator(function(ag) {
callBack(ag.getAudio());
});
};
sonantx.MusicGenerator.prototype.createAudioBuffer = function(callBack) {
this.getAudioGenerator(function(ag) {
ag.getAudioBuffer(callBack);
});
};
})();