The 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()
. The PeriodicWave
itself is created/returned by AudioContext.createPeriodicWave()
.
Constructor
PeriodicWave.PeriodicWave()
- Creates a new
PeriodicWave
object instance using the default values for all properties. If you wish to establish custom property values at the outset, use theAudioContext.createPeriodicWave()
factory method instead.
Properties
None; also, PeriodicWave
doesn't inherit any properties.
Methods
None; also, PeriodicWave
doesn't inherit any methods.
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 'PeriodicWave' in that specification. | Working Draft |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
PeriodicWave
|
Chrome
Full support 14 |
Edge
Full support ≤18 |
Firefox
Full support 25 |
IE
No support No |
Opera
Full support 15 |
Safari
Full support 6 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
Full support 26 |
Opera Android
Full support 14 |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support 1.0 |
PeriodicWave() constructor
|
Chrome Full support 55 Full support 55 Notes' Before Chrome 59, the default values were not supported. |
Edge
Full support ≤79 |
Firefox
Full support 53 |
IE
No support No |
Opera
Full support 42 |
Safari
? |
WebView Android Full support 55 Full support 55 Notes' Before Chrome 59, the default values were not supported. |
Chrome Android Full support 55 Full support 55 Notes' Before Chrome 59, the default values were not supported. |
Firefox Android
Full support 53 |
Opera Android
Full support 42 |
Safari iOS
? |
Samsung Internet Android Full support 6.0 Full support 6.0 Notes' Before Samsung Internet 7.0, the default values were not supported. |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.'
- See implementation notes.
See also
PeriodicWave by Mozilla Contributors is licensed under CC-BY-SA 2.5.