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