-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
139 lines (120 loc) · 3.06 KB
/
index.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
/**
* Simple white noise generator, inspired by Nick Thompson's
* post on Script Processors (https://medium.com/web-audio/61a836e28b42)
*
* @param {AudioContext} context
* @param {object} opts
* @param {boolean} opts.stereo
* @param {number} opts.bufferSize
* @param {number} opts.channels
*/
function WhiteNoise (context, opts) {
// Defaults
opts = opts || {};
var p = this.meta.params;
this._stereo = opts.stereo != null ? !!opts.stereo : !!this.meta.params.stereo.defaultValue;
this._bufferSize = opts.bufferSize || this.meta.params.bufferSize.defaultValue;
this._channels = opts.channels || this.meta.params.channels.defaultValue;
this._context = context;
var node = context.createBufferSource();
this.source = this.output = node;
node.loop = true;
buildBuffer(this);
}
function buildBuffer (module) {
var buffer = module._context.createBuffer(module.channels, module.bufferSize, module._context.sampleRate);
var rand;
for (var i = 0; i < module.bufferSize; i++) {
rand = Math.random() * 2 - 1;
for (var j = 0; j < module.channels; j++) {
buffer.getChannelData(j)[i] = module.stereo ? Math.random() * 2 - 1 : rand;
}
}
module.source.buffer = buffer;
}
WhiteNoise.prototype = Object.create(null, {
connect: {
value: function (dest) {
this.output.connect( dest.input ? dest.input : dest );
}
},
disconnect: {
value: function () {
this.output.disconnect();
}
},
start: {
value: function () {
var fn = this.source.start ? this.source.start : this.source.noteOn;
buildBuffer(this);
fn.apply(this.source, Array.slice(arguments));
}
},
stop: {
value: function () {
var fn = this.source.stop ? this.source.stop : this.source.noteOff;
fn.apply(this.source, Array.slice(arguments));
}
},
/**
* Module parameter metadata.
*/
meta: {
value: {
name: "white-noise",
type: "generator",
params: {
stereo: {
min: 0,
max: 1,
defaultValue: 1,
type: "bool"
},
bufferSize: {
values: [128, 256, 512, 1024, 2048, 4096, 8192, 16384],
defaultValue: 4096,
type: "enum"
},
channels: {
min: 1,
max: 32,
defaultValue: 2,
type: "int"
}
}
}
},
/**
* Public parameters.
*/
stereo: {
enumerable: true,
get: function () { return this._stereo; },
set: function (value) {
this._stereo = !!value;
buildBuffer(this);
}
},
bufferSize: {
enumerable: true,
get: function () { return this._bufferSize; },
set: function (value) {
this._bufferSize = ~this.meta.params.bufferSize.values.indexOf(value)
? value
: this.meta.params.bufferSize.defaultValue;
buildBuffer(this);
}
},
channels: {
enumerable: true,
get: function () { return this._channels; },
set: function (value) {
this._channels = value;
buildBuffer(this);
}
}
});
/**
* Exports.
*/
module.exports = WhiteNoise;