/
MonoSynth.ts
177 lines (158 loc) · 5.38 KB
/
MonoSynth.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
import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope";
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
import { Filter, FilterOptions } from "../component/filter/Filter";
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
import { readOnly, RecursivePartial } from "../core/util/Interface";
import { Monophonic, MonophonicOptions } from "../instrument/Monophonic";
import { OmniOscillator } from "../source/oscillator/OmniOscillator";
import { Source } from "../source/Source";
import { FrequencyEnvelope, FrequencyEnvelopeOptions } from "../component/envelope/FrequencyEnvelope";
import { NormalRange, Seconds, Time } from "../core/type/Units";
import { Signal } from "../signal/Signal";
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
import { OmniOscillatorSynthOptions } from "../source/oscillator/OscillatorInterface";
export interface MonoSynthOptions extends MonophonicOptions {
oscillator: OmniOscillatorSynthOptions;
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
filterEnvelope: Omit<FrequencyEnvelopeOptions, keyof ToneAudioNodeOptions>;
filter: Omit<FilterOptions, keyof ToneAudioNodeOptions>;
}
/**
* MonoSynth is composed of one `oscillator`, one `filter`, and two `envelopes`.
* The amplitude of the Oscillator and the cutoff frequency of the
* Filter are controlled by Envelopes.
* <img src="https://docs.google.com/drawings/d/1gaY1DF9_Hzkodqf8JI1Cg2VZfwSElpFQfI94IQwad38/pub?w=924&h=240">
* @example
* const synth = new Tone.MonoSynth({
* oscillator: {
* type: "square"
* },
* envelope: {
* attack: 0.1
* }
* }).toDestination();
* synth.triggerAttackRelease("C4", "8n");
* @category Instrument
*/
export class MonoSynth extends Monophonic<MonoSynthOptions> {
readonly name = "MonoSynth";
/**
* The oscillator.
*/
readonly oscillator: OmniOscillator<any>;
/**
* The frequency control.
*/
readonly frequency: Signal<"frequency">;
/**
* The detune control.
*/
readonly detune: Signal<"cents">;
/**
* The filter.
*/
readonly filter: Filter;
/**
* The filter envelope.
*/
readonly filterEnvelope: FrequencyEnvelope;
/**
* The amplitude envelope.
*/
readonly envelope: AmplitudeEnvelope;
constructor(options?: RecursivePartial<MonoSynthOptions>);
constructor() {
super(optionsFromArguments(MonoSynth.getDefaults(), arguments));
const options = optionsFromArguments(MonoSynth.getDefaults(), arguments);
this.oscillator = new OmniOscillator(Object.assign(options.oscillator, {
context: this.context,
detune: options.detune,
onstop: () => this.onsilence(this),
}));
this.frequency = this.oscillator.frequency;
this.detune = this.oscillator.detune;
this.filter = new Filter(Object.assign(options.filter, { context: this.context }));
this.filterEnvelope = new FrequencyEnvelope(Object.assign(options.filterEnvelope, { context: this.context }));
this.envelope = new AmplitudeEnvelope(Object.assign(options.envelope, { context: this.context }));
// connect the oscillators to the output
this.oscillator.chain(this.filter, this.envelope, this.output);
// connect the filter envelope
this.filterEnvelope.connect(this.filter.frequency);
readOnly(this, ["oscillator", "frequency", "detune", "filter", "filterEnvelope", "envelope"]);
}
static getDefaults(): MonoSynthOptions {
return Object.assign(Monophonic.getDefaults(), {
envelope: Object.assign(
omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
{
attack: 0.005,
decay: 0.1,
release: 1,
sustain: 0.9,
},
),
filter: Object.assign(
omitFromObject(Filter.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
{
Q: 1,
rolloff: -12,
type: "lowpass",
},
),
filterEnvelope: Object.assign(
omitFromObject(FrequencyEnvelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
{
attack: 0.6,
baseFrequency: 200,
decay: 0.2,
exponent: 2,
octaves: 3,
release: 2,
sustain: 0.5,
}
),
oscillator: Object.assign(
omitFromObject(OmniOscillator.getDefaults(), Object.keys(Source.getDefaults())),
{
type: "sawtooth",
},
),
});
}
/**
* start the attack portion of the envelope
* @param time the time the attack should start
* @param velocity the velocity of the note (0-1)
*/
protected _triggerEnvelopeAttack(time: Seconds, velocity = 1): void {
this.envelope.triggerAttack(time, velocity);
this.filterEnvelope.triggerAttack(time);
this.oscillator.start(time);
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);
}
}
/**
* start the release portion of the envelope
* @param time the time the release should start
*/
protected _triggerEnvelopeRelease(time: Seconds): void {
this.envelope.triggerRelease(time);
this.filterEnvelope.triggerRelease(time);
this.oscillator.stop(time + this.toSeconds(this.envelope.release));
}
getLevelAtTime(time: Time): NormalRange {
time = this.toSeconds(time);
return this.envelope.getValueAtTime(time);
}
dispose(): this {
super.dispose();
this.oscillator.dispose();
this.envelope.dispose();
this.filterEnvelope.dispose();
this.filter.dispose();
return this;
}
}