Web/API/OscillatorNode

From Get docs

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 BaseAudioContext.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 the BaseAudioContext.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 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".

Event handlers

OscillatorNode.onended
Sets the event handler for the ended event, which fires when the tone has stopped playing.

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 the type to custom. This replaces the now-obsolete OscillatorNode.setWaveTable() method.
OscillatorNode.start()
Specifies the exact time to start playing the tone.
OscillatorNode.stop()
Specifies the time to stop playing the tone.

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 [[../../../../../../mdn.github.io/violent-theremin/index|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.setValueAtTime(440, audioCtx.currentTime); // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();

Specifications

Specification Status Comment
Web Audio APIThe definition of 'OscillatorNode' in that specification. Working Draft  

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
OscillatorNode Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

OscillatorNode() constructor

Chrome Full support 55

Notes'

Full support 55

Notes'

Notes' Before Chrome 59, the default values were not supported.

Edge

Full support 79

Firefox

Full support 53

IE

No support No

Opera

Full support 42

Safari

No support No

WebView Android Full support 55

Notes'

Full support 55

Notes'

Notes' Before version 59, the default values were not supported.

Chrome Android Full support 55

Notes'

Full support 55

Notes'

Notes' Before Chrome 59, the default values were not supported.

Firefox Android

Full support 53

Opera Android

Full support 42

Safari iOS

No support No

Samsung Internet Android Full support 6.0

Notes'

Full support 6.0

Notes'

Notes' Before Samsung Internet 7.0, the default values were not supported.

detune Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

frequency Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

onended Chrome

Full support 30

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 17

Safari

Full support 6.1

WebView Android

Full support 4.4

Chrome Android

Full support 30

Firefox Android

Full support 26

Opera Android

Full support 18

Safari iOS

Full support 7

Samsung Internet Android

Full support 2.0

setPeriodicWave Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

start Chrome

Full support 14

Edge

Full support 12

Firefox Full support 25

Notes'

Full support 25

Notes'

Notes' Before Firefox 30, the when parameter was not optional.

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android Full support 26

Notes'

Full support 26

Notes'

Notes' Before Firefox 30, the when parameter was not optional.

Opera Android

Full support 14

Safari iOS

?

Samsung Internet Android

Full support 1.0

stop Chrome

Full support 14

Edge

Full support 12

Firefox Full support 25

Notes'

Full support 25

Notes'

Notes' Before Firefox 30, the when parameter was not optional.

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android Full support 26

Notes'

Full support 26

Notes'

Notes' Before Firefox 30, the when parameter was not optional.

Opera Android

Full support 14

Safari iOS

?

Samsung Internet Android

Full support 1.0

type Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support Yes

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.'
See implementation notes.


See also