OscillatorNode
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.
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 theBaseAudioContext.createOscillator()
factory method; see Creating an AudioNode.
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"
.
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 thetype
tocustom
. 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 Violent Theremin demo (see app.js for relevant code).
// create web audio api context const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // 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
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
OscillatorNode |
20 |
12 |
25 |
No |
15 |
6 |
≤37 |
25 |
25 |
14 |
6 |
1.5 |
OscillatorNode |
55
Before Chrome 59, the default values were not supported.
|
79 |
53 |
No |
42 |
14.1 |
55
Before version 59, the default values were not supported.
|
55
Before Chrome 59, the default values were not supported.
|
53 |
42 |
14.5 |
6.0
Before Samsung Internet 7.0, the default values were not supported.
|
detune |
20 |
12 |
25 |
No |
15 |
6 |
≤37 |
25 |
25 |
14 |
6 |
1.5 |
frequency |
20 |
12 |
25 |
No |
15 |
6 |
≤37 |
25 |
25 |
14 |
6 |
1.5 |
setPeriodicWave |
30 |
12 |
25 |
No |
17 |
8 |
≤37 |
30 |
25 |
18 |
8 |
2.0 |
type |
20 |
12 |
25 |
No |
15 |
6 |
≤37 |
25 |
25 |
14 |
6 |
1.5 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/OscillatorNode