Rohan Singh Dhaka
Rohan Singh Dhaka

Reputation: 193

reducing recorded audio .wav file size

I am using Recorder.js and RecorderWorker.js to record audio. As we know file type will be .wav and and file size is very big. I tried one solution which is got from the previous similar questions asked on stackoverflow but still for 1 min audio, file size >= 6.5 MB. Instead of both the channels I am using single channel. These are the following changes I have mad in my javascript.

In recorder.js-->

this.node.onaudioprocess = function(e){
      if (!recording) return;
      var buffer = [];
      for (var channel = 0; channel <1; channel++){
          buffer.push(e.inputBuffer.getChannelData(channel));
      }
      worker.postMessage({
        command: 'record',
        buffer: buffer
      });
    }

In recorderWorker.js-->

function record(inputBuffer){
  for (var channel = 0; channel < 1; channel++){
    recBuffers[channel].push(inputBuffer[channel]);
  }
  recLength += inputBuffer[0].length;
}

function exportWAV(type){
  var bufferL = mergeBuffers(recBuffers[0], recLength);
  var dataview = encodeWAV(bufferL);
  var audioBlob = new Blob([dataview], { type: type });
  this.postMessage(audioBlob);
}

function getBuffer(){
  var buffers = [];
  for (var channel = 0; channel < 1; channel++){
    buffers.push(mergeBuffers(recBuffers[channel], recLength));
  }
  this.postMessage(buffers);
}

function interleave(inputL, inputR){
  var length = inputL.length;
  var result = new Float32Array(length);
  var index = 0,
    inputIndex = 0;

  while (index < length){
    result[index++] = inputL[inputIndex];
    //result[index++] = inputR[inputIndex];
    inputIndex++;
  }
  return result;
}

/* channel count */
view.setUint16(22, 1, true);
/* block align (channel count * bytes per sample) */
  view.setUint16(32, 2, true);

I tried to convert it into mp3 file type using libmp3lame.js but it is slow plus I can't record >2 min audio using this.

What I am asking is first, is there anything I am doing wrong because of which size is not getting reduced? Second, is there anyother way to reduce the size?

Upvotes: 1

Views: 2665

Answers (1)

LYu
LYu

Reputation: 2416

I found two ways to downsize the exported wav file:

  1. Use a single channel when initializing a new Recorder, source

rec = new Recorder(input,{numChannels:1})

  1. Or you can downsample the rate to further reduce the size, source

Upvotes: 0

Related Questions