UNPKG

12.7 kBTypeScriptView Raw
1import { AudioRange, Cents, Degrees, Frequency, Positive } from "../../core/type/Units.js";
2import { Omit } from "../../core/util/Interface.js";
3import { Signal } from "../../signal/Signal.js";
4import { SourceOptions } from "../Source.js";
5/**
6 * The common interface of all Oscillators
7 */
8export interface ToneOscillatorInterface {
9 /**
10 * The oscillator type without the partialsCount appended to the end
11 * @example
12 * const osc = new Tone.Oscillator();
13 * osc.type = "sine2";
14 * console.log(osc.baseType); // "sine"
15 */
16 baseType: OscillatorType | "pulse" | "pwm";
17 /**
18 * The oscillator's type. Also capable of setting the first x number of partials of the oscillator.
19 * For example: "sine4" would set be the first 4 partials of the sine wave and "triangle8" would
20 * set the first 8 partials of the triangle wave.
21 * @example
22 * return Tone.Offline(() => {
23 * const osc = new Tone.Oscillator().toDestination().start();
24 * osc.type = "sine2";
25 * }, 0.1, 1);
26 */
27 type: ExtendedToneOscillatorType;
28 /**
29 * The frequency value of the oscillator
30 * @example
31 * const osc = new Tone.FMOscillator("Bb4").toDestination().start();
32 * osc.frequency.rampTo("D2", 3);
33 */
34 readonly frequency: Signal<"frequency">;
35 /**
36 * The detune value in cents (100th of a semitone).
37 * @example
38 * const osc = new Tone.PulseOscillator("F3").toDestination().start();
39 * // pitch it 1 octave = 12 semitones = 1200 cents
40 * osc.detune.setValueAtTime(-1200, Tone.now());
41 * osc.detune.setValueAtTime(1200, Tone.now() + 0.5);
42 * osc.detune.linearRampToValueAtTime(0, Tone.now() + 1);
43 * osc.stop(Tone.now() + 1.5);
44 */
45 readonly detune: Signal<"cents">;
46 /**
47 * The phase is the starting position within the oscillator's cycle. For example
48 * a phase of 180 would start halfway through the oscillator's cycle.
49 * @example
50 * return Tone.Offline(() => {
51 * const osc = new Tone.Oscillator({
52 * frequency: 20,
53 * phase: 90
54 * }).toDestination().start();
55 * }, 0.1, 1);
56 */
57 phase: Degrees;
58 /**
59 * The partials describes the relative amplitude of each of the harmonics of the oscillator.
60 * The first value in the array is the first harmonic (i.e. the fundamental frequency), the
61 * second harmonic is an octave up, the third harmonic is an octave and a fifth, etc. The resulting
62 * oscillator output is composed of a sine tone at the relative amplitude at each of the harmonic intervals.
63 *
64 * Setting this value will automatically set the type to "custom".
65 * The value is an empty array when the type is not "custom".
66 * @example
67 * const osc = new Tone.Oscillator("F3").toDestination().start();
68 * setInterval(() => {
69 * // generate 8 random partials
70 * osc.partials = new Array(8).fill(0).map(() => Math.random());
71 * }, 1000);
72 */
73 partials: number[];
74 /**
75 * 'partialCount' offers an alternative way to set the number of used partials.
76 * When partialCount is 0, the maximum number of partials are used when representing
77 * the waveform using the periodicWave. When 'partials' is set, this value is
78 * not settable, but equals the length of the partials array. A square wave wave
79 * is composed of only odd harmonics up through the harmonic series. Partial count
80 * can limit the number of harmonics which are used to generate the waveform.
81 * @example
82 * const osc = new Tone.Oscillator("C3", "square").toDestination().start();
83 * osc.partialCount = 1;
84 * setInterval(() => {
85 * osc.partialCount++;
86 * console.log(osc.partialCount);
87 * }, 500);
88 */
89 partialCount?: number;
90 /**
91 * Returns an array of values which represents the waveform.
92 * @param length The length of the waveform to return
93 */
94 asArray(length: number): Promise<Float32Array>;
95}
96/**
97 * Render a segment of the oscillator to an offline context and return the results as an array
98 */
99export declare function generateWaveform(instance: any, length: number): Promise<Float32Array>;
100/**
101 * The supported number of partials
102 */
103type PartialsRange = 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;
104/**
105 * Oscillators with partials
106 */
107type SineWithPartials = `sine${PartialsRange}`;
108type SquareWithPartials = `square${PartialsRange}`;
109type SawtoothWithPartials = `sawtooth${PartialsRange}`;
110type TriangleWithPartials = `triangle${PartialsRange}`;
111type TypeWithPartials = SineWithPartials | SquareWithPartials | TriangleWithPartials | SawtoothWithPartials;
112interface BaseOscillatorOptions extends SourceOptions {
113 frequency: Frequency;
114 detune: Cents;
115 phase: Degrees;
116}
117export type NonCustomOscillatorType = Exclude<OscillatorType, "custom">;
118type AllNonCustomOscillatorType = NonCustomOscillatorType | TypeWithPartials;
119export type ToneOscillatorType = AllNonCustomOscillatorType | "custom";
120export type ExtendedToneOscillatorType = ToneOscillatorType | "pwm" | "pulse";
121/**
122 * Oscillator Interfaces
123 */
124interface ToneCustomOscillatorOptions extends BaseOscillatorOptions {
125 type: "custom";
126 partials: number[];
127}
128interface ToneTypeOscillatorOptions extends BaseOscillatorOptions {
129 type: NonCustomOscillatorType;
130 partialCount?: number;
131}
132interface TonePartialOscillatorOptions extends BaseOscillatorOptions {
133 type: TypeWithPartials;
134}
135export type ToneOscillatorConstructorOptions = ToneCustomOscillatorOptions | ToneTypeOscillatorOptions | TonePartialOscillatorOptions;
136export interface ToneOscillatorOptions extends BaseOscillatorOptions {
137 type: ToneOscillatorType;
138 partialCount: number;
139 partials: number[];
140}
141/**
142 * FMOscillator Interface
143 */
144interface FMBaseOscillatorOptions extends BaseOscillatorOptions {
145 harmonicity: Positive;
146 modulationIndex: Positive;
147 modulationType: AllNonCustomOscillatorType;
148}
149interface FMCustomOscillatorOptions extends FMBaseOscillatorOptions {
150 type: "custom";
151 partials: number[];
152}
153interface FMTypeOscillatorOptions extends FMBaseOscillatorOptions {
154 type: NonCustomOscillatorType;
155 partialsCount?: number;
156}
157interface FMPartialsOscillatorOptions extends FMBaseOscillatorOptions {
158 type: TypeWithPartials;
159}
160export type FMConstructorOptions = FMTypeOscillatorOptions | FMCustomOscillatorOptions | FMPartialsOscillatorOptions;
161export interface FMOscillatorOptions extends ToneOscillatorOptions {
162 harmonicity: Positive;
163 modulationIndex: Positive;
164 modulationType: AllNonCustomOscillatorType;
165}
166/**
167 * AMOscillator Interface
168 */
169interface AMBaseOscillatorOptions extends BaseOscillatorOptions {
170 harmonicity: Positive;
171 modulationType: AllNonCustomOscillatorType;
172}
173interface AMCustomOscillatorOptions extends AMBaseOscillatorOptions {
174 type: "custom";
175 partials: number[];
176}
177interface AMTypeOscillatorOptions extends AMBaseOscillatorOptions {
178 type: NonCustomOscillatorType;
179 partialsCount?: number;
180}
181interface AMPartialsOscillatorOptions extends AMBaseOscillatorOptions {
182 type: TypeWithPartials;
183}
184export type AMConstructorOptions = AMCustomOscillatorOptions | AMTypeOscillatorOptions | AMPartialsOscillatorOptions;
185export interface AMOscillatorOptions extends ToneOscillatorOptions {
186 harmonicity: Positive;
187 modulationType: AllNonCustomOscillatorType;
188}
189/**
190 * FatOscillator
191 */
192interface FatBaseOscillatorOptions extends BaseOscillatorOptions {
193 spread: Cents;
194 count: Positive;
195}
196interface FatCustomOscillatorOptions extends FatBaseOscillatorOptions {
197 type: "custom";
198 partials: number[];
199}
200interface FatTypeOscillatorOptions extends FatBaseOscillatorOptions {
201 type: NonCustomOscillatorType;
202 partialCount?: number;
203}
204interface FatPartialsOscillatorOptions extends FatBaseOscillatorOptions {
205 type: TypeWithPartials;
206}
207export type FatConstructorOptions = FatCustomOscillatorOptions | FatTypeOscillatorOptions | FatPartialsOscillatorOptions;
208export interface FatOscillatorOptions extends ToneOscillatorOptions {
209 spread: Cents;
210 count: Positive;
211}
212/**
213 * Pulse Oscillator
214 */
215export interface PulseOscillatorOptions extends BaseOscillatorOptions {
216 type: "pulse";
217 width: AudioRange;
218}
219/**
220 * PWM Oscillator
221 */
222export interface PWMOscillatorOptions extends BaseOscillatorOptions {
223 type: "pwm";
224 modulationFrequency: Frequency;
225}
226/**
227 * OMNI OSCILLATOR
228 */
229/**
230 * FM Oscillators with partials
231 */
232type FMSineWithPartials = `fmsine${PartialsRange}`;
233type FMSquareWithPartials = `fmsquare${PartialsRange}`;
234type FMSawtoothWithPartials = `fmsawtooth${PartialsRange}`;
235type FMTriangleWithPartials = `fmtriangle${PartialsRange}`;
236type FMTypeWithPartials = FMSineWithPartials | FMSquareWithPartials | FMSawtoothWithPartials | FMTriangleWithPartials;
237/**
238 * AM Oscillators with partials
239 */
240type AMSineWithPartials = `amsine${PartialsRange}`;
241type AMSquareWithPartials = `amsquare${PartialsRange}`;
242type AMSawtoothWithPartials = `amsawtooth${PartialsRange}`;
243type AMTriangleWithPartials = `amtriangle${PartialsRange}`;
244type AMTypeWithPartials = AMSineWithPartials | AMSquareWithPartials | AMSawtoothWithPartials | AMTriangleWithPartials;
245/**
246 * Fat Oscillators with partials
247 */
248type FatSineWithPartials = `fatsine${PartialsRange}`;
249type FatSquareWithPartials = `fatsquare${PartialsRange}`;
250type FatSawtoothWithPartials = `fatsawtooth${PartialsRange}`;
251type FatTriangleWithPartials = `fattriangle${PartialsRange}`;
252type FatTypeWithPartials = FatSineWithPartials | FatSquareWithPartials | FatSawtoothWithPartials | FatTriangleWithPartials;
253/**
254 * Omni FM
255 */
256interface OmniFMCustomOscillatorOptions extends FMBaseOscillatorOptions {
257 type: "fmcustom";
258 partials: number[];
259}
260interface OmniFMTypeOscillatorOptions extends FMBaseOscillatorOptions {
261 type: "fmsine" | "fmsquare" | "fmsawtooth" | "fmtriangle";
262 partialsCount?: number;
263}
264interface OmniFMPartialsOscillatorOptions extends FMBaseOscillatorOptions {
265 type: FMTypeWithPartials;
266}
267/**
268 * Omni AM
269 */
270interface OmniAMCustomOscillatorOptions extends AMBaseOscillatorOptions {
271 type: "amcustom";
272 partials: number[];
273}
274interface OmniAMTypeOscillatorOptions extends AMBaseOscillatorOptions {
275 type: "amsine" | "amsquare" | "amsawtooth" | "amtriangle";
276 partialsCount?: number;
277}
278interface OmniAMPartialsOscillatorOptions extends AMBaseOscillatorOptions {
279 type: AMTypeWithPartials;
280}
281/**
282 * Omni Fat
283 */
284interface OmniFatCustomOscillatorOptions extends FatBaseOscillatorOptions {
285 type: "fatcustom";
286 partials: number[];
287}
288interface OmniFatTypeOscillatorOptions extends FatBaseOscillatorOptions {
289 type: "fatsine" | "fatsquare" | "fatsawtooth" | "fattriangle";
290 partialsCount?: number;
291}
292interface OmniFatPartialsOscillatorOptions extends FatBaseOscillatorOptions {
293 type: FatTypeWithPartials;
294}
295export type OmniOscillatorType = "fatsine" | "fatsquare" | "fatsawtooth" | "fattriangle" | "fatcustom" | FatTypeWithPartials | "fmsine" | "fmsquare" | "fmsawtooth" | "fmtriangle" | "fmcustom" | FMTypeWithPartials | "amsine" | "amsquare" | "amsawtooth" | "amtriangle" | "amcustom" | AMTypeWithPartials | TypeWithPartials | OscillatorType | "pulse" | "pwm";
296export type OmniOscillatorOptions = PulseOscillatorOptions | PWMOscillatorOptions | OmniFatCustomOscillatorOptions | OmniFatTypeOscillatorOptions | OmniFatPartialsOscillatorOptions | OmniFMCustomOscillatorOptions | OmniFMTypeOscillatorOptions | OmniFMPartialsOscillatorOptions | OmniAMCustomOscillatorOptions | OmniAMTypeOscillatorOptions | OmniAMPartialsOscillatorOptions | ToneOscillatorConstructorOptions;
297type OmitSourceOptions<T extends BaseOscillatorOptions> = Omit<T, "frequency" | "detune" | "context">;
298/**
299 * The settable options for the omni oscillator inside of the source which excludes certain attributes that are defined by the parent class
300 */
301export type OmniOscillatorSynthOptions = OmitSourceOptions<PulseOscillatorOptions> | OmitSourceOptions<PWMOscillatorOptions> | OmitSourceOptions<OmniFatCustomOscillatorOptions> | OmitSourceOptions<OmniFatTypeOscillatorOptions> | OmitSourceOptions<OmniFatPartialsOscillatorOptions> | OmitSourceOptions<OmniFMCustomOscillatorOptions> | OmitSourceOptions<OmniFMTypeOscillatorOptions> | OmitSourceOptions<OmniFMPartialsOscillatorOptions> | OmitSourceOptions<OmniAMCustomOscillatorOptions> | OmitSourceOptions<OmniAMTypeOscillatorOptions> | OmitSourceOptions<OmniAMPartialsOscillatorOptions> | OmitSourceOptions<ToneCustomOscillatorOptions> | OmitSourceOptions<ToneTypeOscillatorOptions> | OmitSourceOptions<TonePartialOscillatorOptions>;
302export {};