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.
Constructor
Section titled “Constructor”new GrainPlayer(url?: string | ToneAudioBuffer | AudioBuffer, onload?: () => void)Parameters
-
url
string | ToneAudioBuffer | AudioBufferoptionalEither the AudioBuffer or the url from which to load the AudioBuffer
-
onload
() => voidoptionalThe function to invoke when the buffer is loaded.
new GrainPlayer(options?: Partial<GrainPlayerOptions>)Parameters
- options
Partial<GrainPlayerOptions>optional
Properties
Section titled “Properties”loop: booleanIf the buffer should loop back to the loopStart when completed
onstop
Section titled “onstop”↳ inherited from Source
onstop: onStopCallbackThe callback to invoke when the source is stopped.
volume
Section titled “volume”↳ inherited from Source
volume: Param<"decibels">The volume of the output in decibels.
const source = new Tone.PWMOscillator().toDestination(); source.volume.value = -6;
grainSize
Section titled “grainSize”grainSize: TimeThe size of each chunk of audio that the buffer is chopped into and played back at.
↳ inherited from Source
mute: booleanMute the output.
const osc = new Tone.Oscillator().toDestination().start(); // mute the output osc.mute = true;
overlap
Section titled “overlap”overlap: TimeThe duration of the cross-fade between successive grains.
read-only ↳ inherited from Source
state: BasicPlaybackStateReturns 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();Methods
Section titled “Methods”restart()
Section titled “restart()”Stop and then restart the player from the beginning (or offset)
Parameters
Returns this
start()
Section titled “start()”↳ inherited from Source
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 nowParameters
Returns this
stop()
Section titled “stop()”↳ inherited from Source
stop(time?: Time): thisStop 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 nowParameters
-
time
TimeoptionalWhen the source should be stopped.
Returns this
sync()
Section titled “sync()”↳ inherited from Source
sync(): thisSync 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
unsync()
Section titled “unsync()”↳ inherited from Source
unsync(): thisUnsync the source to the Transport.
See also: sync
Returns this
getDefaults()
Section titled “getDefaults()”static
getDefaults(): GrainPlayerOptionsReturns all of the default options belonging to the class.
Returns GrainPlayerOptions
Inherited members
Section titled “Inherited members”- From
ToneWithContext: context, blockTime, sampleTime, get(), immediate(), now(), set(), toFrequency(), toSeconds(), toTicks() - From
Tone: debug, version, disposed, toString() - From
ToneAudioNode: channelCount, channelCountMode, channelInterpretation, numberOfInputs, numberOfOutputs, chain(), connect(), disconnect(), fan(), toDestination(), toMaster()