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
|
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <xas/audio.h>
int xas_audio_format_eq(xas_audio_format a, xas_audio_format b) {
return a.channels == b.channels
&& a.sample_size == b.sample_size
&& a.sample_rate == b.sample_rate;
}
void xas_audio_zero(xas_audio_format format,
void *dest,
size_t index,
size_t count) {
size_t stride = format.channels * format.sample_size;
memset((uint8_t *)dest + stride * index,
'\0',
stride * count);
}
void xas_audio_copy(xas_audio_format format,
void *dest,
void *src,
size_t index_dest,
size_t index_src,
size_t count) {
size_t stride = format.channels * format.sample_size;
memcpy(((uint8_t *)dest) + stride * index_dest,
((uint8_t *)src) + stride * index_src,
stride * count);
}
static inline void apply_gain_mono(int16_t *samples,
float gain,
size_t index,
size_t count) {
size_t i;
for (i=index; i<count; i++) {
samples[i] *= (int16_t)roundf(gain * samples[i]);
}
}
static inline void apply_gain_stereo(int16_t *samples,
float gain,
size_t index,
size_t count) {
size_t i;
for (i=index; i<count; i++) {
samples[i*2] *= (int16_t)roundf(gain * samples[i*2]);
samples[i*2+1] *= (int16_t)roundf(gain * samples[i*2+1]);
}
}
void xas_audio_apply_gain(xas_audio_format format,
void *dest,
float gain,
size_t index,
size_t count) {
if (format.channels == XAS_AUDIO_MONO) {
apply_gain_mono(dest, gain, index, count);
} else if (format.channels == XAS_AUDIO_STEREO) {
apply_gain_stereo(dest, gain, index, count);
}
}
static xas_audio_stream *stream_new(enum xas_audio_stream_type type,
void *callback,
xas_audio_cleanup cleanup,
xas_audio_format format,
size_t buffer_size,
void *ctx) {
xas_audio_stream *stream;
size_t total = sizeof(xas_audio_stream)
+ format.channels * format.sample_size * buffer_size;
if ((stream = malloc(total)) == NULL) {
goto error_malloc_stream;
}
stream->type = type;
stream->format.channels = format.channels;
stream->format.sample_size = format.sample_size;
stream->format.sample_rate = format.sample_rate;
stream->buffer_size = buffer_size;
stream->buffer_count = 0;
stream->callback = callback;
stream->cleanup = cleanup;
stream->ctx = ctx;
return stream;
error_malloc_stream:
return NULL;
}
xas_audio_stream *xas_audio_stream_new_sink(xas_audio_drain drain,
xas_audio_cleanup cleanup,
xas_audio_format format,
size_t buffer_size,
void *ctx) {
return stream_new(XAS_AUDIO_STREAM_SINK,
drain,
cleanup,
format,
buffer_size,
ctx);
}
xas_audio_stream *xas_audio_stream_new_source(xas_audio_fill fill,
xas_audio_cleanup cleanup,
xas_audio_format format,
size_t buffer_size,
void *ctx) {
return stream_new(XAS_AUDIO_STREAM_SOURCE,
fill,
cleanup,
format,
buffer_size,
ctx);
}
void xas_audio_stream_destroy(xas_audio_stream *stream) {
if (stream->cleanup) {
stream->cleanup(stream->ctx);
}
free(stream);
}
void *xas_audio_stream_buffer(xas_audio_stream *stream) {
return stream + 1;
}
static inline int stream_flush(xas_audio_stream *sink) {
if (sink->buffer_count == 0) {
return 0;
}
if (sink->drain(sink->ctx,
sink + 1,
sink->buffer_count) < 0) {
goto error_sink_drain;
}
sink->buffer_count = 0;
return 0;
error_sink_drain:
return -1;
}
static inline void *ptr(xas_audio_stream *stream, void *buf, size_t index) {
return ((uint8_t *)buf)
+ stream->format.channels * stream->format.sample_size * index;
}
ssize_t xas_audio_stream_write(xas_audio_stream *sink,
void *samples,
size_t count) {
size_t index_i = 0;
if (sink->buffer_count + count > sink->buffer_size) {
/*
* If the number of samples offered, plus the number of items currently
* in the buffer exceeds the buffer size, fill the buffer to capacity
* and flush it.
*/
size_t remaining = sink->buffer_size - sink->buffer_count;
xas_audio_copy(sink->format,
sink+ 1,
samples,
sink->buffer_count,
index_i,
remaining);
if (sink->drain(sink->ctx,
sink + 1,
sink->buffer_size) < 0) {
goto error_sink_drain;
}
index_i += remaining;
sink->buffer_count = 0;
}
/*
* While there are still samples to buffer or flush...
*/
while (index_i < count) {
size_t remaining = count - index_i;
if (remaining >= sink->buffer_size) {
/*
* If the number of samples remaining is greater than the target
* buffer size, then drain directly from the source buffer to the
* output an amount equal to the buffer size.
*/
if (sink->drain(sink->ctx,
ptr(sink, samples, index_i),
sink->buffer_size) < 0) {
goto error_sink_drain;
}
index_i += sink->buffer_size;
} else {
/*
* Enough of the input has been drained that it can be copied to
* the target buffer.
*/
xas_audio_copy(sink->format,
sink + 1,
samples,
sink->buffer_count,
index_i,
remaining);
index_i += remaining;
sink->buffer_count += remaining;
}
}
return index_i;
error_sink_drain:
return -1;
}
int xas_audio_stream_flush(xas_audio_stream *sink) {
if (sink->type == XAS_AUDIO_STREAM_SINK) {
return stream_flush(sink);
}
errno = EINVAL;
return -1;
}
ssize_t xas_audio_stream_read(xas_audio_stream *source,
void **samples,
size_t count) {
*samples = source + 1;
if (count > source->buffer_size) {
count = source->buffer_size;
}
return source->fill(source->ctx,
*samples,
count);
}
|