2024-05-03 18:31:14 +00:00
|
|
|
import { Monophonic, MonophonicOptions } from "./Monophonic.js";
|
|
|
|
import { MonoSynth, MonoSynthOptions } from "./MonoSynth.js";
|
|
|
|
import { Signal } from "../signal/Signal.js";
|
|
|
|
import { readOnly, RecursivePartial } from "../core/util/Interface.js";
|
|
|
|
import { LFO } from "../source/oscillator/LFO.js";
|
|
|
|
import { Gain } from "../core/context/Gain.js";
|
|
|
|
import { Multiply } from "../signal/Multiply.js";
|
|
|
|
import {
|
|
|
|
Frequency,
|
|
|
|
NormalRange,
|
|
|
|
Positive,
|
|
|
|
Seconds,
|
|
|
|
Time,
|
|
|
|
} from "../core/type/Units.js";
|
|
|
|
import {
|
|
|
|
deepMerge,
|
|
|
|
omitFromObject,
|
|
|
|
optionsFromArguments,
|
|
|
|
} from "../core/util/Defaults.js";
|
|
|
|
import { Param } from "../core/context/Param.js";
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-29 14:48:37 +00:00
|
|
|
* DuoSynth is a monophonic synth composed of two {@link MonoSynth}s 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> {
|
|
|
|
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() {
|
|
|
|
const options = optionsFromArguments(DuoSynth.getDefaults(), arguments);
|
2024-05-06 14:55:55 +00:00
|
|
|
super(options);
|
2019-10-30 22:54:30 +00:00
|
|
|
|
2024-05-03 18:31:14 +00:00
|
|
|
this.voice0 = new MonoSynth(
|
|
|
|
Object.assign(options.voice0, {
|
|
|
|
context: this.context,
|
|
|
|
onsilence: () => this.onsilence(this),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
this.voice1 = new MonoSynth(
|
|
|
|
Object.assign(options.voice1, {
|
|
|
|
context: this.context,
|
|
|
|
})
|
|
|
|
);
|
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,
|
2024-05-03 18:31:14 +00:00
|
|
|
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",
|
2024-05-03 18:31:14 +00:00
|
|
|
gain: options.vibratoAmount,
|
2019-10-30 22:54:30 +00:00
|
|
|
});
|
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",
|
2024-05-03 18:31:14 +00:00
|
|
|
value: 440,
|
2019-10-30 22:54:30 +00:00
|
|
|
});
|
|
|
|
this.detune = new Signal({
|
|
|
|
context: this.context,
|
|
|
|
units: "cents",
|
2024-05-03 18:31:14 +00:00
|
|
|
value: options.detune,
|
2019-10-30 22:54:30 +00:00
|
|
|
});
|
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);
|
|
|
|
|
2024-05-03 18:31:14 +00:00
|
|
|
readOnly(this, [
|
|
|
|
"voice0",
|
|
|
|
"voice1",
|
|
|
|
"frequency",
|
|
|
|
"vibratoAmount",
|
|
|
|
"vibratoRate",
|
|
|
|
]);
|
2019-10-30 22:54:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 22:45:50 +00:00
|
|
|
getLevelAtTime(time: Time): NormalRange {
|
|
|
|
time = this.toSeconds(time);
|
2024-05-03 18:31:14 +00:00
|
|
|
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(
|
2024-05-03 18:31:14 +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,
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0.5,
|
2019-10-30 22:54:30 +00:00
|
|
|
},
|
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,
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0.5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
),
|
2019-11-03 22:45:50 +00:00
|
|
|
voice1: deepMerge(
|
2024-05-03 18:31:14 +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,
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0.5,
|
2019-10-30 22:54:30 +00:00
|
|
|
},
|
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,
|
2024-05-03 18:31:14 +00:00
|
|
|
release: 0.5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
),
|
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;
|
|
|
|
}
|
|
|
|
}
|