createPeriodicWave()
方法在
BaseAudioContext
Interface is used to create a
PeriodicWave
, which is used to define a periodic waveform that can be used to shape the output of an
OscillatorNode
.
var wave = AudioContext.createPeriodicWave(real, imag[, constraints]);
A
PeriodicWave
.
real
An array of cosine terms (traditionally the A terms).
imag
An array of sine terms (traditionally the B terms).
real
and
imag
arrays have to have the same length, otherwise an error is thrown.
constraints
可选
disableNormalization
: If set to
true
, normalization is disabled for the periodic wave. The default is
false
.
If normalized, the resulting wave will have a maximum absolute peak value of 1.
The following example illustrates simple usage of
createPeriodicWave()
,要创建
PeriodicWave
object containing a simple sine 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, {disableNormalization: true});
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 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.
The coefficients of the Fourier transform should be given in ascending order (i.e. etc.) and can be positive or negative. A simple way of manually obtaining such coefficients (though not the best) is to use a graphing calculator.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'createPeriodicWave' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createPeriodicWave
|
Chrome
59
注意事项
|
Edge ≤18 |
Firefox
53
注意事项
|
IE 不支持 No |
Opera
22
|
Safari
6
Prefixed
|
WebView Android
59
注意事项
|
Chrome Android
59
注意事项
|
Firefox Android
53
注意事项
|
Opera Android
22
|
Safari iOS
6
Prefixed
|
Samsung Internet Android
7.0
注意事项
|
| Possible to disable normalisation | Chrome Yes | Edge ≤18 | Firefox 不支持 No | IE 不支持 No | Opera ? | Safari 不支持 No | WebView Android Yes | Chrome Android Yes | Firefox Android 不支持 No | Opera Android ? | Safari iOS 不支持 No | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
见实现注意事项。
要求使用供应商前缀或不同名称。
BaseAudioContext
createAnalyser()
createBiquadFilter()
createBuffer()
createBufferSource()
createChannelMerger()
createChannelSplitter()
createConstantSource()
createConvolver()
createDelay()
createDynamicsCompressor()
createGain()
createIIRFilter()
createOscillator()
createPanner()
createPeriodicWave()
createScriptProcessor()
createStereoPanner()
createWaveShaper()
decodeAudioData()
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
AudioWorkletProcessor
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode