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
|
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <xas/riff.h>
#include <xas/bank.h>
xas_bank *xas_bank_new(xas_audio_format format,
size_t entry_size,
size_t entry_count) {
xas_bank *bank;
size_t entry_size_total = sizeof(xas_bank_entry) + format.sample_size
* entry_size;
size_t total = sizeof(xas_bank) + entry_count
* entry_size_total;
if ((bank = malloc(total)) == NULL) {
goto error_malloc_bank;
}
memset(bank, '\0', total);
bank->format.channels = XAS_AUDIO_MONO;
bank->format.sample_size = format.sample_size;
bank->format.sample_rate = format.sample_rate;
bank->entry_size = entry_size;
bank->entry_count = entry_count;
return bank;
error_malloc_bank:
return NULL;
}
void xas_bank_destroy(xas_bank *bank) {
free(bank);
}
ssize_t xas_bank_record(xas_bank *bank,
xas_audio_stream *input,
size_t entry_index,
size_t count) {
xas_bank_entry *entry = &((xas_bank_entry *)(bank + 1))[entry_index];
size_t left = count,
index_o = 0;
if (!xas_audio_format_eq(bank->format, input->format)) {
errno = EINVAL;
goto error_invalid_format;
}
if (count > bank->entry_size) {
count = bank->entry_size;
}
while (left) {
ssize_t readlen,
amount = left > count? count: left;
void *buf;
if ((readlen = xas_audio_stream_read(input, &buf, amount)) < 0) {
goto error_audio_stream_read;
} else if (readlen == 0) {
break;
}
xas_audio_copy(bank->format,
entry + 1,
buf,
index_o,
0,
readlen);
left -= readlen;
index_o += readlen;
}
return entry->duration = index_o;
error_audio_stream_read:
error_invalid_format:
return -1;
}
ssize_t xas_bank_load_file(xas_bank *bank,
size_t entry_index,
const char *path) {
ssize_t ret;
xas_audio_stream *riff;
if ((riff = xas_riff_open_file(path, O_RDONLY)) == NULL) {
goto error_riff_open_file;
}
if ((ret = xas_bank_record(bank,
riff,
entry_index,
bank->entry_size)) < 0) {
goto error_record;
}
xas_audio_stream_destroy(riff);
return ret;
error_record:
xas_audio_stream_destroy(riff);
error_riff_open_file:
return -1;
}
static int set_gain(xas_bank_player *player, float gain) {
xas_bank_player_set_gain(player, gain);
return 0;
}
xas_bank_player *xas_bank_player_new(xas_bank *bank) {
xas_bank_player *player;
if ((player = malloc(sizeof(*player))) == NULL) {
goto error_malloc_player;
}
player->obj.start = (xas_object_start_callback)xas_bank_player_start;
player->obj.stop = (xas_object_stop_callback)xas_bank_player_stop;
player->obj.set_gain = (xas_object_set_gain_callback)set_gain;
player->bank = bank;
player->status = XAS_BANK_PLAYER_STOPPED;
player->gain = XAS_BANK_PLAYER_DEFAULT_GAIN;
player->entry = 0;
player->index = 0;
return player;
error_malloc_player:
return NULL;
}
void xas_bank_player_destroy(xas_bank_player *player) {
free(player);
}
int xas_bank_player_set_entry(xas_bank_player *player, size_t entry) {
if (entry > player->bank->entry_count) {
errno = EINVAL;
goto error_invalid_entry;
}
player->entry = entry;
return 0;
error_invalid_entry:
return -1;
}
void xas_bank_player_set_gain(xas_bank_player *player, float gain) {
player->gain = gain;
}
int xas_bank_player_start(xas_bank_player *player) {
if (player->status == XAS_BANK_PLAYER_PLAYING) {
return 0;
}
player->status = XAS_BANK_PLAYER_PLAYING;
player->index = 0;
return 0;
}
int xas_bank_player_stop(xas_bank_player *player) {
if (player->status == XAS_BANK_PLAYER_STOPPED) {
return 0;
}
player->status = XAS_BANK_PLAYER_STOPPED;
player->gain = 0.0;
player->entry = 0;
player->index = 0;
return 1;
}
int xas_bank_player_playing(xas_bank_player *player) {
return player->status == XAS_BANK_PLAYER_PLAYING;
}
static ssize_t stream_fill(xas_bank_player *player,
int16_t *dest,
size_t count,
xas_audio_stream *stream) {
xas_bank *bank = player->bank;
size_t index_o = 0,
left = count;
while (left) {
if (player->status == XAS_BANK_PLAYER_PLAYING) {
xas_bank_entry *entry =
&((xas_bank_entry *)(bank + 1))[player->entry];
int16_t *src = (int16_t *)(entry + 1);
size_t remaining = entry->duration - player->index,
amount = remaining < left? remaining: left;
size_t i;
for (i=0; i<amount; i++) {
dest[index_o + i] = player->gain * src[player->index + i];
}
left -= amount;
player->index += amount;
index_o += amount;
if (player->index == entry->duration) {
xas_bank_player_stop(player);
}
} else {
xas_audio_zero(bank->format, dest, index_o, left);
index_o += left;
left = 0;
}
}
return count;
}
void stream_cleanup(xas_bank_player *player, xas_audio_stream *stream) {
return;
}
xas_audio_stream *xas_bank_player_stream_new(xas_bank_player *player) {
return xas_audio_stream_new_source((xas_audio_fill)stream_fill,
(xas_audio_cleanup)stream_cleanup,
player->bank->format,
player->bank->entry_size,
player);
}
|