-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound-stereo.js
380 lines (333 loc) · 8.8 KB
/
sound-stereo.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
import { parsePattern, stringToNote } from './util.js'
import Shared32Array from './shared32array.js'
import Biquad from './biquad.js'
import Oscs from './osc.js'
const PRIVATE_API = ['constructor','valueOf','_reset']
const getMethods = (obj) => {
return Object.getOwnPropertyNames(obj)
.filter(m => !PRIVATE_API.includes(m))
}
class Sound {
constructor () {
this.x0 = 0
this.Lx0 = 0
this.Rx0 = 0
this.t = 0
this._mod = Infinity
this._wavetables_i = 0
this._wavetables = new Array(100).fill(0)
this._widen_buffer = new Array(256)
this._plot_buffer = new Shared32Array(2048)
const returnThis = () => this
this._ignoreNext =
Object.fromEntries(
getMethods(Sound.prototype)
.map(m => [m, returnThis]))
// const returnThisStereo = () => this._stereo
// this._ignoreNextStereo =
// Object.fromEntries(
// getMethods(Sound.prototype)
// .map(m => [m, returnThisStereo]))
const returnIgnoreGrp = () => this._ignoreGrp
this._ignoreGrp =
Object.fromEntries(
getMethods(Sound.prototype)
.map(m => [m, returnIgnoreGrp]))
this._ignoreGrp.end = returnThis
this._ignoreNext.grp = () => this._ignoreGrp
// const returnIgnoreGrpStereo = () => this._ignoreGrpStereo
// this._ignoreGrpStereo =
// Object.fromEntries(
// getMethods(Sound.prototype)
// .map(m => [m, returnIgnoreGrpStereo]))
// this._ignoreGrpStereo.end = returnThisStereo
// this._ignoreNextStereo.grp = () => this._ignoreGrpStereo
// this._stereo =
// Object.fromEntries(
// getMethods(Sound.prototype)
// .map(m => {
// let fn = (this['s' + m] ?? this[m]).bind(this)
// return [m, fn]
// }))
}
_reset (t) {
this.t = t
this.p = n
this._wavetables_i = 0
return this
}
stereo () {
this.Lx0 = this.x0 * .5 + this.Lx0
this.Rx0 = this.x0 * .5 + this.Rx0
this.x0 = 0
return this //._stereo
}
grp () {
return this
}
end () {
return this
}
// val (x=0) {
// this.x0 = (+x).toFinite()
// return this
// }
val (x=0) {
this.Lx0 = this.Rx0 = (+x).toFinite()
return this //._stereo
}
// vol (x=1) {
// this.x0 *= x
// return this
// }
vol (x=1) {
this.Lx0 *= x
this.Rx0 *= x
return this //._stereo
}
mod (x=1,offset=0) {
x = x * 4
this.t %= x
this.p = (n % (br * x))|0
if (this.p === 0) {
this._wavetables.fill(offset*_wavetable_len)
}
return this
}
// exp (x=1) {
// this.x0 *= Math.exp(-this.t * x)
// return this
// }
exp (x=1) {
let exp = Math.exp(-this.t * x)
this.Lx0 *= exp
this.Rx0 *= exp
return this //._stereo
}
// abs () {
// this.x0 = Math.abs(this.x0)
// return this
// }
abs () {
this.Lx0 = Math.abs(this.Lx0)
this.Rx0 = Math.abs(this.Rx0)
return this //._stereo
}
// tanh (x=1) {
// this.x0 = Math.tanh(this.x0 * x)
// return this
// }
tanh (x=1) {
this.Lx0 = Math.tanh(this.Lx0 * x)
this.Rx0 = Math.tanh(this.Rx0 * x)
return this //._stereo
}
// atan (x=1) {
// this.x0 = (2 / Math.PI)*Math.atan((Math.PI / 2) * this.x0 * x)
// return this
// }
atan (x=1) {
this.Lx0 = (2 / Math.PI)*Math.atan((Math.PI / 2) * this.Lx0 * x)
this.Rx0 = (2 / Math.PI)*Math.atan((Math.PI / 2) * this.Rx0 * x)
return this //._stereo
}
// soft (x=1) {
// this.x0 = this.x0 / ((1/x) + Math.abs(this.x0))
// return this
// }
soft (x=1) {
this.Lx0 = this.Lx0 / ((1/x) + Math.abs(this.Lx0))
this.Rx0 = this.Rx0 / ((1/x) + Math.abs(this.Rx0))
return this //._stereo
}
// on (x=1, measure=1/4, count=x) {
// return (t/(measure*4)|0)%count === x-1
// ? this
// : this._ignoreNext
// }
on (x=1, measure=1/4, count=x) {
return (t/(measure*4)|0)%count === x-1
? this //._stereo
: this._ignoreNext //Stereo
}
// TODO: improve this
// play (x,offset=0,speed=1,mod) {
// let N = mod ?? x.length
// this.x0 = x[0][(( ( (this.p+offset)*speed) % N + N) % N)|0] ?? 0
// return this
// }
play (x,offset=0,speed=1,mod) {
let N = mod ?? x[0].length
let p = (( ( (this.p+offset)*speed) % N + N) % N)|0
this.Lx0 = (x[0][p] ?? 0) * .5
this.Rx0 = ((x[1] ?? x[0])[p] ?? 0) * .5
return this //._stereo
}
// widen (x=.5) {
// let half = this.x0 * .5
// this._widen_buffer[n & 255] = half
// this.Lx0 = half
// this.Rx0 = this._widen_buffer[(n+((1-x)*256)) & 255]
// return this._stereo
// }
widen (x=.5) {
this._widen_buffer[n & 255] = this.Rx0
this.Rx0 = this._widen_buffer[(n+((1-x)*256)) & 255]
return this //._stereo
}
// delay (measure=1/16,feedback=.5,amt=.5) {
// let d = _delays[_delays_i++]
// this.x0 = d.delay((bar*measure)|0).feedback(feedback).run(this.x0, amt)
// return this
// }
delay (measure=1/16,feedback=.5,amt=.5) {
let Ld = _delays[_delays_i++]
let Rd = _delays[_delays_i++]
this.Lx0 = Ld.delay((bar*measure)|0).feedback(feedback).run(this.Lx0, amt)
this.Rx0 = Rd.delay((bar*measure)|0).feedback(feedback).run(this.Rx0, amt)
return this //._stereo
}
// daverb (x={}) {
// let d = _daverbs[_daverbs_i++]
// let LR = d.process(this.x0*.5, this.x0*.5, x)
// this.Lx0 = LR[0]
// this.Rx0 = LR[1]
// return this._stereo
// }
daverb (x={}) {
let d = _daverbs[_daverbs_i++]
let LR = d.process(this.Lx0, this.Rx0, x)
this.Lx0 = LR[0]
this.Rx0 = LR[1]
return this //._stereo
}
// panout (x=1,LR=0) { // -1..+1 0=center
// main.Lx0 += this.x0 * x * (1-(.5 + .5*LR))
// main.Rx0 += this.x0 * x * (.5 + .5*LR)
// return this
// }
// out (x=1) {
// main.x0 += this.x0 * x
// return this
// }
out (x=1,LR=0) {
main.Lx0 += this.Lx0 * x * (2-(1 + 1*LR))
main.Rx0 += this.Rx0 * x * (1 + 1*LR)
return this
}
plot (x=1) {
if (i === 0) {
this._plot_buffer.fill(0)
}
let co = bar / (2048*x)
if (i === bar - 1) {
worker.plot(this._plot_buffer, (1/x))
return this
}
if ((i % co)|0 === 0) {
this._plot_buffer[(i/co)|0] = (this.Lx0 + this.Rx0) * .5
}
return this
}
valueOf () {
return (this.Lx0 + this.Rx0) * .5
}
}
// aliases
Sound.prototype.mul = Sound.prototype.vol
// Sound.prototype.smul = Sound.prototype.svol
// Object.assign(Sound.prototype, Biquad)
Object.keys(Biquad).forEach(m => {
Sound.prototype[m] = function (a0, a1, a2, a3) {
this.Lx0 = Biquad[m].call({ x0: this.Lx0 }).x0
this.Rx0 = Biquad[m].call({ x0: this.Rx0 }).x0
return this //._stereo
}
})
self._wavetable = {}
self._wavetable_len = 44100
Object.keys(Oscs).forEach(osc => {
const table =
_wavetable[osc] =
_wavetable[osc] = new Float32Array(_wavetable_len)
for (let i = 0, t = 0; i < _wavetable_len; i++) {
t = i / _wavetable_len
if (osc === 'noise') {
table[i] = Oscs[osc].call(i, 1)
} else {
table[i] = Oscs[osc].call(t, 1)
}
}
// Sound.prototype[osc] = new Function('x=1', `
// let index = this._wavetables[this._wavetables_i]
// this._wavetables[this._wavetables_i++] = (index + x) % 44100
// this.x0 = _wavetable.${osc}[index|0]
// return this
// `)
Sound.prototype[osc] = new Function('x=1', `
let index = this._wavetables[this._wavetables_i]
this._wavetables[this._wavetables_i++] = (index + x) % 44100
this.Lx0 = this.Rx0 = _wavetable.${osc}[index|0] * .5
return this //._stereo
`)
})
const N = Number.prototype
Object.defineProperty(N, 'note', {
get () {
return Math.pow(2, (this - 57)/12) * 440
},
set () {},
})
const S = String.prototype
Object.defineProperty(S, 'sample', {
get () {
let s = samples[this]
if (!s) {
s = samples[this] = zeroSample
worker.fetchSample(this)
}
return s
},
set () {},
})
Object.defineProperty(S, 'pat', {
get () {
let pat = patterns[this] = patterns[this] ?? parsePattern(this)
return pat
},
set () {},
})
Object.defineProperty(S, 'note', {
get () {
return stringToNote(this)
},
set () {},
})
S.seq = function (x) {
return this.pat.seq(x)
}
S.slide = function (x) {
return this.pat.slide(x)
}
const A = Array.prototype
A.play = function (x=1,offset=0) {
let N = this.length
return this[(( (n*x+offset) % N + N) % N)|0]
}
A.seq = function (x) {
let N = this.length
return this[(( (t*(1/(x*4))) % N + N) % N)|0]
}
A.slide = function (x,speed=1) {
let N = this.length
let pos = (( (t*(1/(x*4))) % N + N) % N)
let now = pos|0
let next = (((now + 1) % N + N) % N)|0
let alpha = pos - now
return this[now]+((this[next]-this[now])*Math.pow(alpha, speed))
}
const T = Object.getPrototypeOf(Float32Array).prototype // TypedArray.prototype
T.play = A.play
T.seq = A.seq
T.slide = A.slide
export default Sound