summaryrefslogtreecommitdiffstats
path: root/src/riff.c
blob: 40aae18d7b5d250bb68eec8f90e08579655dbf44 (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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

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

struct _xas_riff {
    int fd;
    size_t size;

    size_t sample_size,
           sample_rate,
           channels;
};

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

            .type = "WAVE"
        },

        .wave = {
            {
                .id   = "fmt ",
                .size = 16
            },

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

        {
            .id   = "data",
            .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 *file_open(const char *path,
                             size_t sample_size,
                             size_t sample_rate,
                             size_t channels,
                             int flags) {
    xas_riff *riff;

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

    riff->fd = (flags & O_CREAT)? open(path, flags, 0644):
                                  open(path, flags);

    if (riff->fd < 0) {
        goto error_open;
    }

    riff->size        = 0;
    riff->sample_size = sample_size;
    riff->sample_rate = sample_rate;
    riff->channels    = channels;

    if (flags & (O_CREAT | O_TRUNC)) {
        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 void file_close(xas_riff *riff, xas_audio_stream *stream) {
    if (lseek(riff->fd, 0, SEEK_SET) < 0) {
        goto error_io;
    }

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

    (void)close(riff->fd);

    free(riff);

error_header_write:
error_io:
    return;
}

static int audio_drain(xas_riff *riff,
                       void *samples,
                       size_t count,
                       xas_audio_stream *stream) {
    size_t len = count * stream->sample_size
                       * stream->channels;

    ssize_t wrlen;

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

    riff->size += wrlen;

    return wrlen / stream->sample_size / stream->channels;

error_write:
    return -1;
}

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

    ssize_t rdlen;

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

    return rdlen / stream->sample_size / stream->channels;

error_read:
    return -1;
}

xas_audio_stream *xas_riff_file_open(const char *path,
                                         size_t sample_size,
                                         size_t sample_rate,
                                         size_t channels,
                                         int flags) {
    xas_audio_stream *stream;
    xas_riff *riff;

    if ((riff = file_open(path,
                          sample_size,
                          sample_rate,
                          channels,
                          flags)) == NULL) {
        goto error_file_open;
    }

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

    return stream;

error_audio_stream_new_sink:
    file_close(riff, NULL);

error_file_open:
    return NULL;
}