Trigger Multiple Notes with Tone.PolySynth

This article explains how to play multiple notes simultaneously to create chords in Tone.js using the Tone.PolySynth class. You will learn how to instantiate a polyphonic synthesizer and use the triggerAttackRelease method with an array of notes to trigger them at the exact same time.

In Tone.js, a standard Tone.Synth is monophonic, meaning it can only play one note at a time. To play chords or trigger multiple notes simultaneously, you must use Tone.PolySynth. Under the hood, PolySynth manages a pool of monophonic voices, automatically allocating a voice for each note you trigger.

Creating the PolySynth

First, instantiate the Tone.PolySynth and connect it to the main audio output (toDestination):

import * as Tone from 'tone';

// Create a PolySynth and connect it to the master output
const synth = new Tone.PolySynth().toDestination();

Triggering Multiple Notes

To trigger multiple notes simultaneously, pass an array of note names (strings) as the first argument to the triggerAttackRelease method.

Here is the basic syntax:

// Define a C Major chord
const chord = ["C4", "E4", "G4"];

// Play the chord for the duration of a half note ("2n")
synth.triggerAttackRelease(chord, "2n");

Complete Code Example

To hear the synthesizer, you must start the Tone.js audio context, which browsers require to be triggered by a user interaction (like a button click).

<button id="play-button">Play Chord</button>

<script type="module">
  import * as Tone from 'https://esm.sh/tone';

  const synth = new Tone.PolySynth().toDestination();

  document.getElementById("play-button").addEventListener("click", async () => {
    // Ensure the audio context is started
    await Tone.start();
    
    // Play a C Major 7th chord (C4, E4, G4, B4) for 1 second
    synth.triggerAttackRelease(["C4", "E4", "G4", "B4"], 1);
  });
</script>

Key Parameters of triggerAttackRelease

  1. Notes (Array): An array of MIDI notes or frequency values (e.g., ["C4", "E4", "G4"] or [261.63, 329.63, 392.00]).
  2. Duration (Time): How long the notes should hold before releasing. This can be in seconds (e.g., 1 or 2.5) or transport notation (e.g., "4n" for a quarter note, "8n" for an eighth note).
  3. Time (Numbers/Notation, Optional): The scheduling time at which the notes should start playing. If omitted, the notes play immediately.