Web/API/BaseAudioContext/createPeriodicWave

From Get docs

The createPeriodicWave() method of the 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.


Syntax

var wave = AudioContext.createPeriodicWave(real, imag[, constraints]);

Returns

A PeriodicWave.

Parameters

real
An array of cosine terms (traditionally the A terms).
imag
An array of sine terms (traditionally the B terms).

The real and imag arrays have to have the same length, otherwise an error is thrown.

constraints Optional
An dictionary object that specifies whether normalization should be disabled (if not specified, normalization is enabled by default.) It takes one property:
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.


Example

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. <math display="inline">\left( {a + bi} \right)e^{i},\left( {c + di} \right)e^{2i},\left( {f + gi} \right)e^{3i}</math>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.

Specifications

Specification Status Comment
Web Audio APIThe definition of 'createPeriodicWave' in that specification. Working Draft

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
createPeriodicWave

Chrome Full support 59

Notes'

Full support 59

Notes'

Notes' Default values supported Full support 57


Full support 10

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support ≤18

Firefox Full support 53

Notes'

Full support 53

Notes'

Notes' Originally implemented on AudioContext in Firefox 25.

IE

No support No

Opera Full support 22


Full support 22


Full support 15

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Safari Full support 6

Prefixed'

Full support 6

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

WebView Android Full support 59

Notes'

Full support 59

Notes'

Notes' Default values supported Full support 57


Full support 4.4.3

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Chrome Android Full support 59

Notes'

Full support 59

Notes'

Notes' Default values supported Full support 57


Full support 33

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Firefox Android Full support 53

Notes'

Full support 53

Notes'

Notes' Originally implemented on AudioContext in Firefox Android 26.

Opera Android Full support 22


Full support 22


Full support 14

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Safari iOS Full support 6

Prefixed'

Full support 6

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Samsung Internet Android Full support 7.0

Notes'

Full support 7.0

Notes'

Notes' Default values supported Full support 7.0


Full support 2.0

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Possible to disable normalisation Chrome

Full support Yes

Edge

Full support ≤18

Firefox

No support No

IE

No support No

Opera

?

Safari

No support No

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

No support No

Opera Android

?

Safari iOS

No support No

Samsung Internet Android

Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.'
See implementation notes.
Requires a vendor prefix or different name for use.'
Requires a vendor prefix or different name for use.


See also