-
Notifications
You must be signed in to change notification settings - Fork 12
/
wav.js
279 lines (221 loc) · 7.59 KB
/
wav.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
wav.js - a javascript audiolib for reading WAVE files
Reads the Format chunk of a WAVE file using the RIFF specification.
Only supports slice() on uncompressed PCM format.
Only supports one Data chunk.
NOTE: Does not auto-correct:
- Incorrect block alignment values
- Incorrect Average Samples Per Second value
- Missing word alignment padding
@author David Lindkvist
@twitter ffdead
*/
/**
* Constructor: Parse Format chunk of WAV files.
*
* Fires onloadend() function after successful load.
*
* @param {File|Blob|ArrayBuffer} RIFF formatted WAV file
*/
function wav(file) {
// status
this.EMPTY = 0; // No data has been loaded yet.
this.LOADING = 1; // Data is currently being loaded.
this.DONE = 2; // The entire read request has been completed.
this.UNSUPPORTED_FORMAT = 3; // Error state - file format not recognized
this.readyState = this.EMPTY;
this.error = undefined;
// original File and loaded ArrayBuffer
this.file = file instanceof Blob ? file : undefined;
this.buffer = file instanceof ArrayBuffer ? file : undefined;;
//ThatcherC
this.dataBuffer = file instanceof ArrayBuffer ? file : undefined;;
// format
this.chunkID = undefined; // must be RIFF
this.chunkSize = undefined; // size of file after this field
this.format = undefined; // must be WAVE
this.compression = undefined; // 1=PCM
this.numChannels = undefined; // Mono = 1, Stereo = 2
this.sampleRate = undefined; // 8000, 44100, etc.
this.byteRate = undefined; // bytes per second
this.blockAlign = undefined; // number of bytes for one sample including all channels.
this.bitsPerSample = undefined; // 8 bits = 8, 16 bits = 16, etc.
// data chunk
this.dataOffset = -1; // index of data block
this.dataLength = -1; // size of data block
// let's take a peek
this.peek();
}
/**
* Load header as an ArrayBuffer and parse format chunks
*/
wav.prototype.peek = function () {
this.readyState = this.LOADING;
// see if buffer is already loaded
if (this.buffer !== undefined) {
return this.parseArrayBuffer();
}
var reader = new FileReader();
var that = this;
// only load the first 44 bytes of the header
var headerBlob = this.sliceFile(0, 44);
reader.readAsArrayBuffer(this.file);
reader.onloadend = function() {
that.buffer = this.result;
that.parseArrayBuffer.apply(that);
};
};
wav.prototype.parseArrayBuffer = function () {
try {
this.parseHeader();
this.parseData();
this.getSamples();
this.readyState = this.DONE;
}
catch (e) {
this.readyState = this.UNSUPPORTED_FORMAT;
this.error = e;
}
// trigger onloadend callback if exists
if (this.onloadend) {
this.onloadend.apply(this);
}
};
/**
* Walk through RIFF and WAVE format chunk
* Based on https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
* and http://www.sonicspot.com/guide/wavefiles.html
*/
wav.prototype.parseHeader = function () {
this.chunkID = this.readText(0, 4);
this.chunkSize = this.readDecimal(4, 4);
if (this.chunkID !== 'RIFF') throw 'NOT_SUPPORTED_FORMAT';
this.format = this.readText(8, 4);
if (this.format !== 'WAVE') throw 'NOT_SUPPORTED_FORMAT';
this.compression = this.readDecimal(20, 2);
this.numChannels = this.readDecimal(22, 2);
this.sampleRate = this.readDecimal(24, 4);
// == SampleRate * NumChannels * BitsPerSample/8
this.byteRate = this.readDecimal(28, 4);
// == NumChannels * BitsPerSample/8
this.blockAlign = this.readDecimal(32, 2);
this.bitsPerSample = this.readDecimal(34, 2);
};
/**
* Walk through all subchunks and look for the Data chunk
*/
wav.prototype.parseData = function () {
var chunkType = this.readText(36, 4);
var chunkSize = this.readDecimal(40, 4);
// only support files where data chunk is first (canonical format)
if (chunkType === 'data') {
this.dataLength = chunkSize;
this.dataOffset = 44;
}
else {
// duration cant be calculated && slice will not work
throw 'NOT_CANONICAL_FORMAT: unsupported "' + chunkType + '"" chunk - was expecting data';
}
};
/**
* Returns slice of file as new wav file
* @param {int} start Start offset in seconds from beginning of file
* @param {int} end Length of requested slice in seconds
*/
wav.prototype.slice = function (start, length, callback) {
var reader = new FileReader();
var that = this;
// use the byterate to calculate number of bytes per second
var start = this.dataOffset + (start * this.byteRate);
var end = start + (length * this.byteRate);
var headerBlob = this.sliceFile(0, 44);
var dataBlob = this.sliceFile(start, end);
// concant header and data slice
var blob = new Blob([headerBlob, dataBlob]);
reader.readAsArrayBuffer(blob);
reader.onloadend = function() {
// update chunkSize in header
var chunkSize = new Uint8Array(this.result, 4, 4);
that.tolittleEndianDecBytes(chunkSize, 36+dataBlob.size);
// update dataChunkSize in header
var dataChunkSize = new Uint8Array(this.result, 40, 4);
that.tolittleEndianDecBytes(dataChunkSize, dataBlob.size);
if (callback) callback.apply(that, [this.result]);
};
};
/*
* do we need direct access to samples?
*/
wav.prototype.getSamples = function () {
if (this.bitsPerSample === 8)
this.dataSamples = new Uint8Array(this.buffer, 44, this.dataLength/this.blockAlign);
else if (this.bitsPerSample === 16)
this.dataSamples = new Int16Array(this.buffer, 44, this.dataLength/this.blockAlign);
}
/**
* Reads slice from buffer as String
*/
wav.prototype.readText = function (start, length) {
var a = new Uint8Array(this.buffer, start, length);
var str = '';
for(var i = 0; i < a.length; i++) {
str += String.fromCharCode(a[i]);
}
return str;
};
/**
* Reads slice from buffer as Decimal
*/
wav.prototype.readDecimal = function (start, length) {
var a = new Uint8Array(this.buffer, start, length);
return this.fromLittleEndianDecBytes(a);
};
/**
* Calculates decimal value from Little-endian decimal byte array
*/
wav.prototype.fromLittleEndianDecBytes = function (a) {
var sum = 0;
for(var i = 0; i < a.length; i++)
sum |= a[i] << (i*8);
return sum;
};
/**
* Populate Little-endian decimal byte array from decimal value
*/
wav.prototype.tolittleEndianDecBytes = function (a, decimalVal) {
for(var i=0; i<a.length; i++) {
a[i] = decimalVal & 0xFF;
decimalVal >>= 8;
}
return a;
};
/**
* Slice the File using either standard slice or webkitSlice
*/
wav.prototype.sliceFile = function (start, end) {
if (this.file.slice) return this.file.slice(start, end);
if (this.file.webkitSlice) return this.file.webkitSlice(start, end);
};
wav.prototype.isCompressed = function () {
return this.compression !== 1;
};
wav.prototype.isMono = function () {
return this.numChannels === 1;
};
wav.prototype.isStereo = function () {
return this.numChannels === 2;
};
wav.prototype.getDuration = function () {
return this.dataLength > -1 ? (this.dataLength / this.byteRate) : -1;
};
/**
* Override toString
*/
wav.prototype.toString = function () {
return (this.file ? this.file.name : 'noname.wav') + ' (' + this.chunkID + '/' + this.format + ')\n' +
'Compression: ' + (this.isCompressed() ? 'yes' : 'no (PCM)') + '\n' +
'Number of channels: ' + this.numChannels + ' (' + (this.isStereo()?'stereo':'mono') + ')\n' +
'Sample rate: ' + this.sampleRate + ' Hz\n'+
'Sample size: ' + this.bitsPerSample + '-bit\n'+
'Duration: ' + Math.round(this.getDuration()) + ' seconds';
};