Tone.js/Tone/instrument/MonoSynth.ts

213 lines
5.5 KiB
TypeScript
Raw Permalink Normal View History

import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope.js";
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope.js";
import { Filter, FilterOptions } from "../component/filter/Filter.js";
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults.js";
import { readOnly, RecursivePartial } from "../core/util/Interface.js";
import { Monophonic, MonophonicOptions } from "../instrument/Monophonic.js";
import { OmniOscillator } from "../source/oscillator/OmniOscillator.js";
import { Source } from "../source/Source.js";
import {
FrequencyEnvelope,
FrequencyEnvelopeOptions,
} from "../component/envelope/FrequencyEnvelope.js";
import { NormalRange, Seconds, Time } from "../core/type/Units.js";
import { Signal } from "../signal/Signal.js";
import {
ToneAudioNode,
ToneAudioNodeOptions,
} from "../core/context/ToneAudioNode.js";
import { OmniOscillatorSynthOptions } from "../source/oscillator/OscillatorInterface.js";
2019-10-29 21:48:59 +00:00
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
* 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() {
const options = optionsFromArguments(
MonoSynth.getDefaults(),
arguments
);
super(options);
this.oscillator = new OmniOscillator(
Object.assign(options.oscillator, {
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;
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);
readOnly(this, [
"oscillator",
"frequency",
"detune",
"filter",
"filterEnvelope",
"envelope",
]);
2019-10-28 22:12:20 +00:00
}
2019-10-29 21:48:59 +00:00
static getDefaults(): MonoSynthOptions {
return Object.assign(Monophonic.getDefaults(), {
2019-10-28 22:12:20 +00:00
envelope: Object.assign(
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-28 22:12:20 +00:00
),
2019-10-29 21:48:59 +00:00
filter: Object.assign(
omitFromObject(
Filter.getDefaults(),
Object.keys(ToneAudioNode.getDefaults())
),
2019-10-29 21:48:59 +00:00
{
2019-10-29 22:09:20 +00:00
Q: 1,
rolloff: -12,
2019-10-29 21:48:59 +00:00
type: "lowpass",
}
2019-10-29 21:48:59 +00:00
),
filterEnvelope: Object.assign(
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-28 22:12:20 +00:00
{
2019-10-29 22:09:20 +00:00
type: "sawtooth",
}
2020-08-06 19:58:02 +00:00
) as OmniOscillatorSynthOptions,
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 {
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);
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
*/
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));
}
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;
}
}