BaseAudioContext.createOscillator()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

io error: No such file or directory (os error 2) (/home/runner/work/yari/yari/mdn/translated-content/files/zh-cn/web/api/baseaudiocontext/index.md)

BaseAudioContext 接口的 createOscillator() 方法创建一个 OscillatorNode,它是一个表示周期性波形的源。它基本上产生一个不变的音调。

Syntax

var oscillatorNode = audioCtx.createOscillator();

Returns

Example

以下示例显示了用于创建振荡器节点的 AudioContext 的基本用法。有关应用示例/信息,请查看我们的Violent Theremin demo(有关相关代码,请参阅see app.js); 另请参阅我们的 OscillatorNode 页面以获取更多信息。

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.setValueAtTime(3000, audioCtx.currentTime); // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();

Specifications

Specification
Web Audio API
# dom-baseaudiocontext-createoscillator

Browser compatibility

BCD tables only load in the browser

See also