-
Notifications
You must be signed in to change notification settings - Fork 280
/
kcm_sample.c
537 lines (420 loc) · 12.4 KB
/
kcm_sample.c
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Sample audio interface.
*
* By Peter Wang.
*
* See LICENSE.txt for copyright information.
*/
/* Title: Sample audio interface
*/
#include "allegro5/allegro.h"
#include "allegro5/allegro_audio.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_audio.h"
#include "allegro5/internal/aintern_vector.h"
ALLEGRO_DEBUG_CHANNEL("audio")
static ALLEGRO_VOICE *allegro_voice = NULL;
static ALLEGRO_MIXER *allegro_mixer = NULL;
static ALLEGRO_MIXER *default_mixer = NULL;
typedef struct AUTO_SAMPLE {
ALLEGRO_SAMPLE_INSTANCE *instance;
int id;
bool locked;
} AUTO_SAMPLE;
static _AL_VECTOR auto_samples = _AL_VECTOR_INITIALIZER(AUTO_SAMPLE);
static bool create_default_mixer(void);
static bool do_play_sample(ALLEGRO_SAMPLE_INSTANCE *spl, ALLEGRO_SAMPLE *data,
float gain, float pan, float speed, ALLEGRO_PLAYMODE loop);
static void free_sample_vector(void);
static int string_to_depth(const char *s)
{
// FIXME: fill in the rest
if (!_al_stricmp(s, "int16")) {
return ALLEGRO_AUDIO_DEPTH_INT16;
}
else {
return ALLEGRO_AUDIO_DEPTH_FLOAT32;
}
}
/* Creates the default voice and mixer if they haven't been created yet. */
static bool create_default_mixer(void)
{
int voice_frequency = 44100;
int voice_depth = ALLEGRO_AUDIO_DEPTH_INT16;
int mixer_frequency = 44100;
int mixer_depth = ALLEGRO_AUDIO_DEPTH_FLOAT32;
ALLEGRO_CONFIG *config = al_get_system_config();
const char *p;
p = al_get_config_value(config, "audio", "primary_voice_frequency");
if (p && p[0] != '\0') {
voice_frequency = atoi(p);
}
p = al_get_config_value(config, "audio", "primary_mixer_frequency");
if (p && p[0] != '\0') {
mixer_frequency = atoi(p);
}
p = al_get_config_value(config, "audio", "primary_voice_depth");
if (p && p[0] != '\0') {
voice_depth = string_to_depth(p);
}
p = al_get_config_value(config, "audio", "primary_mixer_depth");
if (p && p[0] != '\0') {
mixer_depth = string_to_depth(p);
}
if (!allegro_voice) {
allegro_voice = al_create_voice(voice_frequency, voice_depth,
ALLEGRO_CHANNEL_CONF_2);
if (!allegro_voice) {
ALLEGRO_ERROR("al_create_voice failed\n");
goto Error;
}
}
if (!allegro_mixer) {
allegro_mixer = al_create_mixer(mixer_frequency, mixer_depth,
ALLEGRO_CHANNEL_CONF_2);
if (!allegro_mixer) {
ALLEGRO_ERROR("al_create_voice failed\n");
goto Error;
}
}
/* In case this function is called multiple times. */
al_detach_mixer(allegro_mixer);
if (!al_attach_mixer_to_voice(allegro_mixer, allegro_voice)) {
ALLEGRO_ERROR("al_attach_mixer_to_voice failed\n");
goto Error;
}
return true;
Error:
if (allegro_mixer) {
al_destroy_mixer(allegro_mixer);
allegro_mixer = NULL;
}
if (allegro_voice) {
al_destroy_voice(allegro_voice);
allegro_voice = NULL;
}
return false;
}
/* Function: al_create_sample
*/
ALLEGRO_SAMPLE *al_create_sample(void *buf, unsigned int samples,
unsigned int freq, ALLEGRO_AUDIO_DEPTH depth,
ALLEGRO_CHANNEL_CONF chan_conf, bool free_buf)
{
ALLEGRO_SAMPLE *spl;
ASSERT(buf);
if (!freq) {
_al_set_error(ALLEGRO_INVALID_PARAM, "Invalid sample frequency");
return NULL;
}
spl = al_calloc(1, sizeof(*spl));
if (!spl) {
_al_set_error(ALLEGRO_GENERIC_ERROR,
"Out of memory allocating sample data object");
return NULL;
}
spl->depth = depth;
spl->chan_conf = chan_conf;
spl->frequency = freq;
spl->len = samples;
spl->buffer.ptr = buf;
spl->free_buf = free_buf;
spl->dtor_item = _al_kcm_register_destructor("sample", spl, (void (*)(void *)) al_destroy_sample);
return spl;
}
/* Stop any sample instances which are still playing a sample buffer which
* is about to be destroyed.
*/
static void stop_sample_instances_helper(void *object, void (*func)(void *),
void *userdata)
{
ALLEGRO_SAMPLE_INSTANCE *splinst = object;
/* This is ugly. */
if (func == (void (*)(void *)) al_destroy_sample_instance
&& al_get_sample_data(al_get_sample(splinst)) == userdata
&& al_get_sample_instance_playing(splinst))
{
al_stop_sample_instance(splinst);
}
}
/* Function: al_destroy_sample
*/
void al_destroy_sample(ALLEGRO_SAMPLE *spl)
{
if (spl) {
_al_kcm_foreach_destructor(stop_sample_instances_helper,
al_get_sample_data(spl));
_al_kcm_unregister_destructor(spl->dtor_item);
if (spl->free_buf && spl->buffer.ptr) {
al_free(spl->buffer.ptr);
}
spl->buffer.ptr = NULL;
spl->free_buf = false;
al_free(spl);
}
}
/* Function: al_reserve_samples
*/
bool al_reserve_samples(int reserve_samples)
{
int i;
int current_samples_count = (int) _al_vector_size(&auto_samples);
ASSERT(reserve_samples >= 0);
/* If no default mixer has been set by the user, then create a voice
* and a mixer, and set them to be the default one for use with
* al_play_sample().
*/
if (default_mixer == NULL) {
if (!al_restore_default_mixer())
goto Error;
}
if (current_samples_count < reserve_samples) {
/* We need to reserve more samples than currently are reserved. */
for (i = 0; i < reserve_samples - current_samples_count; i++) {
AUTO_SAMPLE *slot = _al_vector_alloc_back(&auto_samples);
slot->id = 0;
slot->instance = al_create_sample_instance(NULL);
slot->locked = false;
if (!slot->instance) {
ALLEGRO_ERROR("al_create_sample failed\n");
goto Error;
}
if (!al_attach_sample_instance_to_mixer(slot->instance, default_mixer)) {
ALLEGRO_ERROR("al_attach_mixer_to_sample failed\n");
goto Error;
}
}
}
else if (current_samples_count > reserve_samples) {
/* We need to reserve fewer samples than currently are reserved. */
while (current_samples_count-- > reserve_samples) {
AUTO_SAMPLE *slot = _al_vector_ref(&auto_samples, current_samples_count);
al_destroy_sample_instance(slot->instance);
_al_vector_delete_at(&auto_samples, current_samples_count);
}
}
return true;
Error:
free_sample_vector();
return false;
}
/* Function: al_get_default_mixer
*/
ALLEGRO_MIXER *al_get_default_mixer(void)
{
return default_mixer;
}
/* Function: al_set_default_mixer
*/
bool al_set_default_mixer(ALLEGRO_MIXER *mixer)
{
ASSERT(mixer != NULL);
if (mixer != default_mixer) {
int i;
default_mixer = mixer;
/* Destroy all current sample instances, recreate them, and
* attach them to the new mixer */
for (i = 0; i < (int) _al_vector_size(&auto_samples); i++) {
AUTO_SAMPLE *slot = _al_vector_ref(&auto_samples, i);
slot->id = 0;
al_destroy_sample_instance(slot->instance);
slot->locked = false;
slot->instance = al_create_sample_instance(NULL);
if (!slot->instance) {
ALLEGRO_ERROR("al_create_sample failed\n");
goto Error;
}
if (!al_attach_sample_instance_to_mixer(slot->instance, default_mixer)) {
ALLEGRO_ERROR("al_attach_mixer_to_sample failed\n");
goto Error;
}
}
}
return true;
Error:
free_sample_vector();
default_mixer = NULL;
return false;
}
/* Function: al_restore_default_mixer
*/
bool al_restore_default_mixer(void)
{
if (!create_default_mixer())
return false;
if (!al_set_default_mixer(allegro_mixer))
return false;
return true;
}
/* Function: al_get_default_voice
*/
ALLEGRO_VOICE *al_get_default_voice(void)
{
return allegro_voice;
}
/* Function: al_set_default_voice
*/
void al_set_default_voice(ALLEGRO_VOICE *voice)
{
if (allegro_voice) {
al_destroy_voice(allegro_voice);
}
allegro_voice = voice;
}
/* Function: al_play_sample
*/
bool al_play_sample(ALLEGRO_SAMPLE *spl, float gain, float pan, float speed,
ALLEGRO_PLAYMODE loop, ALLEGRO_SAMPLE_ID *ret_id)
{
static int next_id = 0;
unsigned int i;
ASSERT(spl);
if (ret_id != NULL) {
ret_id->_id = -1;
ret_id->_index = 0;
}
for (i = 0; i < _al_vector_size(&auto_samples); i++) {
AUTO_SAMPLE *slot = _al_vector_ref(&auto_samples, i);
if (!al_get_sample_instance_playing(slot->instance) && !slot->locked) {
if (!do_play_sample(slot->instance, spl, gain, pan, speed, loop))
break;
if (ret_id != NULL) {
ret_id->_index = (int) i;
ret_id->_id = slot->id = ++next_id;
}
return true;
}
}
return false;
}
static bool do_play_sample(ALLEGRO_SAMPLE_INSTANCE *splinst,
ALLEGRO_SAMPLE *spl, float gain, float pan, float speed, ALLEGRO_PLAYMODE loop)
{
if (!al_set_sample(splinst, spl)) {
ALLEGRO_ERROR("al_set_sample failed\n");
return false;
}
if (!al_set_sample_instance_gain(splinst, gain) ||
!al_set_sample_instance_pan(splinst, pan) ||
!al_set_sample_instance_speed(splinst, speed) ||
!al_set_sample_instance_playmode(splinst, loop)) {
return false;
}
if (!al_play_sample_instance(splinst)) {
ALLEGRO_ERROR("al_play_sample_instance failed\n");
return false;
}
return true;
}
/* Function: al_stop_sample
*/
void al_stop_sample(ALLEGRO_SAMPLE_ID *spl_id)
{
AUTO_SAMPLE *slot;
ASSERT(spl_id->_id != -1);
ASSERT(spl_id->_index < (int) _al_vector_size(&auto_samples));
slot = _al_vector_ref(&auto_samples, spl_id->_index);
if (slot->id == spl_id->_id) {
al_stop_sample_instance(slot->instance);
}
}
/* Function: al_lock_sample_id
*/
ALLEGRO_SAMPLE_INSTANCE* al_lock_sample_id(ALLEGRO_SAMPLE_ID *spl_id)
{
AUTO_SAMPLE *slot;
ASSERT(spl_id->_id != -1);
ASSERT(spl_id->_index < (int) _al_vector_size(&auto_samples));
slot = _al_vector_ref(&auto_samples, spl_id->_index);
if (slot->id == spl_id->_id) {
slot->locked = true;
return slot->instance;
}
return NULL;
}
/* Function: al_unlock_sample_id
*/
void al_unlock_sample_id(ALLEGRO_SAMPLE_ID *spl_id)
{
AUTO_SAMPLE *slot;
ASSERT(spl_id->_id != -1);
ASSERT(spl_id->_index < (int) _al_vector_size(&auto_samples));
slot = _al_vector_ref(&auto_samples, spl_id->_index);
if (slot->id == spl_id->_id) {
slot->locked = false;
}
}
/* Function: al_stop_samples
*/
void al_stop_samples(void)
{
unsigned int i;
for (i = 0; i < _al_vector_size(&auto_samples); i++) {
AUTO_SAMPLE *slot = _al_vector_ref(&auto_samples, i);
al_stop_sample_instance(slot->instance);
}
}
/* Function: al_get_sample_frequency
*/
unsigned int al_get_sample_frequency(const ALLEGRO_SAMPLE *spl)
{
ASSERT(spl);
return spl->frequency;
}
/* Function: al_get_sample_length
*/
unsigned int al_get_sample_length(const ALLEGRO_SAMPLE *spl)
{
ASSERT(spl);
return spl->len;
}
/* Function: al_get_sample_depth
*/
ALLEGRO_AUDIO_DEPTH al_get_sample_depth(const ALLEGRO_SAMPLE *spl)
{
ASSERT(spl);
return spl->depth;
}
/* Function: al_get_sample_channels
*/
ALLEGRO_CHANNEL_CONF al_get_sample_channels(const ALLEGRO_SAMPLE *spl)
{
ASSERT(spl);
return spl->chan_conf;
}
/* Function: al_get_sample_data
*/
void *al_get_sample_data(const ALLEGRO_SAMPLE *spl)
{
ASSERT(spl);
return spl->buffer.ptr;
}
/* Destroy all sample instances, and frees the associated vectors. */
static void free_sample_vector(void)
{
int j;
for (j = 0; j < (int) _al_vector_size(&auto_samples); j++) {
AUTO_SAMPLE *slot = _al_vector_ref(&auto_samples, j);
al_destroy_sample_instance(slot->instance);
}
_al_vector_free(&auto_samples);
}
void _al_kcm_shutdown_default_mixer(void)
{
free_sample_vector();
al_destroy_mixer(allegro_mixer);
al_destroy_voice(allegro_voice);
allegro_mixer = NULL;
allegro_voice = NULL;
default_mixer = NULL;
}
/* vim: set sts=3 sw=3 et: */