Search code examples
javascriptaudioweb-audio-api

Web Audio API AudioParam.value not logging


I am trying to figure out how to read the current value of an AudioParam. When an AudioParam is being modified by an AudioNode through the AudioNode.connect(AudioParam), it doesn't seem to effect AudioParam.value.

Here is an example: I have an oscillator (source) connected to a gainNode (gain). I have another oscillator (mod) routed to a gainNode (modAmp). ModAmp is then connected to gain.gain. I also have a meter for gain.gain, changing the textbox to display gain.gain.value When we play the oscillator, the gain is audibly moving up and down, but the meter stays constant to the original setting. How can I get a real-time reading of the AudioParam?

http://jsfiddle.net/eliotw/3o0d0ovs/4/ (please note that you have to run the script every time you want to run an oscillator)

//create audio context
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new window.AudioContext();

//create source and gain, connect them
var source = context.createOscillator();
var gain = context.createGain();
source.connect(gain);

//create modulator and gain for it and connect them
var mod = context.createOscillator();
var modAmp = context.createGain(); 
mod.connect(modAmp);

//connect modulator gain node to audio param 
modAmp.connect(gain.gain);

//connect to audio context
gain.connect(context.destination);

//source values
source.frequency.value = 220;
gain.gain.value = 0.5;

//mod values
mod.frequency.value = 6;
modAmp.gain.value = 0.5;

source.start(0);
mod.start(0);

setInterval(function() {
      console.log(gain.gain.value);
    },
    600
);

Solution

  • You can't, really. The only way is to connect it to a script processor node or Analyzer node and look at the output bits. The latter (using getFloatTimeDomainData) is probably how I'd do it.