OscillatorNode

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()
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.

特性

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

事件处理程序

OscillatorNode.onended
Sets the event handler for the ended 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() 方法。
OscillatorNode.start()
Specifies the exact time to start playing the tone.
OscillatorNode.stop()
Specifies the time to stop playing the tone.

范例

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();
					

规范

规范 状态 Comment
Web 音频 API
The definition of 'OscillatorNode' in that specification.
工作草案

浏览器兼容性

更新 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 14 Edge ≤18 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
OscillatorNode() 构造函数 Chrome 55
注意事项
55
注意事项
Before Chrome 59, the default values were not supported.
Edge ≤79 Firefox 53 IE 不支持 No Opera 42 Safari ? WebView Android 55
注意事项
55
注意事项
Before version 59, the default values were not supported.
Chrome Android 55
注意事项
55
注意事项
Before Chrome 59, the default values were not supported.
Firefox Android 53 Opera Android 42 Safari iOS ? Samsung Internet Android 6.0
注意事项
6.0
注意事项
Before Samsung Internet 7.0, the default values were not supported.
detune Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
frequency Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
onended Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS ? Samsung Internet Android 1.0
setPeriodicWave Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
start Chrome 14 Edge 12 Firefox 25
注意事项
25
注意事项
Before Firefox 30, the when parameter was not optional.
IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26
注意事项
26
注意事项
Before Firefox 30, the when parameter was not optional.
Opera Android 14 Safari iOS ? Samsung Internet Android 1.0
stop Chrome 14 Edge 12 Firefox 25
注意事项
25
注意事项
Before Firefox 30, the when parameter was not optional.
IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26
注意事项
26
注意事项
Before Firefox 30, the when parameter was not optional.
Opera Android 14 Safari iOS ? Samsung Internet Android 1.0
type Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
见实现注意事项。

另请参阅