Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the audioParam.exponentialRampToValueAtTime work?

I am not able to get a sliding change of volume through exponentialRampToValueAtTime of a GainNode.

Here is an example:

var context = new AudioContext(),
    osc = context.createOscillator(),
    gain = context.createGain();

osc.frequency.value = 440; // A note
osc.start( 0 );
osc.connect( gain );

gain.gain.value = 0;
gain.connect( context.destination );

gain.gain.cancelScheduledValues( 0 );
gain.gain.setValueAtTime( 0, context.currentTime );
gain.gain.exponentialRampToValueAtTime( 1, context.currentTime + 2 );

In my understanding this should gradually increase the volume, until reaching 1 (100%) and this whole process should take 2sec. Is this assumption correct?

If it is, why does stay on 0 for 2 sec, and suddenly switches to full volume?

Thanks in advance for your time and effort.

like image 755
Nikolay Tsenkov Avatar asked Apr 23 '15 09:04

Nikolay Tsenkov


1 Answers

It seems that this function does not like 0 value. FF throws "SyntaxError: An invalid or illegal string was specified". Below code will ramp correctly. See on Plnkr.

var context = new AudioContext(),
    osc = context.createOscillator(),
    gain = context.createGain();

osc.frequency.value = 440.0; // A note
osc.start( 0 );
osc.connect( gain );

gain.connect( context.destination );

gain.gain.setValueAtTime(0.0001, context.currentTime); // <-- line of interest

gain.gain.exponentialRampToValueAtTime(1, context.currentTime + 10 );

UPDATE: "A NotSupportedError exception must be thrown if this value is less than or equal to 0, or if the value at the time of the previous event is less than or equal to 0" according Web Audio specification. As figured out by @cwilso (see comments).

like image 194
Ruslanas Balčiūnas Avatar answered Oct 22 '22 07:10

Ruslanas Balčiūnas