Skip to content
Tone.js

Source

internal class ↳ extends ToneAudioNode<Options>

Base class for sources. start/stop of this.context.transport.

// Multiple state change events can be chained together,
// but must be set in the correct order and with ascending times
// OK
state.start().stop("+0.2");
// OK
state.start().stop("+0.2").start("+0.4").stop("+0.7")
// BAD
state.stop("+0.2").start();
// BAD
state.start("+0.3").stop("+0.2");
new Source(options: SourceOptions)

Parameters

input: undefined

Sources have no inputs

Default: undefined

The callback to invoke when the source is stopped.

output: OutputNode

The output node

volume: Param<"decibels">

The volume of the output in decibels.

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

Mute the output.

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

read-only

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?: Time, offset?: Time, duration?: Time): this

Restart the source.

Parameters

  • time Time optional
  • offset Time optional
  • duration Time optional

Returns this

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

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

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

unsync(): this

Unsync the source to the Transport.

See also: sync

Returns this

static

getDefaults(): SourceOptions

Returns all of the default options belonging to the class.

Returns SourceOptions