OscillatorNode - Web APIs 编辑

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 inputs0
Number of outputs1
Channel count modemax
Channel count2 (not used in the default count mode)
Channel interpretationspeakers

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

SpecificationStatusComment
Web Audio API
The definition of 'OscillatorNode' in that specification.
Working Draft 

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:156 次

字数:7245

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文