-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
352 lines (303 loc) · 12.4 KB
/
player.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
/**
* Note : this is a MODIFIED version of Marcus Geelnard's player
* Altered to support a dynamic number of channels by default, and global volume.
* Original notice follows.
*/
/* -*- mode: javascript; tab-width: 4; indent-tabs-mode: nil; -*-
*
* Copyright (c) 2011-2012 Marcus Geelnard
*
* 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 CPlayer = function()
{
//--------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------
// Oscillators
var osc_sin = function (value)
{
return Math.sin(value * 6.283184);
};
var osc_saw = function (value)
{
return 2 * (value % 1) - 1;
};
var osc_square = function (value)
{
return (value % 1) < 0.5 ? 1 : -1;
};
var osc_tri = function (value)
{
var v2 = (value % 1) * 4;
if(v2 < 2) return v2 - 1;
return 3 - v2;
};
// Array of oscillator functions
var mOscillators =
[
osc_sin,
osc_square,
osc_saw,
osc_tri
];
var getnotefreq = function (n)
{
// 174.61.. / 44100 = 0.003959503758 (F)
return 0.003959503758 * Math.pow(2, (n-128)/12);
};
//--------------------------------------------------------------------------
// Public methods
//--------------------------------------------------------------------------
// Initialize buffers etc.
this.init = function (song, volume, opts)
{
// Handle optional arguments
this.firstRow = 0;
this.lastRow = song.endPattern - 2;
this.firstCol = 0;
this.lastCol = song.songData.length-1;
this.globalVolume = volume||1;
if (opts)
{
this.firstRow = opts.firstRow;
this.lastRow = opts.lastRow;
this.firstCol = opts.firstCol;
this.lastCol = opts.lastCol;
}
// Prepare song info
this.song = song;
this.numSamples = song.rowLen * 32 * (this.lastRow - this.firstRow + 1);
this.numWords = this.numSamples * 2;
// Create work buffers (initially cleared)
this.chnBufWork = new Int32Array(this.numWords);
this.mixBufWork = new Int32Array(this.numWords);
// Init iteration state variables
this.currentCol = this.firstCol;
this.currentRow = this.firstRow;
};
// Generate audio data for a single track
this.generate = function ()
{
// Local variables
var i, j, b, p, row, col, n, cp,
k, t, lfor, e, x, rsample, rowStartSample, f, da;
// Put performance critical items in local variables
var chnBuf = this.chnBufWork,
mixBuf = this.mixBufWork,
waveSamples = this.numSamples,
waveWords = this.numWords,
instr = this.song.songData[this.currentCol],
rowLen = this.song.rowLen;
if (this.currentRow == this.firstRow)
{
// Clear channel buffer
for (b = 0; b < waveWords; b ++)
{
chnBuf[b] = 0;
}
}
// Clear effect state
var low = 0, band = 0, high;
var lsample, filterActive = false;
var loopEnd = this.currentRow + 8;
loopEnd = loopEnd > this.lastRow ? this.lastRow : loopEnd;
for (p = this.currentRow; p <= loopEnd; ++p) // Patterns
{
cp = instr.p[p];
for (row = 0; row < 32; ++row) // Pattern rows
{
// Execute effect command.
var cmdNo = cp ? instr.c[cp - 1].fx[row] : 0;
if (cmdNo)
instr.i[cmdNo-1] = instr.c[cp - 1].fx[row+32] || 0;
// Put performance critical instrument properties in local variables
var osc1 = mOscillators[instr.i[0]],
o1vol = instr.i[1],
o1xenv = instr.i[3],
osc2 = mOscillators[instr.i[4]],
o2vol = instr.i[5],
o2xenv = instr.i[8],
noiseVol = instr.i[9],
attack = instr.i[10] * instr.i[10] * 4,
sustain = instr.i[11] * instr.i[11] * 4,
release = instr.i[12] * instr.i[12] * 4,
oscLFO = mOscillators[instr.i[13]],
lfoAmt = instr.i[14] / 512,
lfoFreq = Math.pow(2, instr.i[15] - 9) / rowLen,
fxLFO = instr.i[16],
fxFilter = instr.i[17],
fxFreq = instr.i[18] * 43.23529 * 3.141592 / 44100,
q = 1 - instr.i[19] / 255,
dist = instr.i[20] * 1e-5,
drive = instr.i[21] / 32,
panAmt = instr.i[22] / 512,
panFreq = 6.283184 * Math.pow(2, instr.i[23] - 9) / rowLen,
dlyAmt = instr.i[24] / 255,
dly = instr.i[25] * rowLen;
// Calculate start sample number for this row in the pattern
rowStartSample = ((p - this.firstRow) * 32 + row) * rowLen;
// Generate notes for this pattern row
for (col = 0; col < 4; ++col) {
n = cp ? instr.c[cp - 1].n[row+col*32] : 0;
if (n) {
// Calculate note frequencies for the oscillators
var o1t = getnotefreq(n + instr.i[2] - 128);
var o2t = getnotefreq(n + instr.i[6] - 128) * (1 + 0.0008 * instr.i[7]);
// Re-trig oscillators
var c1 = 0, c2 = 0;
// Generate one note (attack + sustain + release)
for (j = 0; j < attack + sustain + release; j++) {
// Envelope
e = 1;
if (j < attack)
e = j / attack;
else if (j >= attack + sustain)
e -= (j - attack - sustain) / release;
// Oscillator 1
t = o1t;
if (o1xenv)
t *= e * e;
c1 += t;
rsample = osc1(c1) * o1vol;
// Oscillator 2
t = o2t;
if (o2xenv)
t *= e * e;
c2 += t;
rsample += osc2(c2) * o2vol;
// Noise oscillator
if (noiseVol)
rsample += (2 * Math.random() - 1) * noiseVol;
// Add to (mono) channel buffer
chnBuf[(rowStartSample + j) * 2] += (80 * rsample * e) | 0;
}
}
}
// Perform effects for this pattern row
for (j = 0; j < rowLen; j++) {
// Dry mono-sample
k = (rowStartSample + j) * 2;
rsample = chnBuf[k];
// We only do effects if we have some sound input
if (rsample || filterActive) {
// State variable filter
f = fxFreq;
if (fxLFO)
f *= oscLFO(lfoFreq * k) * lfoAmt + 0.5;
f = 1.5 * Math.sin(f);
low += f * band;
high = q * (rsample - band) - low;
band += f * high;
rsample = fxFilter == 3 ? band : fxFilter == 1 ? high : low;
// Distortion
if (dist) {
rsample *= dist;
rsample = rsample < 1 ? rsample > -1 ? osc_sin(rsample*.25) : -1 : 1;
rsample /= dist;
}
// Drive
rsample *= drive;
// Is the filter active (i.e. still audiable)?
filterActive = rsample * rsample > 1e-5;
// Panning
t = Math.sin(panFreq * k) * panAmt + 0.5;
lsample = rsample * (1 - t);
rsample *= t;
} else
lsample = 0;
// Delay is always done, since it does not need sound input
if (k >= dly) {
// Left channel = left + right[-p] * t
lsample += chnBuf[k-dly+1] * dlyAmt;
// Right channel = right + left[-p] * t
rsample += chnBuf[k-dly] * dlyAmt;
}
// Store in stereo channel buffer (needed for the delay effect)
chnBuf[k] = lsample | 0;
chnBuf[k+1] = rsample | 0;
// ...and add to stereo mix buffer
mixBuf[k] += (lsample*this.globalVolume) | 0;
mixBuf[k+1] += (rsample*this.globalVolume) | 0;
}
}
this.currentRow++;
}
if (this.currentRow > this.lastRow) {
// Step to the next column
this.currentRow = this.firstRow;
this.currentCol++;
}
// Next iteration
return {
done: this.currentCol > this.lastCol,
progress: (this.currentCol - this.firstCol + ((this.currentRow - this.firstRow) / (this.lastRow - this.firstRow + 1))) / (this.lastCol - this.firstCol + 1)
};
};
// Create a WAVE formatted sting from the generated audio data
this.createWave = function()
{
// Local variables
var b, k, x, wave, l1, l2, s, y;
// Turn critical object properties into local variables (performance)
var mixBuf = this.mixBufWork,
waveWords = this.numWords;
// We no longer need the channel working buffer
this.chnBufWork = null;
// Convert to a WAVE file (in a binary string)
l1 = waveWords * 2 - 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);
for (b = 0; b < waveWords;)
{
// 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 < waveWords; ++k, b++)
{
// Note: We clamp here
y = mixBuf[b];
y = y < -32767 ? -32767 : (y > 32767 ? 32767 : y);
x += String.fromCharCode(y & 255, (y >> 8) & 255);
}
wave += x;
}
// Return the wave formatted string
return wave;
};
// Get n samples of wave data at time t [s]. Wave data in range [-2,2].
this.getData = function(t, n)
{
var i = 2 * Math.floor(t * 44100);
var d = new Array(n);
var b = this.mixBufWork;
for (var j = 0; j < 2*n; j += 1)
{
var k = i + j;
d[j] = t > 0 && k < b.length ? b[k] / 32768 : 0;
}
return d;
};
};