UNPKG

5.66 kBPlain TextView Raw
1import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope.js";
2import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope.js";
3import { Filter, FilterOptions } from "../component/filter/Filter.js";
4import { omitFromObject, optionsFromArguments } from "../core/util/Defaults.js";
5import { readOnly, RecursivePartial } from "../core/util/Interface.js";
6import { Monophonic, MonophonicOptions } from "../instrument/Monophonic.js";
7import { OmniOscillator } from "../source/oscillator/OmniOscillator.js";
8import { Source } from "../source/Source.js";
9import {
10 FrequencyEnvelope,
11 FrequencyEnvelopeOptions,
12} from "../component/envelope/FrequencyEnvelope.js";
13import { NormalRange, Seconds, Time } from "../core/type/Units.js";
14import { Signal } from "../signal/Signal.js";
15import {
16 ToneAudioNode,
17 ToneAudioNodeOptions,
18} from "../core/context/ToneAudioNode.js";
19import { OmniOscillatorSynthOptions } from "../source/oscillator/OscillatorInterface.js";
20
21export interface MonoSynthOptions extends MonophonicOptions {
22 oscillator: OmniOscillatorSynthOptions;
23 envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
24 filterEnvelope: Omit<FrequencyEnvelopeOptions, keyof ToneAudioNodeOptions>;
25 filter: Omit<FilterOptions, keyof ToneAudioNodeOptions>;
26}
27
28/**
29 * MonoSynth is composed of one `oscillator`, one `filter`, and two `envelopes`.
30 * The amplitude of the Oscillator and the cutoff frequency of the
31 * Filter are controlled by Envelopes.
32 * <img src="https://docs.google.com/drawings/d/1gaY1DF9_Hzkodqf8JI1Cg2VZfwSElpFQfI94IQwad38/pub?w=924&h=240">
33 * @example
34 * const synth = new Tone.MonoSynth({
35 * oscillator: {
36 * type: "square"
37 * },
38 * envelope: {
39 * attack: 0.1
40 * }
41 * }).toDestination();
42 * synth.triggerAttackRelease("C4", "8n");
43 * @category Instrument
44 */
45export class MonoSynth extends Monophonic<MonoSynthOptions> {
46 readonly name = "MonoSynth";
47
48 /**
49 * The oscillator.
50 */
51 readonly oscillator: OmniOscillator<any>;
52
53 /**
54 * The frequency control.
55 */
56 readonly frequency: Signal<"frequency">;
57
58 /**
59 * The detune control.
60 */
61 readonly detune: Signal<"cents">;
62
63 /**
64 * The filter.
65 */
66 readonly filter: Filter;
67
68 /**
69 * The filter envelope.
70 */
71 readonly filterEnvelope: FrequencyEnvelope;
72
73 /**
74 * The amplitude envelope.
75 */
76 readonly envelope: AmplitudeEnvelope;
77
78 constructor(options?: RecursivePartial<MonoSynthOptions>);
79 constructor() {
80 const options = optionsFromArguments(
81 MonoSynth.getDefaults(),
82 arguments
83 );
84 super(options);
85
86 this.oscillator = new OmniOscillator(
87 Object.assign(options.oscillator, {
88 context: this.context,
89 detune: options.detune,
90 onstop: () => this.onsilence(this),
91 })
92 );
93 this.frequency = this.oscillator.frequency;
94 this.detune = this.oscillator.detune;
95 this.filter = new Filter(
96 Object.assign(options.filter, { context: this.context })
97 );
98 this.filterEnvelope = new FrequencyEnvelope(
99 Object.assign(options.filterEnvelope, { context: this.context })
100 );
101 this.envelope = new AmplitudeEnvelope(
102 Object.assign(options.envelope, { context: this.context })
103 );
104
105 // connect the oscillators to the output
106 this.oscillator.chain(this.filter, this.envelope, this.output);
107
108 // connect the filter envelope
109 this.filterEnvelope.connect(this.filter.frequency);
110
111 readOnly(this, [
112 "oscillator",
113 "frequency",
114 "detune",
115 "filter",
116 "filterEnvelope",
117 "envelope",
118 ]);
119 }
120
121 static getDefaults(): MonoSynthOptions {
122 return Object.assign(Monophonic.getDefaults(), {
123 envelope: Object.assign(
124 omitFromObject(
125 Envelope.getDefaults(),
126 Object.keys(ToneAudioNode.getDefaults())
127 ),
128 {
129 attack: 0.005,
130 decay: 0.1,
131 release: 1,
132 sustain: 0.9,
133 }
134 ),
135 filter: Object.assign(
136 omitFromObject(
137 Filter.getDefaults(),
138 Object.keys(ToneAudioNode.getDefaults())
139 ),
140 {
141 Q: 1,
142 rolloff: -12,
143 type: "lowpass",
144 }
145 ),
146 filterEnvelope: Object.assign(
147 omitFromObject(
148 FrequencyEnvelope.getDefaults(),
149 Object.keys(ToneAudioNode.getDefaults())
150 ),
151 {
152 attack: 0.6,
153 baseFrequency: 200,
154 decay: 0.2,
155 exponent: 2,
156 octaves: 3,
157 release: 2,
158 sustain: 0.5,
159 }
160 ),
161 oscillator: Object.assign(
162 omitFromObject(
163 OmniOscillator.getDefaults(),
164 Object.keys(Source.getDefaults())
165 ),
166 {
167 type: "sawtooth",
168 }
169 ) as OmniOscillatorSynthOptions,
170 });
171 }
172
173 /**
174 * start the attack portion of the envelope
175 * @param time the time the attack should start
176 * @param velocity the velocity of the note (0-1)
177 */
178 protected _triggerEnvelopeAttack(time: Seconds, velocity = 1): void {
179 this.envelope.triggerAttack(time, velocity);
180 this.filterEnvelope.triggerAttack(time);
181 this.oscillator.start(time);
182 if (this.envelope.sustain === 0) {
183 const computedAttack = this.toSeconds(this.envelope.attack);
184 const computedDecay = this.toSeconds(this.envelope.decay);
185 this.oscillator.stop(time + computedAttack + computedDecay);
186 }
187 }
188
189 /**
190 * start the release portion of the envelope
191 * @param time the time the release should start
192 */
193 protected _triggerEnvelopeRelease(time: Seconds): void {
194 this.envelope.triggerRelease(time);
195 this.filterEnvelope.triggerRelease(time);
196 this.oscillator.stop(time + this.toSeconds(this.envelope.release));
197 }
198
199 getLevelAtTime(time: Time): NormalRange {
200 time = this.toSeconds(time);
201 return this.envelope.getValueAtTime(time);
202 }
203
204 dispose(): this {
205 super.dispose();
206 this.oscillator.dispose();
207 this.envelope.dispose();
208 this.filterEnvelope.dispose();
209 this.filter.dispose();
210 return this;
211 }
212}