UNPKG

2.82 kBJavaScriptView Raw
1import { ToneAudioWorklet, } from "../core/worklet/ToneAudioWorklet.js";
2import { Effect } from "./Effect.js";
3import { Gain } from "../core/context/Gain.js";
4import { optionsFromArguments } from "../core/util/Defaults.js";
5import { connectSeries } from "../core/context/ToneAudioNode.js";
6import { Param } from "../core/context/Param.js";
7import { workletName } from "./BitCrusher.worklet.js";
8/**
9 * BitCrusher down-samples the incoming signal to a different bit depth.
10 * Lowering the bit depth of the signal creates distortion. Read more about BitCrushing
11 * on [Wikipedia](https://en.wikipedia.org/wiki/Bitcrusher).
12 * @example
13 * // initialize crusher and route a synth through it
14 * const crusher = new Tone.BitCrusher(4).toDestination();
15 * const synth = new Tone.Synth().connect(crusher);
16 * synth.triggerAttackRelease("C2", 2);
17 *
18 * @category Effect
19 */
20export class BitCrusher extends Effect {
21 constructor() {
22 const options = optionsFromArguments(BitCrusher.getDefaults(), arguments, ["bits"]);
23 super(options);
24 this.name = "BitCrusher";
25 this._bitCrusherWorklet = new BitCrusherWorklet({
26 context: this.context,
27 bits: options.bits,
28 });
29 // connect it up
30 this.connectEffect(this._bitCrusherWorklet);
31 this.bits = this._bitCrusherWorklet.bits;
32 }
33 static getDefaults() {
34 return Object.assign(Effect.getDefaults(), {
35 bits: 4,
36 });
37 }
38 dispose() {
39 super.dispose();
40 this._bitCrusherWorklet.dispose();
41 return this;
42 }
43}
44/**
45 * Internal class which creates an AudioWorklet to do the bit crushing
46 */
47class BitCrusherWorklet extends ToneAudioWorklet {
48 constructor() {
49 const options = optionsFromArguments(BitCrusherWorklet.getDefaults(), arguments);
50 super(options);
51 this.name = "BitCrusherWorklet";
52 this.input = new Gain({ context: this.context });
53 this.output = new Gain({ context: this.context });
54 this.bits = new Param({
55 context: this.context,
56 value: options.bits,
57 units: "positive",
58 minValue: 1,
59 maxValue: 16,
60 param: this._dummyParam,
61 swappable: true,
62 });
63 }
64 static getDefaults() {
65 return Object.assign(ToneAudioWorklet.getDefaults(), {
66 bits: 12,
67 });
68 }
69 _audioWorkletName() {
70 return workletName;
71 }
72 onReady(node) {
73 connectSeries(this.input, node, this.output);
74 const bits = node.parameters.get("bits");
75 this.bits.setParam(bits);
76 }
77 dispose() {
78 super.dispose();
79 this.input.dispose();
80 this.output.dispose();
81 this.bits.dispose();
82 return this;
83 }
84}
85//# sourceMappingURL=BitCrusher.js.map
\No newline at end of file