forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav.d
421 lines (337 loc) · 10.4 KB
/
wav.d
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
/++
Basic .wav file reading and writing.
History:
Written May 15, 2020, but loosely based on code I wrote a
long time ago, at least August 2008 which is the oldest
file I have generated from the original code.
The old code could only write files, the reading support
was all added in 2020.
+/
module arsd.wav;
import core.stdc.stdio;
/++
+/
struct WavWriter {
private FILE* fp;
/++
Opens the file with the given header params.
Make sure you pass the correct params to header, except,
if you have a seekable stream, the data length can be zero
and it will be fixed when you close. If you have a non-seekable
stream though, you must give the size up front.
If you need to go to memory, the best way is to just
append your data to your own buffer, then create a [WavFileHeader]
separately and prepend it. Wav files are simple, aside from
the header and maybe a terminating byte (which isn't really important
anyway), there's nothing special going on.
Throws: Exception on error from [open].
---
auto writer = WavWriter("myfile.wav", WavFileHeader(44100, 2, 16));
writer.write(shortSamples);
---
+/
this(string filename, WavFileHeader header) {
this.header = header;
if(!open(filename))
throw new Exception("Couldn't open file for writing"); // FIXME: errno
}
/++
`WavWriter(WavFileHeader(44100, 2, 16));`
+/
this(WavFileHeader header) @nogc nothrow {
this.header = header;
}
/++
Calls [close]. Errors are ignored.
+/
~this() @nogc {
close();
}
@disable this(this);
private uint size;
private WavFileHeader header;
@nogc:
/++
Returns: true on success, false on error. Check errno for details.
+/
bool open(string filename) {
assert(fp is null);
assert(filename.length < 290);
char[300] fn;
fn[0 .. filename.length] = filename[];
fn[filename.length] = 0;
fp = fopen(fn.ptr, "wb");
if(fp is null)
return false;
if(fwrite(&header, header.sizeof, 1, fp) != 1)
return false;
return true;
}
/++
Writes 8-bit samples to the file. You must have constructed the object with an 8 bit header.
Returns: true on success, false on error. Check errno for details.
+/
bool write(ubyte[] data) {
assert(header.bitsPerSample == 8);
if(fp is null)
return false;
if(fwrite(data.ptr, 1, data.length, fp) != data.length)
return false;
size += data.length;
return true;
}
/++
Writes 16-bit samples to the file. You must have constructed the object with 16 bit header.
Returns: true on success, false on error. Check errno for details.
+/
bool write(short[] data) {
assert(header.bitsPerSample == 16);
if(fp is null)
return false;
if(fwrite(data.ptr, 2, data.length, fp) != data.length)
return false;
size += data.length * 2;
return true;
}
/++
Returns: true on success, false on error. Check errno for details.
+/
bool close() {
if(fp is null)
return true;
// pad odd sized file as required by spec...
if(size & 1) {
fputc(0, fp);
}
if(!header.dataLength) {
// put the length back at the beginning of the file
if(fseek(fp, 0, SEEK_SET) != 0)
return false;
auto n = header.withDataLengthInBytes(size);
if(fwrite(&n, 1, n.sizeof, fp) != 1)
return false;
} else {
assert(header.dataLength == size);
}
if(fclose(fp))
return false;
fp = null;
return true;
}
}
version(LittleEndian) {} else static assert(0, "just needs endian conversion coded in but i was lazy");
align(1)
///
struct WavFileHeader {
align(1):
const ubyte[4] header = ['R', 'I', 'F', 'F'];
int topSize; // dataLength + 36
const ubyte[4] type = ['W', 'A', 'V', 'E'];
const ubyte[4] fmtHeader = ['f', 'm', 't', ' '];
const int fmtHeaderSize = 16;
const ushort audioFormat = 1; // PCM
ushort numberOfChannels;
uint sampleRate;
uint bytesPerSeconds; // bytesPerSampleTimesChannels * sampleRate
ushort bytesPerSampleTimesChannels; // bitsPerSample * channels / 8
ushort bitsPerSample; // 16
const ubyte[4] dataHeader = ['d', 'a', 't', 'a'];
uint dataLength;
// data follows. put a 0 at the end if dataLength is odd.
///
this(uint sampleRate, ushort numberOfChannels, ushort bitsPerSample, uint dataLengthInBytes = 0) @nogc pure @safe nothrow {
assert(bitsPerSample == 8 || bitsPerSample == 16);
this.numberOfChannels = numberOfChannels;
this.sampleRate = sampleRate;
this.bitsPerSample = bitsPerSample;
this.bytesPerSampleTimesChannels = cast(ushort) (numberOfChannels * bitsPerSample / 8);
this.bytesPerSeconds = this.bytesPerSampleTimesChannels * sampleRate;
this.topSize = dataLengthInBytes + 36;
this.dataLength = dataLengthInBytes;
}
///
WavFileHeader withDataLengthInBytes(int dataLengthInBytes) const @nogc pure @safe nothrow {
return WavFileHeader(sampleRate, numberOfChannels, bitsPerSample, dataLengthInBytes);
}
}
static assert(WavFileHeader.sizeof == 44);
/++
After construction, the parameters are set and you can set them.
After that, you process the samples range-style.
It ignores chunks in the file that aren't the basic standard.
It throws exceptions if it isn't a bare-basic PCM wav file.
See [wavReader] for the convenience constructors.
Note that if you are reading a 16 bit file (`bitsPerSample == 16`),
you'll actually need to `cast(short[]) front`.
---
auto reader = wavReader(data[]);
foreach(chunk; reader)
play(chunk);
---
+/
struct WavReader(Range) {
const ushort numberOfChannels;
const int sampleRate;
const ushort bitsPerSample;
int dataLength; // don't modify plz
private uint remainingDataLength;
private Range underlying;
private const(ubyte)[] frontBuffer;
static if(is(Range == CFileChunks)) {
this(FILE* fp) {
underlying = CFileChunks(fp);
this(0);
}
} else {
this(Range r) {
this.underlying = r;
this(0);
}
}
private this(int _initializationDummyVariable) {
this.frontBuffer = underlying.front;
WavFileHeader header;
ubyte[] headerBytes = (cast(ubyte*) &header)[0 .. header.sizeof - 8];
if(this.frontBuffer.length >= headerBytes.length) {
headerBytes[] = this.frontBuffer[0 .. headerBytes.length];
this.frontBuffer = this.frontBuffer[headerBytes.length .. $];
} else {
throw new Exception("Probably not a wav file, or else pass bigger chunks please");
}
if(header.header != ['R', 'I', 'F', 'F'])
throw new Exception("Not a wav file; no RIFF header");
if(header.type != ['W', 'A', 'V', 'E'])
throw new Exception("Not a wav file");
// so technically the spec does NOT require fmt to be the first chunk..
// but im gonna just be lazy
if(header.fmtHeader != ['f', 'm', 't', ' '])
throw new Exception("Malformed or unsupported wav file");
if(header.fmtHeaderSize < 16)
throw new Exception("Unsupported wav format header");
auto additionalSkip = header.fmtHeaderSize - 16;
if(header.audioFormat != 1)
throw new Exception("arsd.wav only supports the most basic wav files and this one has advanced encoding. try converting to a .mp3 file and use arsd.mp3.");
this.numberOfChannels = header.numberOfChannels;
this.sampleRate = header.sampleRate;
this.bitsPerSample = header.bitsPerSample;
if(header.bytesPerSampleTimesChannels != header.bitsPerSample * header.numberOfChannels / 8)
throw new Exception("Malformed wav file: header.bytesPerSampleTimesChannels didn't match");
if(header.bytesPerSeconds != header.bytesPerSampleTimesChannels * header.sampleRate)
throw new Exception("Malformed wav file: header.bytesPerSeconds didn't match");
this.frontBuffer = this.frontBuffer[additionalSkip .. $];
static struct ChunkHeader {
align(1):
ubyte[4] type;
uint size;
}
static assert(ChunkHeader.sizeof == 8);
ChunkHeader current;
ubyte[] chunkHeader = (cast(ubyte*) ¤t)[0 .. current.sizeof];
another_chunk:
// now we're at the next chunk. want to skip until we hit data.
if(this.frontBuffer.length < chunkHeader.length)
throw new Exception("bug in arsd.wav the chunk isn't big enough to handle and im lazy. if you hit this send me your file plz");
chunkHeader[] = frontBuffer[0 .. chunkHeader.length];
frontBuffer = frontBuffer[chunkHeader.length .. $];
if(current.type != ['d', 'a', 't', 'a']) {
// skip unsupported chunk...
drop_more:
if(frontBuffer.length > current.size) {
frontBuffer = frontBuffer[current.size .. $];
} else {
current.size -= frontBuffer.length;
underlying.popFront();
if(underlying.empty) {
throw new Exception("Ran out of data while trying to read wav chunks");
} else {
frontBuffer = underlying.front;
goto drop_more;
}
}
goto another_chunk;
} else {
this.remainingDataLength = current.size;
}
this.dataLength = this.remainingDataLength;
}
@property const(ubyte)[] front() {
return frontBuffer;
}
version(none)
void consumeBytes(size_t count) {
if(this.frontBuffer.length)
this.frontBuffer = this.frontBuffer[count .. $];
}
void popFront() {
remainingDataLength -= front.length;
underlying.popFront();
if(underlying.empty)
frontBuffer = null;
else
frontBuffer = underlying.front;
}
@property bool empty() {
return remainingDataLength == 0 || this.underlying.empty;
}
}
/++
Convenience constructor for [WavReader]
To read from a file, pass a filename, a FILE*, or a range that
reads chunks from a file.
To read from a memory block, just pass it a `ubyte[]` slice.
+/
WavReader!T wavReader(T)(T t) {
return WavReader!T(t);
}
/// ditto
WavReader!DataBlock wavReader(const(ubyte)[] data) {
return WavReader!DataBlock(DataBlock(data));
}
struct DataBlock {
const(ubyte)[] front;
bool empty() { return front.length == 0; }
void popFront() { front = null; }
}
/// Construct a [WavReader] from a filename.
WavReader!CFileChunks wavReader(string filename) {
assert(filename.length < 290);
char[300] fn;
fn[0 .. filename.length] = filename[];
fn[filename.length] = 0;
auto fp = fopen(fn.ptr, "rb");
if(fp is null)
throw new Exception("wav file unopenable"); // FIXME details
return WavReader!CFileChunks(fp);
}
struct CFileChunks {
FILE* fp;
this(FILE* fp) {
this.fp = fp;
buffer = new ubyte[](4096);
refcount = new int;
*refcount = 1;
popFront();
}
this(this) {
if(refcount !is null)
(*refcount) += 1;
}
~this() {
if(refcount is null) return;
(*refcount) -= 1;
if(*refcount == 0) {
fclose(fp);
}
}
//ubyte[4096] buffer;
ubyte[] buffer;
int* refcount;
ubyte[] front;
void popFront() {
auto got = fread(buffer.ptr, 1, buffer.length, fp);
front = buffer[0 .. got];
}
bool empty() {
return front.length == 0 && (feof(fp) ? true : false);
}
}