Skip to content Skip to sidebar Skip to footer

How To Export Last 3s Data Of A Web Audio Stream

Question: I am using web audio API. I need to buffer a non-stop audio stream, like a radio stream. and when I get a notification, I need to get the past 3s audio data and send it t

Solution 1:

You can capture the audio from a stream using the ScriptProcessorNode. Whilst this is deprecated no browser as of now actually implements the new AudioWorker.

varN=1024;
vartime=3; // Desired time of capture;varframe_holder= [];
vartime_per_frame= N / context.sampleRate;
varnum_frames= Math.ceil(time / time_per_frame); // Minimum number to meet timevarscript= context.createScriptProcessor(N,1,1);
script.connect(context.destination);

script.onaudioprocess = function(e) {
  varinput= e.inputBuffer.getChannelData(0);
  varoutput= e.outputBuffer.getChannelData(0);
  varcopy=newFloat32Array(input.length);
  for (var n=0; n<input.length; n++) {
    output[n] = 0.0; // Null this as I guess you are capturing microphone
    copy[n] = input[n];
  }
  // Now we need to see if we have more than 3s worth of framesif (frame_holder.length > num_frames) {
    frame_holder = frame_holder.slice(frame_holder.length-num_frames);
  }
  // Add in the current framevartemp= frame_holder.slice(1); // Cut off first frame;
  frame_holder = temp.concat([copy]); // Add the latest frame
}

Then for actual transmission, you just need to string the copied frames together. It is easier than trying to keep one long array though of course that is also possible.

Post a Comment for "How To Export Last 3s Data Of A Web Audio Stream"