/
mojoal.c
3357 lines (2873 loc) · 108 KB
/
mojoal.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MojoAL; a simple drop-in OpenAL implementation.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <math.h>
#include <float.h>
#include "AL/al.h"
#include "AL/alc.h"
#include "SDL.h"
#define OPENAL_VERSION_MAJOR 1
#define OPENAL_VERSION_MINOR 1
#define OPENAL_VERSION_STRING3(major, minor) #major "." #minor
#define OPENAL_VERSION_STRING2(major, minor) OPENAL_VERSION_STRING3(major, minor)
/* !!! FIXME: make some decisions about VENDOR and RENDERER strings here */
#define OPENAL_VERSION_STRING OPENAL_VERSION_STRING2(OPENAL_VERSION_MAJOR, OPENAL_VERSION_MINOR)
#define OPENAL_VENDOR_STRING "Ryan C. Gordon"
#define OPENAL_RENDERER_STRING "mojoAL"
#define DEFAULT_PLAYBACK_DEVICE "Default OpenAL playback device"
#define DEFAULT_CAPTURE_DEVICE "Default OpenAL capture device"
/* Some OpenAL apps (incorrectly) generate sources in a loop at startup until
it fails. We set an upper limit to protect against this behavior, which
also lets us not need to worry about locking in case another thread would
need to realloc a growing source array. */
#ifndef OPENAL_MAX_SOURCES
#define OPENAL_MAX_SOURCES 128
#endif
/* Number of buffers to allocate at once when we need a new block during alGenBuffers(). */
#ifndef OPENAL_BUFFER_BLOCK_SIZE
#define OPENAL_BUFFER_BLOCK_SIZE 256
#endif
/* AL_EXT_FLOAT32 support... */
#ifndef AL_FORMAT_MONO_FLOAT32
#define AL_FORMAT_MONO_FLOAT32 0x10010
#endif
#ifndef AL_FORMAT_STEREO_FLOAT32
#define AL_FORMAT_STEREO_FLOAT32 0x10011
#endif
/* ALC_EXT_DISCONNECTED support... */
#ifndef ALC_CONNECTED
#define ALC_CONNECTED 0x313
#endif
/*
The locking strategy for this OpenAL implementation is complicated.
Not only do we generally want you to be able to call into OpenAL from any
thread, we'll always have to compete with the SDL audio device thread.
However, we don't want to just throw a big mutex around the whole thing,
not only because can we safely touch two unrelated objects at the same
time, but also because the mixer might make your simple state change call
on the main thread block for several milliseconds if your luck runs out,
killing your framerate. Here's the basic plan:
- Devices are expected to live for the entire life of your OpenAL experience,
so deleting one while another thread is using it is your own fault. Don't
do that.
- Create or destroying a context will lock the SDL audio device, serializing
these calls vs the mixer thread while we add/remove the context on the
device's list. So don't do this in time-critical code.
- The current context is an atomic pointer, so even if there's a MakeCurrent
while an operation is in progress, the operation will either get the new
context or the previous context and set state on whichever. This should
protect everything but context destruction, but if you are still calling
into the AL while destroying the context, shame on you (even there, the
first thing context destruction will do is make the context no-longer
current, which means the race window is pretty small).
- Source and Buffer objects, once generated, aren't freed. If deleted, we
atomically mark them as available for reuse, but the pointers never change
or go away until the AL context (for sources) or device (for buffers) does.
- Since we have a maximum source count, to protect against apps that allocate
sources in a loop until they fail, the source name array is static within
the ALCcontext object, and thus doesn't need a lock for access. Buffer
objects can be generated until we run out of memory, so that array needs to
be dynamic and have a lock, though. When generating sources, we walk the
static array and compare-and-swap the "allocated" field from 0 (available)
to 2 (temporarily claimed), filling in the temporarily-claimed names in
the array passed to alGenSources(). If we run out of sources, we walk back
over this array, setting all the allocated fields back to zero for other
threads to be able to claim, set the error state, and zero out the array.
If we have enough sources, we walk the array and set the allocated fields
to 1 (permanently claimed).
- Buffers are allocated in blocks of OPENAL_BUFFER_BLOCK_SIZE, each block
linked-listed to the next, as allocated. These blocks are never deallocated
as long as the device lives, so they don't need a lock to access (and adding
a new block is an atomic pointer compare-and-swap). Allocating buffers uses
the same compare-and-swap marking technique that allocating sources does,
just in these buffer blocks instead of a static array. We don't (currently)
keep a ALuint name index array of buffers, but instead walk the list of
blocks, OPENAL_BUFFER_BLOCK_SIZE at a time, until we find our target block,
and then index into that.
- Buffer data is owned by the AL, and it's illegal to delete a buffer or
alBufferData() its contents while queued on a source with either AL_BUFFER
or alSourceQueueBuffers(). We keep an atomic refcount for each buffer,
and you can't change its state or delete it when its refcount is > 0, so
there isn't a race with the mixer, and multiple racing calls into the API
will generate an error and return immediately from all except the thread
that managed to get the first reference count increment.
- Buffer queues are a hot mess. alSourceQueueBuffers will build a linked
list of buffers, then atomically move this list into position for the
mixer to obtain it. The mixer will process this list without the need
to be atomic (as it owns it once it atomically claims it from from the
just_queue field where alSourceQueueBuffers staged it). As buffers are
processed, the mixer moves them atomically to a linked list that other
threads can pick up for alSourceUnqueueBuffers. The problem with unqueueing
is that multiple threads can compete. Unlike queueing, where we don't care
which thread wins the race to queue, unqueueing _must_ return buffer names
in the order they were mixed, according to the spec, which means we need a
lock. But! we only need to serialize the alSourceUnqueueBuffers callers,
not the mixer thread, and only long enough to obtain any newly-processed
buffers from the mixer thread and unqueue items from the actual list.
- Capture just locks the SDL audio device for everything, since it's a very
lightweight load and a much simplified API; good enough. The capture device
thread is an almost-constant minimal load (1 or 2 memcpy's, depending on the
ring buffer position), and the worst load on the API side (alcCaptureSamples)
is the same deal, so this never takes long, and is good enough.
- Probably other things. These notes might get updates later.
*/
#if 0
#define FIXME(x)
#else
#define FIXME(x) { \
static int seen = 0; \
if (!seen) { \
seen = 1; \
fprintf(stderr, "FIXME: %s (%s@%s:%d)\n", x, __FUNCTION__, __FILE__, __LINE__); \
} \
}
#endif
/* lifted this ring buffer code from my al_osx project; I wrote it all, so it's stealable. */
typedef struct
{
ALCubyte *buffer;
ALCsizei size;
ALCsizei write;
ALCsizei read;
ALCsizei used;
} RingBuffer;
static void ring_buffer_put(RingBuffer *ring, const void *_data, const ALCsizei size)
{
const ALCubyte *data = (const ALCubyte *) _data;
ALCsizei cpy;
ALCsizei avail;
if (!size) /* just in case... */
return;
/* Putting more data than ring buffer holds in total? Replace it all. */
if (size > ring->size) {
ring->write = 0;
ring->read = 0;
ring->used = ring->size;
SDL_memcpy(ring->buffer, data + (size - ring->size), ring->size);
return;
}
/* Buffer overflow? Push read pointer to oldest sample not overwritten... */
avail = ring->size - ring->used;
if (size > avail) {
ring->read += size - avail;
if (ring->read > ring->size)
ring->read -= ring->size;
}
/* Clip to end of buffer and copy first block... */
cpy = ring->size - ring->write;
if (size < cpy)
cpy = size;
if (cpy) SDL_memcpy(ring->buffer + ring->write, data, cpy);
/* Wrap around to front of ring buffer and copy remaining data... */
avail = size - cpy;
if (avail) SDL_memcpy(ring->buffer, data + cpy, avail);
/* Update write pointer... */
ring->write += size;
if (ring->write > ring->size)
ring->write -= ring->size;
ring->used += size;
if (ring->used > ring->size)
ring->used = ring->size;
}
static ALCsizei ring_buffer_get(RingBuffer *ring, void *_data, ALCsizei size)
{
ALCubyte *data = (ALCubyte *) _data;
ALCsizei cpy;
ALCsizei avail = ring->used;
/* Clamp amount to read to available data... */
if (size > avail)
size = avail;
/* Clip to end of buffer and copy first block... */
cpy = ring->size - ring->read;
if (cpy > size) cpy = size;
if (cpy) SDL_memcpy(data, ring->buffer + ring->read, cpy);
/* Wrap around to front of ring buffer and copy remaining data... */
avail = size - cpy;
if (avail) SDL_memcpy(data + cpy, ring->buffer, avail);
/* Update read pointer... */
ring->read += size;
if (ring->read > ring->size)
ring->read -= ring->size;
ring->used -= size;
return size; /* may have been clamped if there wasn't enough data... */
}
typedef struct ALbuffer
{
SDL_atomic_t allocated;
ALuint name;
ALint channels;
ALint bits; /* always float32 internally, but this is what alBufferData saw */
ALsizei frequency;
ALsizei len; /* length of data in bytes. */
const float *data; /* we only work in Float32 format. */
SDL_atomic_t refcount; /* if zero, can be deleted or alBufferData'd */
} ALbuffer;
typedef struct BufferBlock
{
ALbuffer buffers[OPENAL_BUFFER_BLOCK_SIZE]; /* allocate these in blocks so we can step through faster. */
void *next; /* void* because we'll atomicgetptr it. */
} BufferBlock;
typedef struct BufferQueueItem
{
ALbuffer *buffer;
void *next; /* void* because we'll atomicgetptr it. */
} BufferQueueItem;
typedef struct BufferQueue
{
void *just_queued; /* void* because we'll atomicgetptr it. */
BufferQueueItem *head;
BufferQueueItem *tail;
SDL_atomic_t num_items; /* counts just_queued+head/tail */
} BufferQueue;
typedef struct ALsource
{
SDL_atomic_t allocated;
ALenum state; /* initial, playing, paused, stopped */
ALenum type; /* undetermined, static, streaming */
ALboolean source_relative;
ALboolean looping;
ALfloat gain;
ALfloat min_gain;
ALfloat max_gain;
ALfloat position[3];
ALfloat velocity[3];
ALfloat direction[3];
ALfloat reference_distance;
ALfloat max_distance;
ALfloat rolloff_factor;
ALfloat pitch;
ALfloat cone_inner_angle;
ALfloat cone_outer_angle;
ALfloat cone_outer_gain;
ALbuffer *buffer;
SDL_AudioStream *stream; /* for resampling. */
BufferQueue buffer_queue;
BufferQueue buffer_queue_processed;
SDL_SpinLock buffer_queue_lock; /* this serializes access to the API end. The mixer does not acquire this! */
ALsizei offset; /* offset in bytes for converted stream! */
ALboolean offset_latched; /* AL_SEC_OFFSET, etc, say set values apply to next alSourcePlay if not currently playing! */
ALint queue_channels;
ALsizei queue_frequency;
} ALsource;
struct ALCdevice_struct
{
char *name;
ALCenum error;
ALCboolean iscapture;
ALCboolean connected;
SDL_AudioDeviceID sdldevice;
ALint channels;
ALint frequency;
ALCsizei framesize;
union {
struct {
ALCcontext *contexts;
BufferBlock buffer_blocks; /* buffers are shared between contexts on the same device. */
void *buffer_queue_pool; /* void* because we'll atomicgetptr it. */
} playback;
struct {
RingBuffer ring; /* only used if iscapture */
} capture;
};
};
struct ALCcontext_struct
{
ALCdevice *device;
SDL_atomic_t processing;
ALenum error;
ALCint *attributes;
ALCsizei attributes_count;
struct {
ALfloat gain;
ALfloat position[3];
ALfloat velocity[3];
ALfloat orientation[6];
} listener;
ALenum distance_model;
ALfloat doppler_factor;
ALfloat doppler_velocity;
ALfloat speed_of_sound;
ALsource sources[OPENAL_MAX_SOURCES]; /* this array is indexed by ALuint source name. */
ALCcontext *prev; /* contexts are in a double-linked list */
ALCcontext *next;
};
/* ALC implementation... */
static void *current_context = NULL;
static ALCenum null_device_error = ALC_NO_ERROR;
/* we don't have any device-specific extensions. */
#define ALC_EXTENSION_ITEMS \
ALC_EXTENSION_ITEM(ALC_ENUMERATION_EXT) \
ALC_EXTENSION_ITEM(ALC_EXT_CAPTURE) \
ALC_EXTENSION_ITEM(ALC_EXT_DISCONNECT)
#define AL_EXTENSION_ITEMS \
AL_EXTENSION_ITEM(AL_EXT_FLOAT32)
static void set_alc_error(ALCdevice *device, const ALCenum error)
{
ALCenum *perr = device ? &device->error : &null_device_error;
/* can't set a new error when the previous hasn't been cleared yet. */
if (*perr == ALC_NO_ERROR) {
*perr = error;
}
}
ALCdevice *alcOpenDevice(const ALCchar *devicename)
{
ALCdevice *dev = NULL;
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
return NULL;
}
dev = (ALCdevice *) SDL_calloc(1, sizeof (ALCdevice));
if (!dev) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL;
}
if (!devicename) {
devicename = DEFAULT_PLAYBACK_DEVICE; /* so ALC_DEVICE_SPECIFIER is meaningful */
}
dev->name = SDL_strdup(devicename);
if (!dev->name) {
SDL_free(dev);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL;
}
/* we don't open an SDL audio device until the first context is
created, so we can attempt to match audio formats. */
dev->connected = ALC_TRUE;
dev->iscapture = ALC_FALSE;
return dev;
}
ALCboolean alcCloseDevice(ALCdevice *device)
{
if (!device || device->iscapture) {
return ALC_FALSE;
}
if (device->playback.contexts) {
return ALC_FALSE;
}
if (device->sdldevice) {
SDL_CloseAudioDevice(device->sdldevice);
}
SDL_free(device->name);
SDL_free(device);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return ALC_TRUE;
}
static ALCboolean alcfmt_to_sdlfmt(const ALCenum alfmt, SDL_AudioFormat *sdlfmt, Uint8 *channels, ALCsizei *framesize)
{
switch (alfmt) {
case AL_FORMAT_MONO8:
*sdlfmt = AUDIO_U8;
*channels = 1;
*framesize = 1;
break;
case AL_FORMAT_MONO16:
*sdlfmt = AUDIO_S16SYS;
*channels = 1;
*framesize = 2;
break;
case AL_FORMAT_STEREO8:
*sdlfmt = AUDIO_U8;
*channels = 2;
*framesize = 2;
break;
case AL_FORMAT_STEREO16:
*sdlfmt = AUDIO_S16SYS;
*channels = 2;
*framesize = 4;
break;
case AL_FORMAT_MONO_FLOAT32:
*sdlfmt = AUDIO_F32SYS;
*channels = 1;
*framesize = 4;
break;
case AL_FORMAT_STEREO_FLOAT32:
*sdlfmt = AUDIO_F32SYS;
*channels = 2;
*framesize = 8;
break;
default:
return ALC_FALSE;
}
return ALC_TRUE;
}
/* the just_queued list is backwards. Add it to the queue in the correct order. */
static void queue_new_buffer_items_recursive(BufferQueue *queue, BufferQueueItem *items)
{
if (items == NULL) {
return;
}
queue_new_buffer_items_recursive(queue, items->next);
items->next = NULL;
if (queue->tail) {
queue->tail->next = items;
} else {
queue->head = items;
}
queue->tail = items;
}
static void obtain_newly_queued_buffers(BufferQueue *queue)
{
BufferQueueItem *items;
do {
items = (BufferQueueItem *) SDL_AtomicGetPtr(&queue->just_queued);
} while (!SDL_AtomicCASPtr(&queue->just_queued, items, NULL));
/* Now that we own this pointer, we can just do whatever we want with it.
Nothing touches the head/tail fields other than the mixer thread, so we
move it there. Not even atomically! :)
When setting up these fields in alSourceUnqueueBuffers, there's a lock
used that is never held by the mixer thread (which only touches
just_queued atomically when a buffer is completely processed). */
SDL_assert((queue->tail != NULL) == (queue->head != NULL));
queue_new_buffer_items_recursive(queue, items);
}
static void mix_source_data_float32(ALCcontext *ctx, ALsource *src, const int channels, const ALfloat *panning, const float *data, float *stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
ALsizei i;
if ((left == 0.0f) && (right == 0.0f)) {
return; /* it's silent, don't spend time mixing it. */
}
FIXME("make this efficient. :)");
FIXME("currently expects output to be stereo");
if (channels == 1) {
if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < mixframes; i++) {
const float samp = *(data++);
*(stream++) += samp;
*(stream++) += samp;
}
} else {
for (i = 0; i < mixframes; i++) {
const float samp = *(data++);
*(stream++) += samp * left;
*(stream++) += samp * right;
}
}
} else if (channels == 2) {
if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < mixframes; i++) {
*(stream++) += *(data++);
*(stream++) += *(data++);
}
} else {
for (i = 0; i < mixframes; i++) {
*(stream++) += *(data++) * left;
*(stream++) += *(data++) * right;
}
}
}
}
static ALboolean mix_source_buffer(ALCcontext *ctx, ALsource *src, BufferQueueItem *queue, const ALfloat *panning, float **stream, int *len)
{
const ALbuffer *buffer = queue ? queue->buffer : NULL;
ALboolean processed = AL_TRUE;
/* you can legally queue or set a NULL buffer. */
if (buffer && buffer->data && (buffer->len > 0)) {
const float *data = buffer->data + (src->offset / sizeof (float));
const int bufferframesize = (int) (buffer->channels * sizeof (float));
const int deviceframesize = ctx->device->framesize;
const int framesneeded = *len / deviceframesize;
SDL_assert(src->offset < buffer->len);
if (src->stream) { /* resampling? */
int mixframes, mixlen, remainingmixframes;
while ( (((mixlen = SDL_AudioStreamAvailable(src->stream)) / bufferframesize) < framesneeded) && (src->offset < buffer->len) ) {
const int framesput = (buffer->len - src->offset) / bufferframesize;
const int bytesput = SDL_min(framesput, 1024) * bufferframesize;
FIXME("dynamically adjust frames here?"); /* we hardcode 1024 samples when opening the audio device, too. */
SDL_AudioStreamPut(src->stream, data, bytesput);
src->offset += bytesput;
data += bytesput / sizeof (float);
}
mixframes = SDL_min(mixlen / bufferframesize, framesneeded);
remainingmixframes = mixframes;
while (remainingmixframes > 0) {
const int mixbuflen = 1024;
float mixbuf[mixbuflen / sizeof (float)];
const int mixbufframes = mixbuflen / bufferframesize;
const int getframes = SDL_min(remainingmixframes, mixbufframes);
SDL_AudioStreamGet(src->stream, mixbuf, getframes * bufferframesize);
mix_source_data_float32(ctx, src, buffer->channels, panning, mixbuf, *stream, getframes);
*len -= getframes * deviceframesize;
*stream += getframes * ctx->device->channels;
remainingmixframes -= getframes;
}
} else {
const int framesavail = (buffer->len - src->offset) / bufferframesize;
const int mixframes = SDL_min(framesneeded, framesavail);
mix_source_data_float32(ctx, src, buffer->channels, panning, data, *stream, mixframes);
src->offset += mixframes * bufferframesize;
*len -= mixframes * deviceframesize;
*stream += mixframes * ctx->device->channels;
}
SDL_assert(src->offset <= buffer->len);
processed = src->offset >= buffer->len;
if (processed) {
FIXME("does the offset have to represent the whole queue or just the current buffer?");
src->offset = 0;
}
}
return processed;
}
static inline void mix_source_buffer_queue(ALCcontext *ctx, ALsource *src, BufferQueueItem *queue, const ALfloat *panning, float *stream, int len)
{
while ((len > 0) && (mix_source_buffer(ctx, src, queue, panning, &stream, &len))) {
/* Finished this buffer! */
BufferQueueItem *item = queue;
BufferQueueItem *next = queue ? queue->next : NULL;
void *ptr;
if (queue) {
queue->next = NULL;
queue = next;
}
SDL_assert((src->type == AL_STATIC) || (src->type == AL_STREAMING));
if (src->type == AL_STREAMING) { /* mark buffer processed. */
SDL_assert(item == src->buffer_queue.head);
FIXME("bubble out all these NULL checks"); // these are only here because we check for looping/stopping in this loop, but we really shouldn't enter this loop at all if queue==NULL.
if (item != NULL) {
src->buffer_queue.head = next;
if (!next) {
src->buffer_queue.tail = NULL;
}
SDL_AtomicAdd(&src->buffer_queue.num_items, -1);
/* Move it to the processed queue for alSourceUnqueueBuffers() to pick up. */
do {
ptr = SDL_AtomicGetPtr(&src->buffer_queue_processed.just_queued);
SDL_AtomicSetPtr(&item->next, ptr);
} while (!SDL_AtomicCASPtr(&src->buffer_queue_processed.just_queued, ptr, item));
SDL_AtomicAdd(&src->buffer_queue_processed.num_items, 1);
}
}
if (queue == NULL) { /* nothing else to play? */
if (src->looping) {
FIXME("looping is supposed to move to AL_INITIAL then immediately to AL_PLAYING, but I'm not sure what side effect this is meant to trigger");
if (src->type == AL_STREAMING) {
FIXME("what does looping do with the AL_STREAMING state?");
}
} else {
src->state = AL_STOPPED;
}
break; /* nothing else to mix here, so stop. */
}
}
}
/* All the 3D math here is way overcommented because I HAVE NO IDEA WHAT I'M
DOING and had to research the hell out of what are probably pretty simple
concepts. Pay attention in math class, kids. */
/* calculates cross product. https://en.wikipedia.org/wiki/Cross_product
Basically takes two vectors and gives you a vector that's perpendicular
to both.
*/
static void xyzzy(ALfloat *v, const ALfloat *a, const ALfloat *b)
{
v[0] = (a[1] * b[2]) - (a[2] * b[1]);
v[1] = (a[2] * b[0]) - (a[0] * b[2]);
v[2] = (a[0] * b[1]) - (a[1] * b[0]);
}
/* calculate dot product (multiply each element of two vectors, sum them) */
static ALfloat dotproduct(const ALfloat *a, const ALfloat *b)
{
return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
}
/* calculate distance ("magnitude") in 3D space:
https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
assumes vector starts at (0,0,0). */
static ALfloat magnitude(const ALfloat *v)
{
/* technically, the inital part on this is just a dot product of itself. */
return sqrtf((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
}
/* https://www.khanacademy.org/computing/computer-programming/programming-natural-simulations/programming-vectors/a/vector-magnitude-normalization */
static void normalize(ALfloat *v)
{
const ALfloat mag = magnitude(v);
if (mag == 0.0f) {
SDL_memset(v, '\0', sizeof (*v) * 3);
} else {
v[0] /= mag;
v[1] /= mag;
v[2] /= mag;
}
}
/* Get the sin(angle) and cos(angle) at the same time. Ideally, with one
instruction, like what is offered on the x86.
angle is in radians, not degrees. */
static void sincos(const ALfloat angle, ALfloat *_sin, ALfloat *_cos)
{
FIXME("use sincos() or asm or compiler intrinsics?");
*_sin = sinf(angle);
*_cos = cosf(angle);
}
static ALfloat calculate_distance_attenuation(const ALCcontext *ctx, const ALsource *src, const ALfloat *position)
{
ALfloat distance;
distance = magnitude(position);
/* AL SPEC: "With all the distance models, if the formula can not be
evaluated then the source will not be attenuated. For example, if a
linear model is being used with AL_REFERENCE_DISTANCE equal to
AL_MAX_DISTANCE, then the gain equation will have a divide-by-zero
error in it. In this case, there is no attenuation for that source." */
FIXME("check divisions by zero");
switch (ctx->distance_model) {
case AL_INVERSE_DISTANCE_CLAMPED:
distance = SDL_min(SDL_max(distance, src->reference_distance), src->max_distance);
/* fallthrough */
case AL_INVERSE_DISTANCE:
/* AL SPEC: "gain = AL_REFERENCE_DISTANCE / (AL_REFERENCE_DISTANCE + AL_ROLLOFF_FACTOR * (distance - AL_REFERENCE_DISTANCE))" */
return src->reference_distance / (src->reference_distance + src->rolloff_factor * (distance - src->reference_distance));
case AL_LINEAR_DISTANCE_CLAMPED:
distance = SDL_max(distance, src->reference_distance);
/* fallthrough */
case AL_LINEAR_DISTANCE:
/* AL SPEC: "distance = min(distance, AL_MAX_DISTANCE) // avoid negative gain
gain = (1 - AL_ROLLOFF_FACTOR * (distance - AL_REFERENCE_DISTANCE) / (AL_MAX_DISTANCE - AL_REFERENCE_DISTANCE))" */
return 1.0f - src->rolloff_factor * (SDL_min(distance, src->max_distance) - src->reference_distance) / (src->max_distance - src->reference_distance);
case AL_EXPONENT_DISTANCE_CLAMPED:
distance = SDL_min(SDL_max(distance, src->reference_distance), src->max_distance);
/* fallthrough */
case AL_EXPONENT_DISTANCE:
/* AL SPEC: "gain = (distance / AL_REFERENCE_DISTANCE) ^ (- AL_ROLLOFF_FACTOR)" */
return SDL_powf(distance / src->reference_distance, -src->rolloff_factor);
default: break;
}
SDL_assert(!"Unexpected distance model");
return 1.0f;
}
static void calculate_channel_gains(const ALCcontext *ctx, const ALsource *src, float *gains)
{
/* rolloff==0.0f makes all distance models result in 1.0f,
and we never spatialize non-mono sources, per the AL spec. */
const ALboolean spatialize = (ctx->distance_model != AL_NONE) &&
(src->queue_channels == 1) &&
(src->rolloff_factor != 0.0f);
ALfloat U[3];
ALfloat V[3];
ALfloat N[3];
ALfloat rotated[3];
ALfloat gain;
ALfloat position[3];
FIXME("SIMD all of this");
/* this goes through the steps the AL spec dictates for gain and distance attenuation... */
if (!spatialize) {
/* simpler path through the same AL spec details if not spatializing. */
gain = SDL_min(SDL_max(src->gain, src->min_gain), src->max_gain) * ctx->listener.gain;
gains[0] = gains[1] = gain; /* no spatialization, but AL_GAIN (etc) is still applied. */
return;
}
SDL_memcpy(position, src->position, sizeof (position));
/* if values aren't source-relative, then convert it to be so. */
if (!src->source_relative) {
position[0] -= ctx->listener.position[0];
position[1] -= ctx->listener.position[1];
position[2] -= ctx->listener.position[2];
}
/* AL SPEC: ""1. Distance attenuation is calculated first, including
minimum (AL_REFERENCE_DISTANCE) and maximum (AL_MAX_DISTANCE)
thresholds." */
gain = calculate_distance_attenuation(ctx, src, position);
/* AL SPEC: "2. The result is then multiplied by source gain (AL_GAIN)." */
gain *= src->gain;
/* AL SPEC: "3. If the source is directional (AL_CONE_INNER_ANGLE less
than AL_CONE_OUTER_ANGLE), an angle-dependent attenuation is calculated
depending on AL_CONE_OUTER_GAIN, and multiplied with the distance
dependent attenuation. The resulting attenuation factor for the given
angle and distance between listener and source is multiplied with
source AL_GAIN." */
if (src->cone_inner_angle < src->cone_outer_angle) {
FIXME("directional sources");
}
/* AL SPEC: "4. The effective gain computed this way is compared against
AL_MIN_GAIN and AL_MAX_GAIN thresholds." */
gain = SDL_min(SDL_max(gain, src->min_gain), src->max_gain);
/* AL SPEC: "5. The result is guaranteed to be clamped to [AL_MIN_GAIN,
AL_MAX_GAIN], and subsequently multiplied by listener gain which serves
as an overall volume control. The implementation is free to clamp
listener gain if necessary due to hardware or implementation
constraints." */
gain *= ctx->listener.gain;
/* now figure out positioning. Since we're aiming for stereo, we just
need a simple panning effect. We're going to do what's called
"constant power panning," as explained...
https://dsp.stackexchange.com/questions/21691/algorithm-to-pan-audio
Naturally, we'll need to know the angle between where our listener
is facing and where the source is to make that work...
https://www.youtube.com/watch?v=S_568VZWFJo
...but to do that, we need to rotate so we have the correct side of
the listener, which isn't just a point in space, but has a definite
direction it is facing. More or less, this is what gluLookAt deals
with...
http://www.songho.ca/opengl/gl_camera.html
...although I messed with the algorithm until it did what I wanted.
XYZZY!! https://en.wikipedia.org/wiki/Cross_product#Mnemonic
*/
/* cross product of listener At and Up... */
xyzzy(U, &ctx->listener.orientation[0], &ctx->listener.orientation[3]);
normalize(U);
xyzzy(V, &ctx->listener.orientation[0], U);
SDL_memcpy(N, &ctx->listener.orientation[0], sizeof (N));
normalize(N);
/* we don't need the bottom row of the gluLookAt matrix, since we don't
translate. (Matrix * Vector) is just filling in each element of the
output vector with the dot product of a row of the matrix and the
vector. I made some of these negative to make it work for my purposes,
but that's not what GLU does here.
(This says gluLookAt is left-handed, so maybe that's part of it?)
https://stackoverflow.com/questions/25933581/how-u-v-n-camera-coordinate-system-explained-with-opengl
*/
rotated[0] = dotproduct(position, U);
rotated[1] = -dotproduct(position, V);
rotated[2] = -dotproduct(position, N);
/* At this point, we have rotated vector and we can calculate the angle
from 0 (directly in front of where the listener is facing) to 180
degrees (directly behind) ... */
const ALfloat *at = &ctx->listener.orientation[0];
const ALfloat mags = magnitude(at) * magnitude(rotated);
ALfloat radians = (mags == 0.0f) ? 0.0f : acosf(dotproduct(at, rotated) / mags);
/* and we already have what we need to decide if those degrees are on the
listener's left or right...
https://gamedev.stackexchange.com/questions/43897/determining-if-something-is-on-the-right-or-left-side-of-an-object
...we already did this dot product: it's in rotated[0]. */
/* make it negative to the left, positive to the right. */
if (rotated[0] < 0.0f) {
radians = -radians;
}
/* here comes the Constant Power Panning magic... */
#define SQRT2_DIV2 0.7071067812f /* sqrt(2.0) / 2.0 ... */
/* this might be a terrible idea, which is totally my own doing here,
but here you go: Constant Power Panning only works from -45 to 45
degrees in front of the listener. So we split this into 4 quadrants.
- from -45 to 45: standard panning.
- from 45 to 135: pan full right.
- from 135 to 225: flip angle so it works like standard panning.
- from 225 to -45: pan full left. */
#define RADIANS_45_DEGREES 0.7853981634f
#define RADIANS_135_DEGREES 2.3561944902f
if ((radians >= -RADIANS_45_DEGREES) && (radians <= RADIANS_45_DEGREES)) {
ALfloat sine, cosine;
sincos(radians, &sine, &cosine);
gains[0] = (SQRT2_DIV2 * (cosine - sine));
gains[1] = (SQRT2_DIV2 * (cosine + sine));
} else if ((radians >= RADIANS_45_DEGREES) && (radians <= RADIANS_135_DEGREES)) {
gains[0] = 0.0f;
gains[1] = 1.0f;
} else if ((radians >= -RADIANS_135_DEGREES) && (radians <= -RADIANS_45_DEGREES)) {
gains[0] = 1.0f;
gains[1] = 0.0f;
} else if (radians < 0.0f) { /* back left */
ALfloat sine, cosine;
sincos(-(radians + M_PI), &sine, &cosine);
gains[0] = (SQRT2_DIV2 * (cosine - sine));
gains[1] = (SQRT2_DIV2 * (cosine + sine));
} else { /* back right */
ALfloat sine, cosine;
sincos(-(radians - M_PI), &sine, &cosine);
gains[0] = (SQRT2_DIV2 * (cosine - sine));
gains[1] = (SQRT2_DIV2 * (cosine + sine));
}
#if 0
static ALfloat saved[3];
if (SDL_memcmp(saved, src->position, sizeof (saved)) != 0) {
const ALfloat degrees = radians * (180.0 / M_PI);
printf("srcpos=(%f, %f, %f) rotated=(%f, %f, %f) degrees=%f left=%f right=%f gain=%f\n", src->position[0], src->position[1], src->position[2], rotated[0], rotated[1], rotated[2], degrees, gains[0], gains[1], gain);
SDL_memcpy(saved, src->position, sizeof (saved));
}
#endif
/* apply distance attenuation and gain to positioning. */
gains[0] *= gain;
gains[1] *= gain;
}
static void mix_source(ALCcontext *ctx, ALsource *src, float *stream, int len)
{
float panning[2]; /* we only do stereo for now */
if (SDL_AtomicGet(&src->allocated) != 1) {
return; /* not in use */
} else if (src->state != AL_PLAYING) {
return; /* not playing, don't process. */
}
calculate_channel_gains(ctx, src, panning);
if (src->type == AL_STATIC) {
BufferQueueItem fakequeue = { src->buffer, NULL };
mix_source_buffer_queue(ctx, src, &fakequeue, panning, stream, len);
} else if (src->type == AL_STREAMING) {
obtain_newly_queued_buffers(&src->buffer_queue);
mix_source_buffer_queue(ctx, src, src->buffer_queue.head, panning, stream, len);
}
}
static void mix_context(ALCcontext *ctx, float *stream, int len)
{
ALsizei i;
FIXME("keep a small array of playing sources so we don't have to walk the whole array");
for (i = 0; i < OPENAL_MAX_SOURCES; i++) {
mix_source(ctx, &ctx->sources[i], stream, len);
}
}
/* Disconnected devices move all PLAYING sources to STOPPED, making their buffer queues processed. */
static void mix_disconnected_context(ALCcontext *ctx)
{
ALsizei i;
FIXME("keep a small array of playing sources so we don't have to walk the whole array");
for (i = 0; i < OPENAL_MAX_SOURCES; i++) {
ALsource *src = &ctx->sources[i];
if (SDL_AtomicGet(&src->allocated) != 1) {
continue; /* not in use */
}
obtain_newly_queued_buffers(&src->buffer_queue);
if (src->state == AL_PLAYING) {
src->state = AL_STOPPED;
if (src->type == AL_STREAMING) { /* mark buffers processed. */
while (src->buffer_queue.head) {
void *ptr;
BufferQueueItem *item = src->buffer_queue.head;
src->buffer_queue.head = item->next;
SDL_AtomicAdd(&src->buffer_queue.num_items, -1);
/* Move it to the processed queue for alSourceUnqueueBuffers() to pick up. */
do {
ptr = SDL_AtomicGetPtr(&src->buffer_queue_processed.just_queued);
SDL_AtomicSetPtr(&item->next, ptr);
} while (!SDL_AtomicCASPtr(&src->buffer_queue_processed.just_queued, ptr, item));
SDL_AtomicAdd(&src->buffer_queue_processed.num_items, 1);
}
src->buffer_queue.tail = NULL;
}
}
}
}
/* We process all unsuspended ALC contexts during this call, mixing their
output to (stream). SDL then plays this mixed audio to the hardware. */
static void SDLCALL playback_device_callback(void *userdata, Uint8 *stream, int len)
{
ALCdevice *device = (ALCdevice *) userdata;