Skip to content

Commit 04405af

Browse files
committed
feat: Adding triggerRelease to PluckSynth
works by ramping the resonance down over the 'release' duration
1 parent e931f3b commit 04405af

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

Tone/instrument/PluckSynth.test.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,25 @@ describe("PluckSynth", () => {
1313
return CompareToFile(() => {
1414
const synth = new PluckSynth().toDestination();
1515
synth.triggerAttack("C4");
16-
}, "pluckSynth.wav", 0.26);
16+
}, "pluckSynth.wav", 0.02);
17+
});
18+
19+
it("matches a file with release", () => {
20+
return CompareToFile(() => {
21+
const synth = new PluckSynth({
22+
resonance: 0.97,
23+
release: 0.2
24+
}).toDestination();
25+
synth.triggerAttackRelease("C4", 0.6);
26+
}, "pluckSynth2.wav", 0.06);
1727
});
1828

1929
context("API", () => {
2030

2131
it("can get and set resonance", () => {
2232
const pluck = new PluckSynth();
23-
pluck.resonance.value = 0.4;
24-
expect(pluck.resonance.value).to.be.closeTo(0.4, 0.001);
33+
pluck.resonance = 0.4;
34+
expect(pluck.resonance).to.be.closeTo(0.4, 0.001);
2535
pluck.dispose();
2636
});
2737

@@ -45,7 +55,7 @@ describe("PluckSynth", () => {
4555
resonance: 0.5,
4656
});
4757
expect(pluck.dampening).to.be.closeTo(300, 0.1);
48-
expect(pluck.resonance.value).to.be.closeTo(0.5, 0.001);
58+
expect(pluck.resonance).to.be.closeTo(0.5, 0.001);
4959
pluck.dispose();
5060
});
5161
});

Tone/instrument/PluckSynth.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import { Param } from "../core/context/Param";
21
import { Frequency, NormalRange, Time } from "../core/type/Units";
32
import { LowpassCombFilter } from "../component/filter/LowpassCombFilter";
43
import { deepMerge } from "../core/util/Defaults";
54
import { optionsFromArguments } from "../core/util/Defaults";
65
import { RecursivePartial } from "../core/util/Interface";
7-
import { Signal } from "../signal/Signal";
86
import { Noise } from "../source/Noise";
97
import { Instrument, InstrumentOptions } from "./Instrument";
108

119
export interface PluckSynthOptions extends InstrumentOptions {
1210
attackNoise: number;
1311
dampening: Frequency;
1412
resonance: NormalRange;
13+
release: Time;
1514
}
1615

1716
/**
18-
* Karplus-String string synthesis. Often out of tune.
19-
*
17+
* Karplus-String string synthesis.
2018
* @example
2119
* var plucky = new Tone.PluckSynth().toDestination();
2220
* plucky.triggerAttack("C4");
@@ -41,9 +39,14 @@ export class PluckSynth extends Instrument<PluckSynthOptions> {
4139
attackNoise: number;
4240

4341
/**
44-
* The resonance control.
42+
* The amount of resonance of the pluck. Also correlates to the sustain duration.
4543
*/
46-
readonly resonance: Param<NormalRange>;
44+
resonance: NormalRange;
45+
46+
/**
47+
* The release time which corresponds to a resonance ramp down to 0
48+
*/
49+
release: Time;
4750

4851
constructor(options?: RecursivePartial<PluckSynthOptions>)
4952
constructor() {
@@ -64,7 +67,8 @@ export class PluckSynth extends Instrument<PluckSynthOptions> {
6467
resonance: options.resonance,
6568
});
6669

67-
this.resonance = this._lfcf.resonance;
70+
this.resonance = options.resonance;
71+
this.release = options.release;
6872

6973
this._noise.connect(this._lfcf);
7074
this._lfcf.connect(this.output);
@@ -75,6 +79,7 @@ export class PluckSynth extends Instrument<PluckSynthOptions> {
7579
attackNoise: 1,
7680
dampening: 4000,
7781
resonance: 0.7,
82+
release: 1,
7883
});
7984
}
8085

@@ -97,14 +102,16 @@ export class PluckSynth extends Instrument<PluckSynthOptions> {
97102
this._lfcf.delayTime.setValueAtTime(delayAmount, time);
98103
this._noise.start(time);
99104
this._noise.stop(time + delayAmount * this.attackNoise);
105+
this._lfcf.resonance.cancelScheduledValues(time);
106+
this._lfcf.resonance.setValueAtTime(this.resonance, time);
100107
return this;
101108
}
102109

103110
/**
104-
* PluckSynths' trigger release method doesn't do anything.
111+
* Ramp down the [[resonance]] to 0 over the duration of the release time.
105112
*/
106-
triggerRelease(): this{
107-
// does nothing
113+
triggerRelease(time?: Time): this{
114+
this._lfcf.resonance.linearRampTo(0, this.release, time);
108115
return this;
109116
}
110117

test/audio/compare/pluckSynth2.wav

86.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)