Skip to content
Tone.js

Player

class Source ↳ extends Source<PlayerOptions>

Player is an audio file player with start, loop, and stop functions.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gong_1.mp3").toDestination();
// play as soon as the buffer is loaded
player.autostart = true;
new Player(url?: string | ToneAudioBuffer | AudioBuffer, onload?: () => void)

Parameters

  • url string | ToneAudioBuffer | AudioBuffer optional

    Either the AudioBuffer or the url from which to load the AudioBuffer

  • onload () => void optional

    The function to invoke when the buffer is loaded.

new Player(options?: Partial<PlayerOptions>)

Parameters

autostart: boolean

If the file should play as soon as the buffer is loaded.

fadeIn: Time

The fadeIn time of the amplitude envelope.

fadeOut: Time

The fadeOut time of the amplitude envelope.

↳ inherited from Source

input: undefined

Sources have no inputs

Default: undefined

read-only

name: string

The name of the class

Default: "Player"

↳ inherited from Source

The callback to invoke when the source is stopped.

↳ inherited from Source

output: OutputNode

The output node

↳ inherited from Source

volume: Param<"decibels">

The volume of the output in decibels.

const source = new Tone.PWMOscillator().toDestination();
source.volume.value = -6;

The audio buffer belonging to the player.

read-only

loaded: boolean

If the buffer is loaded

loop: boolean

If the buffer should loop once it’s over.

const player = new Tone.Player("https://tonejs.github.io/audio/drum-samples/breakbeat.mp3").toDestination();
player.loop = true;
player.autostart = true;
loopEnd: Time

If loop is true, the loop will end at this position.

loopStart: Time

If loop is true, the loop will start at this position.

↳ inherited from Source

mute: boolean

Mute the output.

const osc = new Tone.Oscillator().toDestination().start();
// mute the output
osc.mute = true;
playbackRate: number

Normal speed is 1. The pitch will change with the playback rate.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/femalevoices_aa2_A5.mp3").toDestination();
// play at 1/4 speed
player.playbackRate = 0.25;
// play as soon as the buffer is loaded
player.autostart = true;
reverse: boolean

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.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/chime_1.mp3").toDestination();
player.autostart = true;
player.reverse = true;

read-only ↳ inherited from Source

Returns the playback state of the source, either “started” or “stopped”.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/ahntone_c3.mp3", () => {
	player.start();
	console.log(player.state);
}).toDestination();
dispose(): this

Clean up.

Returns this

load(url: string): Promise<Player>

Load the audio file as an audio buffer. Decodes the audio asynchronously and invokes the callback once the audio buffer loads. Note: this does not need to be called if a url was passed in to the constructor. Only use this if you want to manually load a new url.

Parameters

  • url string

    The url of the buffer to load. Filetype support depends on the browser.

Returns Promise<Player>

restart(time?: number, offset?: Time, duration?: Time): this

Stop and then restart the player from the beginning (or offset)

Parameters

  • time number optional

    When the player should start.

  • offset Time optional

    The offset from the beginning of the sample to start at.

  • duration Time optional

    How long the sample should play. If no duration is given, it will default to the full length of the sample (minus any offset)

Returns this

seek(offset: Time, when?: Time): this

Seek to a specific time in the player’s buffer. If the source is no longer playing at that time, it will stop.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3", () => {
	player.start();
	// seek to the offset in 1 second from now
	player.seek(0.4, "+1");
}).toDestination();

Parameters

  • offset Time

    The time to seek to.

  • when Time optional

    The time for the seek event to occur.

Returns this

setLoopPoints(loopStart: Time, loopEnd: Time): this

Set the loop start and end. Will only loop if loop is set to true.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/malevoices_aa2_F3.mp3").toDestination();
// loop between the given points
player.setLoopPoints(0.2, 0.3);
player.loop = true;
player.autostart = true;

Parameters

  • loopStart Time

    The loop start time

  • loopEnd Time

    The loop end time

Returns this

start(time?: Time, offset?: Time, duration?: Time): this

Play the buffer at the given startTime. Optionally add an offset and/or duration which will play the buffer from a position within the buffer for the given duration.

Parameters

  • time Time optional

    When the player should start.

  • offset Time optional

    The offset from the beginning of the sample to start at.

  • duration Time optional

    How long the sample should play. If no duration is given, it will default to the full length of the sample (minus any offset)

Returns this

↳ inherited from Source

stop(time?: Time): this

Stop the source at the specified time. If no time is given, stop the source now.

const source = new Tone.Oscillator().toDestination();
source.start();
source.stop("+0.5"); // stops the source 0.5 seconds from now

Parameters

  • time Time optional

    When the source should be stopped.

Returns this

↳ inherited from Source

sync(): this

Sync the source to the Transport so that all subsequent calls to start and stop are synced to the TransportTime instead of the AudioContext time.

const osc = new Tone.Oscillator().toDestination();
// sync the source so that it plays between 0 and 0.3 on the Transport's timeline
osc.sync().start(0).stop(0.3);
// start the transport.
Tone.Transport.start();
// set it to loop once a second
Tone.Transport.loop = true;
Tone.Transport.loopEnd = 1;

Returns this

↳ inherited from Source

unsync(): this

Unsync the source to the Transport.

See also: sync

Returns this

static

getDefaults(): PlayerOptions

Returns all of the default options belonging to the class.

Returns PlayerOptions