1 | import { LowpassCombFilter } from "../component/filter/LowpassCombFilter.js";
|
2 | import { deepMerge } from "../core/util/Defaults.js";
|
3 | import { optionsFromArguments } from "../core/util/Defaults.js";
|
4 | import { Noise } from "../source/Noise.js";
|
5 | import { Instrument } from "./Instrument.js";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export class PluckSynth extends Instrument {
|
17 | constructor() {
|
18 | const options = optionsFromArguments(PluckSynth.getDefaults(), arguments);
|
19 | super(options);
|
20 | this.name = "PluckSynth";
|
21 | this._noise = new Noise({
|
22 | context: this.context,
|
23 | type: "pink",
|
24 | });
|
25 | this.attackNoise = options.attackNoise;
|
26 | this._lfcf = new LowpassCombFilter({
|
27 | context: this.context,
|
28 | dampening: options.dampening,
|
29 | resonance: options.resonance,
|
30 | });
|
31 | this.resonance = options.resonance;
|
32 | this.release = options.release;
|
33 | this._noise.connect(this._lfcf);
|
34 | this._lfcf.connect(this.output);
|
35 | }
|
36 | static getDefaults() {
|
37 | return deepMerge(Instrument.getDefaults(), {
|
38 | attackNoise: 1,
|
39 | dampening: 4000,
|
40 | resonance: 0.7,
|
41 | release: 1,
|
42 | });
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | get dampening() {
|
50 | return this._lfcf.dampening;
|
51 | }
|
52 | set dampening(fq) {
|
53 | this._lfcf.dampening = fq;
|
54 | }
|
55 | triggerAttack(note, time) {
|
56 | const freq = this.toFrequency(note);
|
57 | time = this.toSeconds(time);
|
58 | const delayAmount = 1 / freq;
|
59 | this._lfcf.delayTime.setValueAtTime(delayAmount, time);
|
60 | this._noise.start(time);
|
61 | this._noise.stop(time + delayAmount * this.attackNoise);
|
62 | this._lfcf.resonance.cancelScheduledValues(time);
|
63 | this._lfcf.resonance.setValueAtTime(this.resonance, time);
|
64 | return this;
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | triggerRelease(time) {
|
70 | this._lfcf.resonance.linearRampTo(0, this.release, time);
|
71 | return this;
|
72 | }
|
73 | dispose() {
|
74 | super.dispose();
|
75 | this._noise.dispose();
|
76 | this._lfcf.dispose();
|
77 | return this;
|
78 | }
|
79 | }
|
80 |
|
\ | No newline at end of file |