Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a phase offset for an OscillatorNode in the Web Audio API?

I'm trying to implement Stereo Phase as it is described here: http://www.image-line.com/support/FLHelp/html/plugins/3x%20OSC.htm

"Stereo Phase (SP) - Allows you to set different phase offset for the left and right channels of the generator. The offset results in the oscillator starting at a different point on the oscillator's shape (for example, start at the highest value of the sine function instead at the zero point). Stereo phase offset adds to the richness and stereo panorama of the sound produced."

I'm trying to achieve this for an OscillatorNode. My only idea is to use createPeriodicWave (https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#dfn-createPeriodicWave) However, the description of create periodic wave from the specification is above my understanding and I have not found any examples via Google.

Any help in deciphering the description of createPeriodicWave would be helpful as would any other ideas about how to achieve this effect.

Thanks!

like image 996
robert Avatar asked Jul 07 '13 15:07

robert


1 Answers

Mcclellan and others,

This answer helped and subsequently warped me into the world of Fourier. With the help of a page on the subject and some Wikipedia, I think I got the square and sawtooth patterns down, but the triangle pattern still eludes me. Does anyone know?

It indeed gives you the ability to phase shift, as this article by Nick Thompson explains (although he calls the AudioContext methods differently, but the principle is the same).

As far as the square and sawtooth patterns go:

var n = 4096;
var real = new Float32Array(n);
var imag = new Float32Array(n);
var ac = new AudioContext();
var osc = ac.createOscillator();

/* Pick a wave pattern */

/* Sine 
imag[1] = 1; */

/* Sawtooth 
for(x=1;x<n;x++)
    imag[x] = 2.0 / (Math.pow(-1, x) * Math.PI * x); */

/* Square */
for(x=1;x<n;x+=2)
    imag[x] = 4.0 / (Math.PI * x);

var wave = ac.createPeriodicWave(real, imag);

osc.setPeriodicWave(wave);  
osc.connect(ac.destination);
osc.start();
osc.stop(2); /* Have it stop after 2 seconds */

This will play the activated pattern, the square pattern in this case. What would the triangle formula look like?

like image 147
Lucas van Heerikhuizen Avatar answered Sep 20 '22 08:09

Lucas van Heerikhuizen