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");
});Constructor
Section titled “Constructor”new ToneAudioBuffer(url?: string | ToneAudioBuffer | AudioBuffer, onload?: (buffer: ToneAudioBuffer) => void, onerror?: (error: Error) => void)Parameters
-
url
string | ToneAudioBuffer | AudioBufferoptionalThe url to load, or the audio buffer to set.
-
onload
(buffer: ToneAudioBuffer) => voidoptionalA 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) => voidoptionalThe callback to invoke if there is an error
new ToneAudioBuffer(options?: Partial<ToneAudioBufferOptions>)Parameters
- options
Partial<ToneAudioBufferOptions>optional
Properties
Section titled “Properties”onload
Section titled “onload”onload: (buffer: ToneAudioBuffer) => voidCallback when the buffer is loaded.
Default: noOp
baseUrl
Section titled “baseUrl”static
baseUrl: stringA path which is prefixed before every url.
Default: ""
downloads
Section titled “downloads”static
downloads: Promise<void>[]All of the downloads
Default: []
numberOfChannels
Section titled “numberOfChannels”read-only
numberOfChannels: numberThe number of discrete audio channels. Returns 0 if no buffer is loaded.
sampleRate
Section titled “sampleRate”read-only
sampleRate: numberThe sample rate of the AudioBuffer
Methods
Section titled “Methods”fromArray()
Section titled “fromArray()”fromArray(array: Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]): thisSet 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 | undefinedThe audio buffer stored in the object.
Returns AudioBuffer | undefined
getChannelData()
Section titled “getChannelData()”getChannelData(channel: number): Float32ArrayReturns the Float32Array representing the PCM audio data for the specific channel.
Parameters
-
channel
numberThe channel number to return
Returns Float32Array The audio as a TypedArray
load()
Section titled “load()”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
stringThe 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): thisPass in an AudioBuffer or ToneAudioBuffer to set the value of this buffer.
Parameters
- buffer
ToneAudioBuffer | AudioBuffer
Returns this
slice()
Section titled “slice()”slice(start: number, end?: number): ToneAudioBufferCut a subsection of the array and return a buffer of the subsection. Does not modify the original buffer
Parameters
-
start
numberThe time to start the slice
-
end
numberoptionalThe end time to slice. If none is given will default to the end of the buffer
Returns ToneAudioBuffer
toArray()
Section titled “toArray()”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
numberoptionalOptionally only copy a single channel from the array.
Returns Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]
toMono()
Section titled “toMono()”toMono(chanNum?: number): thisSums multiple channels into 1 channel
Parameters
-
chanNum
numberoptionalOptionally only copy a single channel from the array.
Returns this
fromArray()
Section titled “fromArray()”static
fromArray(array: Float32Array<ArrayBuffer> | Float32Array<ArrayBuffer>[]): ToneAudioBufferCreate 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
fromUrl()
Section titled “fromUrl()”static
fromUrl(url: string): Promise<ToneAudioBuffer>Creates a ToneAudioBuffer from a URL, returns a promise which resolves to a ToneAudioBuffer
Parameters
-
url
stringThe url to load.
Returns Promise<ToneAudioBuffer> A promise which resolves to a ToneAudioBuffer
getDefaults()
Section titled “getDefaults()”static
getDefaults(): ToneAudioBufferOptionsReturns all of the default options belonging to the class.
Returns ToneAudioBufferOptions
load()
Section titled “load()”static
load(url: string): Promise<AudioBuffer>Loads a url using fetch and returns the AudioBuffer.
Parameters
- url
string
Returns Promise<AudioBuffer>
loaded()
Section titled “loaded()”static
loaded(): Promise<void>Returns a Promise which resolves when all of the buffers have loaded
Returns Promise<void>
supportsType()
Section titled “supportsType()”static
supportsType(url: string): booleanChecks 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 trueParameters
-
url
stringThe url/extension to test
Returns boolean If the file extension can be played
Inherited members
Section titled “Inherited members”- From
Tone: debug, version, disposed, toString()