2019-07-18 18:07:25 +00:00
|
|
|
import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope";
|
|
|
|
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
|
2019-08-12 04:13:52 +00:00
|
|
|
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
|
2019-08-08 18:15:56 +00:00
|
|
|
import { Cents, Frequency, Time } from "../core/type/Units";
|
2019-07-19 17:21:02 +00:00
|
|
|
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
|
2019-07-18 18:07:25 +00:00
|
|
|
import { readOnly } from "../core/util/Interface";
|
|
|
|
import { RecursivePartial } from "../core/util/Interface";
|
2019-08-12 04:13:52 +00:00
|
|
|
import { Signal } from "../signal/Signal";
|
2019-07-19 16:32:17 +00:00
|
|
|
import { OmniOscillator } from "../source/oscillator/OmniOscillator";
|
|
|
|
import { OmniOscillatorConstructorOptions } from "../source/oscillator/OscillatorInterface";
|
2019-08-12 04:13:52 +00:00
|
|
|
import { Source, SourceOptions } from "../source/Source";
|
2019-07-18 18:07:25 +00:00
|
|
|
import { Monophonic, MonophonicOptions } from "./Monophonic";
|
|
|
|
|
2019-08-12 04:13:52 +00:00
|
|
|
/**
|
|
|
|
* The settable oscillator options. Does not include optiosn like
|
|
|
|
* "context" and "frequency" since those are controlled by the synth
|
|
|
|
*/
|
|
|
|
type LimitedOscillatorOptions = Omit<
|
|
|
|
Omit<OmniOscillatorConstructorOptions, keyof SourceOptions>,
|
|
|
|
"frequency" | "detune">;
|
|
|
|
|
2019-07-18 18:07:25 +00:00
|
|
|
export interface SynthOptions extends MonophonicOptions {
|
2019-08-12 04:13:52 +00:00
|
|
|
oscillator: LimitedOscillatorOptions;
|
|
|
|
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
|
2019-07-18 18:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-30 19:35:27 +00:00
|
|
|
* Synth is composed simply of a {@link OmniOscillator} routed through an {@link AmplitudeEnvelope}.
|
2019-07-18 18:07:25 +00:00
|
|
|
* ```
|
2019-07-30 19:35:27 +00:00
|
|
|
* +----------------+ +-------------------+
|
|
|
|
* | OmniOscillator +>--> AmplitudeEnvelope +>--> Output
|
|
|
|
* +----------------+ +-------------------+
|
2019-07-18 18:07:25 +00:00
|
|
|
* ```
|
2019-07-30 19:35:27 +00:00
|
|
|
* @param options the options available for the synth.
|
|
|
|
* @example
|
2019-07-25 15:32:56 +00:00
|
|
|
* var synth = new Synth().toDestination();
|
2019-07-18 18:07:25 +00:00
|
|
|
* synth.triggerAttackRelease("C4", "8n");
|
|
|
|
*/
|
|
|
|
export class Synth extends Monophonic<SynthOptions> {
|
|
|
|
|
2019-08-03 01:09:35 +00:00
|
|
|
readonly name = "Synth";
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The oscillator.
|
|
|
|
*/
|
2019-08-08 18:15:56 +00:00
|
|
|
readonly oscillator: OmniOscillator<any>;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The frequency signal
|
|
|
|
*/
|
2019-08-08 18:15:56 +00:00
|
|
|
readonly frequency: Signal<Frequency>;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The detune signal
|
|
|
|
*/
|
2019-08-08 18:15:56 +00:00
|
|
|
readonly detune: Signal<Cents>;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The envelope
|
|
|
|
*/
|
2019-08-10 03:06:30 +00:00
|
|
|
readonly envelope: AmplitudeEnvelope;
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
constructor(options?: RecursivePartial<SynthOptions>);
|
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(Synth.getDefaults(), arguments));
|
|
|
|
const options = optionsFromArguments(Synth.getDefaults(), arguments);
|
|
|
|
|
2019-08-08 18:15:56 +00:00
|
|
|
this.oscillator = new OmniOscillator(Object.assign({
|
|
|
|
context: this.context,
|
2019-08-12 04:13:52 +00:00
|
|
|
onstop: () => this.onsilence(this),
|
2019-08-08 18:15:56 +00:00
|
|
|
}, options.oscillator));
|
|
|
|
|
|
|
|
this.frequency = this.oscillator.frequency;
|
|
|
|
this.detune = this.oscillator.detune;
|
|
|
|
|
2019-08-10 03:06:30 +00:00
|
|
|
this.envelope = new AmplitudeEnvelope(Object.assign({
|
|
|
|
context: this.context,
|
|
|
|
}, options.envelope));
|
2019-07-18 18:07:25 +00:00
|
|
|
|
|
|
|
// connect the oscillators to the output
|
|
|
|
this.oscillator.chain(this.envelope, this.output);
|
|
|
|
readOnly(this, ["oscillator", "frequency", "detune", "envelope"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): SynthOptions {
|
|
|
|
return Object.assign(Monophonic.getDefaults(), {
|
|
|
|
envelope: Object.assign(
|
2019-08-12 04:13:52 +00:00
|
|
|
omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
|
2019-07-18 18:07:25 +00:00
|
|
|
{
|
|
|
|
attack : 0.005,
|
|
|
|
decay : 0.1,
|
|
|
|
release : 1,
|
|
|
|
sustain : 0.3,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
oscillator: Object.assign(
|
2019-08-12 04:13:52 +00:00
|
|
|
omitFromObject(OmniOscillator.getDefaults(), [...Object.keys(Source.getDefaults()), "frequency", "detune"]),
|
2019-07-18 18:07:25 +00:00
|
|
|
{
|
|
|
|
type: "triangle",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the attack portion of the envelope
|
|
|
|
* @param time the time the attack should start
|
|
|
|
* @param velocity the velocity of the note (0-1)
|
|
|
|
*/
|
|
|
|
protected _triggerEnvelopeAttack(time?: Time, velocity: number = 1): void {
|
|
|
|
const computedTime = this.toSeconds(time);
|
|
|
|
// the envelopes
|
|
|
|
this.envelope.triggerAttack(computedTime, velocity);
|
|
|
|
this.oscillator.start(computedTime);
|
|
|
|
// if there is no release portion, stop the oscillator
|
|
|
|
if (this.envelope.sustain === 0) {
|
|
|
|
const computedAttack = this.toSeconds(this.envelope.attack);
|
|
|
|
const computedDecay = this.toSeconds(this.envelope.decay);
|
|
|
|
this.oscillator.stop(computedTime + computedAttack + computedDecay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the release portion of the envelope
|
|
|
|
* @param time the time the release should start
|
|
|
|
*/
|
|
|
|
protected _triggerEnvelopeRelease(time: Time): void {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
this.envelope.triggerRelease(time);
|
|
|
|
this.oscillator.stop(time + this.toSeconds(this.envelope.release));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clean up
|
|
|
|
*/
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this.oscillator.dispose();
|
|
|
|
this.envelope.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|