OscillatorNode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

The OscillatorNode interface represents a periodic waveform, such as a sine wave. It is an AudioScheduledSourceNode audio-processing module that causes a specified frequency of a given wave to be created—in effect, a constant tone.

EventTarget AudioNode AudioScheduledSourceNode OscillatorNode
Number of inputs 0
Number of outputs 1
Channel count mode max
Channel count 2 (not used in the default count mode)
Channel interpretation speakers

Constructor

OscillatorNode()

Creates a new instance of an OscillatorNode object, optionally providing an object specifying default values for the node's properties. As an alternative, you can use the BaseAudioContext.createOscillator() factory method; see Creating an AudioNode.

Instance properties

Also inherits properties from its parent, AudioScheduledSourceNode.

OscillatorNode.frequency

An a-rate AudioParam representing the frequency of oscillation in hertz (though the AudioParam returned is read-only, the value it represents is not). The default value is 440 Hz (a standard middle-A note).

OscillatorNode.detune

An a-rate AudioParam representing detuning of oscillation in cents (though the AudioParam returned is read-only, the value it represents is not). The default value is 0.

OscillatorNode.type

A string which specifies the shape of waveform to play; this can be one of a number of standard values, or custom to use a PeriodicWave to describe a custom waveform. Different waves will produce different tones. Standard values are "sine", "square", "sawtooth", "triangle" and "custom". The default is "sine".

Instance methods

Also inherits methods from its parent, AudioScheduledSourceNode.

OscillatorNode.setPeriodicWave()

Sets a PeriodicWave which describes a periodic waveform to be used instead of one of the standard waveforms; calling this sets the type to custom.

AudioScheduledSourceNode.start()

Specifies the exact time to start playing the tone.

AudioScheduledSourceNode.stop()

Specifies the time to stop playing the tone.

Events

Also inherits events from its parent, AudioScheduledSourceNode.

Examples

The following example shows basic usage of an AudioContext to create an oscillator node and to start playing a tone on it. For an applied example, check out our Violent Theremin demo (see app.js for relevant code).

js
// create web audio api context
const audioCtx = new AudioContext();

// create Oscillator node
const oscillator = audioCtx.createOscillator();

oscillator.type = "square";
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();

Specifications

Specification
Web Audio API
# OscillatorNode

Browser compatibility

BCD tables only load in the browser

See also