Oscillator
class Source ↳ extends Source<ToneOscillatorOptions>
Oscillator supports a number of features including phase rotation, multiple oscillator types (see Oscillator.type), and Transport syncing (see Oscillator.syncFrequency).
// make and start a 440hz sine tone const osc = new Tone.Oscillator(440, "sine").toDestination().start();
Constructor
Section titled “Constructor”new Oscillator(frequency?: Frequency, type?: ToneOscillatorType)Parameters
-
frequency
FrequencyoptionalStarting frequency
-
type
ToneOscillatorTypeoptionalThe oscillator type. Read more about type below.
new Oscillator(options?: Partial<ToneOscillatorConstructorOptions>)Parameters
- options
Partial<ToneOscillatorConstructorOptions>optional
Properties
Section titled “Properties”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;
baseType
Section titled “baseType”baseType: OscillatorTypeThe oscillator type without the partialsCount appended to the end
const osc = new Tone.Oscillator(); osc.type = "sine2"; console.log(osc.baseType); // "sine"
↳ inherited from Source
mute: booleanMute the output.
const osc = new Tone.Oscillator().toDestination().start(); // mute the output osc.mute = true;
partialCount
Section titled “partialCount”partialCount: number‘partialCount’ offers an alternative way to set the number of used partials. When partialCount is 0, the maximum number of partials are used when representing the waveform using the periodicWave. When ‘partials’ is set, this value is not settable, but equals the length of the partials array. A square wave wave is composed of only odd harmonics up through the harmonic series. Partial count can limit the number of harmonics which are used to generate the waveform.
const osc = new Tone.Oscillator("C3", "square").toDestination().start();
osc.partialCount = 1;
setInterval(() => {
osc.partialCount++;
console.log(osc.partialCount);
}, 500);partials
Section titled “partials”partials: number[]The partials describes the relative amplitude of each of the harmonics of the oscillator. The first value in the array is the first harmonic (i.e. the fundamental frequency), the second harmonic is an octave up, the third harmonic is an octave and a fifth, etc. The resulting oscillator output is composed of a sine tone at the relative amplitude at each of the harmonic intervals.
Setting this value will automatically set the type to “custom”. The value is an empty array when the type is not “custom”.
const osc = new Tone.Oscillator("F3").toDestination().start();
setInterval(() => {
// generate 8 random partials
osc.partials = new Array(8).fill(0).map(() => Math.random());
}, 1000);phase: numberThe phase is the starting position within the oscillator’s cycle. For example a phase of 180 would start halfway through the oscillator’s cycle.
return Tone.Offline(() => {
const osc = new Tone.Oscillator({
frequency: 20,
phase: 90
}).toDestination().start();
}, 0.1, 1);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();type: ToneOscillatorTypeThe oscillator’s type. Also capable of setting the first x number of partials of the oscillator. For example: “sine4” would set be the first 4 partials of the sine wave and “triangle8” would set the first 8 partials of the triangle wave.
return Tone.Offline(() => {
const osc = new Tone.Oscillator().toDestination().start();
osc.type = "sine2";
}, 0.1, 1);Methods
Section titled “Methods”asArray()
Section titled “asArray()”asArray(length?: number): Promise<Float32Array<ArrayBuffer>>Returns an array of values which represents the waveform.
Parameters
-
length
numberoptional default1024The length of the waveform to return
Returns Promise<Float32Array<ArrayBuffer>>
getInitialValue()
Section titled “getInitialValue()”getInitialValue(): numberReturns the initial value of the oscillator when stopped. E.g. a “sine” oscillator with phase = 90 would return an initial value of -1.
Returns number
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
syncFrequency()
Section titled “syncFrequency()”syncFrequency(): thisSync the signal to the Transport’s bpm. Any changes to the transports bpm, will also affect the oscillators frequency.
const osc = new Tone.Oscillator().toDestination().start(); osc.frequency.value = 440; // the ratio between the bpm and the frequency will be maintained osc.syncFrequency(); // double the tempo Tone.Transport.bpm.value *= 2; // the frequency of the oscillator is doubled to 880
Returns this
unsync()
Section titled “unsync()”↳ inherited from Source
unsync(): thisUnsync the source to the Transport.
See also: sync
Returns this
unsyncFrequency()
Section titled “unsyncFrequency()”unsyncFrequency(): thisUnsync the oscillator’s frequency from the Transport.
See also: syncFrequency
Returns this
getDefaults()
Section titled “getDefaults()”static
getDefaults(): ToneOscillatorOptionsReturns all of the default options belonging to the class.
Returns ToneOscillatorOptions
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()