setPeriodicWave()
方法在
OscillatorNode
interface is used to point to a
PeriodicWave
defining a periodic waveform that can be used to shape the oscillator's output, when
type
is
custom
.
This replaces the now-obsolete
OscillatorNode.setWaveTable()
.
OscillatorNode.setPeriodicWave(wave);
wave
PeriodicWave
object representing the waveform to use as the shape of the oscillator's output.
The following example illustrates simple usage of
createPeriodicWave()
, recreating a sine wave from a periodic wave.
var real = new Float32Array(2); var imag = new Float32Array(2); var ac = new AudioContext(); var osc = ac.createOscillator(); real[0] = 0; imag[0] = 0; real[1] = 1; imag[1] = 0; var wave = ac.createPeriodicWave(real, imag); osc.setPeriodicWave(wave); osc.connect(ac.destination); osc.start(); osc.stop(2);
This works because a sound that contains only a fundamental tone is by definition a sine wave.
Here, we create a
PeriodicWave
with two values. The first value is the DC offset, which is the value at which the oscillator starts. 0 is good here, because we want to start the curve at the middle of the [-1.0; 1.0] range.
The second and subsequent values are sine and cosine components. You can think of it as the result of a Fourier transform, where you get frequency domain values from time domain value. Here, with
createPeriodicWave()
, you specify the frequencies, and the browser performs a an inverse Fourier transform to get a time domain buffer for the frequency of the oscillator. Here, we only set one component at full volume (1.0) on the fundamental tone, so we get a sine wave.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'setPeriodicWave' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
OscillatorNode
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
AudioWorkletProcessor
BaseAudioContext
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode