Skip to content
Tone.js

GrainPlayer

class Source ↳ extends Source<GrainPlayerOptions>

GrainPlayer implements granular synthesis. Granular Synthesis enables you to adjust pitch and playback rate independently. The grainSize is the amount of time each small chunk of audio is played for and the overlap is the amount of crossfading transition time between successive grains.

new GrainPlayer(url?: string | ToneAudioBuffer | AudioBuffer, onload?: () => void)

Parameters

  • url string | ToneAudioBuffer | AudioBuffer optional

    Either the AudioBuffer or the url from which to load the AudioBuffer

  • onload () => void optional

    The function to invoke when the buffer is loaded.

new GrainPlayer(options?: Partial<GrainPlayerOptions>)

Parameters

The audio buffer belonging to the player.

detune: number

Adjust the pitch independently of the playbackRate.

↳ inherited from Source

input: undefined

Sources have no inputs

Default: undefined

loop: boolean

If the buffer should loop back to the loopStart when completed

read-only

name: string

The name of the class

Default: "GrainPlayer"

↳ inherited from Source

The callback to invoke when the source is stopped.

↳ inherited from Source

output: OutputNode

The output node

↳ inherited from Source

volume: Param<"decibels">

The volume of the output in decibels.

const source = new Tone.PWMOscillator().toDestination();
source.volume.value = -6;
grainSize: Time

The size of each chunk of audio that the buffer is chopped into and played back at.

read-only

loaded: boolean

If all the buffer is loaded

loopEnd: Time

The loop end time.

loopStart: Time

The loop start time.

↳ inherited from Source

mute: boolean

Mute the output.

const osc = new Tone.Oscillator().toDestination().start();
// mute the output
osc.mute = true;
overlap: Time

The duration of the cross-fade between successive grains.

playbackRate: number

The playback rate of the sample

reverse: boolean

The direction the buffer should play in

read-only ↳ inherited from Source

Returns the playback state of the source, either “started” or “stopped”.

const player = new Tone.Player("https://tonejs.github.io/audio/berklee/ahntone_c3.mp3", () => {
	player.start();
	console.log(player.state);
}).toDestination();
dispose(): this

Clean up.

Returns this

restart(time?: number, offset?: Time, duration?: Time): this

Stop and then restart the player from the beginning (or offset)

Parameters

  • time number optional

    When the player should start.

  • offset Time optional

    The offset from the beginning of the sample to start at.

  • duration Time optional

    How long the sample should play. If no duration is given, it will default to the full length of the sample (minus any offset)

Returns this

↳ inherited from Source

start(time?: Time, offset?: Time, duration?: Time): this

Start the source at the specified time. If no time is given, start the source now.

const source = new Tone.Oscillator().toDestination();
source.start("+0.5"); // starts the source 0.5 seconds from now

Parameters

  • time Time optional

    When the source should be started.

  • offset Time optional

  • duration Time optional

Returns this

↳ inherited from Source

stop(time?: Time): this

Stop the source at the specified time. If no time is given, stop the source now.

const source = new Tone.Oscillator().toDestination();
source.start();
source.stop("+0.5"); // stops the source 0.5 seconds from now

Parameters

  • time Time optional

    When the source should be stopped.

Returns this

↳ inherited from Source

sync(): this

Sync the source to the Transport so that all subsequent calls to start and stop are synced to the TransportTime instead of the AudioContext time.

const osc = new Tone.Oscillator().toDestination();
// sync the source so that it plays between 0 and 0.3 on the Transport's timeline
osc.sync().start(0).stop(0.3);
// start the transport.
Tone.Transport.start();
// set it to loop once a second
Tone.Transport.loop = true;
Tone.Transport.loopEnd = 1;

Returns this

↳ inherited from Source

unsync(): this

Unsync the source to the Transport.

See also: sync

Returns this

static

getDefaults(): GrainPlayerOptions

Returns all of the default options belonging to the class.

Returns GrainPlayerOptions