mirror of
https://github.com/Tonejs/Tone.js
synced 2024-12-27 03:53:07 +00:00
parent
444d6179c4
commit
aad2d8c890
1 changed files with 45 additions and 21 deletions
|
@ -31,7 +31,6 @@ export interface PlayerOptions extends SourceOptions {
|
|||
* @category Source
|
||||
*/
|
||||
export class Player extends Source<PlayerOptions> {
|
||||
|
||||
readonly name: string = "Player";
|
||||
|
||||
/**
|
||||
|
@ -86,12 +85,22 @@ export class Player extends Source<PlayerOptions> {
|
|||
* @param url Either the AudioBuffer or the url from which to load the AudioBuffer
|
||||
* @param onload The function to invoke when the buffer is loaded.
|
||||
*/
|
||||
constructor(url?: string | AudioBuffer | ToneAudioBuffer, onload?: () => void);
|
||||
constructor(
|
||||
url?: string | AudioBuffer | ToneAudioBuffer,
|
||||
onload?: () => void
|
||||
);
|
||||
constructor(options?: Partial<PlayerOptions>);
|
||||
constructor() {
|
||||
|
||||
super(optionsFromArguments(Player.getDefaults(), arguments, ["url", "onload"]));
|
||||
const options = optionsFromArguments(Player.getDefaults(), arguments, ["url", "onload"]);
|
||||
super(
|
||||
optionsFromArguments(Player.getDefaults(), arguments, [
|
||||
"url",
|
||||
"onload",
|
||||
])
|
||||
);
|
||||
const options = optionsFromArguments(Player.getDefaults(), arguments, [
|
||||
"url",
|
||||
"onload",
|
||||
]);
|
||||
|
||||
this._buffer = new ToneAudioBuffer({
|
||||
onload: this._onload.bind(this, options.onload),
|
||||
|
@ -157,8 +166,11 @@ export class Player extends Source<PlayerOptions> {
|
|||
|
||||
// delete the source from the active sources
|
||||
this._activeSources.delete(source);
|
||||
if (this._activeSources.size === 0 && !this._synced &&
|
||||
this._state.getValueAtTime(this.now()) === "started") {
|
||||
if (
|
||||
this._activeSources.size === 0 &&
|
||||
!this._synced &&
|
||||
this._state.getValueAtTime(this.now()) === "started"
|
||||
) {
|
||||
// remove the 'implicitEnd' event and replace with an explicit end
|
||||
this._state.cancel(this.now());
|
||||
this._state.setStateAtTime("stopped", this.now());
|
||||
|
@ -196,7 +208,10 @@ export class Player extends Source<PlayerOptions> {
|
|||
|
||||
// compute the duration which is either the passed in duration of the buffer.duration - offset
|
||||
const origDuration = duration;
|
||||
duration = defaultArg(duration, Math.max(this._buffer.duration - computedOffset, 0));
|
||||
duration = defaultArg(
|
||||
duration,
|
||||
Math.max(this._buffer.duration - computedOffset, 0)
|
||||
);
|
||||
let computedDuration = this.toSeconds(duration);
|
||||
|
||||
// scale it by the playback rate
|
||||
|
@ -223,9 +238,13 @@ export class Player extends Source<PlayerOptions> {
|
|||
// cancel the previous stop
|
||||
this._state.cancel(startTime + computedDuration);
|
||||
// if it's not looping, set the state change at the end of the sample
|
||||
this._state.setStateAtTime("stopped", startTime + computedDuration, {
|
||||
this._state.setStateAtTime(
|
||||
"stopped",
|
||||
startTime + computedDuration,
|
||||
{
|
||||
implicitEnd: true,
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// add it to the array of active sources
|
||||
|
@ -236,7 +255,11 @@ export class Player extends Source<PlayerOptions> {
|
|||
source.start(startTime, computedOffset);
|
||||
} else {
|
||||
// subtract the fade out time
|
||||
source.start(startTime, computedOffset, computedDuration - this.toSeconds(this.fadeOut));
|
||||
source.start(
|
||||
startTime,
|
||||
computedOffset,
|
||||
computedDuration - this.toSeconds(this.fadeOut)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -245,7 +268,7 @@ export class Player extends Source<PlayerOptions> {
|
|||
*/
|
||||
protected _stop(time?: Time): void {
|
||||
const computedTime = this.toSeconds(time);
|
||||
this._activeSources.forEach(source => source.stop(computedTime));
|
||||
this._activeSources.forEach((source) => source.stop(computedTime));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -318,7 +341,7 @@ export class Player extends Source<PlayerOptions> {
|
|||
assertRange(this.toSeconds(loopStart), 0, this.buffer.duration);
|
||||
}
|
||||
// get the current source
|
||||
this._activeSources.forEach(source => {
|
||||
this._activeSources.forEach((source) => {
|
||||
source.loopStart = loopStart;
|
||||
});
|
||||
}
|
||||
|
@ -335,7 +358,7 @@ export class Player extends Source<PlayerOptions> {
|
|||
assertRange(this.toSeconds(loopEnd), 0, this.buffer.duration);
|
||||
}
|
||||
// get the current source
|
||||
this._activeSources.forEach(source => {
|
||||
this._activeSources.forEach((source) => {
|
||||
source.loopEnd = loopEnd;
|
||||
});
|
||||
}
|
||||
|
@ -367,7 +390,7 @@ export class Player extends Source<PlayerOptions> {
|
|||
}
|
||||
this._loop = loop;
|
||||
// set the loop of all of the sources
|
||||
this._activeSources.forEach(source => {
|
||||
this._activeSources.forEach((source) => {
|
||||
source.loop = loop;
|
||||
});
|
||||
if (loop) {
|
||||
|
@ -399,17 +422,18 @@ export class Player extends Source<PlayerOptions> {
|
|||
const stopEvent = this._state.getNextState("stopped", now);
|
||||
if (stopEvent && stopEvent.implicitEnd) {
|
||||
this._state.cancel(stopEvent.time);
|
||||
this._activeSources.forEach(source => source.cancelStop());
|
||||
this._activeSources.forEach((source) => source.cancelStop());
|
||||
}
|
||||
|
||||
// set all the sources
|
||||
this._activeSources.forEach(source => {
|
||||
this._activeSources.forEach((source) => {
|
||||
source.playbackRate.setValueAtTime(rate, now);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* If the buffer should be reversed
|
||||
* If the buffer should be reversed. Note that this sets the underlying [[ToneAudioBuffer.reverse]], so
|
||||
* if multiple players are pointing at the same ToneAudioBuffer, they will all be reversed.
|
||||
* @example
|
||||
* const player = new Tone.Player("https://tonejs.github.io/audio/berklee/chime_1.mp3").toDestination();
|
||||
* player.autostart = true;
|
||||
|
@ -432,7 +456,7 @@ export class Player extends Source<PlayerOptions> {
|
|||
dispose(): this {
|
||||
super.dispose();
|
||||
// disconnect all of the players
|
||||
this._activeSources.forEach(source => source.dispose());
|
||||
this._activeSources.forEach((source) => source.dispose());
|
||||
this._activeSources.clear();
|
||||
this._buffer.dispose();
|
||||
return this;
|
||||
|
|
Loading…
Reference in a new issue