Skip to content

Commit

Permalink
feat: Adding triggerRelease to PluckSynth
Browse files Browse the repository at this point in the history
works by ramping the resonance down over the 'release' duration
  • Loading branch information
tambien committed Sep 29, 2019
1 parent e931f3b commit 04405af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
18 changes: 14 additions & 4 deletions Tone/instrument/PluckSynth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,25 @@ describe("PluckSynth", () => {
return CompareToFile(() => {
const synth = new PluckSynth().toDestination();
synth.triggerAttack("C4");
}, "pluckSynth.wav", 0.26);
}, "pluckSynth.wav", 0.02);
});

it("matches a file with release", () => {
return CompareToFile(() => {
const synth = new PluckSynth({
resonance: 0.97,
release: 0.2
}).toDestination();
synth.triggerAttackRelease("C4", 0.6);
}, "pluckSynth2.wav", 0.06);
});

context("API", () => {

it("can get and set resonance", () => {
const pluck = new PluckSynth();
pluck.resonance.value = 0.4;
expect(pluck.resonance.value).to.be.closeTo(0.4, 0.001);
pluck.resonance = 0.4;
expect(pluck.resonance).to.be.closeTo(0.4, 0.001);
pluck.dispose();
});

Expand All @@ -45,7 +55,7 @@ describe("PluckSynth", () => {
resonance: 0.5,
});
expect(pluck.dampening).to.be.closeTo(300, 0.1);
expect(pluck.resonance.value).to.be.closeTo(0.5, 0.001);
expect(pluck.resonance).to.be.closeTo(0.5, 0.001);
pluck.dispose();
});
});
Expand Down
27 changes: 17 additions & 10 deletions Tone/instrument/PluckSynth.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Param } from "../core/context/Param";
import { Frequency, NormalRange, Time } from "../core/type/Units";
import { LowpassCombFilter } from "../component/filter/LowpassCombFilter";
import { deepMerge } from "../core/util/Defaults";
import { optionsFromArguments } from "../core/util/Defaults";
import { RecursivePartial } from "../core/util/Interface";
import { Signal } from "../signal/Signal";
import { Noise } from "../source/Noise";
import { Instrument, InstrumentOptions } from "./Instrument";

export interface PluckSynthOptions extends InstrumentOptions {
attackNoise: number;
dampening: Frequency;
resonance: NormalRange;
release: Time;
}

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

/**
* The resonance control.
* The amount of resonance of the pluck. Also correlates to the sustain duration.
*/
readonly resonance: Param<NormalRange>;
resonance: NormalRange;

/**
* The release time which corresponds to a resonance ramp down to 0
*/
release: Time;

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

this.resonance = this._lfcf.resonance;
this.resonance = options.resonance;
this.release = options.release;

this._noise.connect(this._lfcf);
this._lfcf.connect(this.output);
Expand All @@ -75,6 +79,7 @@ export class PluckSynth extends Instrument<PluckSynthOptions> {
attackNoise: 1,
dampening: 4000,
resonance: 0.7,
release: 1,
});
}

Expand All @@ -97,14 +102,16 @@ export class PluckSynth extends Instrument<PluckSynthOptions> {
this._lfcf.delayTime.setValueAtTime(delayAmount, time);
this._noise.start(time);
this._noise.stop(time + delayAmount * this.attackNoise);
this._lfcf.resonance.cancelScheduledValues(time);
this._lfcf.resonance.setValueAtTime(this.resonance, time);
return this;
}

/**
* PluckSynths' trigger release method doesn't do anything.
* Ramp down the [[resonance]] to 0 over the duration of the release time.
*/
triggerRelease(): this{
// does nothing
triggerRelease(time?: Time): this{
this._lfcf.resonance.linearRampTo(0, this.release, time);
return this;
}

Expand Down
Binary file added test/audio/compare/pluckSynth2.wav
Binary file not shown.

0 comments on commit 04405af

Please sign in to comment.