-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit-crusher.js
55 lines (50 loc) · 1.54 KB
/
bit-crusher.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
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* A AudioWorklet-based BitCrusher demo from the spec example.
*
* @class BitCrusher
* @extends AudioWorkletProcessor
* @see https://webaudio.github.io/web-audio-api/#the-bitcrusher-node
*/
class BitCrusher extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{ name: "bitDepth", defaultValue: 12, minValue: 1, maxValue: 16 },
{
name: "frequencyReduction",
defaultValue: 0.5,
minValue: 0,
maxValue: 1,
},
];
}
constructor(options) {
super(options);
this.phase_ = 0;
this.lastSampleValue_ = 0;
}
process(inputs, outputs, parameters) {
let input = inputs[0];
let output = outputs[0];
let bitDepth = parameters.bitDepth;
let frequencyReduction = parameters.frequencyReduction;
for (let channel = 0; channel < input.length; ++channel) {
let inputChannel = input[channel];
let outputChannel = output[channel];
for (let i = 0; i < inputChannel.length; ++i) {
let step = Math.pow(0.5, bitDepth[i]);
this.phase_ += frequencyReduction[i];
if (this.phase_ >= 1.0) {
this.phase_ -= 1.0;
this.lastSampleValue_ =
step * Math.floor(inputChannel[i] / step + 0.5);
}
outputChannel[i] = this.lastSampleValue_;
}
}
return true;
}
}
registerProcessor("bit-crusher", BitCrusher);