Tone.js/Tone/instrument/NoiseSynth.ts

167 lines
4.1 KiB
TypeScript
Raw Normal View History

import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope";
import { NormalRange, Time } from "../core/type/Units";
2019-09-17 17:54:24 +00:00
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
import { RecursivePartial } from "../core/util/Interface";
2019-09-17 17:52:57 +00:00
import { Noise, NoiseOptions } from "../source/Noise";
import { Instrument, InstrumentOptions } from "./Instrument";
import {
ToneAudioNode,
ToneAudioNodeOptions,
} from "../core/context/ToneAudioNode";
2019-09-17 17:54:24 +00:00
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
import { Source } from "../source/Source";
2019-09-17 17:52:57 +00:00
export interface NoiseSynthOptions extends InstrumentOptions {
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
noise: Omit<NoiseOptions, keyof ToneAudioNodeOptions>;
}
/**
* Tone.NoiseSynth is composed of [[Noise]] through an [[AmplitudeEnvelope]].
2019-10-23 03:04:52 +00:00
* ```
2019-09-17 17:52:57 +00:00
* +-------+ +-------------------+
* | Noise +>--> AmplitudeEnvelope +>--> Output
* +-------+ +-------------------+
* ```
* @example
* const noiseSynth = new Tone.NoiseSynth().toDestination();
2019-10-25 20:54:33 +00:00
* noiseSynth.triggerAttackRelease("8n", 0.05);
2019-09-17 17:55:51 +00:00
* @category Instrument
*/
export class NoiseSynth extends Instrument<NoiseSynthOptions> {
readonly name = "NoiseSynth";
2019-09-01 08:58:56 +00:00
/**
* The noise source.
*/
readonly noise: Noise;
2019-09-01 08:58:56 +00:00
/**
* The amplitude envelope.
*/
readonly envelope: AmplitudeEnvelope;
constructor(options?: RecursivePartial<NoiseSynthOptions>);
constructor() {
super(optionsFromArguments(NoiseSynth.getDefaults(), arguments));
const options = optionsFromArguments(
NoiseSynth.getDefaults(),
arguments
);
this.noise = new Noise(
Object.assign(
{
context: this.context,
},
options.noise
)
);
2019-09-17 17:54:24 +00:00
this.envelope = new AmplitudeEnvelope(
Object.assign(
{
context: this.context,
},
options.envelope
)
);
// connect the noise to the output
this.noise.chain(this.envelope, this.output);
}
static getDefaults(): NoiseSynthOptions {
2019-09-17 17:52:57 +00:00
return Object.assign(Instrument.getDefaults(), {
envelope: Object.assign(
omitFromObject(
Envelope.getDefaults(),
Object.keys(ToneAudioNode.getDefaults())
),
2019-09-17 17:52:57 +00:00
{
2019-09-17 17:54:24 +00:00
decay: 0.1,
sustain: 0.0,
}
2019-09-17 17:52:57 +00:00
),
noise: Object.assign(
omitFromObject(
Noise.getDefaults(),
Object.keys(Source.getDefaults())
),
2019-09-17 17:52:57 +00:00
{
type: "white",
}
2019-09-17 17:52:57 +00:00
),
});
}
2019-09-01 08:58:56 +00:00
/**
* Start the attack portion of the envelopes. Unlike other
* instruments, Tone.NoiseSynth doesn't have a note.
* @example
* const noiseSynth = new Tone.NoiseSynth().toDestination();
2019-09-01 08:58:56 +00:00
* noiseSynth.triggerAttack();
*/
2019-10-16 18:59:03 +00:00
triggerAttack(time?: Time, velocity: NormalRange = 1): this {
time = this.toSeconds(time);
2019-09-01 08:58:56 +00:00
// the envelopes
this.envelope.triggerAttack(time, velocity);
2019-09-01 08:58:56 +00:00
// start the noise
this.noise.start(time);
if (this.envelope.sustain === 0) {
this.noise.stop(
time +
this.toSeconds(this.envelope.attack) +
this.toSeconds(this.envelope.decay)
);
}
return this;
}
2019-09-01 08:58:56 +00:00
/**
* Start the release portion of the envelopes.
*/
2019-10-16 18:59:03 +00:00
triggerRelease(time?: Time): this {
time = this.toSeconds(time);
this.envelope.triggerRelease(time);
this.noise.stop(time + this.toSeconds(this.envelope.release));
return this;
}
sync(): this {
2020-08-26 08:58:14 +00:00
if (this._syncState()) {
this._syncMethod("triggerAttack", 0);
this._syncMethod("triggerRelease", 0);
}
return 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);
duration = this.toSeconds(duration);
this.triggerAttack(time, velocity);
this.triggerRelease(time + duration);
return this;
}
dispose(): this {
super.dispose();
this.noise.dispose();
this.envelope.dispose();
return this;
}
}