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 |
OscillatorNode()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.Inherits properties from its parent, AudioScheduledSourceNode, and adds the following properties:
OscillatorNode.frequencyAudioParam 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.detuneAudioParam 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.typecustom 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".OscillatorNode.onendedended event, which fires when the tone has stopped playing.Inherits methods from its parent, AudioScheduledSourceNode, and adds the following:
OscillatorNode.setPeriodicWave()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()OscillatorNode.stop()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.setValueAtTime(440, audioCtx.currentTime); // value in hertz oscillator.connect(audioCtx.destination); oscillator.start();
| Specification | Status | Comment |
|---|---|---|
| Web Audio API The definition of 'OscillatorNode' in that specification. | Working Draft |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 14 | Yes | 25 | No | 15 | 6 |
OscillatorNode() constructor |
55
|
? | 53 | No | 42 | ? |
detune |
14 | 12 | 25 | No | 15 | 6 |
frequency |
14 | 12 | 25 | No | 15 | 6 |
onended |
14 | 12 | 25 | No | 15 | 6 |
type |
14 | 12 | 25 | No | 15 | 6 |
setPeriodicWave |
14 | 12 | 25 | No | 15 | 6 |
start |
14 | 12 | 25
|
No | 15 | 6 |
stop |
14 | 12 | 25
|
No | 15 | 6 |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | 18 | Yes | 26 | 15 | ? | Yes |
OscillatorNode() constructor |
55
|
55
|
? | 53 | 42 | ? | 6.0 |
detune |
Yes | 18 | Yes | 26 | 15 | ? | Yes |
frequency |
Yes | 18 | Yes | 26 | 15 | ? | Yes |
onended |
Yes | 18 | Yes | 26 | 15 | ? | Yes |
type |
Yes | 18 | Yes | 26 | 15 | ? | Yes |
setPeriodicWave |
Yes | 18 | Yes | 26 | 15 | ? | Yes |
start |
Yes | 18 | Yes | 26
|
15 | ? | Yes |
stop |
Yes | 18 | Yes | 26
|
15 | ? | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/OscillatorNode