OscillatorNode:type 屬性
OscillatorNode 介面的 type 屬性指定振盪器將輸出的 波形 的形狀。有幾種常見的波形可供選擇,還可以選擇指定自定義波形形狀。波形的形狀會影響產生的音色。
值
一個指定振盪器波形形狀的字串。可用的值有:
sine-
一個 正弦波。這是預設值。
squaresawtooth-
一個 鋸齒波。
triangle-
一個 三角波。
custom-
自定義波形。你永遠不能手動將
type設定為custom;相反,請使用setPeriodicWave()方法來提供表示波形的資料。這樣做會自動將type設定為custom。
另請參閱 不同振盪器節點型別,以視覺化不同波形形狀。
異常
InvalidStateErrorDOMException-
如果指定了
custom值,則會丟擲此異常。要設定自定義波形,只需呼叫setPeriodicWave()。這樣做會自動為您設定型別。
示例
以下示例展示瞭如何使用 AudioContext 建立振盪器節點。如需實際示例,請檢視我們的 Violent Theremin 演示(檢視 app.js 獲取相關程式碼)。
js
// create web audio api context
const audioCtx = new AudioContext();
// create Oscillator node
const oscillator = audioCtx.createOscillator();
oscillator.type = "square";
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); // value in hertz
oscillator.start();
規範
| 規範 |
|---|
| Web Audio API # dom-oscillatornode-type |
瀏覽器相容性
載入中…