annotate WAVE.js @ 1174:dd7ba3054bf9

Test create drag and drop active. Auto-builds on page HTML from specification.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Thu, 11 Feb 2016 12:06:54 +0000
parents c0022a09c4f6
children a19d06d13ab0
rev   line source
n@1153 1 // Decode and perform WAVE file byte level manipulation
n@1153 2
n@1153 3 find_subarray = function(arr,subarr) {
n@1153 4 var arr_length = arr.length;
n@1153 5 var subarr_length = subarr.length;
n@1153 6 var last_check_index = arr_length - subarr_length;
n@1153 7
n@1153 8 positionLoop:
n@1153 9 for (var i=0; i <= last_check_index; i++)
n@1153 10 {
n@1153 11 for (var j=0; j< subarr_length; j++)
n@1153 12 {
n@1153 13 if (arr[i + j] !== subarr[j]) {
n@1153 14 continue positionLoop;
n@1153 15 }
n@1153 16 }
n@1153 17 return i;
n@1153 18 }
n@1153 19 return -1;
n@1153 20 };
n@1153 21
n@1153 22 function WAVE()
n@1153 23 {
n@1153 24 // The WAVE file object
n@1153 25 this.status == 'WAVE_DECLARED'
n@1153 26
n@1153 27 this.decoded_data = null;
n@1153 28
n@1153 29 this.RIFF = String(); //ChunkID
n@1153 30 this.size; //ChunkSize
n@1153 31 this.FT_Header; //Format
n@1153 32 this.fmt_marker; //Subchunk1ID
n@1153 33 this.formatDataLength; //Subchunk1Size
n@1153 34 this.type; //AudioFormat
n@1153 35 this.num_channels; //NumChannels
n@1153 36 this.sample_rate; //SampleRate
n@1153 37 this.byte_rate; //ByteRate
n@1153 38 this.block_align; //BlockAlign
n@1153 39 this.bits_per_sample; //BitsPerSample
n@1153 40 this.data_header; //Subchunk2ID
n@1153 41 this.data_size; //Subchunk2Size
n@1153 42 this.num_samples;
n@1153 43
n@1153 44 this.open = function(IOArrayBuffer)
n@1153 45 {
n@1153 46 var IOView8 = new Uint8Array(IOArrayBuffer);
n@1153 47 IOView8.subarray(0,4).forEach(function(i){
n@1153 48 var char = String.fromCharCode(i);
n@1153 49 this.RIFF = this.RIFF.concat(char);
n@1153 50 },this);
n@1153 51 if (this.RIFF != 'RIFF')
n@1153 52 {
n@1153 53 console.log('WAVE ERR - Not a RIFF file');
n@1153 54 return 1;
n@1153 55 }
n@1153 56 this.size = 0;
n@1153 57 IOView8.subarray(4,8).forEach(function(i,a){this.size += Number(i)<<(8*a);},this);
n@1153 58 this.FT_Header = String();
n@1153 59 IOView8.subarray(8,12).forEach(function(i){this.FT_Header = this.FT_Header.concat(String.fromCharCode(i));},this);
n@1153 60 this.fmt_marker = String();
n@1153 61 IOView8.subarray(12,16).forEach(function(i){this.fmt_marker = this.fmt_marker.concat(String.fromCharCode(i));},this);
n@1153 62 this.formatDataLength = 0;
n@1153 63 IOView8.subarray(16,20).forEach(function(i,a){this.formatDataLength += Number(i)<<(8*a);},this);
n@1153 64 this.type = 0;
n@1153 65 IOView8.subarray(20,22).forEach(function(i,a){this.type += Number(i)<<(8*a);},this);
n@1153 66 this.num_channels = 0;
n@1153 67 IOView8.subarray(22,24).forEach(function(i,a){this.num_channels += Number(i)<<(8*a);},this);
n@1153 68 this.sample_rate = 0;
n@1153 69 IOView8.subarray(24,28).forEach(function(i,a){this.sample_rate += Number(i)<<(8*a);},this);
n@1153 70 this.byte_rate = 0;
n@1153 71 IOView8.subarray(28,32).forEach(function(i,a){this.byte_rate += Number(i)<<(8*a);},this);
n@1153 72 this.block_align = 0;
n@1153 73 IOView8.subarray(32,34).forEach(function(i,a){this.block_align += Number(i)<<(8*a);},this);
n@1153 74 this.bits_per_sample = 0;
n@1153 75 IOView8.subarray(34,36).forEach(function(i,a){this.bits_per_sample += Number(i)<<(8*a);},this);
n@1153 76
n@1153 77 // Find the data header first
n@1153 78 var data_start = find_subarray(IOView8,[100, 97, 116, 97]);
n@1153 79
n@1153 80 this.data_header = String();
n@1153 81 IOView8.subarray(data_start,data_start+4).forEach(function(i){this.data_header = this.data_header.concat(String.fromCharCode(i));},this);
n@1153 82 this.data_size = 0;
n@1153 83 IOView8.subarray(data_start+4,data_start+8).forEach(function(i,a){this.data_size += Number(i)<<(8*a);},this);
n@1153 84
n@1153 85 this.num_samples = this.data_size / this.block_align;
n@1153 86
n@1153 87 this.decoded_data = [];
n@1153 88 if (this.type != 1 && this.type != 3) {
n@1153 89 console.log("Neither PCM nor IEEE float, cannot decode");
n@1153 90 return 1;
n@1153 91 }
n@1153 92 for (var c=0; c<this.num_channels; c++)
n@1153 93 {
n@1153 94 this.decoded_data.push(new Float32Array(this.num_samples));
n@1153 95 }
n@1153 96 var sampleDataOffset = data_start+8;
n@1153 97
n@1153 98 // Now need to decode the data from sampleDataOffset
n@1153 99 // Data is always interleved
n@1153 100 var data_view;
n@1153 101 if (this.type == 3)
n@1153 102 {
n@1153 103 // Already in float
n@1153 104 if (this.bits_per_sample == 32) {
n@1153 105 data_view = new Float32Array(IOArrayBuffer.slice(sampleDataOffset,sampleDataOffset+this.data_size));
n@1153 106 } else if (this.bits_per_sample == 64) {
n@1153 107 data_view = new Float64Array(IOArrayBuffer.slice(sampleDataOffset,sampleDataOffset+this.data_size));
n@1153 108 }
n@1153 109 } else if (this.type == 1)
n@1153 110 {
n@1157 111 data_view = new Float32Array(this.num_samples*this.num_channels);
n@1153 112 integerConvert(new Uint8Array(IOArrayBuffer.slice(sampleDataOffset,sampleDataOffset+this.data_size)),data_view,this.bits_per_sample/8);
n@1153 113 }
n@1153 114 deInterlace(data_view,this.decoded_data);
n@1153 115 return 0;
n@1153 116 };
n@1153 117 }
n@1153 118
n@1153 119 function deInterlace(src_array, dst_array)
n@1153 120 {
n@1153 121 var number = src_array.length;
n@1153 122 var channels = dst_array.length;
n@1153 123 var channel_index = 0;
n@1153 124 var dst_index = 0;
n@1153 125 for (var n=0; n<number; n++)
n@1153 126 {
n@1153 127 dst_array[channel_index][dst_index] = src_array[n];
n@1153 128 channel_index++;
n@1153 129 if (channel_index >= channels) {
n@1153 130 channel_index = 0;
n@1153 131 dst_index++;
n@1153 132 }
n@1153 133 }
n@1153 134 }
n@1153 135
n@1153 136 function integerConvert(srcView,dstView,srcBytes)
n@1153 137 {
n@1153 138 //Convert integers of a Uint8Array of certain byte length into a Float32Array
n@1153 139 var number = dstView.length;
n@1153 140 var outBits = srcBytes*8;
n@1153 141 var endShift = 32 - outBits;
n@1153 142 if (srcView.length != dstView.length*srcBytes)
n@1153 143 {
n@1153 144 return -1;
n@1153 145 }
n@1153 146 for (var n=0; n<number; n++)
n@1153 147 {
n@1153 148 var intData;
n@1153 149 var srcIndex = n*srcBytes;
n@1153 150 srcView.subarray(srcIndex,srcIndex+srcBytes).forEach(function(i,a){intData += Number(i)<<(8*a);},this);
n@1153 151 intData = (intData << (endShift));
n@1153 152 dstView[n] = intData / 2147483648;
n@1153 153 }
n@1153 154 }