Class ToneAudioBuffer

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.

Example

const buffer = new Tone.ToneAudioBuffer("https://tonejs.github.io/audio/casio/A1.mp3", () => {
console.log("loaded");
});

Hierarchy

  • Tone
    • ToneAudioBuffer

Constructors

  • Parameters

    • Optional url: string | AudioBuffer | ToneAudioBuffer

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

    • Optional onload: ((buffer) => void)

      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.

    • Optional onerror: ((error) => void)

      The callback to invoke if there is an error

        • (error): void
        • Parameters

          • error: Error

          Returns void

    Returns ToneAudioBuffer

  • Parameters

    • Optional options: Partial<ToneAudioBufferOptions>

    Returns ToneAudioBuffer

Properties

debug: boolean = false

Set this debug flag to log all events that happen in this class.

name: string = "ToneAudioBuffer"
onload: ((buffer) => void) = noOp

Callback when the buffer is loaded.

Type declaration

baseUrl: string = ""

A path which is prefixed before every url.

downloads: Promise<void>[] = []

All of the downloads

version: string = version

The version number semver

Accessors

  • get disposed(): boolean
  • Indicates if the instance was disposed. 'Disposing' an instance means that all of the Web Audio nodes that were created for the instance are disconnected and freed for garbage collection.

    Returns boolean

Methods

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

    Parameters

    • array: Float32Array | Float32Array[]

      The array to fill the audio buffer

    Returns this

  • 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

  • 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

  • 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 = ...

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

    Returns ToneAudioBuffer

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

    Parameters

    • Optional channel: number

      Optionally only copy a single channel from the array.

    Returns Float32Array | Float32Array[]

  • Sums multiple channels into 1 channel

    Parameters

    • Optional chanNum: number

      Optionally only copy a single channel from the array.

    Returns this

  • Convert the class to a string

    Returns string

    Example

    const osc = new Tone.Oscillator();
    console.log(osc.toString());
  • Checks a url's extension to see if the current browser can play that file type.

    Parameters

    • url: string

      The url/extension to test

    Returns boolean

    If the file extension can be played

    Static

    Example

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