Skip to main content

Why is onAudioProcess returning the buffer over and over?

I feel like theres a very simple mistake here, I just cannot figure it out. Basically I am trying to record my voice and then store the chunks in an array as they come in. Heres the code:

 const audioBuffers = []
 var audioCtx = new AudioContext()
    navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {
    
        var input = audioCtx.createMediaStreamSource(stream)
        var processor = audioCtx.processAudio()
        //processAudio created below
        var outputNode = audioCtx.destination
        
        input.connect(processor)
        processor.connect(outputNode)
    
    });
    
    setTimeout(function(){
        audioCtx.close()
        console.log(audioBuffers)
    }, 3000)
    
    
    AudioContext.prototype.processAudio = function () {
      
      //reduce volume of output
      function edit(input, output){
          for(var i = 0; i < output.length; i++){
              output[i] = input[i] / 2
          }
          return output
      }
      
      //process audio
      var tuner = this.createScriptProcessor(bufferSize, 1, 1);
      tuner.onaudioprocess = function (e) {
          var input = e.inputBuffer.getChannelData(0);
          var output = e.outputBuffer.getChannelData(0);
          output = edit(input, output)
          audioBuffers.push(output)
      };
      return tuner;
  }

For some reason, the same array is pushed to "audioBuffers" everytime onAudioProcess is called.

Float32Array(2048) [-0.00006328763265628368, -0.00005047679951530881, -0.00003455079422565177...
Float32Array(2048) [-0.00006328763265628368, -0.00005047679951530881, -0.00003455079422565177...
Float32Array(2048) [-0.00006328763265628368, -0.00005047679951530881, -0.00003455079422565177...
Float32Array(2048) [-0.00006328763265628368, -0.00005047679951530881, -0.00003455079422565177...
Float32Array(2048) [-0.00006328763265628368, -0.00005047679951530881, -0.00003455079422565177...
Float32Array(2048) [-0.00006328763265628368, -0.00005047679951530881, -0.00003455079422565177...

This is the results from "console.log(audioBuffers)" why is this happening?

Via Active questions tagged javascript - Stack Overflow https://ift.tt/w71lyAH

Comments