Configure Tone.GrainPlayer Grain Size and Overlap

This article explains how to configure the grainSize and overlap parameters of the Tone.GrainPlayer class in Tone.js to control granular synthesis. You will learn what these parameters do, how they affect your audio playback, and how to adjust them both during initialization and dynamically during runtime.


Understanding the Parameters

Tone.GrainPlayer works by splitting an audio buffer into tiny segments called “grains” and playing them back sequentially. To control the texture and behavior of this playback, you primarily manipulate two parameters:


Setting Parameters During Initialization

You can set the grainSize and overlap directly in the options object when creating a new instance of Tone.GrainPlayer.

const player = new Tone.GrainPlayer({
  url: "https://tonejs.github.io/audio/bermee/gong.mp3",
  onload: () => {
    player.start();
  },
  grainSize: 0.2,  // Each grain is 200 milliseconds long
  overlap: 0.1     // Grains overlap by 100 milliseconds
}).toDestination();

Modifying Parameters Dynamically

To change the grain behavior in real-time (for example, in response to user input or an automated modulation source), you can assign new values directly to the properties of your Tone.GrainPlayer instance.

// Change grain size to 500 milliseconds
player.grainSize = 0.5;

// Change overlap to 50 milliseconds for a more stuttered effect
player.overlap = 0.05;

Tips for Tweaking the Sound