2019-09-01 06:27:50 +00:00
|
|
|
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";
|
2019-09-01 06:27:50 +00:00
|
|
|
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";
|
2021-12-18 22:31:00 +00:00
|
|
|
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-01 06:27:50 +00:00
|
|
|
|
2019-09-17 17:52:57 +00:00
|
|
|
export interface NoiseSynthOptions extends InstrumentOptions {
|
|
|
|
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
|
|
|
|
noise: Omit<NoiseOptions, keyof ToneAudioNodeOptions>;
|
2019-09-01 06:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-18 22:31:00 +00:00
|
|
|
* 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
|
|
|
|
* +-------+ +-------------------+
|
|
|
|
* ```
|
2019-09-01 06:27:50 +00:00
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* 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
|
2019-09-01 06:27:50 +00:00
|
|
|
*/
|
|
|
|
export class NoiseSynth extends Instrument<NoiseSynthOptions> {
|
|
|
|
readonly name = "NoiseSynth";
|
|
|
|
|
2019-09-01 08:58:56 +00:00
|
|
|
/**
|
|
|
|
* The noise source.
|
|
|
|
*/
|
2019-09-01 06:27:50 +00:00
|
|
|
readonly noise: Noise;
|
|
|
|
|
2019-09-01 08:58:56 +00:00
|
|
|
/**
|
|
|
|
* The amplitude envelope.
|
|
|
|
*/
|
2019-09-01 06:27:50 +00:00
|
|
|
readonly envelope: AmplitudeEnvelope;
|
|
|
|
|
2021-12-18 22:31:00 +00:00
|
|
|
constructor(options?: RecursivePartial<NoiseSynthOptions>);
|
2019-09-01 06:27:50 +00:00
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(NoiseSynth.getDefaults(), arguments));
|
2021-12-18 22:31:00 +00:00
|
|
|
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
|
|
|
|
2021-12-18 22:31:00 +00:00
|
|
|
this.envelope = new AmplitudeEnvelope(
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
context: this.context,
|
|
|
|
},
|
|
|
|
options.envelope
|
|
|
|
)
|
|
|
|
);
|
2019-09-01 06:27:50 +00:00
|
|
|
|
|
|
|
// 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(
|
2021-12-18 22:31:00 +00:00
|
|
|
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,
|
2021-12-18 22:31:00 +00:00
|
|
|
}
|
2019-09-17 17:52:57 +00:00
|
|
|
),
|
|
|
|
noise: Object.assign(
|
2021-12-18 22:31:00 +00:00
|
|
|
omitFromObject(
|
|
|
|
Noise.getDefaults(),
|
|
|
|
Object.keys(Source.getDefaults())
|
|
|
|
),
|
2019-09-17 17:52:57 +00:00
|
|
|
{
|
|
|
|
type: "white",
|
2021-12-18 22:31:00 +00:00
|
|
|
}
|
2019-09-17 17:52:57 +00:00
|
|
|
),
|
|
|
|
});
|
2019-09-01 06:27:50 +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
|
2020-04-17 02:24:18 +00:00
|
|
|
* 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 {
|
2019-09-01 06:27:50 +00:00
|
|
|
time = this.toSeconds(time);
|
2019-09-01 08:58:56 +00:00
|
|
|
// the envelopes
|
2019-09-01 06:27:50 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
2019-09-01 08:58:56 +00:00
|
|
|
// start the noise
|
2019-09-01 06:27:50 +00:00
|
|
|
this.noise.start(time);
|
|
|
|
if (this.envelope.sustain === 0) {
|
2021-12-18 22:31:00 +00:00
|
|
|
this.noise.stop(
|
|
|
|
time +
|
|
|
|
this.toSeconds(this.envelope.attack) +
|
|
|
|
this.toSeconds(this.envelope.decay)
|
|
|
|
);
|
2019-09-01 06:27:50 +00:00
|
|
|
}
|
|
|
|
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 {
|
2019-09-01 06:27:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-09-01 06:27:50 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-12-18 22:31:00 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2019-09-01 06:27:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|