PeriodicWave

在此页

 PeriodicWave  interface defines a periodic waveform that can be used to shape the output of an  OscillatorNode  .

 PeriodicWave  has no inputs or outputs; it is used to define custom oscillators when calling  OscillatorNode.setPeriodicWave()   PeriodicWave  itself is created/returned by  AudioContext.createPeriodicWave()  .

构造函数

 PeriodicWave.PeriodicWave() 

特性

None; also,  PeriodicWave  doesn't inherit any properties.

方法

None; also,  PeriodicWave  doesn't inherit any methods.

范例

The following example illustrates simple usage of  createPeriodicWave()  , to create a  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. $\left\left(a+bi\right\right)e^\left\{i\right\} , \left\left(c+di\right\right)e^\left\{2i\right\} , \left\left(f+gi\right\right)e^\left\{3i\right\}$ 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 'PeriodicWave' in that specification.

浏览器兼容性

 Desktop Mobile Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome 14 Edge ≤18 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 Chrome 55 注意事项 55 注意事项 Before Chrome 59, the default values were not supported. Edge ≤79 Firefox 53 IE 不支持 No Opera 42 Safari ? WebView Android 55 注意事项 55 注意事项 Before Chrome 59, the default values were not supported. Chrome Android 55 注意事项 55 注意事项 Before Chrome 59, the default values were not supported. Firefox Android 53 Opera Android 42 Safari iOS ? Samsung Internet Android 6.0 注意事项 6.0 注意事项 Before Samsung Internet 7.0, the default values were not supported.

另请参阅

1. Web 音频 API
2.  PeriodicWave 
3. 构造函数
4. Related pages for Web Audio API