Skip to content
Tone.js

ToneAudioBuffer

class Core ↳ extends Tone

AudioBuffer loading and storage. ToneAudioBuffer is used internally by all classes that make requests for audio files such as Tone.Player, Tone.Sampler and Tone.Convolver.

const buffer = new Tone.ToneAudioBuffer("https://tonejs.github.io/audio/casio/A1.mp3", () => {
	console.log("loaded");
});
new ToneAudioBuffer(url?: string | ToneAudioBuffer | AudioBuffer, onload?: (buffer: ToneAudioBuffer) => void, onerror?: (error: Error) => void)

Parameters

  • url string | ToneAudioBuffer | AudioBuffer optional

    The url to load, or the audio buffer to set.

  • onload (buffer: ToneAudioBuffer) => void optional

    A callback which is invoked after the buffer is loaded. It’s recommended to use ToneAudioBuffer.on('load', callback) instead since it will give you a callback when all buffers are loaded.

  • onerror (error: Error) => void optional

    The callback to invoke if there is an error

new ToneAudioBuffer(options?: Partial<ToneAudioBufferOptions>)

Parameters

read-only

name: string

The name of the class

Default: "ToneAudioBuffer"

onload: (buffer: ToneAudioBuffer) => void

Callback when the buffer is loaded.

Default: noOp

static

baseUrl: string

A path which is prefixed before every url.

Default: ""

static

downloads: Promise<void>[]

All of the downloads

Default: []

read-only

duration: number

The duration of the buffer in seconds.

read-only

length: number

The length of the buffer in samples

read-only

loaded: boolean

If the buffer is loaded or not

read-only

numberOfChannels: number

The number of discrete audio channels. Returns 0 if no buffer is loaded.

reverse: boolean

Reverse the buffer.

read-only

sampleRate: number

The sample rate of the AudioBuffer

dispose(): this

clean up

Returns this

fromArray(array: Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]): this

Set the audio buffer from the array. To create a multichannel AudioBuffer, pass in a multidimensional array.

Parameters

  • array Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]

    The array to fill the audio buffer

Returns this

get(): AudioBuffer | undefined

The audio buffer stored in the object.

Returns AudioBuffer | undefined

getChannelData(channel: number): Float32Array

Returns the Float32Array representing the PCM audio data for the specific channel.

Parameters

  • channel number

    The channel number to return

Returns Float32Array The audio as a TypedArray

load(url: string): Promise<ToneAudioBuffer>

Makes an fetch request for the selected url then decodes the file as an audio buffer. Invokes the callback once the audio buffer loads.

Parameters

  • url string

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

Returns Promise<ToneAudioBuffer> A Promise which resolves with this ToneAudioBuffer

set(buffer: ToneAudioBuffer | AudioBuffer): this

Pass in an AudioBuffer or ToneAudioBuffer to set the value of this buffer.

Parameters

Returns this

slice(start: number, end?: number): ToneAudioBuffer

Cut a subsection of the array and return a buffer of the subsection. Does not modify the original buffer

Parameters

  • start number

    The time to start the slice

  • end number optional

    The end time to slice. If none is given will default to the end of the buffer

Returns ToneAudioBuffer

toArray(channel?: number): Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]

Get the buffer as an array. Single channel buffers will return a 1-dimensional Float32Array, and multichannel buffers will return multidimensional arrays.

Parameters

  • channel number optional

    Optionally only copy a single channel from the array.

Returns Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]

toMono(chanNum?: number): this

Sums multiple channels into 1 channel

Parameters

  • chanNum number optional

    Optionally only copy a single channel from the array.

Returns this

static

fromArray(array: Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]): ToneAudioBuffer

Create a ToneAudioBuffer from the array. To create a multichannel AudioBuffer, pass in a multidimensional array.

Parameters

  • array Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]

    The array to fill the audio buffer

Returns ToneAudioBuffer A ToneAudioBuffer created from the array

static

fromUrl(url: string): Promise<ToneAudioBuffer>

Creates a ToneAudioBuffer from a URL, returns a promise which resolves to a ToneAudioBuffer

Parameters

  • url string

    The url to load.

Returns Promise<ToneAudioBuffer> A promise which resolves to a ToneAudioBuffer

static

getDefaults(): ToneAudioBufferOptions

Returns all of the default options belonging to the class.

Returns ToneAudioBufferOptions

static

load(url: string): Promise<AudioBuffer>

Loads a url using fetch and returns the AudioBuffer.

Parameters

  • url string

Returns Promise<AudioBuffer>

static

loaded(): Promise<void>

Returns a Promise which resolves when all of the buffers have loaded

Returns Promise<void>

static

supportsType(url: string): boolean

Checks a url’s extension to see if the current browser can play that file type.

Tone.ToneAudioBuffer.supportsType("wav"); // returns true
Tone.ToneAudioBuffer.supportsType("path/to/file.wav"); // returns true

Parameters

  • url string

    The url/extension to test

Returns boolean If the file extension can be played