-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrainSamplerProcessor.js
293 lines (243 loc) · 9.18 KB
/
GrainSamplerProcessor.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
class GrainSamplerWorkletProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: 'attack',
defaultValue: 4000,
minValue: 0,
maxValue: 44100,
automationRate: 'a-rate',
},
{
name: 'hold',
defaultValue: 7000,
minValue: 0,
maxValue: 44100,
automationRate: 'a-rate',
},
{
name: 'release',
defaultValue: 6000,
minValue: 0,
maxValue: 44100,
automationRate: 'a-rate',
},
// Grain spread
{
name: 'spread',
defaultValue: 30000,
minValue: 0,
maxValue: 44100,
automationRate: 'k-rate',
},
// Grain density
{
name: 'density',
defaultValue: 10,
minValue: 1,
maxValue: 10,
automationRate: 'k-rate',
},
{
name: 'densityJitter',
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: 'k-rate',
},
// Grain reverse
{
name: 'reverse',
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: 'k-rate',
},
{
name: 'freeze',
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: 'k-rate',
},
{
name: 'mix',
defaultValue: 0.3,
minValue: 0,
maxValue: 1,
automationRate: 'a-rate',
},
{
name: 'travel',
defaultValue: 1,
minValue: 0.25,
maxValue: 3,
automationRate: 'a-rate',
},
{
name: 'playbackPosition',
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: 'k-rate',
},
];
}
constructor() {
super();
// Global sampleCount;
this.globalSampleIndex = 0;
// `sampleBuffers` will be set by postMessage
this.sampleBuffers = [];
this.sampleBufferLength = 0;
this.grains = [];
this.grainsCount = 10;
const grainLength = this.getGrainDefaultLength();
for( let i = 0; i < this.grainsCount; ++i ) {
this.grains[ i ] = [
{
offset: Math.floor( ( 44100 / this.grainsCount ) * i ),
position: 0,
playbackPosition: 0,
length: grainLength,
smear: 0,
},
{
offset: Math.floor( ( 44100 / this.grainsCount ) * i ),
position: 0,
playbackPosition: 0,
length: grainLength,
smear: 0,
},
];
}
this.port.onmessage = messageEvent => {
messageEvent.data.forEach( ( buffer, index ) => {
this.sampleBuffers[ index ] = new Float32Array( buffer );
} );
this.sampleBufferLength = this.sampleBuffers[ 0 ].length;
console.log( this.sampleBuffers );
};
}
getGrainDefaultLength() {
return (
this.getParamDefaultValue( 'attack' ) +
this.getParamDefaultValue( 'hold' ) +
this.getParamDefaultValue( 'release' )
);
}
getGrainLength( parameters, sampleIndex ) {
return (
this.getParameterValue( parameters, 'attack', sampleIndex ) +
this.getParameterValue( parameters, 'hold', sampleIndex ) +
this.getParameterValue( parameters, 'release', sampleIndex )
);
}
getParamDescriptor( paramName ) {
return this.constructor.parameterDescriptors.find( d => {
return d.name === paramName;
} );
}
getParamDefaultValue( paramName ) {
return this.getParamDescriptor( paramName ).defaultValue;
}
getParamMaxValue( paramName ) {
return this.getParamDescriptor( paramName ).maxValue;
}
getParameterValue(params, paramName, index) {
const param = params[paramName];
return param.length > 1 ?
param[index] :
param[0];
}
calculateGainForSampleIndex( grainSampleIndex, parameters, sampleIndex ) {
const attackTime = this.getParameterValue( parameters, 'attack', sampleIndex );
const holdTime = this.getParameterValue( parameters, 'hold', sampleIndex );
const releaseTime = this.getParameterValue( parameters, 'release', sampleIndex );
const mix = this.getParameterValue( parameters, 'mix', sampleIndex );
let index = grainSampleIndex;
if( index < attackTime ) {
return ( index / attackTime ) * mix;
}
index -= attackTime;
if( index <= holdTime ) {
return 1 * mix;
}
index -= holdTime;
if( index <= releaseTime ) {
return ( 1 - ( index / releaseTime ) ) * mix;
}
return 0;
}
playGrain( grain, channelIndex, outputChannel, sampleIndex, parameters ) {
const grainChannel = grain[ channelIndex ];
// Calculate where in the sample buffer this grain currently is.
const sampleBufferIndex = Math.round(
grainChannel.position +
grainChannel.playbackPosition +
grainChannel.offset +
grainChannel.smear
);
// Use the above index to fetch the sample value (an audio sample) from the sample buffer(s)
const sampleValue = this.sampleBuffers[ channelIndex ][ sampleBufferIndex % this.sampleBufferLength ];
// Calculate the gain
const gain = this.calculateGainForSampleIndex( grainChannel.position, parameters, sampleIndex );
const reverse = this.getParameterValue( parameters, 'reverse', sampleIndex );
const travel = this.getParameterValue( parameters, 'travel', sampleIndex );
// Write the sample value to the output channel
outputChannel[ sampleIndex ] += sampleValue * gain;
// Now that the sample has been written, the grain's position can be moved
// either forwards or backwards, depending on the direction (reverse or not)
// and travel multiplier.
if( reverse ) {
grainChannel.position -= travel;
if( grainChannel.position < 0 ) {
grainChannel.position = grainChannel.length - 1;
}
}
else {
grainChannel.position += travel;
}
// If the grain's position has hit its limit, reset it.
if( grainChannel.position >= grainChannel.length || grainChannel.position === 0 ) {
// Use the attack, hold, and release values to calculate a new length.
grainChannel.length = this.getGrainLength( parameters, sampleIndex );
// Set new starting position
grainChannel.position = reverse ? grainChannel.length - 1 : 0;
// Calculate a new smear value
const smearValue = Math.random() * this.getParameterValue( parameters, 'spread', sampleIndex );
// Reset both channels
const pos = this.getParameterValue( parameters, 'playbackPosition', sampleIndex );
for( let i = 0, il = grain.length; i < il; ++i ) {
grain[ i ].position = 0;
grain[ i ].playbackPosition = Math.round( pos * this.sampleBufferLength );
grain[ i ].smear = Math.floor( smearValue );
}
}
}
process(inputs, outputs, parameters) {
const outputPort = outputs[0];
const numOutputChannels = outputPort.length;
for (let channelIndex = 0; channelIndex < numOutputChannels; ++channelIndex) {
const sampleBufferChannel = this.sampleBuffers[ channelIndex ];
const outputChannel = outputPort[channelIndex];
const sampleCount = outputChannel.length;
for (let sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
outputChannel[ sampleIndex ] = 0;
const density = this.getParameterValue( parameters, 'density', sampleIndex );
for( let i = 0; i < density; ++i ) {
this.playGrain(
this.grains[ i ],
channelIndex,
outputChannel,
sampleIndex,
parameters
);
}
}
}
this.globalSampleIndex = currentFrame;
return true;
}
}
registerProcessor('GrainSamplerWorkletProcessor', GrainSamplerWorkletProcessor);