# HG changeset patch # User Nicholas Jillings # Date 1452955862 0 # Node ID 6281b02dcb58c92b8de562669fcd4a4f88de7768 # Parent d39c99d8389151770a2662fc685b8f7a2482e832 Added WAVE.js, conversion of some C scripts to JS for WAVE file manipulation. By default, will use the WAVE decoder first before fallback to browser decoders. All browsers now support Integer 8-/16-/24-/34-bit and IEEE Float 32 WAVE files. diff -r d39c99d83891 -r 6281b02dcb58 WAVE.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WAVE.js Sat Jan 16 14:51:02 2016 +0000 @@ -0,0 +1,154 @@ +// Decode and perform WAVE file byte level manipulation + +find_subarray = function(arr,subarr) { + var arr_length = arr.length; + var subarr_length = subarr.length; + var last_check_index = arr_length - subarr_length; + + positionLoop: + for (var i=0; i <= last_check_index; i++) + { + for (var j=0; j< subarr_length; j++) + { + if (arr[i + j] !== subarr[j]) { + continue positionLoop; + } + } + return i; + } + return -1; +}; + +function WAVE() +{ + // The WAVE file object + this.status == 'WAVE_DECLARED' + + this.decoded_data = null; + + this.RIFF = String(); //ChunkID + this.size; //ChunkSize + this.FT_Header; //Format + this.fmt_marker; //Subchunk1ID + this.formatDataLength; //Subchunk1Size + this.type; //AudioFormat + this.num_channels; //NumChannels + this.sample_rate; //SampleRate + this.byte_rate; //ByteRate + this.block_align; //BlockAlign + this.bits_per_sample; //BitsPerSample + this.data_header; //Subchunk2ID + this.data_size; //Subchunk2Size + this.num_samples; + + this.open = function(IOArrayBuffer) + { + var IOView8 = new Uint8Array(IOArrayBuffer); + IOView8.subarray(0,4).forEach(function(i){ + var char = String.fromCharCode(i); + this.RIFF = this.RIFF.concat(char); + },this); + if (this.RIFF != 'RIFF') + { + console.log('WAVE ERR - Not a RIFF file'); + return 1; + } + this.size = 0; + IOView8.subarray(4,8).forEach(function(i,a){this.size += Number(i)<<(8*a);},this); + this.FT_Header = String(); + IOView8.subarray(8,12).forEach(function(i){this.FT_Header = this.FT_Header.concat(String.fromCharCode(i));},this); + this.fmt_marker = String(); + IOView8.subarray(12,16).forEach(function(i){this.fmt_marker = this.fmt_marker.concat(String.fromCharCode(i));},this); + this.formatDataLength = 0; + IOView8.subarray(16,20).forEach(function(i,a){this.formatDataLength += Number(i)<<(8*a);},this); + this.type = 0; + IOView8.subarray(20,22).forEach(function(i,a){this.type += Number(i)<<(8*a);},this); + this.num_channels = 0; + IOView8.subarray(22,24).forEach(function(i,a){this.num_channels += Number(i)<<(8*a);},this); + this.sample_rate = 0; + IOView8.subarray(24,28).forEach(function(i,a){this.sample_rate += Number(i)<<(8*a);},this); + this.byte_rate = 0; + IOView8.subarray(28,32).forEach(function(i,a){this.byte_rate += Number(i)<<(8*a);},this); + this.block_align = 0; + IOView8.subarray(32,34).forEach(function(i,a){this.block_align += Number(i)<<(8*a);},this); + this.bits_per_sample = 0; + IOView8.subarray(34,36).forEach(function(i,a){this.bits_per_sample += Number(i)<<(8*a);},this); + + // Find the data header first + var data_start = find_subarray(IOView8,[100, 97, 116, 97]); + + this.data_header = String(); + IOView8.subarray(data_start,data_start+4).forEach(function(i){this.data_header = this.data_header.concat(String.fromCharCode(i));},this); + this.data_size = 0; + IOView8.subarray(data_start+4,data_start+8).forEach(function(i,a){this.data_size += Number(i)<<(8*a);},this); + + this.num_samples = this.data_size / this.block_align; + + this.decoded_data = []; + if (this.type != 1 && this.type != 3) { + console.log("Neither PCM nor IEEE float, cannot decode"); + return 1; + } + for (var c=0; c= channels) { + channel_index = 0; + dst_index++; + } + } +} + +function integerConvert(srcView,dstView,srcBytes) +{ + //Convert integers of a Uint8Array of certain byte length into a Float32Array + var number = dstView.length; + var outBits = srcBytes*8; + var endShift = 32 - outBits; + if (srcView.length != dstView.length*srcBytes) + { + return -1; + } + for (var n=0; n - + (1) Very Annoying diff -r d39c99d83891 -r 6281b02dcb58 index.html --- a/index.html Fri Jan 15 11:04:23 2016 +0000 +++ b/index.html Sat Jan 16 14:51:02 2016 +0000 @@ -20,6 +20,7 @@ +