Reverse Audio in Tone.Player

Reversing audio playback in Tone.js is a straightforward process that allows developers to create reverse echo effects, build-ups, and unique soundscapes. This article provides a direct guide on how to reverse the playback of an audio buffer using the Tone.Player node, detailing the properties you need to modify and providing a practical code example.

Using the reverse Property

The simplest way to play an audio file backward in Tone.js is by using the reverse property of the Tone.Player instance. This property is a boolean value. When set to true, Tone.js automatically reverses the audio buffer during playback.

Here is a complete code example demonstrating how to load an audio file, reverse it, and play it back:

// Create a new player and load the audio file
const player = new Tone.Player({
  url: "https://tonejs.github.io/audio/berlin/feudal.mp3",
  onload: () => {
    // Enable reverse playback once the buffer is loaded
    player.reverse = true;
    
    // Start playback
    player.start();
  }
}).toDestination();

Key Considerations

  1. Loading State: You must ensure the audio buffer is fully loaded before attempting to play it. Wrapping your playback logic inside the onload callback ensures the buffer is ready.
  2. Dynamic Toggling: You can toggle the reverse property dynamically. If you change player.reverse = false to player.reverse = true while the player is stopped, it will play in reverse the next time player.start() is called.
  3. Reversing the Buffer Directly: Alternatively, you can reverse the underlying buffer directly using player.buffer.reverse = true. However, using player.reverse = true is the recommended approach as it is cleaner and specifically designed for the Tone.Player class.