1 | import { ToneAudioWorklet, } from "../core/worklet/ToneAudioWorklet.js";
|
2 | import { Effect } from "./Effect.js";
|
3 | import { Gain } from "../core/context/Gain.js";
|
4 | import { optionsFromArguments } from "../core/util/Defaults.js";
|
5 | import { connectSeries } from "../core/context/ToneAudioNode.js";
|
6 | import { Param } from "../core/context/Param.js";
|
7 | import { workletName } from "./BitCrusher.worklet.js";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export 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 |
|
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 |
|
46 |
|
47 | class 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 |
|
\ | No newline at end of file |