/
kcm_instance.c
690 lines (528 loc) · 14.9 KB
/
kcm_instance.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/**
* Originally digi.c from allegro wiki
* Original authors: KC/Milan
*
* Converted to allegro5 by Ryan Dickie
*/
/* Title: Sample Instance functions
*/
#include <math.h>
#include <stdio.h>
#include "allegro5/allegro_audio.h"
#include "allegro5/internal/aintern_audio.h"
#include "allegro5/internal/aintern_audio_cfg.h"
static void maybe_lock_mutex(ALLEGRO_MUTEX *mutex)
{
if (mutex) {
al_lock_mutex(mutex);
}
}
static void maybe_unlock_mutex(ALLEGRO_MUTEX *mutex)
{
if (mutex) {
al_unlock_mutex(mutex);
}
}
/* _al_kcm_stream_set_mutex:
* This function sets a sample's mutex pointer to the specified value. It is
* ALLEGRO_MIXER aware, and will recursively set any attached streams' mutex
* to the same value.
*/
void _al_kcm_stream_set_mutex(ALLEGRO_SAMPLE_INSTANCE *stream, ALLEGRO_MUTEX *mutex)
{
ASSERT(stream);
if (stream->mutex == mutex)
return;
stream->mutex = mutex;
/* If this is a mixer, we need to make sure all the attached streams also
* set the same mutex.
*/
if (stream->is_mixer) {
ALLEGRO_MIXER *mixer = (ALLEGRO_MIXER *)stream;
int i;
for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) {
ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i);
ALLEGRO_SAMPLE_INSTANCE *spl = *slot;
_al_kcm_stream_set_mutex(spl, mutex);
}
}
}
/* stream_free:
* This function is ALLEGRO_MIXER aware and frees the memory associated with
* the sample or mixer, and detaches any attached streams or mixers.
*/
static void stream_free(ALLEGRO_SAMPLE_INSTANCE *spl)
{
if (spl) {
/* Make sure we free the mixer buffer and de-reference the attached
* streams if this is a mixer stream.
*/
if (spl->is_mixer) {
ALLEGRO_MIXER *mixer = (ALLEGRO_MIXER *)spl;
int i;
_al_kcm_stream_set_mutex(&mixer->ss, NULL);
for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) {
ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i);
ALLEGRO_SAMPLE_INSTANCE *spl = *slot;
spl->parent.u.ptr = NULL;
spl->spl_read = NULL;
al_free(spl->matrix);
spl->matrix = NULL;
}
_al_vector_free(&mixer->streams);
if (spl->spl_data.buffer.ptr) {
ASSERT(spl->spl_data.free_buf);
al_free(spl->spl_data.buffer.ptr);
spl->spl_data.buffer.ptr = NULL;
}
spl->spl_data.free_buf = false;
}
ASSERT(! spl->spl_data.free_buf);
al_free(spl);
}
}
/* _al_kcm_detach_from_parent:
* This detaches the sample, stream, or mixer from anything it may be attached
* to.
*/
void _al_kcm_detach_from_parent(ALLEGRO_SAMPLE_INSTANCE *spl)
{
ALLEGRO_MIXER *mixer;
int i;
if (!spl || !spl->parent.u.ptr)
return;
if (spl->parent.is_voice) {
al_detach_voice(spl->parent.u.voice);
return;
}
mixer = spl->parent.u.mixer;
/* Search through the streams and check for this one */
for (i = _al_vector_size(&mixer->streams) - 1; i >= 0; i--) {
ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&mixer->streams, i);
if (*slot == spl) {
maybe_lock_mutex(mixer->ss.mutex);
_al_vector_delete_at(&mixer->streams, i);
spl->parent.u.mixer = NULL;
_al_kcm_stream_set_mutex(spl, NULL);
spl->spl_read = NULL;
maybe_unlock_mutex(mixer->ss.mutex);
break;
}
}
al_free(spl->matrix);
spl->matrix = NULL;
}
/* Function: al_create_sample_instance
*/
ALLEGRO_SAMPLE_INSTANCE *al_create_sample_instance(ALLEGRO_SAMPLE *sample_data)
{
ALLEGRO_SAMPLE_INSTANCE *spl;
spl = al_calloc(1, sizeof(*spl));
if (!spl) {
_al_set_error(ALLEGRO_GENERIC_ERROR,
"Out of memory allocating sample object");
return NULL;
}
if (sample_data) {
spl->spl_data = *sample_data;
}
spl->spl_data.free_buf = false;
spl->loop = ALLEGRO_PLAYMODE_ONCE;
spl->speed = 1.0f;
spl->gain = 1.0f;
spl->pan = 0.0f;
spl->pos = 0;
spl->loop_start = 0;
spl->loop_end = sample_data ? sample_data->len : 0;
spl->step = 0;
spl->matrix = NULL;
spl->is_mixer = false;
spl->spl_read = NULL;
spl->mutex = NULL;
spl->parent.u.ptr = NULL;
spl->dtor_item = _al_kcm_register_destructor("sample_instance", spl,
(void (*)(void *))al_destroy_sample_instance);
return spl;
}
/* This function is ALLEGRO_MIXER aware */
/* Function: al_destroy_sample_instance
*/
void al_destroy_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl)
{
_al_kcm_destroy_sample(spl, true);
}
/* Internal function: _al_kcm_destroy_sample
*/
void _al_kcm_destroy_sample(ALLEGRO_SAMPLE_INSTANCE *spl, bool unregister)
{
if (spl) {
if (unregister) {
_al_kcm_unregister_destructor(spl->dtor_item);
}
_al_kcm_detach_from_parent(spl);
stream_free(spl);
}
}
/* Function: al_play_sample_instance
*/
bool al_play_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return al_set_sample_instance_playing(spl, true);
}
/* Function: al_stop_sample_instance
*/
bool al_stop_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return al_set_sample_instance_playing(spl, false);
}
/* Function: al_get_sample_instance_frequency
*/
unsigned int al_get_sample_instance_frequency(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->spl_data.frequency;
}
/* Function: al_get_sample_instance_length
*/
unsigned int al_get_sample_instance_length(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->spl_data.len;
}
/* Function: al_get_sample_instance_position
*/
unsigned int al_get_sample_instance_position(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
if (spl->parent.u.ptr && spl->parent.is_voice) {
ALLEGRO_VOICE *voice = spl->parent.u.voice;
return al_get_voice_position(voice);
}
return spl->pos;
}
/* Function: al_get_sample_instance_speed
*/
float al_get_sample_instance_speed(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->speed;
}
/* Function: al_get_sample_instance_gain
*/
float al_get_sample_instance_gain(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->gain;
}
/* Function: al_get_sample_instance_pan
*/
float al_get_sample_instance_pan(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->pan;
}
/* Function: al_get_sample_instance_time
*/
float al_get_sample_instance_time(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return (float)(spl->spl_data.len)
/ (float)spl->spl_data.frequency;
}
/* Function: al_get_sample_instance_depth
*/
ALLEGRO_AUDIO_DEPTH al_get_sample_instance_depth(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->spl_data.depth;
}
/* Function: al_get_sample_instance_channels
*/
ALLEGRO_CHANNEL_CONF al_get_sample_instance_channels(
const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->spl_data.chan_conf;
}
/* Function: al_get_sample_instance_playmode
*/
ALLEGRO_PLAYMODE al_get_sample_instance_playmode(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return spl->loop;
}
/* Function: al_get_sample_instance_playing
*/
bool al_get_sample_instance_playing(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
if (spl->parent.u.ptr && spl->parent.is_voice) {
ALLEGRO_VOICE *voice = spl->parent.u.voice;
return al_get_voice_playing(voice);
}
return spl->is_playing;
}
/* Function: al_get_sample_instance_attached
*/
bool al_get_sample_instance_attached(const ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return (spl->parent.u.ptr != NULL);
}
/* Function: al_set_sample_instance_position
*/
bool al_set_sample_instance_position(ALLEGRO_SAMPLE_INSTANCE *spl,
unsigned int val)
{
ASSERT(spl);
if (spl->parent.u.ptr && spl->parent.is_voice) {
ALLEGRO_VOICE *voice = spl->parent.u.voice;
if (!al_set_voice_position(voice, val))
return false;
}
else {
maybe_lock_mutex(spl->mutex);
spl->pos = val;
maybe_unlock_mutex(spl->mutex);
}
return true;
}
/* Function: al_set_sample_instance_length
*/
bool al_set_sample_instance_length(ALLEGRO_SAMPLE_INSTANCE *spl,
unsigned int val)
{
ASSERT(spl);
if (spl->is_playing) {
_al_set_error(ALLEGRO_INVALID_OBJECT,
"Attempted to change the length of a playing sample");
return false;
}
spl->spl_data.len = val;
spl->loop_end = val;
return true;
}
/* Function: al_set_sample_instance_speed
*/
bool al_set_sample_instance_speed(ALLEGRO_SAMPLE_INSTANCE *spl, float val)
{
ASSERT(spl);
if (fabsf(val) < (1.0f/64.0f)) {
_al_set_error(ALLEGRO_INVALID_PARAM,
"Attempted to set zero speed");
return false;
}
if (spl->parent.u.ptr && spl->parent.is_voice) {
_al_set_error(ALLEGRO_GENERIC_ERROR,
"Could not set voice playback speed");
return false;
}
spl->speed = val;
if (spl->parent.u.mixer) {
ALLEGRO_MIXER *mixer = spl->parent.u.mixer;
maybe_lock_mutex(spl->mutex);
spl->step = (spl->spl_data.frequency) * spl->speed;
spl->step_denom = mixer->ss.spl_data.frequency;
/* Don't wanna be trapped with a step value of 0 */
if (spl->step == 0) {
if (spl->speed > 0.0f)
spl->step = 1;
else
spl->step = -1;
}
maybe_unlock_mutex(spl->mutex);
}
return true;
}
/* Function: al_set_sample_instance_gain
*/
bool al_set_sample_instance_gain(ALLEGRO_SAMPLE_INSTANCE *spl, float val)
{
ASSERT(spl);
if (spl->parent.u.ptr && spl->parent.is_voice) {
_al_set_error(ALLEGRO_GENERIC_ERROR,
"Could not set gain of sample attached to voice");
return false;
}
if (spl->gain != val) {
spl->gain = val;
/* If attached to a mixer already, need to recompute the sample
* matrix to take into account the gain.
*/
if (spl->parent.u.mixer) {
ALLEGRO_MIXER *mixer = spl->parent.u.mixer;
maybe_lock_mutex(spl->mutex);
_al_kcm_mixer_rejig_sample_matrix(mixer, spl);
maybe_unlock_mutex(spl->mutex);
}
}
return true;
}
/* Function: al_set_sample_instance_pan
*/
bool al_set_sample_instance_pan(ALLEGRO_SAMPLE_INSTANCE *spl, float val)
{
ASSERT(spl);
if (spl->parent.u.ptr && spl->parent.is_voice) {
_al_set_error(ALLEGRO_GENERIC_ERROR,
"Could not set panning of sample attached to voice");
return false;
}
if (val != ALLEGRO_AUDIO_PAN_NONE && (val < -1.0 || val > 1.0)) {
_al_set_error(ALLEGRO_GENERIC_ERROR, "Invalid pan value");
return false;
}
if (spl->pan != val) {
spl->pan = val;
/* If attached to a mixer already, need to recompute the sample
* matrix to take into account the panning.
*/
if (spl->parent.u.mixer) {
ALLEGRO_MIXER *mixer = spl->parent.u.mixer;
maybe_lock_mutex(spl->mutex);
_al_kcm_mixer_rejig_sample_matrix(mixer, spl);
maybe_unlock_mutex(spl->mutex);
}
}
return true;
}
/* Function: al_set_sample_instance_playmode
*/
bool al_set_sample_instance_playmode(ALLEGRO_SAMPLE_INSTANCE *spl,
ALLEGRO_PLAYMODE val)
{
ASSERT(spl);
if (val < ALLEGRO_PLAYMODE_ONCE || val > ALLEGRO_PLAYMODE_BIDIR) {
_al_set_error(ALLEGRO_INVALID_PARAM,
"Invalid loop mode");
return false;
}
maybe_lock_mutex(spl->mutex);
spl->loop = val;
if (spl->loop != ALLEGRO_PLAYMODE_ONCE) {
if (spl->pos < spl->loop_start)
spl->pos = spl->loop_start;
else if (spl->pos > spl->loop_end-1)
spl->pos = spl->loop_end-1;
}
maybe_unlock_mutex(spl->mutex);
return true;
}
/* Function: al_set_sample_instance_playing
*/
bool al_set_sample_instance_playing(ALLEGRO_SAMPLE_INSTANCE *spl, bool val)
{
ASSERT(spl);
if (!spl->parent.u.ptr || !spl->spl_data.buffer.ptr) {
spl->is_playing = val;
return true;
}
if (spl->parent.is_voice) {
/* parent is voice */
ALLEGRO_VOICE *voice = spl->parent.u.voice;
return al_set_voice_playing(voice, val);
}
/* parent is mixer */
maybe_lock_mutex(spl->mutex);
spl->is_playing = val;
if (!val)
spl->pos = 0;
maybe_unlock_mutex(spl->mutex);
return true;
}
/* Function: al_detach_sample_instance
*/
bool al_detach_sample_instance(ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
_al_kcm_detach_from_parent(spl);
ASSERT(spl->spl_read == NULL);
return true;
}
/* Function: al_set_sample
*/
bool al_set_sample(ALLEGRO_SAMPLE_INSTANCE *spl, ALLEGRO_SAMPLE *data)
{
sample_parent_t old_parent;
bool need_reattach;
ASSERT(spl);
/* Stop the sample if it is playing. */
if (spl->is_playing) {
if (!al_set_sample_instance_playing(spl, false)) {
/* Shouldn't happen. */
ASSERT(false);
return false;
}
}
if (!data) {
if (spl->parent.u.ptr) {
_al_kcm_detach_from_parent(spl);
}
spl->spl_data.buffer.ptr = NULL;
return true;
}
/* Have data. */
need_reattach = false;
if (spl->parent.u.ptr != NULL) {
if (spl->spl_data.frequency != data->frequency ||
spl->spl_data.depth != data->depth ||
spl->spl_data.chan_conf != data->chan_conf) {
old_parent = spl->parent;
need_reattach = true;
_al_kcm_detach_from_parent(spl);
}
}
spl->spl_data = *data;
spl->spl_data.free_buf = false;
spl->pos = 0;
spl->loop_start = 0;
spl->loop_end = data->len;
/* Should we reset the loop mode? */
if (need_reattach) {
if (old_parent.is_voice) {
if (!al_attach_sample_instance_to_voice(spl, old_parent.u.voice)) {
spl->spl_data.buffer.ptr = NULL;
return false;
}
}
else {
if (!al_attach_sample_instance_to_mixer(spl, old_parent.u.mixer)) {
spl->spl_data.buffer.ptr = NULL;
return false;
}
}
}
return true;
}
/* Function: al_get_sample
*/
ALLEGRO_SAMPLE *al_get_sample(ALLEGRO_SAMPLE_INSTANCE *spl)
{
ASSERT(spl);
return &(spl->spl_data);
}
/* Function: al_set_sample_instance_channel_matrix
*/
bool al_set_sample_instance_channel_matrix(ALLEGRO_SAMPLE_INSTANCE *spl, const float *matrix)
{
ASSERT(spl);
ASSERT(matrix);
if (spl->parent.u.ptr && spl->parent.is_voice) {
_al_set_error(ALLEGRO_GENERIC_ERROR,
"Could not set channel matrix of sample attached to voice");
return false;
}
if (spl->parent.u.mixer) {
ALLEGRO_MIXER *mixer = spl->parent.u.mixer;
size_t dst_chans = al_get_channel_count(mixer->ss.spl_data.chan_conf);
size_t src_chans = al_get_channel_count(spl->spl_data.chan_conf);
ASSERT(spl->matrix);
maybe_lock_mutex(spl->mutex);
memcpy(spl->matrix, matrix, dst_chans * src_chans * sizeof(float));
maybe_unlock_mutex(spl->mutex);
}
return true;
}
/* vim: set sts=3 sw=3 et: */