0
# Audio Sources
1
2
Audio generation including oscillators, noise sources, and audio file players for creating sound content and musical material.
3
4
## Capabilities
5
6
### Oscillators
7
8
Basic waveform generators for synthesis.
9
10
```typescript { .api }
11
/**
12
* Basic oscillator with standard waveforms
13
*/
14
class Oscillator {
15
constructor(frequency?: Frequency, type?: OscillatorType);
16
17
/** Frequency parameter */
18
frequency: Signal<"frequency">;
19
20
/** Detune parameter in cents */
21
detune: Signal<"cents">;
22
23
/** Oscillator type */
24
type: OscillatorType;
25
26
/** Phase offset */
27
phase: Degrees;
28
29
/** Start oscillator */
30
start(time?: Time): this;
31
32
/** Stop oscillator */
33
stop(time?: Time): this;
34
35
/** Connect to destination */
36
toDestination(): this;
37
}
38
39
/**
40
* Unified oscillator interface supporting multiple oscillator types
41
*/
42
class OmniOscillator {
43
constructor(frequency?: Frequency, type?: OmniOscillatorType);
44
45
readonly frequency: Signal<"frequency">;
46
readonly detune: Signal<"cents">;
47
type: OmniOscillatorType;
48
phase: Degrees;
49
50
start(time?: Time): this;
51
stop(time?: Time): this;
52
toDestination(): this;
53
}
54
55
/**
56
* Low Frequency Oscillator for modulation
57
*/
58
class LFO {
59
constructor(frequency?: Frequency, min?: number, max?: number);
60
61
/** LFO frequency */
62
frequency: Signal<"frequency">;
63
64
/** Minimum output value */
65
min: number;
66
67
/** Maximum output value */
68
max: number;
69
70
/** LFO waveform type */
71
type: "sine" | "square" | "triangle" | "sawtooth";
72
73
/** Phase offset */
74
phase: Degrees;
75
76
/** Amplitude of LFO */
77
amplitude: Signal<"normalRange">;
78
79
start(time?: Time): this;
80
stop(time?: Time): this;
81
connect(destination: AudioParam | AudioNode): this;
82
}
83
```
84
85
### Advanced Oscillators
86
87
Complex oscillators with modulation capabilities.
88
89
```typescript { .api }
90
/**
91
* Amplitude modulated oscillator
92
*/
93
class AMOscillator {
94
constructor(frequency?: Frequency, type?: OscillatorType, modulationType?: OscillatorType);
95
96
readonly frequency: Signal<"frequency">;
97
readonly detune: Signal<"cents">;
98
99
/** Modulation frequency */
100
modulationFrequency: Signal<"frequency">;
101
102
/** Harmonicity ratio */
103
harmonicity: Signal<"positive">;
104
105
type: OscillatorType;
106
modulationType: OscillatorType;
107
108
start(time?: Time): this;
109
stop(time?: Time): this;
110
}
111
112
/**
113
* Frequency modulated oscillator
114
*/
115
class FMOscillator {
116
constructor(frequency?: Frequency, type?: OscillatorType, modulationType?: OscillatorType);
117
118
readonly frequency: Signal<"frequency">;
119
readonly detune: Signal<"cents">;
120
121
/** Modulation index */
122
modulationIndex: Signal<"positive">;
123
124
/** Harmonicity ratio */
125
harmonicity: Signal<"positive">;
126
127
type: OscillatorType;
128
modulationType: OscillatorType;
129
130
start(time?: Time): this;
131
stop(time?: Time): this;
132
}
133
134
/**
135
* Multiple detuned oscillators for thick sound
136
*/
137
class FatOscillator {
138
constructor(frequency?: Frequency, type?: OscillatorType, spread?: Cents);
139
140
readonly frequency: Signal<"frequency">;
141
readonly detune: Signal<"cents">;
142
143
/** Number of oscillators */
144
count: number;
145
146
/** Detune spread between oscillators */
147
spread: Cents;
148
149
type: OscillatorType;
150
151
start(time?: Time): this;
152
stop(time?: Time): this;
153
}
154
155
/**
156
* Pulse width oscillator
157
*/
158
class PulseOscillator {
159
constructor(frequency?: Frequency, width?: NormalRange);
160
161
readonly frequency: Signal<"frequency">;
162
readonly detune: Signal<"cents">;
163
164
/** Pulse width */
165
width: Signal<"normalRange">;
166
167
start(time?: Time): this;
168
stop(time?: Time): this;
169
}
170
171
/**
172
* Pulse width modulated oscillator
173
*/
174
class PWMOscillator {
175
constructor(frequency?: Frequency, modulationFrequency?: Frequency);
176
177
readonly frequency: Signal<"frequency">;
178
readonly detune: Signal<"cents">;
179
180
/** Modulation frequency */
181
modulationFrequency: Signal<"frequency">;
182
183
start(time?: Time): this;
184
stop(time?: Time): this;
185
}
186
```
187
188
### Audio File Players
189
190
Sources for playing back recorded audio.
191
192
```typescript { .api }
193
/**
194
* Audio file player
195
*/
196
class Player {
197
constructor(url?: string | AudioBuffer | ToneAudioBuffer, onload?: () => void);
198
199
/** Playback rate */
200
playbackRate: Signal<"positive">;
201
202
/** Loop the audio */
203
loop: boolean;
204
205
/** Loop start time */
206
loopStart: Time;
207
208
/** Loop end time */
209
loopEnd: Time;
210
211
/** Reverse playback */
212
reverse: boolean;
213
214
/** Auto-start playback when loaded */
215
autostart: boolean;
216
217
/** Fade in time */
218
fadeIn: Time;
219
220
/** Fade out time */
221
fadeOut: Time;
222
223
/** Current loaded state */
224
loaded: boolean;
225
226
/** Buffer duration */
227
buffer: ToneAudioBuffer;
228
229
/** Load audio file */
230
load(url: string): Promise<this>;
231
232
/** Start playback */
233
start(time?: Time, offset?: Time, duration?: Time): this;
234
235
/** Stop playback */
236
stop(time?: Time): this;
237
238
/** Seek to position */
239
seek(offset: Time, time?: Time): this;
240
241
/** Connect to destination */
242
toDestination(): this;
243
}
244
245
/**
246
* Collection of audio players organized by name
247
*/
248
class Players {
249
constructor(
250
urls: {[name: string]: string} | ToneAudioBuffer[],
251
onload?: () => void,
252
baseUrl?: string
253
);
254
255
/** Volume control */
256
volume: Volume;
257
258
/** Mute all players */
259
mute: boolean;
260
261
/** Base URL for loading */
262
baseUrl: string;
263
264
/** Get player by name */
265
player(name: string): Player;
266
267
/** Check if player exists */
268
has(name: string): boolean;
269
270
/** Get player (alias for player()) */
271
get(name: string): Player;
272
273
/** Start all players */
274
startAll(time?: Time): this;
275
276
/** Stop all players */
277
stopAll(time?: Time): this;
278
279
/** Connect to destination */
280
toDestination(): this;
281
}
282
283
/**
284
* Granular synthesis player
285
*/
286
class GrainPlayer {
287
constructor(url?: string | ToneAudioBuffer, onload?: () => void);
288
289
/** Playback rate */
290
playbackRate: Signal<"positive">;
291
292
/** Grain size */
293
grainSize: Signal<"time">;
294
295
/** Overlap between grains */
296
overlap: Signal<"time">;
297
298
/** Loop the buffer */
299
loop: boolean;
300
301
/** Loaded buffer */
302
buffer: ToneAudioBuffer;
303
304
/** Load audio file */
305
load(url: string): Promise<this>;
306
307
start(time?: Time, offset?: Time, duration?: Time): this;
308
stop(time?: Time): this;
309
toDestination(): this;
310
}
311
312
/**
313
* Low-level buffer source wrapper
314
*/
315
class ToneBufferSource {
316
constructor(buffer?: ToneAudioBuffer | AudioBuffer, onended?: () => void);
317
318
/** Playback rate */
319
playbackRate: Signal<"positive">;
320
321
/** Loop the buffer */
322
loop: boolean;
323
324
/** Loop start */
325
loopStart: number;
326
327
/** Loop end */
328
loopEnd: number;
329
330
/** Curve for attack/release */
331
curve: "linear" | "exponential";
332
333
/** Fade in time */
334
fadeIn: Time;
335
336
/** Fade out time */
337
fadeOut: Time;
338
339
start(time?: Time, offset?: Time, duration?: Time, gain?: NormalRange): this;
340
stop(time?: Time): this;
341
}
342
```
343
344
### Noise Sources
345
346
Generators for various types of noise.
347
348
```typescript { .api }
349
/**
350
* Noise generator
351
*/
352
class Noise {
353
constructor(type?: NoiseType);
354
355
/** Noise type */
356
type: NoiseType;
357
358
/** Playback state */
359
state: "started" | "stopped";
360
361
start(time?: Time): this;
362
stop(time?: Time): this;
363
restart(time?: Time): this;
364
toDestination(): this;
365
}
366
367
type NoiseType = "white" | "brown" | "pink";
368
```
369
370
### Input Sources
371
372
Sources for capturing external audio.
373
374
```typescript { .api }
375
/**
376
* Microphone and media device input
377
*/
378
class UserMedia {
379
constructor(volume?: Decibels);
380
381
/** Volume control */
382
volume: Volume;
383
384
/** Mute the input */
385
mute: boolean;
386
387
/** Device constraints */
388
constraints: MediaStreamConstraints;
389
390
/** Open media device */
391
open(deviceId?: string): Promise<this>;
392
393
/** Close media device */
394
close(): this;
395
396
/** Get available devices */
397
static enumerateDevices(): Promise<MediaDeviceInfo[]>;
398
399
/** Check for user media support */
400
static supported: boolean;
401
402
toDestination(): this;
403
}
404
```
405
406
**Usage Examples:**
407
408
```typescript
409
import { Oscillator, Player, LFO, Noise, start } from "tone";
410
411
await start();
412
413
// Basic oscillator
414
const osc = new Oscillator(440, "sawtooth").toDestination();
415
osc.start();
416
osc.stop("+2");
417
418
// LFO modulation
419
const lfo = new LFO(2, 200, 800);
420
lfo.connect(osc.frequency);
421
lfo.start();
422
423
// Audio player
424
const player = new Player("path/to/audio.mp3", () => {
425
console.log("Loaded");
426
}).toDestination();
427
428
player.autostart = true;
429
player.loop = true;
430
431
// Multiple players
432
const players = new Players({
433
kick: "kick.mp3",
434
snare: "snare.mp3",
435
hihat: "hihat.mp3"
436
}).toDestination();
437
438
players.player("kick").start();
439
players.player("snare").start("+0.5");
440
441
// Noise source
442
const noise = new Noise("pink").toDestination();
443
noise.start();
444
noise.stop("+1");
445
```
446
447
## Types
448
449
```typescript { .api }
450
type OscillatorType = "sine" | "square" | "triangle" | "sawtooth";
451
type OmniOscillatorType = OscillatorType | "fatsine" | "fatsquare" | "fatsawtooth" | "fattriangle" | string;
452
453
interface PlayerOptions {
454
onload: () => void;
455
onerror: (error: Error) => void;
456
playbackRate: Positive;
457
loop: boolean;
458
autostart: boolean;
459
loopStart: Time;
460
loopEnd: Time;
461
reverse: boolean;
462
fadeIn: Time;
463
fadeOut: Time;
464
}
465
466
interface OscillatorOptions {
467
frequency: Frequency;
468
detune: Cents;
469
type: OncillatorType;
470
phase: Degrees;
471
volume: Decibels;
472
}
473
474
interface LFOOptions {
475
frequency: Frequency;
476
min: number;
477
max: number;
478
phase: Degrees;
479
type: "sine" | "square" | "triangle" | "sawtooth";
480
amplitude: NormalRange;
481
}
482
```