Updating docs on Player.reverse

fixes #986
This commit is contained in:
Yotam Mann 2021-12-20 11:59:20 -05:00
parent 444d6179c4
commit aad2d8c890

View file

@ -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, {
implicitEnd: true,
});
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,14 +268,14 @@ 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));
}
/**
* Stop and then restart the player from the beginning (or offset)
* @param time When the player should start.
* @param offset The offset from the beginning of the sample to start at.
* @param duration How long the sample should play. If no duration is given,
* @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)
*/
restart(time?: Seconds, offset?: Time, duration?: Time): this {
@ -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;