forked from bsondermann/Blackbox2Sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WavFile.pde
621 lines (477 loc) · 17.7 KB
/
WavFile.pde
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import java.io.*;
enum IOState {READING, WRITING, CLOSED};
static class WavFile
{
private final static int BUFFER_SIZE = 4096;
private final static int FMT_CHUNK_ID = 0x20746D66;
private final static int DATA_CHUNK_ID = 0x61746164;
private final static int RIFF_CHUNK_ID = 0x46464952;
private final static int RIFF_TYPE_ID = 0x45564157;
private File file;
private IOState ioState;
private int bytesPerSample;
private long numFrames;
private FileOutputStream oStream;
private FileInputStream iStream;
private double floatScale;
private double floatOffset;
private boolean wordAlignAdjust;
// Wav Header
private int numChannels;
private long sampleRate;
private int blockAlign;
private int validBits;
// Buffering
private byte[] buffer;
private int bufferPointer;
private int bytesRead;
private long frameCounter;
private WavFile()
{
buffer = new byte[BUFFER_SIZE];
}
public int getNumChannels()
{
return numChannels;
}
public long getNumFrames()
{
return numFrames;
}
public long getFramesRemaining()
{
return numFrames - frameCounter;
}
public long getSampleRate()
{
return sampleRate;
}
public int getValidBits()
{
return validBits;
}
public static WavFile newWavFile(File file, int numChannels, long numFrames, int validBits, long sampleRate) throws IOException, WavFileException
{
WavFile wavFile = new WavFile();
wavFile.file = file;
wavFile.numChannels = numChannels;
wavFile.numFrames = numFrames;
wavFile.sampleRate = sampleRate;
wavFile.bytesPerSample = (validBits + 7) / 8;
wavFile.blockAlign = wavFile.bytesPerSample * numChannels;
wavFile.validBits = validBits;
if (numChannels < 1 || numChannels > 65535) throw new WavFileException("Illegal number of channels, valid range 1 to 65536");
if (numFrames < 0) throw new WavFileException("Number of frames must be positive");
if (validBits < 2 || validBits > 65535) throw new WavFileException("Illegal number of valid bits, valid range 2 to 65536");
if (sampleRate < 0) throw new WavFileException("Sample rate must be positive");
wavFile.oStream = new FileOutputStream(file);
long dataChunkSize = wavFile.blockAlign * numFrames;
long mainChunkSize = 4 +
8 +
16 +
8 +
dataChunkSize;
if (dataChunkSize % 2 == 1) {
mainChunkSize += 1;
wavFile.wordAlignAdjust = true;
}
else {
wavFile.wordAlignAdjust = false;
}
putLE(RIFF_CHUNK_ID, wavFile.buffer, 0, 4);
putLE(mainChunkSize, wavFile.buffer, 4, 4);
putLE(RIFF_TYPE_ID, wavFile.buffer, 8, 4);
wavFile.oStream.write(wavFile.buffer, 0, 12);
long averageBytesPerSecond = sampleRate * wavFile.blockAlign;
putLE(FMT_CHUNK_ID, wavFile.buffer, 0, 4);
putLE(16, wavFile.buffer, 4, 4);
putLE(1, wavFile.buffer, 8, 2);
putLE(numChannels, wavFile.buffer, 10, 2);
putLE(sampleRate, wavFile.buffer, 12, 4);
putLE(averageBytesPerSecond, wavFile.buffer, 16, 4);
putLE(wavFile.blockAlign, wavFile.buffer, 20, 2);
putLE(validBits, wavFile.buffer, 22, 2);
wavFile.oStream.write(wavFile.buffer, 0, 24);
putLE(DATA_CHUNK_ID, wavFile.buffer, 0, 4);
putLE(dataChunkSize, wavFile.buffer, 4, 4);
wavFile.oStream.write(wavFile.buffer, 0, 8);
if (wavFile.validBits > 8)
{
wavFile.floatOffset = 0;
wavFile.floatScale = Long.MAX_VALUE >> (64 - wavFile.validBits);
}
else
{
wavFile.floatOffset = 1;
wavFile.floatScale = 0.5 * ((1 << wavFile.validBits) - 1);
}
wavFile.bufferPointer = 0;
wavFile.bytesRead = 0;
wavFile.frameCounter = 0;
wavFile.ioState = IOState.WRITING;
return wavFile;
}
public WavFile openWavFile(File file) throws IOException, WavFileException
{
WavFile wavFile = new WavFile();
wavFile.file = file;
wavFile.iStream = new FileInputStream(file);
int bytesRead = wavFile.iStream.read(wavFile.buffer, 0, 12);
if (bytesRead != 12) throw new WavFileException("Not enough wav file bytes for header");
long riffChunkID = getLE(wavFile.buffer, 0, 4);
long chunkSize = getLE(wavFile.buffer, 4, 4);
long riffTypeID = getLE(wavFile.buffer, 8, 4);
if (riffChunkID != RIFF_CHUNK_ID) throw new WavFileException("Invalid Wav Header data, incorrect riff chunk ID");
if (riffTypeID != RIFF_TYPE_ID) throw new WavFileException("Invalid Wav Header data, incorrect riff type ID");
if (file.length() != chunkSize+8) {
throw new WavFileException("Header chunk size (" + chunkSize + ") does not match file size (" + file.length() + ")");
}
boolean foundFormat = false;
boolean foundData = false;
while (true)
{
bytesRead = wavFile.iStream.read(wavFile.buffer, 0, 8);
if (bytesRead == -1) throw new WavFileException("Reached end of file without finding format chunk");
if (bytesRead != 8) throw new WavFileException("Could not read chunk header");
long chunkID = getLE(wavFile.buffer, 0, 4);
chunkSize = getLE(wavFile.buffer, 4, 4);
long numChunkBytes = (chunkSize%2 == 1) ? chunkSize+1 : chunkSize;
if (chunkID == FMT_CHUNK_ID)
{
foundFormat = true;
bytesRead = wavFile.iStream.read(wavFile.buffer, 0, 16);
int compressionCode = (int) getLE(wavFile.buffer, 0, 2);
if (compressionCode != 1) throw new WavFileException("Compression Code " + compressionCode + " not supported");
wavFile.numChannels = (int) getLE(wavFile.buffer, 2, 2);
wavFile.sampleRate = getLE(wavFile.buffer, 4, 4);
wavFile.blockAlign = (int) getLE(wavFile.buffer, 12, 2);
wavFile.validBits = (int) getLE(wavFile.buffer, 14, 2);
if (wavFile.numChannels == 0) throw new WavFileException("Number of channels specified in header is equal to zero");
if (wavFile.blockAlign == 0) throw new WavFileException("Block Align specified in header is equal to zero");
if (wavFile.validBits < 2) throw new WavFileException("Valid Bits specified in header is less than 2");
if (wavFile.validBits > 64) throw new WavFileException("Valid Bits specified in header is greater than 64, this is greater than a long can hold");
wavFile.bytesPerSample = (wavFile.validBits + 7) / 8;
if (wavFile.bytesPerSample * wavFile.numChannels != wavFile.blockAlign)
throw new WavFileException("Block Align does not agree with bytes required for validBits and number of channels");
numChunkBytes -= 16;
if (numChunkBytes > 0) wavFile.iStream.skip(numChunkBytes);
}
else if (chunkID == DATA_CHUNK_ID)
{
if (foundFormat == false) throw new WavFileException("Data chunk found before Format chunk");
if (chunkSize % wavFile.blockAlign != 0) throw new WavFileException("Data Chunk size is not multiple of Block Align");
wavFile.numFrames = chunkSize / wavFile.blockAlign;
foundData = true;
break;
}
else
{
wavFile.iStream.skip(numChunkBytes);
}
}
if (foundData == false) throw new WavFileException("Did not find a data chunk");
if (wavFile.validBits > 8)
{
wavFile.floatOffset = 0;
wavFile.floatScale = 1 << (wavFile.validBits - 1);
}
else
{
wavFile.floatOffset = -1;
wavFile.floatScale = 0.5 * ((1 << wavFile.validBits) - 1);
}
wavFile.bufferPointer = 0;
wavFile.bytesRead = 0;
wavFile.frameCounter = 0;
wavFile.ioState = IOState.READING;
return wavFile;
}
private long getLE(byte[] buffer, int pos, int numBytes)
{
numBytes --;
pos += numBytes;
long val = buffer[pos] & 0xFF;
for (int b=0 ; b<numBytes ; b++) val = (val << 8) + (buffer[--pos] & 0xFF);
return val;
}
private static void putLE(long val, byte[] buffer, int pos, int numBytes)
{
for (int b=0 ; b<numBytes ; b++)
{
buffer[pos] = (byte) (val & 0xFF);
val >>= 8;
pos ++;
}
}
private void writeSample(long val) throws IOException
{
for (int b=0 ; b<bytesPerSample ; b++)
{
if (bufferPointer == BUFFER_SIZE)
{
oStream.write(buffer, 0, BUFFER_SIZE);
bufferPointer = 0;
}
buffer[bufferPointer] = (byte) (val & 0xFF);
val >>= 8;
bufferPointer ++;
}
}
private long readSample() throws IOException, WavFileException
{
long val = 0;
for (int b=0 ; b<bytesPerSample ; b++)
{
if (bufferPointer == bytesRead)
{
int read = iStream.read(buffer, 0, BUFFER_SIZE);
if (read == -1) throw new WavFileException("Not enough data available");
bytesRead = read;
bufferPointer = 0;
}
int v = buffer[bufferPointer];
if (b < bytesPerSample-1 || bytesPerSample == 1) v &= 0xFF;
val += v << (b * 8);
bufferPointer ++;
}
return val;
}
public int readFrames(int[] sampleBuffer, int numFramesToRead) throws IOException, WavFileException
{
return readFrames(sampleBuffer, 0, numFramesToRead);
}
public int readFrames(int[] sampleBuffer, int offset, int numFramesToRead) throws IOException, WavFileException
{
if (ioState != IOState.READING) throw new IOException("Cannot read from WavFile instance");
for (int f=0 ; f<numFramesToRead ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++)
{
sampleBuffer[offset] = (int) readSample();
offset ++;
}
frameCounter ++;
}
return numFramesToRead;
}
public int readFrames(int[][] sampleBuffer, int numFramesToRead) throws IOException, WavFileException
{
return readFrames(sampleBuffer, 0, numFramesToRead);
}
public int readFrames(int[][] sampleBuffer, int offset, int numFramesToRead) throws IOException, WavFileException
{
if (ioState != IOState.READING) throw new IOException("Cannot read from WavFile instance");
for (int f=0 ; f<numFramesToRead ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++) sampleBuffer[c][offset] = (int) readSample();
offset ++;
frameCounter ++;
}
return numFramesToRead;
}
public int writeFrames(int[] sampleBuffer, int numFramesToWrite) throws IOException, WavFileException
{
return writeFrames(sampleBuffer, 0, numFramesToWrite);
}
public int writeFrames(int[] sampleBuffer, int offset, int numFramesToWrite) throws IOException, WavFileException
{
if (ioState != IOState.WRITING) throw new IOException("Cannot write to WavFile instance");
for (int f=0 ; f<numFramesToWrite ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++)
{
writeSample(sampleBuffer[offset]);
offset ++;
}
frameCounter ++;
}
return numFramesToWrite;
}
public int writeFrames(int[][] sampleBuffer, int numFramesToWrite) throws IOException, WavFileException
{
return writeFrames(sampleBuffer, 0, numFramesToWrite);
}
public int writeFrames(int[][] sampleBuffer, int offset, int numFramesToWrite) throws IOException, WavFileException
{
if (ioState != IOState.WRITING) throw new IOException("Cannot write to WavFile instance");
for (int f=0 ; f<numFramesToWrite ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++) writeSample(sampleBuffer[c][offset]);
offset ++;
frameCounter ++;
}
return numFramesToWrite;
}
public int readFrames(long[] sampleBuffer, int numFramesToRead) throws IOException, WavFileException
{
return readFrames(sampleBuffer, 0, numFramesToRead);
}
public int readFrames(long[] sampleBuffer, int offset, int numFramesToRead) throws IOException, WavFileException
{
if (ioState != IOState.READING) throw new IOException("Cannot read from WavFile instance");
for (int f=0 ; f<numFramesToRead ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++)
{
sampleBuffer[offset] = readSample();
offset ++;
}
frameCounter ++;
}
return numFramesToRead;
}
public int readFrames(long[][] sampleBuffer, int numFramesToRead) throws IOException, WavFileException
{
return readFrames(sampleBuffer, 0, numFramesToRead);
}
public int readFrames(long[][] sampleBuffer, int offset, int numFramesToRead) throws IOException, WavFileException
{
if (ioState != IOState.READING) throw new IOException("Cannot read from WavFile instance");
for (int f=0 ; f<numFramesToRead ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++) sampleBuffer[c][offset] = readSample();
offset ++;
frameCounter ++;
}
return numFramesToRead;
}
public int writeFrames(long[] sampleBuffer, int numFramesToWrite) throws IOException, WavFileException
{
return writeFrames(sampleBuffer, 0, numFramesToWrite);
}
public int writeFrames(long[] sampleBuffer, int offset, int numFramesToWrite) throws IOException, WavFileException
{
if (ioState != IOState.WRITING) throw new IOException("Cannot write to WavFile instance");
for (int f=0 ; f<numFramesToWrite ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++)
{
writeSample(sampleBuffer[offset]);
offset ++;
}
frameCounter ++;
}
return numFramesToWrite;
}
public int writeFrames(long[][] sampleBuffer, int numFramesToWrite) throws IOException, WavFileException
{
return writeFrames(sampleBuffer, 0, numFramesToWrite);
}
public int writeFrames(long[][] sampleBuffer, int offset, int numFramesToWrite) throws IOException, WavFileException
{
if (ioState != IOState.WRITING) throw new IOException("Cannot write to WavFile instance");
for (int f=0 ; f<numFramesToWrite ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++) writeSample(sampleBuffer[c][offset]);
offset ++;
frameCounter ++;
}
return numFramesToWrite;
}
public int readFrames(double[] sampleBuffer, int numFramesToRead) throws IOException, WavFileException
{
return readFrames(sampleBuffer, 0, numFramesToRead);
}
public int readFrames(double[] sampleBuffer, int offset, int numFramesToRead) throws IOException, WavFileException
{
if (ioState != IOState.READING) throw new IOException("Cannot read from WavFile instance");
for (int f=0 ; f<numFramesToRead ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++)
{
sampleBuffer[offset] = floatOffset + (double) readSample() / floatScale;
offset ++;
}
frameCounter ++;
}
return numFramesToRead;
}
public int readFrames(double[][] sampleBuffer, int numFramesToRead) throws IOException, WavFileException
{
return readFrames(sampleBuffer, 0, numFramesToRead);
}
public int readFrames(double[][] sampleBuffer, int offset, int numFramesToRead) throws IOException, WavFileException
{
if (ioState != IOState.READING) throw new IOException("Cannot read from WavFile instance");
for (int f=0 ; f<numFramesToRead ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++) sampleBuffer[c][offset] = floatOffset + (double) readSample() / floatScale;
offset ++;
frameCounter ++;
}
return numFramesToRead;
}
public int writeFrames(double[] sampleBuffer, int numFramesToWrite) throws IOException, WavFileException
{
return writeFrames(sampleBuffer, 0, numFramesToWrite);
}
public int writeFrames(double[] sampleBuffer, int offset, int numFramesToWrite) throws IOException, WavFileException
{
if (ioState != IOState.WRITING) throw new IOException("Cannot write to WavFile instance");
for (int f=0 ; f<numFramesToWrite ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++)
{
writeSample((long) (floatScale * (floatOffset + sampleBuffer[offset])));
offset ++;
}
frameCounter ++;
}
return numFramesToWrite;
}
public int writeFrames(double[][] sampleBuffer, int numFramesToWrite) throws IOException, WavFileException
{
return writeFrames(sampleBuffer, 0, numFramesToWrite);
}
public int writeFrames(double[][] sampleBuffer, int offset, int numFramesToWrite) throws IOException, WavFileException
{
if (ioState != IOState.WRITING) throw new IOException("Cannot write to WavFile instance");
for (int f=0 ; f<numFramesToWrite ; f++)
{
if (frameCounter == numFrames) return f;
for (int c=0 ; c<numChannels ; c++) writeSample((long) (floatScale * (floatOffset + sampleBuffer[c][offset])));
offset ++;
frameCounter ++;
}
return numFramesToWrite;
}
public void close() throws IOException
{
if (iStream != null)
{
iStream.close();
iStream = null;
}
if (oStream != null)
{
if (bufferPointer > 0) oStream.write(buffer, 0, bufferPointer);
if (wordAlignAdjust) oStream.write(0);
oStream.close();
oStream = null;
}
ioState = IOState.CLOSED;
}
public void display()
{
display(System.out);
}
public void display(PrintStream out)
{
out.printf("File: %s\n", file);
out.printf("Channels: %d, Frames: %d\n", numChannels, numFrames);
out.printf("IO State: %s\n", ioState);
out.printf("Sample Rate: %d, Block Align: %d\n", sampleRate, blockAlign);
out.printf("Valid Bits: %d, Bytes per sample: %d\n", validBits, bytesPerSample);
}
}