2019-10-28 22:12:20 +00:00
|
|
|
import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope";
|
2019-10-29 21:48:59 +00:00
|
|
|
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
|
|
|
|
import { Filter, FilterOptions } from "../component/filter/Filter";
|
2019-10-28 22:12:20 +00:00
|
|
|
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
|
|
|
|
import { readOnly, RecursivePartial } from "../core/util/Interface";
|
2019-10-29 21:48:59 +00:00
|
|
|
import { Monophonic, MonophonicOptions } from "../instrument/Monophonic";
|
2019-10-28 22:12:20 +00:00
|
|
|
import { OmniOscillator } from "../source/oscillator/OmniOscillator";
|
|
|
|
import { Source } from "../source/Source";
|
2019-10-29 21:48:59 +00:00
|
|
|
import { FrequencyEnvelope, FrequencyEnvelopeOptions } from "../component/envelope/FrequencyEnvelope";
|
2019-11-03 22:41:47 +00:00
|
|
|
import { NormalRange, Seconds, Time } from "../core/type/Units";
|
2019-10-29 21:48:59 +00:00
|
|
|
import { Signal } from "../signal/Signal";
|
2019-10-31 19:43:16 +00:00
|
|
|
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
|
2019-10-29 21:48:59 +00:00
|
|
|
import { OmniOscillatorSynthOptions } from "../source/oscillator/OscillatorInterface";
|
|
|
|
|
|
|
|
export interface MonoSynthOptions extends MonophonicOptions {
|
|
|
|
oscillator: OmniOscillatorSynthOptions;
|
|
|
|
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
|
|
|
|
filterEnvelope: Omit<FrequencyEnvelopeOptions, keyof ToneAudioNodeOptions>;
|
|
|
|
filter: Omit<FilterOptions, keyof ToneAudioNodeOptions>;
|
|
|
|
}
|
2019-10-28 22:12:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MonoSynth is composed of one `oscillator`, one `filter`, and two `envelopes`.
|
|
|
|
* The amplitude of the Oscillator and the cutoff frequency of the
|
|
|
|
* Filter are controlled by Envelopes.
|
|
|
|
* <img src="https://docs.google.com/drawings/d/1gaY1DF9_Hzkodqf8JI1Cg2VZfwSElpFQfI94IQwad38/pub?w=924&h=240">
|
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const synth = new Tone.MonoSynth({
|
2019-10-29 21:48:59 +00:00
|
|
|
* oscillator: {
|
|
|
|
* type: "square"
|
|
|
|
* },
|
|
|
|
* envelope: {
|
|
|
|
* attack: 0.1
|
|
|
|
* }
|
2019-10-28 22:12:20 +00:00
|
|
|
* }).toDestination();
|
|
|
|
* synth.triggerAttackRelease("C4", "8n");
|
2019-11-05 03:29:14 +00:00
|
|
|
* @category Instrument
|
2019-10-28 22:12:20 +00:00
|
|
|
*/
|
2019-10-29 21:48:59 +00:00
|
|
|
export class MonoSynth extends Monophonic<MonoSynthOptions> {
|
2019-10-28 22:12:20 +00:00
|
|
|
|
|
|
|
readonly name = "MonoSynth";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The oscillator.
|
|
|
|
*/
|
|
|
|
readonly oscillator: OmniOscillator<any>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The frequency control.
|
|
|
|
*/
|
|
|
|
readonly frequency: Signal<"frequency">;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The detune control.
|
|
|
|
*/
|
|
|
|
readonly detune: Signal<"cents">;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The filter.
|
|
|
|
*/
|
|
|
|
readonly filter: Filter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The filter envelope.
|
|
|
|
*/
|
|
|
|
readonly filterEnvelope: FrequencyEnvelope;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The amplitude envelope.
|
|
|
|
*/
|
|
|
|
readonly envelope: AmplitudeEnvelope;
|
|
|
|
|
2019-11-03 23:09:39 +00:00
|
|
|
constructor(options?: RecursivePartial<MonoSynthOptions>);
|
2019-10-28 22:12:20 +00:00
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(MonoSynth.getDefaults(), arguments));
|
|
|
|
const options = optionsFromArguments(MonoSynth.getDefaults(), arguments);
|
|
|
|
|
2020-04-17 02:24:18 +00:00
|
|
|
this.oscillator = new OmniOscillator(Object.assign(options.oscillator, {
|
2019-10-29 22:09:20 +00:00
|
|
|
context: this.context,
|
|
|
|
detune: options.detune,
|
|
|
|
onstop: () => this.onsilence(this),
|
|
|
|
}));
|
2019-10-28 22:12:20 +00:00
|
|
|
this.frequency = this.oscillator.frequency;
|
|
|
|
this.detune = this.oscillator.detune;
|
2019-10-29 21:48:59 +00:00
|
|
|
this.filter = new Filter(Object.assign(options.filter, { context: this.context }));
|
|
|
|
this.filterEnvelope = new FrequencyEnvelope(Object.assign(options.filterEnvelope, { context: this.context }));
|
|
|
|
this.envelope = new AmplitudeEnvelope(Object.assign(options.envelope, { context: this.context }));
|
2019-10-28 22:12:20 +00:00
|
|
|
|
|
|
|
// connect the oscillators to the output
|
|
|
|
this.oscillator.chain(this.filter, this.envelope, this.output);
|
2019-10-29 21:48:59 +00:00
|
|
|
|
2019-10-28 22:12:20 +00:00
|
|
|
// connect the filter envelope
|
|
|
|
this.filterEnvelope.connect(this.filter.frequency);
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-10-28 22:12:20 +00:00
|
|
|
readOnly(this, ["oscillator", "frequency", "detune", "filter", "filterEnvelope", "envelope"]);
|
|
|
|
}
|
|
|
|
|
2019-10-29 21:48:59 +00:00
|
|
|
static getDefaults(): MonoSynthOptions {
|
2019-11-03 22:41:47 +00:00
|
|
|
return Object.assign(Monophonic.getDefaults(), {
|
2019-10-28 22:12:20 +00:00
|
|
|
envelope: Object.assign(
|
2019-10-29 21:48:59 +00:00
|
|
|
omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
|
2019-10-28 22:12:20 +00:00
|
|
|
{
|
|
|
|
attack: 0.005,
|
|
|
|
decay: 0.1,
|
|
|
|
release: 1,
|
|
|
|
sustain: 0.9,
|
|
|
|
},
|
|
|
|
),
|
2019-10-29 21:48:59 +00:00
|
|
|
filter: Object.assign(
|
|
|
|
omitFromObject(Filter.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
|
|
|
|
{
|
2019-10-29 22:09:20 +00:00
|
|
|
Q: 1,
|
|
|
|
rolloff: -12,
|
2019-10-29 21:48:59 +00:00
|
|
|
type: "lowpass",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
filterEnvelope: Object.assign(
|
2020-04-17 02:24:18 +00:00
|
|
|
omitFromObject(FrequencyEnvelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
|
2019-10-29 21:48:59 +00:00
|
|
|
{
|
2019-10-29 22:09:20 +00:00
|
|
|
attack: 0.6,
|
2019-10-29 21:48:59 +00:00
|
|
|
baseFrequency: 200,
|
|
|
|
decay: 0.2,
|
|
|
|
exponent: 2,
|
|
|
|
octaves: 3,
|
|
|
|
release: 2,
|
|
|
|
sustain: 0.5,
|
|
|
|
}
|
|
|
|
),
|
2019-10-28 22:12:20 +00:00
|
|
|
oscillator: Object.assign(
|
|
|
|
omitFromObject(OmniOscillator.getDefaults(), Object.keys(Source.getDefaults())),
|
|
|
|
{
|
2019-10-29 22:09:20 +00:00
|
|
|
type: "sawtooth",
|
2019-10-28 22:12:20 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the attack portion of the envelope
|
|
|
|
* @param time the time the attack should start
|
|
|
|
* @param velocity the velocity of the note (0-1)
|
|
|
|
*/
|
2019-11-17 18:09:19 +00:00
|
|
|
protected _triggerEnvelopeAttack(time: Seconds, velocity = 1): void {
|
2019-11-03 22:41:47 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
|
|
|
this.filterEnvelope.triggerAttack(time);
|
|
|
|
this.oscillator.start(time);
|
2019-10-28 22:12:20 +00:00
|
|
|
if (this.envelope.sustain === 0) {
|
|
|
|
const computedAttack = this.toSeconds(this.envelope.attack);
|
|
|
|
const computedDecay = this.toSeconds(this.envelope.decay);
|
2019-11-03 22:41:47 +00:00
|
|
|
this.oscillator.stop(time + computedAttack + computedDecay);
|
2019-10-28 22:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the release portion of the envelope
|
|
|
|
* @param time the time the release should start
|
|
|
|
*/
|
2019-11-03 22:41:47 +00:00
|
|
|
protected _triggerEnvelopeRelease(time: Seconds): void {
|
2019-10-28 22:12:20 +00:00
|
|
|
this.envelope.triggerRelease(time);
|
2019-10-29 21:48:59 +00:00
|
|
|
this.filterEnvelope.triggerRelease(time);
|
2019-10-28 22:12:20 +00:00
|
|
|
this.oscillator.stop(time + this.toSeconds(this.envelope.release));
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:41:47 +00:00
|
|
|
getLevelAtTime(time: Time): NormalRange {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
return this.envelope.getValueAtTime(time);
|
|
|
|
}
|
|
|
|
|
2019-10-28 22:12:20 +00:00
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this.oscillator.dispose();
|
|
|
|
this.envelope.dispose();
|
|
|
|
this.filterEnvelope.dispose();
|
|
|
|
this.filter.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|