2019-10-30 23:22:24 +00:00
|
|
|
import { Monophonic, MonophonicOptions } from "./Monophonic";
|
2019-10-30 22:54:30 +00:00
|
|
|
import { MonoSynth, MonoSynthOptions } from "./MonoSynth";
|
2019-11-03 20:44:51 +00:00
|
|
|
import { Signal } from "../signal/Signal";
|
2019-11-03 22:45:50 +00:00
|
|
|
import { readOnly, RecursivePartial } from "../core/util/Interface";
|
2019-11-03 20:44:51 +00:00
|
|
|
import { LFO } from "../source/oscillator/LFO";
|
|
|
|
import { Gain, } from "../core/context/Gain";
|
2019-10-30 22:54:30 +00:00
|
|
|
import { Multiply } from "../signal/Multiply";
|
2019-11-03 22:45:50 +00:00
|
|
|
import { Frequency, NormalRange, Positive, Seconds, Time } from "../core/type/Units";
|
|
|
|
import { deepMerge, omitFromObject, optionsFromArguments } from "../core/util/Defaults";
|
|
|
|
import { Param } from "../core/context/Param";
|
2019-10-30 22:54:30 +00:00
|
|
|
|
2019-10-30 23:22:24 +00:00
|
|
|
export interface DuoSynthOptions extends MonophonicOptions {
|
2019-11-03 22:45:50 +00:00
|
|
|
voice0: Omit<MonoSynthOptions, keyof MonophonicOptions>;
|
|
|
|
voice1: Omit<MonoSynthOptions, keyof MonophonicOptions>;
|
2019-10-30 22:54:30 +00:00
|
|
|
harmonicity: Positive;
|
2019-11-03 22:45:50 +00:00
|
|
|
vibratoRate: Frequency;
|
|
|
|
vibratoAmount: Positive;
|
2019-10-30 22:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-03 23:07:13 +00:00
|
|
|
* DuoSynth is a monophonic synth composed of two [[MonoSynths]] run in parallel with control over the
|
2019-10-30 22:54:30 +00:00
|
|
|
* frequency ratio between the two voices and vibrato effect.
|
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const duoSynth = new Tone.DuoSynth().toDestination();
|
2019-10-30 22:54:30 +00:00
|
|
|
* duoSynth.triggerAttackRelease("C4", "2n");
|
2019-11-05 03:29:14 +00:00
|
|
|
* @category Instrument
|
2019-10-30 22:54:30 +00:00
|
|
|
*/
|
2019-11-03 22:45:50 +00:00
|
|
|
export class DuoSynth extends Monophonic<DuoSynthOptions> {
|
2019-10-30 22:54:30 +00:00
|
|
|
|
2019-11-03 22:45:50 +00:00
|
|
|
readonly name: string = "DuoSynth";
|
2019-11-03 20:44:51 +00:00
|
|
|
|
2019-11-03 22:45:50 +00:00
|
|
|
readonly frequency: Signal<"frequency">;
|
2019-11-03 20:44:51 +00:00
|
|
|
readonly detune: Signal<"cents">;
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-10-30 22:54:30 +00:00
|
|
|
/**
|
|
|
|
* the first voice
|
|
|
|
*/
|
|
|
|
readonly voice0: MonoSynth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the second voice
|
|
|
|
*/
|
|
|
|
readonly voice1: MonoSynth;
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-10-30 22:54:30 +00:00
|
|
|
/**
|
|
|
|
* The amount of vibrato
|
|
|
|
*/
|
2019-11-03 22:45:50 +00:00
|
|
|
public vibratoAmount: Param<"normalRange">;
|
2019-10-30 22:54:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the vibrato frequency
|
|
|
|
*/
|
|
|
|
public vibratoRate: Signal<"frequency">;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Harmonicity is the ratio between the two voices. A harmonicity of
|
|
|
|
* 1 is no change. Harmonicity = 2 means a change of an octave.
|
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const duoSynth = new Tone.DuoSynth().toDestination();
|
2019-11-04 03:23:56 +00:00
|
|
|
* duoSynth.triggerAttackRelease("C4", "2n");
|
2019-10-30 22:54:30 +00:00
|
|
|
* // pitch voice1 an octave below voice0
|
|
|
|
* duoSynth.harmonicity.value = 0.5;
|
|
|
|
*/
|
|
|
|
public harmonicity: Signal<"positive">;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The vibrato LFO.
|
|
|
|
*/
|
|
|
|
private _vibrato: LFO;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the vibrato gain
|
|
|
|
*/
|
2019-11-03 22:45:50 +00:00
|
|
|
private _vibratoGain: Gain<"normalRange">;
|
2019-10-30 22:54:30 +00:00
|
|
|
|
|
|
|
constructor(options?: RecursivePartial<DuoSynthOptions>);
|
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(DuoSynth.getDefaults(), arguments));
|
|
|
|
const options = optionsFromArguments(DuoSynth.getDefaults(), arguments);
|
|
|
|
|
2020-04-17 02:24:18 +00:00
|
|
|
this.voice0 = new MonoSynth(Object.assign(options.voice0, {
|
|
|
|
context: this.context,
|
2019-11-03 22:45:50 +00:00
|
|
|
onsilence: () => this.onsilence(this)
|
2019-11-03 20:44:51 +00:00
|
|
|
}));
|
2020-04-17 02:24:18 +00:00
|
|
|
this.voice1 = new MonoSynth(Object.assign(options.voice1, {
|
|
|
|
context: this.context,
|
2019-11-03 20:44:51 +00:00
|
|
|
}));
|
2019-10-30 22:54:30 +00:00
|
|
|
|
|
|
|
this.harmonicity = new Multiply({
|
|
|
|
context: this.context,
|
|
|
|
units: "positive",
|
|
|
|
value: options.harmonicity,
|
|
|
|
});
|
|
|
|
|
2019-11-03 22:45:50 +00:00
|
|
|
this._vibrato = new LFO({
|
|
|
|
frequency: options.vibratoRate,
|
2019-10-30 23:22:24 +00:00
|
|
|
context: this.context,
|
|
|
|
min: -50,
|
|
|
|
max: 50
|
2019-11-03 22:45:50 +00:00
|
|
|
});
|
2019-11-03 20:44:51 +00:00
|
|
|
// start the vibrato immediately
|
2019-10-30 22:54:30 +00:00
|
|
|
this._vibrato.start();
|
|
|
|
this.vibratoRate = this._vibrato.frequency;
|
|
|
|
this._vibratoGain = new Gain({
|
|
|
|
context: this.context,
|
2019-11-03 22:45:50 +00:00
|
|
|
units: "normalRange",
|
2019-10-30 22:54:30 +00:00
|
|
|
gain: options.vibratoAmount
|
|
|
|
});
|
2019-11-03 22:45:50 +00:00
|
|
|
this.vibratoAmount = this._vibratoGain.gain;
|
2019-10-30 22:54:30 +00:00
|
|
|
|
|
|
|
this.frequency = new Signal({
|
|
|
|
context: this.context,
|
|
|
|
units: "frequency",
|
|
|
|
value: 440
|
|
|
|
});
|
|
|
|
this.detune = new Signal({
|
|
|
|
context: this.context,
|
|
|
|
units: "cents",
|
|
|
|
value: options.detune
|
|
|
|
});
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-10-30 22:54:30 +00:00
|
|
|
// control the two voices frequency
|
|
|
|
this.frequency.connect(this.voice0.frequency);
|
|
|
|
this.frequency.chain(this.harmonicity, this.voice1.frequency);
|
|
|
|
|
|
|
|
this._vibrato.connect(this._vibratoGain);
|
|
|
|
this._vibratoGain.fan(this.voice0.detune, this.voice1.detune);
|
|
|
|
|
|
|
|
this.detune.fan(this.voice0.detune, this.voice1.detune);
|
|
|
|
|
|
|
|
this.voice0.connect(this.output);
|
|
|
|
this.voice1.connect(this.output);
|
|
|
|
|
|
|
|
readOnly(this, ["voice0", "voice1", "frequency", "vibratoAmount", "vibratoRate"]);
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:45:50 +00:00
|
|
|
getLevelAtTime(time: Time): NormalRange {
|
|
|
|
time = this.toSeconds(time);
|
|
|
|
return this.voice0.envelope.getValueAtTime(time) + this.voice1.envelope.getValueAtTime(time);
|
2019-11-03 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 22:54:30 +00:00
|
|
|
static getDefaults(): DuoSynthOptions {
|
2019-11-03 22:45:50 +00:00
|
|
|
return deepMerge(Monophonic.getDefaults(), {
|
2019-10-30 22:54:30 +00:00
|
|
|
vibratoAmount: 0.5,
|
|
|
|
vibratoRate: 5,
|
|
|
|
harmonicity: 1.5,
|
2019-11-03 22:45:50 +00:00
|
|
|
voice0: deepMerge(
|
2020-04-17 02:24:18 +00:00
|
|
|
omitFromObject(MonoSynth.getDefaults(), Object.keys(Monophonic.getDefaults())),
|
2019-11-03 22:45:50 +00:00
|
|
|
{
|
|
|
|
filterEnvelope: {
|
2019-10-30 22:54:30 +00:00
|
|
|
attack: 0.01,
|
|
|
|
decay: 0.0,
|
|
|
|
sustain: 1,
|
|
|
|
release: 0.5
|
|
|
|
},
|
2019-11-03 22:45:50 +00:00
|
|
|
envelope: {
|
2019-10-30 22:54:30 +00:00
|
|
|
attack: 0.01,
|
|
|
|
decay: 0.0,
|
|
|
|
sustain: 1,
|
|
|
|
release: 0.5
|
|
|
|
}
|
2019-11-03 22:45:50 +00:00
|
|
|
}),
|
|
|
|
voice1: deepMerge(
|
2020-04-17 02:24:18 +00:00
|
|
|
omitFromObject(MonoSynth.getDefaults(), Object.keys(Monophonic.getDefaults())),
|
2019-11-03 22:45:50 +00:00
|
|
|
{
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-11-03 22:45:50 +00:00
|
|
|
filterEnvelope: {
|
2019-10-30 22:54:30 +00:00
|
|
|
attack: 0.01,
|
|
|
|
decay: 0.0,
|
|
|
|
sustain: 1,
|
|
|
|
release: 0.5
|
|
|
|
},
|
2019-11-03 22:45:50 +00:00
|
|
|
envelope: {
|
2019-10-30 22:54:30 +00:00
|
|
|
attack: 0.01,
|
|
|
|
decay: 0.0,
|
|
|
|
sustain: 1,
|
|
|
|
release: 0.5
|
|
|
|
}
|
2019-11-03 22:45:50 +00:00
|
|
|
}),
|
2021-10-13 17:32:29 +00:00
|
|
|
}) as unknown as DuoSynthOptions;
|
2019-10-30 22:54:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2019-11-03 22:45:50 +00:00
|
|
|
* Trigger the attack portion of the note
|
2019-10-30 22:54:30 +00:00
|
|
|
*/
|
2019-11-03 22:45:50 +00:00
|
|
|
protected _triggerEnvelopeAttack(time: Seconds, velocity: number): void {
|
|
|
|
// @ts-ignore
|
|
|
|
this.voice0._triggerEnvelopeAttack(time, velocity);
|
|
|
|
// @ts-ignore
|
|
|
|
this.voice1._triggerEnvelopeAttack(time, velocity);
|
2019-10-30 22:54:30 +00:00
|
|
|
}
|
2020-04-17 02:24:18 +00:00
|
|
|
|
2019-10-30 22:54:30 +00:00
|
|
|
/**
|
2019-11-03 22:45:50 +00:00
|
|
|
* Trigger the release portion of the note
|
2019-10-30 22:54:30 +00:00
|
|
|
*/
|
2019-11-03 22:45:50 +00:00
|
|
|
protected _triggerEnvelopeRelease(time: Seconds) {
|
|
|
|
// @ts-ignore
|
|
|
|
this.voice0._triggerEnvelopeRelease(time);
|
|
|
|
// @ts-ignore
|
|
|
|
this.voice1._triggerEnvelopeRelease(time);
|
|
|
|
return this;
|
2019-10-30 22:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this.voice0.dispose();
|
|
|
|
this.voice1.dispose();
|
|
|
|
this.frequency.dispose();
|
|
|
|
this.detune.dispose();
|
|
|
|
this._vibrato.dispose();
|
|
|
|
this.vibratoRate.dispose();
|
|
|
|
this._vibratoGain.dispose();
|
|
|
|
this.harmonicity.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|