UNPKG

2.61 kBJavaScriptView Raw
1import { LowpassCombFilter } from "../component/filter/LowpassCombFilter.js";
2import { deepMerge } from "../core/util/Defaults.js";
3import { optionsFromArguments } from "../core/util/Defaults.js";
4import { Noise } from "../source/Noise.js";
5import { Instrument } from "./Instrument.js";
6/**
7 * Karplus-Strong string synthesis.
8 * @example
9 * const plucky = new Tone.PluckSynth().toDestination();
10 * plucky.triggerAttack("C4", "+0.5");
11 * plucky.triggerAttack("C3", "+1");
12 * plucky.triggerAttack("C2", "+1.5");
13 * plucky.triggerAttack("C1", "+2");
14 * @category Instrument
15 */
16export 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 * The dampening control. i.e. the lowpass filter frequency of the comb filter
46 * @min 0
47 * @max 7000
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 * Ramp down the {@link resonance} to 0 over the duration of the release time.
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//# sourceMappingURL=PluckSynth.js.map
\No newline at end of file