大网数据广告活动
Web 标准与前端开发

OscillatorNode

· 52CE 我爱测

`OscillatorNode`** 接口表示一个振荡器,它产生一个周期的波形信号(如正弦波)。它是一个 AudioScheduledSourceNode 音频处理模块,这个模块会生成一个指定频率的波形信号(即一个固定的音调)

Web Audio API

OscillatorNode 接口表示一个振荡器,它产生一个周期的波形信号(如正弦波)。它是一个 AudioScheduledSourceNode 音频处理模块,这个模块会生成一个指定频率的波形信号(即一个固定的音调)

一个 OscillatorNode 对象是通过 AudioContext.createOscillator() 方法创建的。它总是有一个输出,但没有输入。它的基础属性(定义见 AudioNode )默认如下:

<table class="properties"> <tbody> <tr> <th scope="row">Number of inputs</th> <td><code>0</code></td> </tr> <tr> <th scope="row">Number of outputs</th> <td><code>1</code></td> </tr> <tr> <th scope="row">Channel count mode</th> <td><code>max</code></td> </tr> <tr> <th scope="row">Channel count</th> <td><code>2</code> (not used in the default count mode)</td> </tr> <tr> <th scope="row">Channel interpretation</th> <td><code>speakers</code></td> </tr> </tbody> </table>

构造函数

  • OscillatorNode.OscillatorNode
    • : 创建一个 OscillatorNode 对象的示例,为 node属性提供可选的一个定义默认值的对象。如果默认值可接受,你可以简单地调用AudioContext.createOscillator()工厂方法。

属性

继承自父类 AudioScheduledSourceNode,并添加下列属性:

  • OscillatorNode.frequency
    • : 一个 a-rate AudioParam 对象的属性代表了振动的频率(单位为赫兹 hertz) (虽然返回的AudioParam 是只读的,但是它所表示的值是可以修改的)。默认值是 440 Hz (基本的中 A 音高).
  • OscillatorNode.detune
    • : 一个 a-rate AudioParam 对象的属性代表振动的音高微调(单位是 cent 音分) (虽然返回的AudioParam 是只读的,但是它所表示的值是可以修改的).。默认值是 0。
  • OscillatorNode.type
    • : 一个字符串,决定 OscillatorNode 播放的声音的周期波形; 它的值可以是基础值中的一个或者用户使用 PeriodicWave。不同的波形可以产生不同的声调。基础值有 "sine"、"square"、"sawtooth"、"triangle" 和 "custom"。默认值是 "sine"。

方法

继承自父级, AudioScheduledSourceNode, 自有方法如下:

  • OscillatorNode.setPeriodicWave()
    • : 设置一个 PeriodicWave ,它描述了一个周期的波形常常替代标准波形之一; 调用这个方法来设置用户自定义的波形。它取代了已经废弃了的 OscillatorNode.setWaveTable() 方法。

示例

下面示例展示了 AudioContext 的基本使用 来创建一个 oscillator 节点 并使用它来播放音乐。这是已经在运行的例子,可以看这里 Violent Theremin demo (see app.js 是相关代码).

// 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 = 440; // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();

规范

浏览器兼容性

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。