1 | import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope.js";
|
2 | import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope.js";
|
3 | import { Filter, FilterOptions } from "../component/filter/Filter.js";
|
4 | import { omitFromObject, optionsFromArguments } from "../core/util/Defaults.js";
|
5 | import { readOnly, RecursivePartial } from "../core/util/Interface.js";
|
6 | import { Monophonic, MonophonicOptions } from "../instrument/Monophonic.js";
|
7 | import { OmniOscillator } from "../source/oscillator/OmniOscillator.js";
|
8 | import { Source } from "../source/Source.js";
|
9 | import {
|
10 | FrequencyEnvelope,
|
11 | FrequencyEnvelopeOptions,
|
12 | } from "../component/envelope/FrequencyEnvelope.js";
|
13 | import { NormalRange, Seconds, Time } from "../core/type/Units.js";
|
14 | import { Signal } from "../signal/Signal.js";
|
15 | import {
|
16 | ToneAudioNode,
|
17 | ToneAudioNodeOptions,
|
18 | } from "../core/context/ToneAudioNode.js";
|
19 | import { OmniOscillatorSynthOptions } from "../source/oscillator/OscillatorInterface.js";
|
20 |
|
21 | export 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 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | export class MonoSynth extends Monophonic<MonoSynthOptions> {
|
46 | readonly name = "MonoSynth";
|
47 |
|
48 | |
49 |
|
50 |
|
51 | readonly oscillator: OmniOscillator<any>;
|
52 |
|
53 | |
54 |
|
55 |
|
56 | readonly frequency: Signal<"frequency">;
|
57 |
|
58 | |
59 |
|
60 |
|
61 | readonly detune: Signal<"cents">;
|
62 |
|
63 | |
64 |
|
65 |
|
66 | readonly filter: Filter;
|
67 |
|
68 | |
69 |
|
70 |
|
71 | readonly filterEnvelope: FrequencyEnvelope;
|
72 |
|
73 | |
74 |
|
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 |
|
175 |
|
176 |
|
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 |
|
191 |
|
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 | }
|