UNPKG

6.73 kBTypeScriptView Raw
1import { ToneAudioBuffer } from "../../core/context/ToneAudioBuffer.js";
2import { Positive, Seconds, Time } from "../../core/type/Units.js";
3import { Source, SourceOptions } from "../Source.js";
4export interface PlayerOptions extends SourceOptions {
5 onload: () => void;
6 onerror: (error: Error) => void;
7 playbackRate: Positive;
8 loop: boolean;
9 autostart: boolean;
10 loopStart: Time;
11 loopEnd: Time;
12 reverse: boolean;
13 fadeIn: Time;
14 fadeOut: Time;
15 url?: ToneAudioBuffer | string | AudioBuffer;
16}
17/**
18 * Player is an audio file player with start, loop, and stop functions.
19 * @example
20 * const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gong_1.mp3").toDestination();
21 * // play as soon as the buffer is loaded
22 * player.autostart = true;
23 * @category Source
24 */
25export declare class Player extends Source<PlayerOptions> {
26 readonly name: string;
27 /**
28 * If the file should play as soon
29 * as the buffer is loaded.
30 */
31 autostart: boolean;
32 /**
33 * The buffer
34 */
35 private _buffer;
36 /**
37 * if the buffer should loop once it's over
38 */
39 private _loop;
40 /**
41 * if 'loop' is true, the loop will start at this position
42 */
43 private _loopStart;
44 /**
45 * if 'loop' is true, the loop will end at this position
46 */
47 private _loopEnd;
48 /**
49 * the playback rate
50 */
51 private _playbackRate;
52 /**
53 * All of the active buffer source nodes
54 */
55 private _activeSources;
56 /**
57 * The fadeIn time of the amplitude envelope.
58 */
59 fadeIn: Time;
60 /**
61 * The fadeOut time of the amplitude envelope.
62 */
63 fadeOut: Time;
64 /**
65 * @param url Either the AudioBuffer or the url from which to load the AudioBuffer
66 * @param onload The function to invoke when the buffer is loaded.
67 */
68 constructor(url?: string | AudioBuffer | ToneAudioBuffer, onload?: () => void);
69 constructor(options?: Partial<PlayerOptions>);
70 static getDefaults(): PlayerOptions;
71 /**
72 * Load the audio file as an audio buffer.
73 * Decodes the audio asynchronously and invokes
74 * the callback once the audio buffer loads.
75 * Note: this does not need to be called if a url
76 * was passed in to the constructor. Only use this
77 * if you want to manually load a new url.
78 * @param url The url of the buffer to load. Filetype support depends on the browser.
79 */
80 load(url: string): Promise<this>;
81 /**
82 * Internal callback when the buffer is loaded.
83 */
84 private _onload;
85 /**
86 * Internal callback when the buffer is done playing.
87 */
88 private _onSourceEnd;
89 /**
90 * Play the buffer at the given startTime. Optionally add an offset
91 * and/or duration which will play the buffer from a position
92 * within the buffer for the given duration.
93 *
94 * @param time When the player should start.
95 * @param offset The offset from the beginning of the sample to start at.
96 * @param duration How long the sample should play. If no duration is given, it will default to the full length of the sample (minus any offset)
97 */
98 start(time?: Time, offset?: Time, duration?: Time): this;
99 /**
100 * Internal start method
101 */
102 protected _start(startTime?: Time, offset?: Time, duration?: Time): void;
103 /**
104 * Stop playback.
105 */
106 protected _stop(time?: Time): void;
107 /**
108 * Stop and then restart the player from the beginning (or offset)
109 * @param time When the player should start.
110 * @param offset The offset from the beginning of the sample to start at.
111 * @param duration How long the sample should play. If no duration is given,
112 * it will default to the full length of the sample (minus any offset)
113 */
114 restart(time?: Seconds, offset?: Time, duration?: Time): this;
115 protected _restart(time?: Seconds, offset?: Time, duration?: Time): void;
116 /**
117 * Seek to a specific time in the player's buffer. If the
118 * source is no longer playing at that time, it will stop.
119 * @param offset The time to seek to.
120 * @param when The time for the seek event to occur.
121 * @example
122 * const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3", () => {
123 * player.start();
124 * // seek to the offset in 1 second from now
125 * player.seek(0.4, "+1");
126 * }).toDestination();
127 */
128 seek(offset: Time, when?: Time): this;
129 /**
130 * Set the loop start and end. Will only loop if loop is set to true.
131 * @param loopStart The loop start time
132 * @param loopEnd The loop end time
133 * @example
134 * const player = new Tone.Player("https://tonejs.github.io/audio/berklee/malevoices_aa2_F3.mp3").toDestination();
135 * // loop between the given points
136 * player.setLoopPoints(0.2, 0.3);
137 * player.loop = true;
138 * player.autostart = true;
139 */
140 setLoopPoints(loopStart: Time, loopEnd: Time): this;
141 /**
142 * If loop is true, the loop will start at this position.
143 */
144 get loopStart(): Time;
145 set loopStart(loopStart: Time);
146 /**
147 * If loop is true, the loop will end at this position.
148 */
149 get loopEnd(): Time;
150 set loopEnd(loopEnd: Time);
151 /**
152 * The audio buffer belonging to the player.
153 */
154 get buffer(): ToneAudioBuffer;
155 set buffer(buffer: ToneAudioBuffer);
156 /**
157 * If the buffer should loop once it's over.
158 * @example
159 * const player = new Tone.Player("https://tonejs.github.io/audio/drum-samples/breakbeat.mp3").toDestination();
160 * player.loop = true;
161 * player.autostart = true;
162 */
163 get loop(): boolean;
164 set loop(loop: boolean);
165 /**
166 * Normal speed is 1. The pitch will change with the playback rate.
167 * @example
168 * const player = new Tone.Player("https://tonejs.github.io/audio/berklee/femalevoices_aa2_A5.mp3").toDestination();
169 * // play at 1/4 speed
170 * player.playbackRate = 0.25;
171 * // play as soon as the buffer is loaded
172 * player.autostart = true;
173 */
174 get playbackRate(): Positive;
175 set playbackRate(rate: Positive);
176 /**
177 * If the buffer should be reversed. Note that this sets the underlying {@link ToneAudioBuffer.reverse}, so
178 * if multiple players are pointing at the same ToneAudioBuffer, they will all be reversed.
179 * @example
180 * const player = new Tone.Player("https://tonejs.github.io/audio/berklee/chime_1.mp3").toDestination();
181 * player.autostart = true;
182 * player.reverse = true;
183 */
184 get reverse(): boolean;
185 set reverse(rev: boolean);
186 /**
187 * If the buffer is loaded
188 */
189 get loaded(): boolean;
190 dispose(): this;
191}
192
\No newline at end of file