Advertisement
alseambusher

Parse wav

Jan 8th, 2016
10,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. reader.on('end', function(){
  2.     raw = Buffer.concat(raw) // concat all buffers into one
  3.     data = [] // this will hold the processed data
  4.     for (var i=0; i < raw.length; i += format.blockAlign){
  5.         str = ""
  6.         // Note that here I am taking only the first channel
  7.         for(var j=0; j<format.blockAlign/format.channels; j++)
  8.             if(format.endianness == "LE") //decide how to combine using endianess
  9.                 str = raw[i+j].toString(16) + str
  10.             else
  11.                 str += raw[i+j].toString(16)
  12.             data.push(hexToInt(str))
  13.     }
  14.     /*
  15.         Now do whatever you want with "data"
  16.     */
  17. });
  18.  
  19. // This function handles negative numbers as well
  20. function hexToInt (hex) {
  21.     if (hex.length % 2 != 0) {
  22.         hex = "0" + hex;
  23.     }
  24.     var num = parseInt(hex, 16);
  25.     var maxVal = Math.pow(2, hex.length / 2 * 8);
  26.     if (num > maxVal / 2 - 1) {
  27.         num = num - maxVal
  28.     }
  29.     return num;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement