/
MetalSynth.ts
272 lines (240 loc) · 7.22 KB
/
MetalSynth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
import { Filter } from "../component/filter/Filter";
import { Gain } from "../core/context/Gain";
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
import { Frequency, NormalRange, Positive, Seconds, Time } from "../core/type/Units";
import { deepMerge, omitFromObject, optionsFromArguments } from "../core/util/Defaults";
import { noOp, RecursivePartial } from "../core/util/Interface";
import { Multiply } from "../signal/Multiply";
import { Scale } from "../signal/Scale";
import { Signal } from "../signal/Signal";
import { FMOscillator } from "../source/oscillator/FMOscillator";
import { Monophonic, MonophonicOptions } from "./Monophonic";
export interface MetalSynthOptions extends MonophonicOptions {
harmonicity: Positive;
modulationIndex: Positive;
octaves: number;
resonance: Frequency;
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
}
/**
* 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
* and amplitude envelope which is good for making metallophone sounds.
* Based on CymbalSynth by [@polyrhythmatic](https://github.com/polyrhythmatic).
* Inspiration from [Sound on Sound](https://shorturl.at/rSZ12).
* @category Instrument
*/
export class MetalSynth extends Monophonic<MetalSynthOptions> {
readonly name: string = "MetalSynth";
/**
* The frequency of the cymbal
*/
readonly frequency: Signal<"frequency">;
/**
* The detune applied to the oscillators
*/
readonly detune: Signal<"cents">;
/**
* The array of FMOscillators
*/
private _oscillators: FMOscillator[] = [];
/**
* The frequency multipliers
*/
private _freqMultipliers: Multiply[] = [];
/**
* The gain node for the envelope.
*/
private _amplitude: Gain;
/**
* Highpass the output
*/
private _highpass: Filter;
/**
* The number of octaves the highpass
* filter frequency ramps
*/
private _octaves: number;
/**
* Scale the body envelope for the highpass filter
*/
private _filterFreqScaler: Scale;
/**
* The envelope which is connected both to the
* amplitude and a highpass filter's cutoff frequency.
* The lower-limit of the filter is controlled by the [[resonance]]
*/
readonly envelope: Envelope;
constructor(options?: RecursivePartial<MetalSynthOptions>)
constructor() {
super(optionsFromArguments(MetalSynth.getDefaults(), arguments));
const options = optionsFromArguments(MetalSynth.getDefaults(), arguments);
this.detune = new Signal({
context: this.context,
units: "cents",
value: options.detune,
});
this.frequency = new Signal({
context: this.context,
units: "frequency",
});
this._amplitude = new Gain({
context: this.context,
gain: 0,
}).connect(this.output);
this._highpass = new Filter({
// Q: -3.0102999566398125,
Q: 0,
context: this.context,
type: "highpass",
}).connect(this._amplitude);
for (let i = 0; i < inharmRatios.length; i++) {
const osc = new FMOscillator({
context: this.context,
harmonicity: options.harmonicity,
modulationIndex: options.modulationIndex,
modulationType: "square",
onstop: i === 0 ? () => this.onsilence(this) : noOp,
type: "square",
});
osc.connect(this._highpass);
this._oscillators[i] = osc;
const mult = new Multiply({
context: this.context,
value: inharmRatios[i],
});
this._freqMultipliers[i] = mult;
this.frequency.chain(mult, osc.frequency);
this.detune.connect(osc.detune);
}
this._filterFreqScaler = new Scale({
context: this.context,
max: 7000,
min: this.toFrequency(options.resonance),
});
this.envelope = new Envelope({
attack: options.envelope.attack,
attackCurve: "linear",
context: this.context,
decay: options.envelope.decay,
release: options.envelope.release,
sustain: 0,
});
this.envelope.chain(this._filterFreqScaler, this._highpass.frequency);
this.envelope.connect(this._amplitude.gain);
// set the octaves
this._octaves = options.octaves;
this.octaves = options.octaves;
}
static getDefaults(): MetalSynthOptions {
return deepMerge(Monophonic.getDefaults(), {
envelope: Object.assign(
omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
{
attack: 0.001,
decay: 1.4,
release: 0.2,
},
),
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.
*/
protected _triggerEnvelopeAttack(time: Seconds, velocity: NormalRange = 1): this {
this.envelope.triggerAttack(time, velocity);
this._oscillators.forEach(osc => osc.start(time));
if (this.envelope.sustain === 0) {
this._oscillators.forEach(osc => {
osc.stop(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.
*/
protected _triggerEnvelopeRelease(time: Seconds): this {
this.envelope.triggerRelease(time);
this._oscillators.forEach(osc => osc.stop(time + this.toSeconds(this.envelope.release)));
return this;
}
getLevelAtTime(time: Time): NormalRange {
time = this.toSeconds(time);
return this.envelope.getValueAtTime(time);
}
/**
* The modulationIndex of the oscillators which make up the source.
* see [[FMOscillator.modulationIndex]]
* @min 1
* @max 100
*/
get modulationIndex(): number {
return this._oscillators[0].modulationIndex.value;
}
set modulationIndex(val) {
this._oscillators.forEach(osc => (osc.modulationIndex.value = val));
}
/**
* The harmonicity of the oscillators which make up the source.
* see Tone.FMOscillator.harmonicity
* @min 0.1
* @max 10
*/
get harmonicity(): number {
return this._oscillators[0].harmonicity.value;
}
set harmonicity(val) {
this._oscillators.forEach(osc => (osc.harmonicity.value = val));
}
/**
* The lower level of the highpass filter which is attached to the envelope.
* This value should be between [0, 7000]
* @min 0
* @max 7000
*/
get resonance(): Frequency {
return this._filterFreqScaler.min;
}
set resonance(val) {
this._filterFreqScaler.min = this.toFrequency(val);
this.octaves = this._octaves;
}
/**
* The number of octaves above the "resonance" frequency
* that the filter ramps during the attack/decay envelope
* @min 0
* @max 8
*/
get octaves(): number {
return this._octaves;
}
set octaves(val) {
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();
this.detune.dispose();
this._filterFreqScaler.dispose();
this._amplitude.dispose();
this.envelope.dispose();
this._highpass.dispose();
return this;
}
}