comparison WAVE.js @ 1384:7cd2a8dcdc51

Safari does not support Views having the forEach function. Implemented work around.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 15 Feb 2016 13:48:24 +0000
parents c277da1e89ca
children a4ad9e55b5b8
comparison
equal deleted inserted replaced
1383:1c5fa95cf158 1384:7cd2a8dcdc51
16 } 16 }
17 return i; 17 return i;
18 } 18 }
19 return -1; 19 return -1;
20 }; 20 };
21
22 function convertToInteger(arr) {
23 var value = 0;
24 for (var i=0; i<arr.length; i++)
25 {
26 value += arr[i]<<(i*8);
27 }
28 return value;
29 }
30 function convertToString(arr) {
31 var str = "";
32 for (var i=0; i<arr.length; i++)
33 {
34 str = str.concat(String.fromCharCode(arr[i]));
35 }
36 return str;
37 }
21 38
22 function WAVE() 39 function WAVE()
23 { 40 {
24 // The WAVE file object 41 // The WAVE file object
25 this.status == 'WAVE_DECLARED' 42 this.status == 'WAVE_DECLARED'
42 this.num_samples; 59 this.num_samples;
43 60
44 this.open = function(IOArrayBuffer) 61 this.open = function(IOArrayBuffer)
45 { 62 {
46 var IOView8 = new Uint8Array(IOArrayBuffer); 63 var IOView8 = new Uint8Array(IOArrayBuffer);
47 IOView8.subarray(0,4).forEach(function(i){ 64 this.RIFF = convertToString(IOView8.subarray(0,4));
48 var char = String.fromCharCode(i);
49 this.RIFF = this.RIFF.concat(char);
50 },this);
51 if (this.RIFF != 'RIFF') 65 if (this.RIFF != 'RIFF')
52 { 66 {
53 console.log('WAVE ERR - Not a RIFF file'); 67 console.log('WAVE ERR - Not a RIFF file');
54 return 1; 68 return 1;
55 } 69 }
56 this.size = 0; 70 this.size = convertToInteger(IOView8.subarray(4,8));
57 IOView8.subarray(4,8).forEach(function(i,a){this.size += Number(i)<<(8*a);},this); 71 this.FT_Header = convertToString(IOView8.subarray(8,12));
58 this.FT_Header = String(); 72 this.fmt_marker = convertToString(IOView8.subarray(12,16));
59 IOView8.subarray(8,12).forEach(function(i){this.FT_Header = this.FT_Header.concat(String.fromCharCode(i));},this); 73 this.formatDataLength = convertToInteger(IOView8.subarray(16,20));
60 this.fmt_marker = String(); 74 this.type = convertToInteger(IOView8.subarray(20,22));
61 IOView8.subarray(12,16).forEach(function(i){this.fmt_marker = this.fmt_marker.concat(String.fromCharCode(i));},this); 75 this.num_channels = convertToInteger(IOView8.subarray(22,24));
62 this.formatDataLength = 0; 76 this.sample_rate = convertToInteger(IOView8.subarray(24,28));
63 IOView8.subarray(16,20).forEach(function(i,a){this.formatDataLength += Number(i)<<(8*a);},this); 77 this.byte_rate = convertToInteger(IOView8.subarray(28,32));
64 this.type = 0; 78 this.block_align = convertToInteger(IOView8.subarray(32,34));
65 IOView8.subarray(20,22).forEach(function(i,a){this.type += Number(i)<<(8*a);},this); 79 this.bits_per_sample = convertToInteger(IOView8.subarray(34,36));
66 this.num_channels = 0;
67 IOView8.subarray(22,24).forEach(function(i,a){this.num_channels += Number(i)<<(8*a);},this);
68 this.sample_rate = 0;
69 IOView8.subarray(24,28).forEach(function(i,a){this.sample_rate += Number(i)<<(8*a);},this);
70 this.byte_rate = 0;
71 IOView8.subarray(28,32).forEach(function(i,a){this.byte_rate += Number(i)<<(8*a);},this);
72 this.block_align = 0;
73 IOView8.subarray(32,34).forEach(function(i,a){this.block_align += Number(i)<<(8*a);},this);
74 this.bits_per_sample = 0;
75 IOView8.subarray(34,36).forEach(function(i,a){this.bits_per_sample += Number(i)<<(8*a);},this);
76 80
77 // Find the data header first 81 // Find the data header first
78 var data_start = find_subarray(IOView8,[100, 97, 116, 97]); 82 var data_start = find_subarray(IOView8,[100, 97, 116, 97]);
79 83
80 this.data_header = String(); 84 this.data_header = convertToString(IOView8.subarray(data_start,data_start+4));
81 IOView8.subarray(data_start,data_start+4).forEach(function(i){this.data_header = this.data_header.concat(String.fromCharCode(i));},this); 85 this.data_size = convertToInteger(IOView8.subarray(data_start+4,data_start+8));
82 this.data_size = 0;
83 IOView8.subarray(data_start+4,data_start+8).forEach(function(i,a){this.data_size += Number(i)<<(8*a);},this);
84 86
85 this.num_samples = this.data_size / this.block_align; 87 this.num_samples = this.data_size / this.block_align;
86 88
87 this.decoded_data = []; 89 this.decoded_data = [];
88 if (this.type != 1 && this.type != 3) { 90 if (this.type != 1 && this.type != 3) {
143 { 145 {
144 return -1; 146 return -1;
145 } 147 }
146 for (var n=0; n<number; n++) 148 for (var n=0; n<number; n++)
147 { 149 {
148 var intData;
149 var srcIndex = n*srcBytes; 150 var srcIndex = n*srcBytes;
150 srcView.subarray(srcIndex,srcIndex+srcBytes).forEach(function(i,a){intData += Number(i)<<(8*a);},this); 151 var intData = convertToInteger(srcView.subarray(srcIndex,srcIndex+srcBytes));
151 intData = (intData << (endShift)); 152 intData = (intData << (endShift));
152 dstView[n] = intData / 2147483648; 153 dstView[n] = intData / 2147483648;
153 } 154 }
154 } 155 }