{{APIRef()}}
The OscillatorNode
interface represents a periodic waveform, like a sine wave. It is an {{domxref("AudioNode")}} audio-processing module that causes a given frequency of sine wave to be created — in effect, a constant tone.
An OscillatorNode
is created using the {{domxref("AudioContext.createOscillator")}} method. It always has exactly one output and no inputs, both with the same amount of channels. Its basic property defaults (see {{domxref("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
Properties
Inherits properties from its parent, {{domxref("AudioNode")}}.
- {{domxref("OscillatorNode.frequency")}}
-
An {{a-rate}} {{domxref("AudioParam")}} representing the frequency of oscillation in hertz (though the
AudioParam
returned is read-only, the value it represents is not.)
- {{domxref("OscillatorNode.detune")}}
-
An {{a-rate}} {{domxref("AudioParam")}} representing detuning of oscillation in cents (though the
AudioParam
returned is read-only, the value it represents is not.)
- {{domxref("OscillatorNode.type")}}
- Represents the shape of the oscillator wave generated. Different waves will produce different tones.
Methods
Inherits methods from its parent, {{domxref("AudioNode")}}.
- {{domxref("OscillatorNode.start")}}
- As defined in {{domxref("AudioBufferSourceNode")}}, this method specifies the exact time to start playing the tone.
- {{domxref("OscillatorNode.stop")}}
- As defined in {{domxref("AudioBufferSourceNode")}}, this method specifies the exact time to stop playing the tone.
- {{domxref("OscillatorNode.setPeriodicWave")}}
-
Used to point to a {{domxref("PeriodicWave")}} defining a periodic waveform that can be used to shape the oscillator's output, when
type = "custom"
is used. This replaces the now-obsolete {{domxref("OscillatorNode.setWaveTable")}}.
Event handlers
- {{domxref("OscillatorNode.onended")}}
-
Used to set the event handler for the
ended
event, which fires when the tone has stopped playing.
Examples
The following example shows basic usage of an {{ domxref("AudioContext") }} to create an oscillator node. 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 = 3000; // value in hertz oscillator.start();
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('Web Audio API', '#the-oscillatornode-interface', 'OscillatorNode')}} | {{Spec2('Web Audio API')}} |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 14 {{property_prefix("webkit")}} | 23 | {{CompatNo}} | 15 {{property_prefix("webkit")}} 22 (unprefixed) |
6 {{property_prefix("webkit")}} |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | 28 {{property_prefix("webkit")}} | 25 | 1.2 | {{CompatNo}} | {{CompatNo}} | 6 {{property_prefix("webkit")}} |
See also