Skip to content
Tone.js

ToneAudioBuffers

class Core ↳ extends Tone

A data structure for holding multiple buffers in a Map-like datastructure.

const pianoSamples = new Tone.ToneAudioBuffers({
	A1: "https://tonejs.github.io/audio/casio/A1.mp3",
	A2: "https://tonejs.github.io/audio/casio/A2.mp3",
}, () => {
	const player = new Tone.Player().toDestination();
	// play one of the samples when they all load
	player.buffer = pianoSamples.get("A2");
	player.start();
});
// To pass in additional parameters in the second parameter
const buffers = new Tone.ToneAudioBuffers({
	 urls: {
		 A1: "A1.mp3",
		 A2: "A2.mp3",
	 },
	 onload: () => console.log("loaded"),
	 baseUrl: "https://tonejs.github.io/audio/casio/"
});
new ToneAudioBuffers(urls?: ToneAudioBuffersUrlMap, onload?: () => void, baseUrl?: string)

Parameters

  • urls ToneAudioBuffersUrlMap optional

    An object literal or array of urls to load.

  • onload () => void optional

    The callback to invoke when the buffers are loaded.

  • baseUrl string optional

    A prefix url to add before all the urls

new ToneAudioBuffers(options?: Partial<ToneAudioBuffersOptions>)

Parameters

baseUrl: string

A path which is prefixed before every url.

read-only

name: string

The name of the class

Default: "ToneAudioBuffers"

read-only

loaded: boolean

If the buffers are loaded or not

add(name: string | number, url: string | ToneAudioBuffer | AudioBuffer, callback?: () => void, onerror?: (e: Error) => void): this

Add a buffer by name and url to the Buffers

Parameters

  • name string | number

    A unique name to give the buffer

  • url string | ToneAudioBuffer | AudioBuffer

    Either the url of the bufer, or a buffer which will be added with the given name.

  • callback () => void optional default noOp

    The callback to invoke when the url is loaded.

  • onerror (e: Error) => void optional default noOp

    Invoked if the buffer can’t be loaded

Returns this

dispose(): this

disconnect and dispose.

Returns this

get(name: string | number): ToneAudioBuffer

Get a buffer by name. If an array was loaded, then use the array index.

Parameters

  • name string | number

    The key or index of the buffer.

Returns ToneAudioBuffer

has(name: string | number): boolean

True if the buffers object has a buffer by that name.

Parameters

  • name string | number

    The key or index of the buffer.

Returns boolean

static

Returns all of the default options belonging to the class.

Returns ToneAudioBuffersOptions