Tone.js/Tone/instrument/Synth.ts

166 lines
4.3 KiB
TypeScript
Raw Normal View History

import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope.js";
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope.js";
import {
ToneAudioNode,
ToneAudioNodeOptions,
} from "../core/context/ToneAudioNode.js";
import { NormalRange, Seconds, Time } from "../core/type/Units.js";
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults.js";
import { readOnly } from "../core/util/Interface.js";
import { RecursivePartial } from "../core/util/Interface.js";
import { Signal } from "../signal/Signal.js";
import { OmniOscillator } from "../source/oscillator/OmniOscillator.js";
import {
OmniOscillatorOptions,
OmniOscillatorSynthOptions,
} from "../source/oscillator/OscillatorInterface.js";
import { Source } from "../source/Source.js";
import { Monophonic, MonophonicOptions } from "./Monophonic.js";
2019-07-18 18:07:25 +00:00
export interface SynthOptions extends MonophonicOptions {
oscillator: OmniOscillatorSynthOptions;
2019-08-12 04:13:52 +00:00
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
2019-07-18 18:07:25 +00:00
}
/**
2024-04-29 14:48:37 +00:00
* Synth is composed simply of a {@link OmniOscillator} routed through an {@link AmplitudeEnvelope}.
2019-07-18 18:07:25 +00:00
* ```
* +----------------+ +-------------------+
* | OmniOscillator +>--> AmplitudeEnvelope +>--> Output
* +----------------+ +-------------------+
2019-07-18 18:07:25 +00:00
* ```
* @example
* const synth = new Tone.Synth().toDestination();
2019-07-18 18:07:25 +00:00
* synth.triggerAttackRelease("C4", "8n");
2019-09-16 14:15:23 +00:00
* @category Instrument
2019-07-18 18:07:25 +00:00
*/
export class Synth<
Options extends SynthOptions = SynthOptions,
> extends Monophonic<Options> {
readonly name: string = "Synth";
2019-07-18 18:07:25 +00:00
/**
2019-09-14 20:39:18 +00:00
* The oscillator.
2019-07-18 18:07:25 +00:00
*/
readonly oscillator: OmniOscillator<any>;
2019-07-18 18:07:25 +00:00
/**
* The frequency signal
*/
readonly frequency: Signal<"frequency">;
2019-07-18 18:07:25 +00:00
/**
* The detune signal
*/
readonly detune: Signal<"cents">;
2019-07-18 18:07:25 +00:00
/**
* The envelope
*/
readonly envelope: AmplitudeEnvelope;
2019-07-18 18:07:25 +00:00
2019-08-27 15:57:00 +00:00
/**
* @param options the options available for the synth.
*/
2019-07-18 18:07:25 +00:00
constructor(options?: RecursivePartial<SynthOptions>);
constructor() {
const options = optionsFromArguments(Synth.getDefaults(), arguments);
super(options);
2019-07-18 18:07:25 +00:00
this.oscillator = new OmniOscillator(
Object.assign(
{
context: this.context,
detune: options.detune,
onstop: () => this.onsilence(this),
},
options.oscillator
)
);
this.frequency = this.oscillator.frequency;
this.detune = this.oscillator.detune;
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(
omitFromObject(
Envelope.getDefaults(),
Object.keys(ToneAudioNode.getDefaults())
),
2019-07-18 18:07:25 +00:00
{
2019-09-14 22:12:44 +00:00
attack: 0.005,
decay: 0.1,
release: 1,
sustain: 0.3,
}
2019-07-18 18:07:25 +00:00
),
oscillator: Object.assign(
omitFromObject(OmniOscillator.getDefaults(), [
...Object.keys(Source.getDefaults()),
"frequency",
"detune",
]),
2019-07-18 18:07:25 +00:00
{
type: "triangle",
}
2020-08-06 19:58:02 +00:00
) as OmniOscillatorOptions,
2019-07-18 18:07:25 +00:00
});
}
/**
2019-09-14 20:39:18 +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-07-18 18:07:25 +00:00
*/
protected _triggerEnvelopeAttack(time: Seconds, velocity: number): void {
2019-07-18 18:07:25 +00:00
// the envelopes
this.envelope.triggerAttack(time, velocity);
this.oscillator.start(time);
2019-07-18 18:07:25 +00:00
// 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(time + computedAttack + computedDecay);
2019-07-18 18:07:25 +00:00
}
}
/**
2019-09-14 20:39:18 +00:00
* start the release portion of the envelope
* @param time the time the release should start
2019-07-18 18:07:25 +00:00
*/
protected _triggerEnvelopeRelease(time: Seconds): void {
2019-07-18 18:07:25 +00:00
this.envelope.triggerRelease(time);
this.oscillator.stop(time + this.toSeconds(this.envelope.release));
}
getLevelAtTime(time: Time): NormalRange {
time = this.toSeconds(time);
return this.envelope.getValueAtTime(time);
}
2019-07-18 18:07:25 +00:00
/**
2019-09-14 20:39:18 +00:00
* clean up
2019-07-18 18:07:25 +00:00
*/
dispose(): this {
super.dispose();
this.oscillator.dispose();
this.envelope.dispose();
return this;
}
}