-
Notifications
You must be signed in to change notification settings - Fork 0
/
worklet.js
95 lines (92 loc) · 2.94 KB
/
worklet.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
const saw = v => v - Math.floor(v);
class BetterOscillatorProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.phase = 0;
this.sync_phase = 0;
this.prev_sync_phase = 0
}
static get parameterDescriptors() {
return [
{
name: "phase",
defaultValue: 0,
max: 1,
min: 0
},
{
name: "duty",
defaultValue: 0.5,
min: 0,
max: 1
},
{
name: "frequency",
defaultValue: 440,
min: Number.EPSILON
},
{
name: "wave",
defaultValue: 3,
min: 0,
max: 3
},
{
name: "sync",
defaultValue: 0,
min: 0,
}
];
}
process(input, outputs, params) {
for (let z = 0; z < outputs.length; z++) {
const out = outputs[z][0];
const outlen = out.length;
const freq = params.frequency.length === 1;
const phase = params.phase.length === 1;
const wave = params.wave.length === 1;
const duty = params.duty.length === 1;
const sync = params.sync.length === 1
const inp = input[0][0].length === 0;
let back = 0
for (let x = 0; x < outlen; x++) {
this.sync_phase = (this.prev_sync_phase) % (params.sync[sync ? 0 : x]/sampleRate)
if (params.sync[sync ? 0 : x] !== 0 && this.prev_sync_phase >= (params.sync[sync ? 0 : x]/sampleRate)) {
this.phase = 0
back = x
}
this.prev_sync_phase = this.sync_phase
const main = (params.frequency[freq ? 0 : x] * (x-back)) / sampleRate;
// noise
if (params.wave[wave ? 0 : x] >= 4) {
out[x] = (Math.random() * 2) - 1
} else if (params.wave[wave ? 0 : x] >= 3) {
// sine wave made using bulit-in Math.sin
out[x] = Math.sin(
(main + this.phase + params.phase[phase ? 0 : x]) * 2 * Math.PI
);
// sawtooth wave using linear piecewise floor
} else if (params.wave[wave ? 0 : x] >= 2) {
out[x] = 2 * saw(main + this.phase + params.phase[phase ? 0 : x]) - 1;
// pulse wave using difference of phase shifted saws and variable DC threshold
} else if (params.wave[wave ? 0 : x] >= 1) {
const temp = main + this.phase + params.phase[phase ? 0 : x];
out[x] = (saw(temp) - saw(temp + params.duty[duty ? 0 : x])) > 0 ? 1 : -1
// triangle wave using absolute value of amplitude shifted sawtooth wave
} else if (params.wave[wave ? 0 : x] >= 0) {
out[x] =
4 *
Math.abs(
saw(main + this.phase + params.phase[phase ? 0 : x]) - 1 / 2
) - 1;
}
this.prev_sync_phase += 1 / sampleRate;
}
this.phase +=
(params.frequency[freq ? 0 : outlen - 1] * outlen) / sampleRate;
this.phase %= sampleRate;
return true;
}
}
}
registerProcessor("better-oscillator", BetterOscillatorProcessor);