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
- Loading State: You must ensure the audio buffer is
fully loaded before attempting to play it. Wrapping your playback logic
inside the
onloadcallback ensures the buffer is ready. - Dynamic Toggling: You can toggle the
reverseproperty dynamically. If you changeplayer.reverse = falsetoplayer.reverse = truewhile the player is stopped, it will play in reverse the next timeplayer.start()is called. - Reversing the Buffer Directly: Alternatively, you
can reverse the underlying buffer directly using
player.buffer.reverse = true. However, usingplayer.reverse = trueis the recommended approach as it is cleaner and specifically designed for theTone.Playerclass.