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.
An OscillatorNode
is created using the AudioContext.createOscillator()
method. It always has exactly one output and no inputs. Its basic property defaults (see AudioNode
for definitions) are:
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. If the default values are acceptable, you can simply call theAudioContext.createOscillator()
factory method.
Properties
Inherits properties from its parent, AudioScheduledSourceNode
, and adds the following properties:
OscillatorNode.frequency
- An a-rate
AudioParam
representing the frequency of oscillation in hertz (though theAudioParam
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 theAudioParam
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 aPeriodicWave
to describe a custom waveform. Different waves will produce different tones. Standard values are"sine"
,"square"
,"sawtooth"
,"triangle"
and"custom"
. The default is"sine"
.
Methods
Inherits methods from its parent, AudioScheduledSourceNode
, and adds the following:
OscillatorNode.setPeriodicWave()
- Sets a
PeriodicWave
which describes a periodic waveform to be used instead of one of the standard waveforms; calling this sets thetype
tocustom
. This replaces the now-obsoleteOscillatorNode.setWaveTable()
method.
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).
// create web audio api context var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // create Oscillator node var oscillator = audioCtx.createOscillator(); oscillator.type = 'square'; oscillator.frequency.value = 440; // value in hertz oscillator.connect(audioCtx.destination); oscillator.start();
Specifications
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'OscillatorNode' in that specification. |
Working Draft |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 14 webkit | (Yes) | 25 | No support | 15 webkit 22 (unprefixed) |
6 webkit |
constructor | 55.0 | ? | ? | ? | 42 | ? |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|---|
Basic support | No support | (Yes) | (Yes) | 25 | 1.2 | No support | No support | 6 webkit | 28 webkit |
constructor | No support | 55.0 | ? | ? | ? | ? | 42 | ? | 55.0 |