2019-09-04 00:20:08 +00:00
|
|
|
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
|
2019-09-01 04:20:20 +00:00
|
|
|
import { Filter } from "../component/filter/Filter";
|
|
|
|
import { Gain } from "../core/context/Gain";
|
2019-09-04 00:20:08 +00:00
|
|
|
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
|
2019-09-01 06:10:32 +00:00
|
|
|
import { Cents, Frequency, NormalRange, Positive, Time } from "../core/type/Units";
|
2019-09-04 00:20:08 +00:00
|
|
|
import { deepMerge, omitFromObject, optionsFromArguments } from "../core/util/Defaults";
|
|
|
|
import { noOp, RecursivePartial } from "../core/util/Interface";
|
2019-09-01 04:20:20 +00:00
|
|
|
import { Multiply } from "../signal/Multiply";
|
|
|
|
import { Scale } from "../signal/Scale";
|
|
|
|
import { Signal } from "../signal/Signal";
|
|
|
|
import { FMOscillator } from "../source/oscillator/FMOscillator";
|
2019-09-04 00:20:08 +00:00
|
|
|
import { Monophonic, MonophonicOptions } from "./Monophonic";
|
2019-09-01 04:20:20 +00:00
|
|
|
|
2019-09-04 00:20:08 +00:00
|
|
|
interface MetalSynthOptions extends MonophonicOptions {
|
2019-09-01 04:20:20 +00:00
|
|
|
harmonicity: Positive;
|
|
|
|
modulationIndex: Positive;
|
|
|
|
octaves: number;
|
|
|
|
resonance: Frequency;
|
2019-09-04 00:20:08 +00:00
|
|
|
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
|
2019-09-01 04:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inharmonic ratio of frequencies based on the Roland TR-808
|
|
|
|
* Taken from https://ccrma.stanford.edu/papers/tr-808-cymbal-physically-informed-circuit-bendable-digital-model
|
|
|
|
*/
|
|
|
|
const inharmRatios: number[] = [1.0, 1.483, 1.932, 2.546, 2.630, 3.897];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A highly inharmonic and spectrally complex source with a highpass filter
|
2019-09-04 00:20:08 +00:00
|
|
|
* and amplitude envelope which is good for making metallophone sounds.
|
2019-09-01 04:20:20 +00:00
|
|
|
* Based on CymbalSynth by [@polyrhythmatic](https://github.com/polyrhythmatic).
|
2019-09-04 00:20:08 +00:00
|
|
|
* Inspiration from [Sound on Sound](https://shorturl.at/rSZ12).
|
2019-09-01 04:20:20 +00:00
|
|
|
*/
|
2019-09-01 06:10:32 +00:00
|
|
|
export class MetalSynth extends Monophonic<MetalSynthOptions> {
|
2019-09-01 04:20:20 +00:00
|
|
|
|
|
|
|
readonly name = "MetalSynth";
|
|
|
|
|
2019-09-01 09:19:19 +00:00
|
|
|
/**
|
|
|
|
* The frequency of the cymbal
|
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
readonly frequency: Signal<Frequency>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The detune applied to the oscillators
|
|
|
|
*/
|
|
|
|
readonly detune: Signal<Cents>;
|
2019-09-01 09:19:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The array of FMOscillators
|
|
|
|
*/
|
|
|
|
private _oscillators: FMOscillator[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The frequency multipliers
|
|
|
|
*/
|
2019-09-01 04:20:20 +00:00
|
|
|
private _freqMultipliers: Multiply[] = [];
|
2019-09-01 09:19:19 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-04 00:20:08 +00:00
|
|
|
* The gain node for the envelope.
|
2019-09-01 09:19:19 +00:00
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
private _amplitude: Gain;
|
2019-09-01 09:19:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Highpass the output
|
|
|
|
*/
|
2019-09-01 04:20:20 +00:00
|
|
|
private _highpass: Filter;
|
2019-09-01 09:19:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of octaves the highpass
|
|
|
|
* filter frequency ramps
|
|
|
|
*/
|
2019-09-01 04:20:20 +00:00
|
|
|
private _octaves: number;
|
|
|
|
|
2019-09-01 09:19:19 +00:00
|
|
|
/**
|
2019-09-04 00:20:08 +00:00
|
|
|
* Scale the body envelope for the highpass filter
|
2019-09-01 09:19:19 +00:00
|
|
|
*/
|
|
|
|
private _filterFreqScaler: Scale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The envelope which is connected both to the
|
2019-09-04 00:20:08 +00:00
|
|
|
* amplitude and a highpass filter's cutoff frequency.
|
|
|
|
* The lower-limit of the filter is controlled by the [[#resonance]]
|
2019-09-01 09:19:19 +00:00
|
|
|
*/
|
2019-09-01 06:10:32 +00:00
|
|
|
readonly envelope: Envelope;
|
|
|
|
|
2019-09-01 04:20:20 +00:00
|
|
|
constructor(options?: RecursivePartial<MetalSynthOptions>)
|
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(MetalSynth.getDefaults(), arguments));
|
|
|
|
const options = optionsFromArguments(MetalSynth.getDefaults(), arguments);
|
|
|
|
|
2019-09-04 00:20:08 +00:00
|
|
|
this.detune = new Signal({
|
|
|
|
context: this.context,
|
|
|
|
units: "cents",
|
|
|
|
value: options.detune,
|
|
|
|
});
|
2019-09-01 04:20:20 +00:00
|
|
|
|
2019-09-04 00:20:08 +00:00
|
|
|
this.frequency = new Signal({
|
|
|
|
context: this.context,
|
|
|
|
units: "frequency",
|
|
|
|
});
|
2019-09-01 04:20:20 +00:00
|
|
|
|
2019-09-04 00:20:08 +00:00
|
|
|
this._amplitude = new Gain({
|
|
|
|
context: this.context,
|
|
|
|
gain: 0,
|
|
|
|
}).connect(this.output);
|
2019-09-01 04:20:20 +00:00
|
|
|
|
|
|
|
this._highpass = new Filter({
|
|
|
|
Q: -3.0102999566398125,
|
2019-09-04 00:20:08 +00:00
|
|
|
context: this.context,
|
2019-09-01 04:20:20 +00:00
|
|
|
type: "highpass",
|
2019-09-04 00:20:08 +00:00
|
|
|
}).connect(this._amplitude);
|
2019-09-01 04:20:20 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < inharmRatios.length; i++) {
|
|
|
|
const osc = new FMOscillator({
|
2019-09-04 00:20:08 +00:00
|
|
|
context: this.context,
|
2019-09-01 04:20:20 +00:00
|
|
|
harmonicity: options.harmonicity,
|
|
|
|
modulationIndex: options.modulationIndex,
|
|
|
|
modulationType: "square",
|
2019-09-04 00:20:08 +00:00
|
|
|
onstop: i === 0 ? () => this.onsilence(this) : noOp,
|
2019-09-01 04:20:20 +00:00
|
|
|
type: "square",
|
|
|
|
});
|
|
|
|
osc.connect(this._highpass);
|
|
|
|
this._oscillators[i] = osc;
|
|
|
|
|
2019-09-04 00:20:08 +00:00
|
|
|
const mult = new Multiply({
|
|
|
|
context: this.context,
|
|
|
|
value: inharmRatios[i],
|
|
|
|
});
|
2019-09-01 04:20:20 +00:00
|
|
|
this._freqMultipliers[i] = mult;
|
|
|
|
this.frequency.chain(mult, osc.frequency);
|
2019-09-04 00:20:08 +00:00
|
|
|
this.detune.connect(osc.detune);
|
2019-09-01 04:20:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 00:20:08 +00:00
|
|
|
this._filterFreqScaler = new Scale({
|
|
|
|
context: this.context,
|
|
|
|
max: 7000,
|
|
|
|
min: this.toFrequency(options.resonance),
|
|
|
|
});
|
2019-09-01 06:10:32 +00:00
|
|
|
|
|
|
|
this.envelope = new Envelope({
|
2019-09-01 09:19:19 +00:00
|
|
|
attack: options.envelope.attack,
|
|
|
|
attackCurve: "linear",
|
2019-09-04 00:20:08 +00:00
|
|
|
context: this.context,
|
2019-09-01 09:19:19 +00:00
|
|
|
decay: options.envelope.decay,
|
|
|
|
release: options.envelope.release,
|
2019-09-04 00:20:08 +00:00
|
|
|
sustain: 0,
|
2019-09-01 06:10:32 +00:00
|
|
|
});
|
2019-09-01 09:19:19 +00:00
|
|
|
|
2019-09-01 04:20:20 +00:00
|
|
|
this.envelope.chain(this._filterFreqScaler, this._highpass.frequency);
|
2019-09-04 00:20:08 +00:00
|
|
|
this.envelope.connect(this._amplitude.gain);
|
|
|
|
// set the octaves
|
|
|
|
this._octaves = options.octaves;
|
|
|
|
this.octaves = options.octaves;
|
2019-09-01 04:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): MetalSynthOptions {
|
2019-09-04 00:20:08 +00:00
|
|
|
return deepMerge(Monophonic.getDefaults(), {
|
|
|
|
envelope: Object.assign(
|
|
|
|
omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
|
|
|
|
{
|
|
|
|
attack: 0.001,
|
|
|
|
decay: 1.4,
|
|
|
|
release: 0.2,
|
|
|
|
},
|
|
|
|
),
|
2019-09-01 04:20:20 +00:00
|
|
|
harmonicity: 5.1,
|
|
|
|
modulationIndex: 32,
|
|
|
|
octaves: 1.5,
|
|
|
|
resonance: 4000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the attack.
|
|
|
|
* @param time When the attack should be triggered.
|
|
|
|
* @param velocity The velocity that the envelope should be triggered at.
|
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
protected _triggerEnvelopeAttack(time: Time, velocity: NormalRange = 1): this {
|
2019-09-01 04:20:20 +00:00
|
|
|
this.envelope.triggerAttack(time, velocity);
|
|
|
|
this._oscillators.forEach(osc => osc.start(time));
|
|
|
|
if (this.envelope.sustain === 0) {
|
|
|
|
this._oscillators.forEach(osc => {
|
|
|
|
osc.stop(this.toSeconds(time) + this.toSeconds(this.envelope.attack) + this.toSeconds(this.envelope.decay));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the release of the envelope.
|
|
|
|
* @param time When the release should be triggered.
|
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
protected _triggerEnvelopeRelease(time: Time): this {
|
2019-09-01 04:20:20 +00:00
|
|
|
this.envelope.triggerRelease(time);
|
|
|
|
this._oscillators.forEach(osc => osc.stop(this.toSeconds(time) + this.toSeconds(this.envelope.release)));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The modulationIndex of the oscillators which make up the source.
|
2019-09-04 00:20:08 +00:00
|
|
|
* see [[FMOscillator#modulationIndex]]
|
2019-09-01 04:20:20 +00:00
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
get modulationIndex(): number {
|
2019-09-01 04:20:20 +00:00
|
|
|
return this._oscillators[0].modulationIndex.value;
|
|
|
|
}
|
2019-09-04 00:20:08 +00:00
|
|
|
set modulationIndex(val) {
|
2019-09-01 04:20:20 +00:00
|
|
|
this._oscillators.forEach(osc => (osc.modulationIndex.value = val));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The harmonicity of the oscillators which make up the source.
|
|
|
|
* see Tone.FMOscillator.harmonicity
|
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
get harmonicity(): number {
|
2019-09-01 04:20:20 +00:00
|
|
|
return this._oscillators[0].harmonicity.value;
|
|
|
|
}
|
2019-09-04 00:20:08 +00:00
|
|
|
set harmonicity(val) {
|
2019-09-01 04:20:20 +00:00
|
|
|
this._oscillators.forEach(osc => (osc.harmonicity.value = val));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-04 00:20:08 +00:00
|
|
|
* The lower level of the highpass filter which is attached to the envelope.
|
|
|
|
* This value should be between [0, 7000]
|
2019-09-01 04:20:20 +00:00
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
get resonance(): number {
|
2019-09-01 04:20:20 +00:00
|
|
|
return this._filterFreqScaler.min;
|
|
|
|
}
|
2019-09-04 00:20:08 +00:00
|
|
|
set resonance(val) {
|
2019-09-01 04:20:20 +00:00
|
|
|
this._filterFreqScaler.min = val;
|
|
|
|
this.octaves = this._octaves;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of octaves above the "resonance" frequency
|
|
|
|
* that the filter ramps during the attack/decay envelope
|
|
|
|
*/
|
2019-09-04 00:20:08 +00:00
|
|
|
get octaves(): number {
|
2019-09-01 04:20:20 +00:00
|
|
|
return this._octaves;
|
|
|
|
}
|
2019-09-04 00:20:08 +00:00
|
|
|
set octaves(val) {
|
2019-09-01 04:20:20 +00:00
|
|
|
this._octaves = val;
|
|
|
|
this._filterFreqScaler.max = this._filterFreqScaler.min * Math.pow(2, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this._oscillators.forEach(osc => osc.dispose());
|
|
|
|
this._freqMultipliers.forEach(freqMult => freqMult.dispose());
|
|
|
|
this.frequency.dispose();
|
2019-09-04 00:20:08 +00:00
|
|
|
this.detune.dispose();
|
2019-09-01 04:20:20 +00:00
|
|
|
this._filterFreqScaler.dispose();
|
2019-09-04 00:20:08 +00:00
|
|
|
this._amplitude.dispose();
|
2019-09-01 04:20:20 +00:00
|
|
|
this.envelope.dispose();
|
|
|
|
this._highpass.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|