Adding triggerAttackRelease example

addresses #989
This commit is contained in:
Yotam Mann 2021-12-18 17:31:00 -05:00
parent dd12f771ab
commit 69ab1b91c6

View file

@ -4,7 +4,10 @@ import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
import { RecursivePartial } from "../core/util/Interface"; import { RecursivePartial } from "../core/util/Interface";
import { Noise, NoiseOptions } from "../source/Noise"; import { Noise, NoiseOptions } from "../source/Noise";
import { Instrument, InstrumentOptions } from "./Instrument"; import { Instrument, InstrumentOptions } from "./Instrument";
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode"; import {
ToneAudioNode,
ToneAudioNodeOptions,
} from "../core/context/ToneAudioNode";
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope"; import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
import { Source } from "../source/Source"; import { Source } from "../source/Source";
@ -26,7 +29,6 @@ export interface NoiseSynthOptions extends InstrumentOptions {
* @category Instrument * @category Instrument
*/ */
export class NoiseSynth extends Instrument<NoiseSynthOptions> { export class NoiseSynth extends Instrument<NoiseSynthOptions> {
readonly name = "NoiseSynth"; readonly name = "NoiseSynth";
/** /**
@ -39,17 +41,30 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
*/ */
readonly envelope: AmplitudeEnvelope; readonly envelope: AmplitudeEnvelope;
constructor(options?: RecursivePartial<NoiseSynthOptions>) constructor(options?: RecursivePartial<NoiseSynthOptions>);
constructor() { constructor() {
super(optionsFromArguments(NoiseSynth.getDefaults(), arguments)); super(optionsFromArguments(NoiseSynth.getDefaults(), arguments));
const options = optionsFromArguments(NoiseSynth.getDefaults(), arguments); const options = optionsFromArguments(
this.noise = new Noise(Object.assign({ NoiseSynth.getDefaults(),
arguments
);
this.noise = new Noise(
Object.assign(
{
context: this.context, context: this.context,
}, options.noise)); },
options.noise
)
);
this.envelope = new AmplitudeEnvelope(Object.assign({ this.envelope = new AmplitudeEnvelope(
Object.assign(
{
context: this.context, context: this.context,
}, options.envelope)); },
options.envelope
)
);
// connect the noise to the output // connect the noise to the output
this.noise.chain(this.envelope, this.output); this.noise.chain(this.envelope, this.output);
@ -58,17 +73,23 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
static getDefaults(): NoiseSynthOptions { static getDefaults(): NoiseSynthOptions {
return Object.assign(Instrument.getDefaults(), { return Object.assign(Instrument.getDefaults(), {
envelope: Object.assign( envelope: Object.assign(
omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())), omitFromObject(
Envelope.getDefaults(),
Object.keys(ToneAudioNode.getDefaults())
),
{ {
decay: 0.1, decay: 0.1,
sustain: 0.0, sustain: 0.0,
}, }
), ),
noise: Object.assign( noise: Object.assign(
omitFromObject(Noise.getDefaults(), Object.keys(Source.getDefaults())), omitFromObject(
Noise.getDefaults(),
Object.keys(Source.getDefaults())
),
{ {
type: "white", type: "white",
}, }
), ),
}); });
} }
@ -87,7 +108,11 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
// start the noise // start the noise
this.noise.start(time); this.noise.start(time);
if (this.envelope.sustain === 0) { if (this.envelope.sustain === 0) {
this.noise.stop(time + this.toSeconds(this.envelope.attack) + this.toSeconds(this.envelope.decay)); this.noise.stop(
time +
this.toSeconds(this.envelope.attack) +
this.toSeconds(this.envelope.decay)
);
} }
return this; return this;
} }
@ -110,7 +135,21 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
return this; return this;
} }
triggerAttackRelease(duration: Time, time?: Time, velocity: NormalRange = 1): this { /**
* Trigger the attack and then the release after the duration.
* @param duration The amount of time to hold the note for
* @param time The time the note should start
* @param velocity The volume of the note (0-1)
* @example
* const noiseSynth = new Tone.NoiseSynth().toDestination();
* // hold the note for 0.5 seconds
* noiseSynth.triggerAttackRelease(0.5);
*/
triggerAttackRelease(
duration: Time,
time?: Time,
velocity: NormalRange = 1
): this {
time = this.toSeconds(time); time = this.toSeconds(time);
duration = this.toSeconds(duration); duration = this.toSeconds(duration);
this.triggerAttack(time, velocity); this.triggerAttack(time, velocity);