n@482: // Decode and perform WAVE file byte level manipulation n@482: n@482: find_subarray = function(arr,subarr) { n@482: var arr_length = arr.length; n@482: var subarr_length = subarr.length; n@482: var last_check_index = arr_length - subarr_length; n@482: n@482: positionLoop: n@482: for (var i=0; i <= last_check_index; i++) n@482: { n@482: for (var j=0; j< subarr_length; j++) n@482: { n@482: if (arr[i + j] !== subarr[j]) { n@482: continue positionLoop; n@482: } n@482: } n@482: return i; n@482: } n@482: return -1; n@482: }; n@482: n@482: function WAVE() n@482: { n@482: // The WAVE file object n@482: this.status == 'WAVE_DECLARED' n@482: n@482: this.decoded_data = null; n@482: n@482: this.RIFF = String(); //ChunkID n@482: this.size; //ChunkSize n@482: this.FT_Header; //Format n@482: this.fmt_marker; //Subchunk1ID n@482: this.formatDataLength; //Subchunk1Size n@482: this.type; //AudioFormat n@482: this.num_channels; //NumChannels n@482: this.sample_rate; //SampleRate n@482: this.byte_rate; //ByteRate n@482: this.block_align; //BlockAlign n@482: this.bits_per_sample; //BitsPerSample n@482: this.data_header; //Subchunk2ID n@482: this.data_size; //Subchunk2Size n@482: this.num_samples; n@482: n@482: this.open = function(IOArrayBuffer) n@482: { n@482: var IOView8 = new Uint8Array(IOArrayBuffer); n@482: IOView8.subarray(0,4).forEach(function(i){ n@482: var char = String.fromCharCode(i); n@482: this.RIFF = this.RIFF.concat(char); n@482: },this); n@482: if (this.RIFF != 'RIFF') n@482: { n@482: console.log('WAVE ERR - Not a RIFF file'); n@482: return 1; n@482: } n@482: this.size = 0; n@482: IOView8.subarray(4,8).forEach(function(i,a){this.size += Number(i)<<(8*a);},this); n@482: this.FT_Header = String(); n@482: IOView8.subarray(8,12).forEach(function(i){this.FT_Header = this.FT_Header.concat(String.fromCharCode(i));},this); n@482: this.fmt_marker = String(); n@482: IOView8.subarray(12,16).forEach(function(i){this.fmt_marker = this.fmt_marker.concat(String.fromCharCode(i));},this); n@482: this.formatDataLength = 0; n@482: IOView8.subarray(16,20).forEach(function(i,a){this.formatDataLength += Number(i)<<(8*a);},this); n@482: this.type = 0; n@482: IOView8.subarray(20,22).forEach(function(i,a){this.type += Number(i)<<(8*a);},this); n@482: this.num_channels = 0; n@482: IOView8.subarray(22,24).forEach(function(i,a){this.num_channels += Number(i)<<(8*a);},this); n@482: this.sample_rate = 0; n@482: IOView8.subarray(24,28).forEach(function(i,a){this.sample_rate += Number(i)<<(8*a);},this); n@482: this.byte_rate = 0; n@482: IOView8.subarray(28,32).forEach(function(i,a){this.byte_rate += Number(i)<<(8*a);},this); n@482: this.block_align = 0; n@482: IOView8.subarray(32,34).forEach(function(i,a){this.block_align += Number(i)<<(8*a);},this); n@482: this.bits_per_sample = 0; n@482: IOView8.subarray(34,36).forEach(function(i,a){this.bits_per_sample += Number(i)<<(8*a);},this); n@482: n@482: // Find the data header first n@482: var data_start = find_subarray(IOView8,[100, 97, 116, 97]); n@482: n@482: this.data_header = String(); n@482: IOView8.subarray(data_start,data_start+4).forEach(function(i){this.data_header = this.data_header.concat(String.fromCharCode(i));},this); n@482: this.data_size = 0; n@482: IOView8.subarray(data_start+4,data_start+8).forEach(function(i,a){this.data_size += Number(i)<<(8*a);},this); n@482: n@482: this.num_samples = this.data_size / this.block_align; n@482: n@482: this.decoded_data = []; n@482: if (this.type != 1 && this.type != 3) { n@482: console.log("Neither PCM nor IEEE float, cannot decode"); n@482: return 1; n@482: } n@482: for (var c=0; c= channels) { n@482: channel_index = 0; n@482: dst_index++; n@482: } n@482: } n@482: } n@482: n@482: function integerConvert(srcView,dstView,srcBytes) n@482: { n@482: //Convert integers of a Uint8Array of certain byte length into a Float32Array n@482: var number = dstView.length; n@482: var outBits = srcBytes*8; n@482: var endShift = 32 - outBits; n@482: if (srcView.length != dstView.length*srcBytes) n@482: { n@482: return -1; n@482: } n@482: for (var n=0; n