Convert Figma logo to code with AI

Tonejs logoTone.js

A Web Audio framework for making interactive music in the browser.

13,373
975
13,373
108

Top Related Projects

23,743

Javascript audio library for the modern web.

4,430

A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.

Audio waveform player

1,184

Band.js - Music composer interface for the Web Audio API.

Quick Overview

Tone.js is a powerful Web Audio framework for creating interactive music in the browser. It provides a high-level API for synthesizing sounds, scheduling events, and creating audio effects, making it easier for developers to work with audio in web applications.

Pros

  • Easy to use, with a simple and intuitive API
  • Extensive documentation and examples
  • Cross-browser compatibility
  • Wide range of built-in instruments and effects

Cons

  • Performance can be an issue with complex audio processing
  • Limited support for older browsers
  • Steep learning curve for advanced audio concepts
  • Dependency on Web Audio API, which may not be available in all environments

Code Examples

Creating a simple synth and playing a note:

const synth = new Tone.Synth().toDestination();
synth.triggerAttackRelease("C4", "8n");

Setting up a loop with multiple instruments:

const kick = new Tone.MembraneSynth().toDestination();
const snare = new Tone.NoiseSynth().toDestination();

const loop = new Tone.Loop((time) => {
  kick.triggerAttackRelease("C1", "8n", time);
  snare.triggerAttackRelease("8n", time + 0.5);
}, "1n");

Tone.Transport.start();
loop.start();

Adding effects to an instrument:

const synth = new Tone.Synth().toDestination();
const reverb = new Tone.Reverb(2).toDestination();
const delay = new Tone.FeedbackDelay("8n", 0.5).toDestination();

synth.connect(reverb);
synth.connect(delay);

synth.triggerAttackRelease("C4", "8n");

Getting Started

  1. Include Tone.js in your project:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.49/Tone.js"></script>
    
  2. Create a simple synth and play a note:

    // Wait for user interaction before starting audio
    document.querySelector('button').addEventListener('click', async () => {
      await Tone.start();
      console.log('Audio is ready');
    
      const synth = new Tone.Synth().toDestination();
      synth.triggerAttackRelease("C4", "8n");
    });
    
  3. Experiment with different instruments, effects, and scheduling methods provided by Tone.js to create more complex audio applications.

Competitor Comparisons

23,743

Javascript audio library for the modern web.

Pros of Howler.js

  • Simpler API, easier to get started for basic audio playback
  • Smaller file size, lighter weight for simple audio needs
  • Better cross-browser compatibility, especially for older browsers

Cons of Howler.js

  • Limited advanced audio manipulation capabilities
  • Fewer built-in effects and audio processing features
  • Less suitable for complex, timing-sensitive audio applications

Code Comparison

Howler.js:

var sound = new Howl({
  src: ['sound.mp3'],
  autoplay: true,
  loop: true,
  volume: 0.5,
});

Tone.js:

const player = new Tone.Player("sound.mp3").toDestination();
player.loop = true;
player.volume.value = -6;
Tone.loaded().then(() => {
  player.start();
});

Howler.js provides a more straightforward approach for basic audio playback, while Tone.js offers more granular control and integration with other audio components. Tone.js is better suited for complex audio applications, music production, and precise timing control, whereas Howler.js excels in simplicity and ease of use for general web audio needs.

4,430

A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.

Pros of SoundJS

  • Part of the larger CreateJS suite, offering integration with other libraries for a comprehensive creative toolkit
  • Simpler API for basic audio playback and management
  • Better support for older browsers and legacy systems

Cons of SoundJS

  • Less focused on advanced audio synthesis and manipulation
  • Limited features for music creation and composition
  • Less active development and community compared to Tone.js

Code Comparison

SoundJS:

createjs.Sound.registerSound("path/to/sound.mp3", "soundID");
createjs.Sound.play("soundID");

Tone.js:

const player = new Tone.Player("path/to/sound.mp3").toDestination();
player.start();

Summary

SoundJS is better suited for simpler audio tasks and projects requiring broad browser compatibility, while Tone.js excels in advanced audio manipulation and music creation. SoundJS offers easier integration with other CreateJS libraries, but Tone.js provides more powerful features for audio synthesis and processing. The choice between them depends on the specific requirements of your project and the level of audio complexity needed.

Audio waveform player

Pros of wavesurfer.js

  • Specialized in audio waveform visualization
  • Lightweight and focused on a single purpose
  • Easier to integrate for projects primarily needing waveform display

Cons of wavesurfer.js

  • Limited audio manipulation capabilities
  • Less suitable for complex audio synthesis or processing
  • Narrower scope of features compared to Tone.js

Code Comparison

wavesurfer.js:

var wavesurfer = WaveSurfer.create({
    container: '#waveform',
    waveColor: 'violet',
    progressColor: 'purple'
});
wavesurfer.load('audio.mp3');

Tone.js:

const player = new Tone.Player("audio.mp3").toDestination();
Tone.loaded().then(() => {
    player.start();
});

Summary

wavesurfer.js excels in audio visualization, offering a lightweight solution for projects focused on waveform display. However, it lacks the extensive audio manipulation capabilities of Tone.js. Tone.js provides a more comprehensive audio framework, suitable for complex audio synthesis and processing tasks. The code comparison illustrates the different focus of each library: wavesurfer.js emphasizes waveform creation, while Tone.js demonstrates audio playback and manipulation. Choose wavesurfer.js for simple waveform visualization needs, and Tone.js for more advanced audio programming requirements.

1,184

Band.js - Music composer interface for the Web Audio API.

Pros of band.js

  • Simpler API, potentially easier for beginners to get started
  • Focuses on creating music using traditional notation and instruments
  • Includes built-in support for MIDI file parsing

Cons of band.js

  • Less actively maintained (last update was in 2017)
  • More limited in terms of advanced audio manipulation and effects
  • Smaller community and ecosystem compared to Tone.js

Code Comparison

band.js example:

var conductor = new BandJS();
conductor.setTimeSignature(4, 4);
conductor.setTempo(120);

var piano = conductor.createInstrument();
piano.note('quarter', 'C4');
piano.note('quarter', 'E4');
piano.note('half', 'G4');

Tone.js example:

const synth = new Tone.Synth().toDestination();
const now = Tone.now();

synth.triggerAttackRelease("C4", "4n", now);
synth.triggerAttackRelease("E4", "4n", now + 0.5);
synth.triggerAttackRelease("G4", "2n", now + 1);

Both libraries allow for creating simple melodies, but Tone.js offers more precise timing control and a wider range of synthesis options. band.js uses a more traditional music notation approach, while Tone.js provides a more programmatic interface for audio manipulation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Tone.js

codecov

Tone.js is a Web Audio framework for creating interactive music in the browser. The architecture of Tone.js aims to be familiar to both musicians and audio programmers creating web-based audio applications. On the high-level, Tone offers common DAW (digital audio workstation) features like a global transport for synchronizing and scheduling events as well as prebuilt synths and effects. Additionally, Tone provides high-performance building blocks to create your own synthesizers, effects, and complex control signals.

Installation

There are two ways to incorporate Tone.js into a project. First, it can be installed locally into a project using npm:

npm install tone      // Install the latest stable version
npm install tone@next // Or, alternatively, use the 'next' version

Add Tone.js to a project using the JavaScript import syntax:

import * as Tone from "tone";

Tone.js is also hosted at unpkg.com. It can be added directly within an HTML document, as long as it precedes any project scripts. See the example here for more details.

<script src="http://unpkg.com/tone"></script>

Hello Tone

//create a synth and connect it to the main output (your speakers)
const synth = new Tone.Synth().toDestination();

//play a middle 'C' for the duration of an 8th note
synth.triggerAttackRelease("C4", "8n");

Tone.Synth

Tone.Synth is a basic synthesizer with a single oscillator and an ADSR envelope.

triggerAttack / triggerRelease

triggerAttack starts the note (the amplitude is rising), and triggerRelease is when the amplitude is going back to 0 (i.e. note off).

const synth = new Tone.Synth().toDestination();
const now = Tone.now();
// trigger the attack immediately
synth.triggerAttack("C4", now);
// wait one second before triggering the release
synth.triggerRelease(now + 1);

triggerAttackRelease

triggerAttackRelease is a combination of triggerAttack and triggerRelease

The first argument to the note which can either be a frequency in hertz (like 440) or as "pitch-octave" notation (like "D#2").

The second argument is the duration that the note is held. This value can either be in seconds, or as a tempo-relative value.

The third (optional) argument of triggerAttackRelease is when along the AudioContext time the note should play. It can be used to schedule events in the future.

const synth = new Tone.Synth().toDestination();
const now = Tone.now();
synth.triggerAttackRelease("C4", "8n", now);
synth.triggerAttackRelease("E4", "8n", now + 0.5);
synth.triggerAttackRelease("G4", "8n", now + 1);

Time

Web Audio has advanced, sample accurate scheduling capabilities. The AudioContext time is what the Web Audio API uses to schedule events, starts at 0 when the page loads and counts up in seconds.

Tone.now() gets the current time of the AudioContext.

setInterval(() => console.log(Tone.now()), 100);

Tone.js abstracts away the AudioContext time. Instead of defining all values in seconds, any method which takes time as an argument can accept a number or a string. For example "4n" is a quarter-note, "8t" is an eighth-note triplet, and "1m" is one measure.

Read about Time encodings.

Starting Audio

IMPORTANT: Browsers will not play any audio until a user clicks something (like a play button). Run your Tone.js code only after calling Tone.start() from a event listener which is triggered by a user action such as "click" or "keydown".

Tone.start() returns a promise, the audio will be ready only after that promise is resolved. Scheduling or playing audio before the AudioContext is running will result in silence or incorrect scheduling.

//attach a click listener to a play button
document.querySelector("button")?.addEventListener("click", async () => {
	await Tone.start();
	console.log("audio is ready");
});

Scheduling

Transport

Tone.getTransport() returns the main timekeeper. Unlike the AudioContext clock, it can be started, stopped, looped and adjusted on the fly. You can think of it like the arrangement view in a Digital Audio Workstation.

Multiple events and parts can be arranged and synchronized along the Transport. Tone.Loop is a simple way to create a looped callback that can be scheduled to start and stop.

// create two monophonic synths
const synthA = new Tone.FMSynth().toDestination();
const synthB = new Tone.AMSynth().toDestination();
//play a note every quarter-note
const loopA = new Tone.Loop((time) => {
	synthA.triggerAttackRelease("C2", "8n", time);
}, "4n").start(0);
//play another note every off quarter-note, by starting it "8n"
const loopB = new Tone.Loop((time) => {
	synthB.triggerAttackRelease("C4", "8n", time);
}, "4n").start("8n");
// all loops start when the Transport is started
Tone.getTransport().start();
// ramp up to 800 bpm over 10 seconds
Tone.getTransport().bpm.rampTo(800, 10);

Since Javascript callbacks are not precisely timed, the sample-accurate time of the event is passed into the callback function. Use this time value to schedule the events.

Instruments

There are numerous synths to choose from including Tone.FMSynth, Tone.AMSynth and Tone.NoiseSynth.

All of these instruments are monophonic (single voice) which means that they can only play one note at a time.

To create a polyphonic synthesizer, use Tone.PolySynth, which accepts a monophonic synth as its first parameter and automatically handles the note allocation so you can pass in multiple notes. The API is similar to the monophonic synths, except triggerRelease must be given a note or array of notes.

const synth = new Tone.PolySynth(Tone.Synth).toDestination();
const now = Tone.now();
synth.triggerAttack("D4", now);
synth.triggerAttack("F4", now + 0.5);
synth.triggerAttack("A4", now + 1);
synth.triggerAttack("C5", now + 1.5);
synth.triggerAttack("E5", now + 2);
synth.triggerRelease(["D4", "F4", "A4", "C5", "E5"], now + 4);

Samples

Sound generation is not limited to synthesized sounds. You can also load a sample and play that back in a number of ways. Tone.Player is one way to load and play back an audio file.

const player = new Tone.Player(
	"https://tonejs.github.io/audio/berklee/gong_1.mp3"
).toDestination();
Tone.loaded().then(() => {
	player.start();
});

Tone.loaded() returns a promise which resolves when all audio files are loaded. It's a helpful shorthand instead of waiting on each individual audio buffer's onload event to resolve.

Tone.Sampler

Multiple samples can also be combined into an instrument. If you have audio files organized by note, Tone.Sampler will pitch shift the samples to fill in gaps between notes. So for example, if you only have every 3rd note on a piano sampled, you could turn that into a full piano sample.

Unlike the other synths, Tone.Sampler is polyphonic so doesn't need to be passed into Tone.PolySynth

const sampler = new Tone.Sampler({
	urls: {
		C4: "C4.mp3",
		"D#4": "Ds4.mp3",
		"F#4": "Fs4.mp3",
		A4: "A4.mp3",
	},
	release: 1,
	baseUrl: "https://tonejs.github.io/audio/salamander/",
}).toDestination();

Tone.loaded().then(() => {
	sampler.triggerAttackRelease(["Eb4", "G4", "Bb4"], 4);
});

Effects

In the above examples, the sources were always connected directly to the Destination, but the output of the synth could also be routed through one (or more) effects before going to the speakers.

const player = new Tone.Player({
	url: "https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3",
	loop: true,
	autostart: true,
});
//create a distortion effect
const distortion = new Tone.Distortion(0.4).toDestination();
//connect a player to the distortion
player.connect(distortion);

The connection routing is flexible, connections can run serially or in parallel.

const player = new Tone.Player({
	url: "https://tonejs.github.io/audio/drum-samples/loops/ominous.mp3",
	autostart: true,
});
const filter = new Tone.Filter(400, "lowpass").toDestination();
const feedbackDelay = new Tone.FeedbackDelay(0.125, 0.5).toDestination();

// connect the player to the feedback delay and filter in parallel
player.connect(filter);
player.connect(feedbackDelay);

Multiple nodes can be connected to the same input enabling sources to share effects. Tone.Gain is useful utility node for creating complex routing.

Signals

Like the underlying Web Audio API, Tone.js is built with audio-rate signal control over nearly everything. This is a powerful feature which allows for sample-accurate synchronization and scheduling of parameters.

Signal properties have a few built in methods for creating automation curves.

For example, the frequency parameter on Oscillator is a Signal so you can create a smooth ramp from one frequency to another.

const osc = new Tone.Oscillator().toDestination();
// start at "C4"
osc.frequency.value = "C4";
// ramp to "C2" over 2 seconds
osc.frequency.rampTo("C2", 2);
// start the oscillator for 2 seconds
osc.start().stop("+3");

AudioContext

Tone.js creates an AudioContext when it loads and shims it for maximum browser compatibility using standardized-audio-context. The AudioContext can be accessed at Tone.getContext. Or set your own AudioContext using Tone.setContext(audioContext).

MIDI

To use MIDI files, you'll first need to convert them into a JSON format which Tone.js can understand using Midi.

Performance

Tone.js makes extensive use of the native Web Audio Nodes such as the GainNode and WaveShaperNode for all signal processing, which enables Tone.js to work well on both desktop and mobile browsers.

This wiki article has some suggestions related to performance for best practices.

Testing

Tone.js runs an extensive test suite using mocha and chai with nearly 100% coverage. Passing builds on the 'dev' branch are published on npm as tone@next.

Contributing

There are many ways to contribute to Tone.js. Check out this wiki if you're interested.

References and Inspiration

NPM DownloadsLast 30 Days