summaryrefslogtreecommitdiffstats
path: root/src/riff.c
blob: fb895b6566e486af627172491f0f69feebc29d9c (plain)
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
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <xas/audio.h>
#include <xas/riff.h>

struct _xas_riff {
    const char *file;
    int fd;
    size_t size;

    xas_audio_format format;
};

static int header_write(xas_riff *riff) {
    xas_riff_wave_header header = {
        .riff = {
            {
                .id    = XAS_RIFF_HEADER_MAIN_ID,
                .size = sizeof(xas_riff_main_chunk)
                      + sizeof(xas_riff_wave_chunk)
                      + riff->size
            },

            .type = XAS_RIFF_HEADER_WAVE_ID,
        },

        .wave = {
            {
                .id   = XAS_RIFF_HEADER_WAVE_FMT_ID,
                .size = sizeof(xas_riff_wave_chunk)
                      - sizeof(xas_riff_chunk)
            },

            .type             = XAS_RIFF_WAVE_DEFAULT_TYPE,
            .channels         = riff->format.channels,
            .sample_rate      = riff->format.sample_rate,
            .sample_size      = riff->format.sample_size,
            .sample_size_bits = riff->format.sample_size << 3,
            .byte_rate        = riff->format.channels
                              * riff->format.sample_size
                              * riff->format.sample_rate
        },

        {
            .id   = XAS_RIFF_HEADER_WAVE_DATA_ID,
            .size = riff->size
        }
    };

    if (lseek(riff->fd, 0, SEEK_SET) < 0) {
        goto error_io;
    }

    if (write(riff->fd, &header, sizeof(header)) < 0) {
        goto error_io;
    }

    if (lseek(riff->fd, 0, SEEK_END) < 0) {
        goto error_io;
    }

    return 0;

error_io:
    return -1;
}

static xas_riff *new_file(const char *path,
                            xas_audio_format format,
                            int flags) {
    xas_riff *riff;

    if ((riff = malloc(sizeof(*riff))) == NULL) {
        goto error_malloc_riff;
    }

    flags |= O_CREAT;

    if ((riff->fd = open(path, flags, 0644)) < 0) {
        goto error_open;
    }

    riff->file = path;
    riff->size = 0;

    riff->format.channels    = format.channels;
    riff->format.sample_size = format.sample_size;
    riff->format.sample_rate = format.sample_rate;

    if (header_write(riff) < 0) {
        goto error_header_write;
    }

    return riff;

error_header_write:
    close(riff->fd);

error_open:
    free(riff);

error_malloc_riff:
    return NULL;
}

static int wave_header_parse(xas_riff *riff,
                             xas_riff_wave_header *header) {
    if (memcmp(header->riff.header.id,
               XAS_RIFF_HEADER_MAIN_ID,
               strlen(XAS_RIFF_HEADER_MAIN_ID)) != 0) {
        goto error_invalid_header_main_id;
    }

    if (header->riff.header.size < sizeof(xas_riff_main_chunk)
                                 + sizeof(xas_riff_wave_chunk)) {
        goto error_invalid_header_wave_chunk_size;
    }

    if (memcmp(header->riff.type,
               XAS_RIFF_HEADER_WAVE_ID,
               strlen(XAS_RIFF_HEADER_WAVE_ID)) != 0) {
        goto error_invalid_wave_chunk_id;
    }

    if (memcmp(header->wave.header.id,
               XAS_RIFF_HEADER_WAVE_FMT_ID,
               strlen(XAS_RIFF_HEADER_WAVE_FMT_ID)) != 0) {
        goto error_invalid_wave_format_id;
    }

    if (header->wave.header.size != sizeof(xas_riff_wave_chunk)
                                  - sizeof(xas_riff_chunk)) {
        goto error_invalid_wave_format_size;
    }

    if (header->wave.type != XAS_RIFF_WAVE_DEFAULT_TYPE) {
        goto error_invalid_wave_format_type;
    }

    switch (header->wave.channels) {
        case XAS_AUDIO_MONO:
        case XAS_AUDIO_STEREO:
            break;

        default:
            goto error_invalid_wave_channels;
    }

    if (header->wave.byte_rate != header->wave.channels
                                * header->wave.sample_size
                                * header->wave.sample_rate) {
        goto error_invalid_wave_byte_rate;
    }

    if (header->wave.sample_size_bits != header->wave.sample_size << 3) {
        goto error_invalid_wave_sample_size_bits;
    }

    if (memcmp(header->data.id,
               XAS_RIFF_HEADER_WAVE_DATA_ID,
               strlen(XAS_RIFF_HEADER_WAVE_DATA_ID)) != 0) {
        goto error_invalid_wave_data_format_id;
    }

    riff->format.channels    = header->wave.channels;
    riff->format.sample_size = header->wave.sample_size;
    riff->format.sample_rate = header->wave.sample_rate;

    return 0;

error_invalid_wave_data_format_id:
error_invalid_wave_sample_size_bits:
error_invalid_wave_byte_rate:
error_invalid_wave_channels:
error_invalid_wave_format_type:
error_invalid_wave_format_size:
error_invalid_wave_format_id:
error_invalid_wave_chunk_id:
error_invalid_header_wave_chunk_size:
error_invalid_header_main_id:
    return -1;
}

static xas_riff *open_fd(int fd) {
    xas_riff *riff;
    xas_riff_wave_header header;

    ssize_t readlen;

    if ((riff = malloc(sizeof(*riff))) == NULL) {
        goto error_malloc_riff;
    }

    riff->file = NULL;
    riff->fd   = fd;
    riff->size = 0;

    riff->format.channels    = 0;
    riff->format.sample_size = 0;
    riff->format.sample_rate = 0;

    if ((readlen = read(fd, &header, sizeof(header))) < 0) {
        goto error_read;
    }

    if (readlen != sizeof(header)) {
        errno= EINVAL;

        goto error_wave_header_short;
    }

    if (wave_header_parse(riff, &header) < 0) {
        goto error_wave_header_parse;
    }

    return riff;

error_wave_header_parse:
error_wave_header_short:
error_read:
    free(riff);

error_malloc_riff:
    return NULL;
}

static xas_riff *open_file(const char *path, int flags) {
    xas_riff *riff;
    xas_riff_wave_header header;

    ssize_t readlen;

    if ((riff = malloc(sizeof(*riff))) == NULL) {
        goto error_malloc_riff;
    }

    flags &= ~(O_CREAT | O_TRUNC);

    if ((riff->fd = open(path, flags)) < 0) {
        goto error_open;
    }

    riff->file = path;
    riff->size = 0;

    riff->format.channels    = 0;
    riff->format.sample_size = 0;
    riff->format.sample_rate = 0;

    if (lseek(riff->fd, 0, SEEK_SET) < 0) {
        goto error_lseek;
    }

    if ((readlen = read(riff->fd, &header, sizeof(header))) < 0) {
        goto error_read;
    }

    if (readlen != sizeof(header)) {
        errno= EINVAL;

        goto error_wave_header_short;
    }

    if (wave_header_parse(riff, &header) < 0) {
        goto error_wave_header_parse;
    }

    return riff;

error_wave_header_parse:
error_wave_header_short:
error_read:
error_lseek:
    close(riff->fd);

error_open:
    free(riff);

error_malloc_riff:
    return NULL;
}

static void close_file(xas_riff *riff) {
    if (lseek(riff->fd, 0, SEEK_SET) == 0) {
        (void)header_write(riff);
    }

    if (riff->file) {
        (void)close(riff->fd);
    }

    free(riff);
}

static ssize_t audio_drain(xas_riff *riff,
                           void *samples,
                           size_t count) {
    size_t stride = riff->format.channels * riff->format.sample_size,
           len    = stride * count;

    ssize_t wrlen;

    if ((wrlen = write(riff->fd, samples, len)) < 0) {
        goto error_write;
    }

    riff->size += wrlen;

    return wrlen / stride;

error_write:
    return -1;
}

static ssize_t audio_fill(xas_riff *riff,
                          void *samples,
                          size_t count) {
    size_t stride = riff->format.channels * riff->format.sample_size,
           len    = count * stride;

    ssize_t rdlen;

    if ((rdlen = read(riff->fd, samples, len)) < 0) {
        goto error_read;
    }

    return rdlen / stride;

error_read:
    return -1;
}

xas_audio_stream *xas_riff_new_file(const char *path,
                                        xas_audio_format format,
                                        int flags) {
    xas_audio_stream *stream;
    xas_riff *riff;

    if ((riff = new_file(path, format, flags)) == NULL) {
        goto error_new_file;
    }

    if (flags & (O_RDWR | O_WRONLY)) {
        if ((stream = xas_audio_stream_new_sink((xas_audio_drain)audio_drain,
                                                  (xas_audio_cleanup)close_file,
                                                  format,
                                                  4096,
                                                  riff)) == NULL) {
            goto error_audio_stream_new_sink;
        }
    } else {
        if ((stream = xas_audio_stream_new_source((xas_audio_fill)audio_fill,
                                                    (xas_audio_cleanup)close_file,
                                                    format,
                                                    4096,
                                                    riff)) == NULL) {
            goto error_audio_stream_new_sink;
        }
    }

    return stream;

error_audio_stream_new_sink:
    close_file(riff);

error_new_file:
    return NULL;
}

xas_audio_stream *xas_riff_open_file(const char *path, int flags) {
    xas_audio_stream *stream;
    xas_riff *riff;

    if ((riff = open_file(path, flags)) == NULL) {
        goto error_open_file;
    }

    if (flags & (O_RDWR | O_WRONLY)) {
        if ((stream = xas_audio_stream_new_sink((xas_audio_drain)audio_drain,
                                                  (xas_audio_cleanup)close_file,
                                                  riff->format,
                                                  4096,
                                                  riff)) == NULL) {
            goto error_audio_stream_new_sink;
        }
    } else {
        if ((stream = xas_audio_stream_new_source((xas_audio_fill)audio_fill,
                                                    (xas_audio_cleanup)close_file,
                                                    riff->format,
                                                    4096,
                                                    riff)) == NULL) {
            goto error_audio_stream_new_sink;
        }
    }

    return stream;

error_audio_stream_new_sink:
    close_file(riff);

error_open_file:
    return NULL;
}

xas_audio_stream *xas_riff_open_fd(int fd) {
    xas_audio_stream *stream;
    xas_riff *riff;

    if ((riff = open_fd(fd)) == NULL) {
        goto error_open_fd;
    }

    if ((stream = xas_audio_stream_new_source((xas_audio_fill)audio_fill,
                                                (xas_audio_cleanup)close_file,
                                                riff->format,
                                                4096,
                                                riff)) == NULL) {
        goto error_audio_stream_new_sink;
    }

    return stream;

error_audio_stream_new_sink:
    free(riff);

error_open_fd:
    return NULL;
}