-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRandomNoiseProcessor.js
77 lines (69 loc) · 1.88 KB
/
RandomNoiseProcessor.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
const tempo = 30;
const types = {
pink: "pink",
brown: "brown",
white: "white",
}
function generateBrownNoise(buffer) {
let lastOut = 0.0;
for (let i = 0; i < buffer.length; i++) {
const whiteNoise = (Math.random() * 2 - 1) / tempo;
buffer[i] = (lastOut + (0.02 * whiteNoise)) / 1.02;
lastOut = buffer[i];
buffer[i] *= 3.5; // Amplify the sound
}
}
// Function to generate Pink noise
function generatePinkNoise(buffer) {
let b0 = 0;
let b1 = 0;
let b2 = 0;
let b3 = 0;
let b4 = 0;
let b5 = 0;
let b6 = 0;
for (let i = 0; i < buffer.length; i++) {
const whiteNoise = (Math.random() * 2 - 1) / tempo;
b0 = 0.99886 * b0 + whiteNoise * 0.0555179;
b1 = 0.99332 * b1 + whiteNoise * 0.0750759;
b2 = 0.96900 * b2 + whiteNoise * 0.1538520;
b3 = 0.86650 * b3 + whiteNoise * 0.3104856;
b4 = 0.55000 * b4 + whiteNoise * 0.5329522;
b5 = -0.7616 * b5 - whiteNoise * 0.0168980;
buffer[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + whiteNoise * 0.5362;
buffer[i] *= 0.11; // Amplify the sound
b6 = whiteNoise * 0.115926;
}
}
// Function to generate White noise
function generateWhiteNoise(buffer) {
for (let i = 0; i < buffer.length; i++) {
buffer[i] = (Math.random() * 2 - 1) / tempo;
}
}
class RandomNoiseProcessor extends AudioWorkletProcessor {
constructor (options) {
super();
this.type = options.processorOptions.type;
};
process(inputs, outputs) {
const output = outputs[0];
output.forEach((channel) => {
switch (this.type) {
case types.brown:
generateBrownNoise(channel);
break;
case types.pink:
generatePinkNoise(channel);
break;
case types.white:
generateWhiteNoise(channel);
break;
default:
break;
}
});
return true;
}
}
registerProcessor("RandomNoiseProcessor", RandomNoiseProcessor);