annotate WAVE.js @ 497:da8b22838465 Dev_main

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