-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmojoal.c
4145 lines (3586 loc) · 137 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>
#ifdef _MSC_VER
#define AL_API __declspec(dllexport)
#define ALC_API __declspec(dllexport)
#if !defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#endif
#include "AL/al.h"
#include "AL/alc.h"
#include "SDL.h"
#ifdef __SSE__ /* if you are on x86 or x86-64, we assume you have SSE1 by now. */
#define NEED_SCALAR_FALLBACK 0
#elif (defined(__ARM_ARCH) && (__ARM_ARCH >= 8)) /* ARMv8 always has NEON. */
#define NEED_SCALAR_FALLBACK 0
#elif (defined(__APPLE__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) /* All ARMv7 chips from Apple have NEON. */
#define NEED_SCALAR_FALLBACK 0
#elif (defined(__WINDOWS__) || defined(__WINRT__)) && defined(_M_ARM) /* all WinRT-level Microsoft devices have NEON */
#define NEED_SCALAR_FALLBACK 0
#else
#define NEED_SCALAR_FALLBACK 1
#endif
/* Some platforms fail to define __ARM_NEON__, others need it or arm_neon.h will fail. */
#if (defined(__ARM_ARCH) || defined(_M_ARM))
# if !NEED_SCALAR_FALLBACK && !defined(__ARM_NEON__)
# define __ARM_NEON__ 1
# endif
#endif
#ifdef __SSE__
#include <xmmintrin.h>
#endif
#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif
#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.
- Creating 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 1
#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
/* restrict is from C99, but __restrict works with both Visual Studio and GCC. */
#if !defined(restrict) && ((!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901)))
#define restrict __restrict
#endif
#ifdef _MSC_VER
#define SIMDALIGNEDSTRUCT __declspec(align(16)) struct
#elif (defined(__GNUC__) || defined(__clang__))
#define SIMDALIGNEDSTRUCT struct __attribute__((aligned(16)))
#else
#define SIMDALIGNEDSTRUCT struct
#endif
#ifdef __SSE__ /* we assume you always have this on x86/x86-64 chips. SSE1 is 20 years old! */
#define has_sse 1
#endif
#ifdef __ARM_NEON__
#if NEED_SCALAR_FALLBACK
static int has_neon = 0;
#else
#define has_neon 1
#endif
#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... */
}
static void *calloc_simd_aligned(const size_t len)
{
Uint8 *retval = NULL;
Uint8 *ptr = (Uint8 *) SDL_calloc(1, len + 16 + sizeof (void *));
if (ptr) {
void **storeptr;
retval = ptr + sizeof (void *);
retval += 16 - (((size_t) retval) % 16);
storeptr = (void **) retval;
storeptr--;
*storeptr = ptr;
}
return retval;
}
static void free_simd_aligned(void *ptr)
{
if (ptr) {
void **realptr = (void **) ptr;
realptr--;
SDL_free(*realptr);
}
}
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 ALsource;
SIMDALIGNEDSTRUCT ALsource
{
/* keep these first to help guarantee that its elements are aligned for SIMD */
ALfloat position[4];
ALfloat velocity[4];
ALfloat direction[4];
ALfloat panning[2]; /* we only do stereo for now */
SDL_atomic_t allocated;
SDL_SpinLock lock;
ALenum state; /* initial, playing, paused, stopped */
ALenum type; /* undetermined, static, streaming */
ALboolean recalc;
ALboolean source_relative;
ALboolean looping;
ALfloat gain;
ALfloat min_gain;
ALfloat max_gain;
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;
};
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
{
/* keep these first to help guarantee that its elements are aligned for SIMD */
ALsource sources[OPENAL_MAX_SOURCES]; /* this array is indexed by ALuint source name. */
SIMDALIGNEDSTRUCT {
ALfloat position[4];
ALfloat velocity[4];
ALfloat orientation[8];
ALfloat gain;
} listener;
ALCdevice *device;
SDL_atomic_t processing;
ALenum error;
ALCint *attributes;
ALCsizei attributes_count;
ALCboolean recalc;
ALenum distance_model;
ALfloat doppler_factor;
ALfloat doppler_velocity;
ALfloat speed_of_sound;
SDL_atomic_t to_be_played[OPENAL_MAX_SOURCES / (sizeof (SDL_atomic_t) * 8)];
int playlist[OPENAL_MAX_SOURCES / (sizeof (SDL_atomic_t) * 8)];
ALCcontext *prev; /* contexts are in a double-linked list */
ALCcontext *next;
};
/* 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);
}
/* You probably need to hold a lock before you call this (currently). */
static void source_mark_all_buffers_processed(ALsource *src)
{
obtain_newly_queued_buffers(&src->buffer_queue);
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;
}
/* You probably need to hold a lock before you call this (currently). */
static void source_release_buffer_queue(ALCcontext *ctx, ALsource *src)
{
BufferQueueItem *i;
void *ptr;
/* move any buffer queue items to the device's available pool for reuse. */
obtain_newly_queued_buffers(&src->buffer_queue);
if (src->buffer_queue.tail != NULL) {
for (i = src->buffer_queue.head; i; i = i->next) {
(void) SDL_AtomicDecRef(&i->buffer->refcount);
}
do {
ptr = SDL_AtomicGetPtr(&ctx->device->playback.buffer_queue_pool);
SDL_AtomicSetPtr(&src->buffer_queue.tail->next, ptr);
} while (!SDL_AtomicCASPtr(&ctx->device->playback.buffer_queue_pool, ptr, src->buffer_queue.head));
}
src->buffer_queue.head = src->buffer_queue.tail = NULL;
SDL_AtomicLock(&src->buffer_queue_lock);
obtain_newly_queued_buffers(&src->buffer_queue_processed);
if (src->buffer_queue_processed.tail != NULL) {
for (i = src->buffer_queue_processed.head; i; i = i->next) {
(void) SDL_AtomicDecRef(&i->buffer->refcount);
}
do {
ptr = SDL_AtomicGetPtr(&ctx->device->playback.buffer_queue_pool);
SDL_AtomicSetPtr(&src->buffer_queue_processed.tail->next, ptr);
} while (!SDL_AtomicCASPtr(&ctx->device->playback.buffer_queue_pool, ptr, src->buffer_queue_processed.head));
}
src->buffer_queue_processed.head = src->buffer_queue_processed.tail = NULL;
SDL_AtomicUnlock(&src->buffer_queue_lock);
}
/* 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;
}
}
/* all data written before the release barrier must be available before the recalc flag changes. */ \
#define context_needs_recalc(ctx) SDL_MemoryBarrierRelease(); ctx->recalc = AL_TRUE;
#define source_needs_recalc(src) SDL_MemoryBarrierRelease(); src->recalc = AL_TRUE;
ALCdevice *alcOpenDevice(const ALCchar *devicename)
{
ALCdevice *dev = NULL;
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
return NULL;
}
#ifdef __SSE__
if (!SDL_HasSSE()) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL; /* whoa! Better order a new Pentium III from Gateway 2000! */
}
#endif
#if defined(__ARM_NEON__) && !NEED_SCALAR_FALLBACK
if (!SDL_HasNEON()) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL; /* :( */
}
#elif defined(__ARM_NEON__) && NEED_SCALAR_FALLBACK
has_neon = SDL_HasNEON();
#endif
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)
{
BufferBlock *bb;
BufferQueueItem *item;
if (!device || device->iscapture) {
return ALC_FALSE;
}
if (device->playback.contexts) {
return ALC_FALSE;
}
for (bb = &device->playback.buffer_blocks; bb; bb = bb->next) {
ALbuffer *buf = bb->buffers;
int i;
for (i = 0; i < SDL_arraysize(bb->buffers); i++, buf++) {
if (SDL_AtomicGet(&buf->allocated) == 1) {
return ALC_FALSE;
}
}
}
if (device->sdldevice) {
SDL_CloseAudioDevice(device->sdldevice);
}
bb = device->playback.buffer_blocks.next;
while (bb) {
BufferBlock *next = bb->next;
SDL_free(bb);
bb = next;
}
item = (BufferQueueItem *) device->playback.buffer_queue_pool;
while (item) {
BufferQueueItem *next = item->next;
SDL_free(item);
item = next;
}
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;
}
static void mix_float32_c1_scalar(const ALfloat * restrict panning, const float * restrict data, float * restrict stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
const int unrolled = mixframes / 4;
const int leftover = mixframes % 4;
ALsizei i;
if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < unrolled; i++, data += 4, stream += 8) {
const float samp0 = data[0];
const float samp1 = data[1];
const float samp2 = data[2];
const float samp3 = data[3];
stream[0] += samp0;
stream[1] += samp0;
stream[2] += samp1;
stream[3] += samp1;
stream[4] += samp2;
stream[5] += samp2;
stream[6] += samp3;
stream[7] += samp3;
}
for (i = 0; i < leftover; i++, stream += 2) {
const float samp = *(data++);
stream[0] += samp;
stream[1] += samp;
}
} else {
for (i = 0; i < unrolled; i++, data += 4, stream += 8) {
const float samp0 = data[0];
const float samp1 = data[1];
const float samp2 = data[2];
const float samp3 = data[3];
stream[0] += samp0 * left;
stream[1] += samp0 * right;
stream[2] += samp1 * left;
stream[3] += samp1 * right;
stream[4] += samp2 * left;
stream[5] += samp2 * right;
stream[6] += samp3 * left;
stream[7] += samp3 * right;
}
for (i = 0; i < leftover; i++, stream += 2) {
const float samp = *(data++);
stream[0] += samp * left;
stream[1] += samp * right;
}
}
}
static void mix_float32_c2_scalar(const ALfloat * restrict panning, const float * restrict data, float * restrict stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
const int unrolled = mixframes / 4;
const int leftover = mixframes % 4;
ALsizei i;
if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < unrolled; i++, stream += 8, data += 8) {
stream[0] += data[0];
stream[1] += data[1];
stream[2] += data[2];
stream[3] += data[3];
stream[4] += data[4];
stream[5] += data[5];
stream[6] += data[6];
stream[7] += data[7];
}
for (i = 0; i < leftover; i++, stream += 2, data += 2) {
stream[0] += data[0];
stream[1] += data[1];
}
} else {
for (i = 0; i < unrolled; i++, stream += 8, data += 8) {
stream[0] += data[0] * left;
stream[1] += data[1] * right;
stream[2] += data[2] * left;
stream[3] += data[3] * right;
stream[4] += data[4] * left;
stream[5] += data[5] * right;
stream[6] += data[6] * left;
stream[7] += data[7] * right;
}
for (i = 0; i < leftover; i++, stream += 2, data += 2) {
stream[0] += data[0] * left;
stream[1] += data[1] * right;
}
}
}
#ifdef __SSE__
static void mix_float32_c1_sse(const ALfloat * restrict panning, const float * restrict data, float * restrict stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
const int unrolled = mixframes / 8;
const int leftover = mixframes % 8;
ALsizei i;
/* We can align this to 16 in one special case. */
if ( ((((size_t)data) % 16) == 8) && ((((size_t)stream) % 16) == 0) && (mixframes >= 2) ) {
stream[0] += data[0] * left;
stream[1] += data[0] * right;
stream[2] += data[1] * left;
stream[3] += data[1] * right;
stream += 4;
data += 2;
mix_float32_c1_sse(panning, data + 2, stream + 2, mixframes - 2);
} else if ( (((size_t)stream) % 16) || (((size_t)data) % 16) ) {
/* unaligned, do scalar version. */
mix_float32_c1_scalar(panning, data, stream, mixframes);
} else if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < unrolled; i++, data += 8, stream += 16) {
/* We have 8 SSE registers, load 6 of them, have two for math (unrolled once). */
{
const __m128 vdataload1 = _mm_load_ps(data);
const __m128 vdataload2 = _mm_load_ps(data+4);
const __m128 vstream1 = _mm_load_ps(stream);
const __m128 vstream2 = _mm_load_ps(stream+4);
const __m128 vstream3 = _mm_load_ps(stream+8);
const __m128 vstream4 = _mm_load_ps(stream+12);
_mm_store_ps(stream, _mm_add_ps(vstream1, _mm_shuffle_ps(vdataload1, vdataload1, _MM_SHUFFLE(0, 0, 1, 1))));
_mm_store_ps(stream+4, _mm_add_ps(vstream2, _mm_shuffle_ps(vdataload1, vdataload1, _MM_SHUFFLE(2, 2, 3, 3))));
_mm_store_ps(stream+8, _mm_add_ps(vstream3, _mm_shuffle_ps(vdataload2, vdataload2, _MM_SHUFFLE(0, 0, 1, 1))));
_mm_store_ps(stream+12, _mm_add_ps(vstream4, _mm_shuffle_ps(vdataload2, vdataload2, _MM_SHUFFLE(2, 2, 3, 3))));
}
}
for (i = 0; i < leftover; i++, stream += 2) {
const float samp = *(data++);
stream[0] += samp;
stream[1] += samp;
}
} else {
const __m128 vleftright = { left, right, left, right };
for (i = 0; i < unrolled; i++, data += 8, stream += 16) {
/* We have 8 SSE registers, load 6 of them, have two for math (unrolled once). */
const __m128 vdataload1 = _mm_load_ps(data);
const __m128 vdataload2 = _mm_load_ps(data+4);
const __m128 vstream1 = _mm_load_ps(stream);
const __m128 vstream2 = _mm_load_ps(stream+4);
const __m128 vstream3 = _mm_load_ps(stream+8);
const __m128 vstream4 = _mm_load_ps(stream+12);
_mm_store_ps(stream, _mm_add_ps(vstream1, _mm_mul_ps(_mm_shuffle_ps(vdataload1, vdataload1, _MM_SHUFFLE(0, 0, 1, 1)), vleftright)));
_mm_store_ps(stream+4, _mm_add_ps(vstream2, _mm_mul_ps(_mm_shuffle_ps(vdataload1, vdataload1, _MM_SHUFFLE(2, 2, 3, 3)), vleftright)));
_mm_store_ps(stream+8, _mm_add_ps(vstream3, _mm_mul_ps(_mm_shuffle_ps(vdataload2, vdataload2, _MM_SHUFFLE(0, 0, 1, 1)), vleftright)));
_mm_store_ps(stream+12, _mm_add_ps(vstream4, _mm_mul_ps(_mm_shuffle_ps(vdataload2, vdataload2, _MM_SHUFFLE(2, 2, 3, 3)), vleftright)));
}
for (i = 0; i < leftover; i++, stream += 2) {
const float samp = *(data++);
stream[0] += samp * left;
stream[1] += samp * right;
}
}
}
static void mix_float32_c2_sse(const ALfloat * restrict panning, const float * restrict data, float * restrict stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
const int unrolled = mixframes / 4;
const int leftover = mixframes % 4;
ALsizei i;
/* We can align this to 16 in one special case. */
if ( ((((size_t)stream) % 16) == 8) && ((((size_t)data) % 16) == 8) && mixframes ) {
stream[0] += data[0] * left;
stream[1] += data[1] * right;
stream += 2;
data += 2;
mix_float32_c2_sse(panning, data + 2, stream + 2, mixframes - 1);
} else if ( (((size_t)stream) % 16) || (((size_t)data) % 16) ) {
/* unaligned, do scalar version. */
mix_float32_c2_scalar(panning, data, stream, mixframes);
} else if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < unrolled; i++, data += 8, stream += 8) {
const __m128 vdata1 = _mm_load_ps(data);
const __m128 vdata2 = _mm_load_ps(data+4);
const __m128 vstream1 = _mm_load_ps(stream);
const __m128 vstream2 = _mm_load_ps(stream+4);
_mm_store_ps(stream, _mm_add_ps(vstream1, vdata1));
_mm_store_ps(stream+4, _mm_add_ps(vstream2, vdata2));
}
for (i = 0; i < leftover; i++, stream += 2, data += 2) {
stream[0] += data[0];
stream[1] += data[1];
}
} else {
const __m128 vleftright = { left, right, left, right };
for (i = 0; i < unrolled; i++, data += 8, stream += 8) {
const __m128 vdata1 = _mm_load_ps(data);
const __m128 vdata2 = _mm_load_ps(data+4);
const __m128 vstream1 = _mm_load_ps(stream);
const __m128 vstream2 = _mm_load_ps(stream+4);
_mm_store_ps(stream, _mm_add_ps(vstream1, _mm_mul_ps(vdata1, vleftright)));
_mm_store_ps(stream+4, _mm_add_ps(vstream2, _mm_mul_ps(vdata2, vleftright)));
}
for (i = 0; i < leftover; i++, stream += 2, data += 2) {
stream[0] += data[0] * left;
stream[1] += data[1] * right;
}
}
}
#endif
#ifdef __ARM_NEON__
static void mix_float32_c1_neon(const ALfloat * restrict panning, const float * restrict data, float * restrict stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
const int unrolled = mixframes / 8;
const int leftover = mixframes % 8;
ALsizei i;
/* We can align this to 16 in one special case. */
if ( ((((size_t)data) % 16) == 8) && ((((size_t)stream) % 16) == 0) && (mixframes >= 2) ) {
stream[0] += data[0] * left;
stream[1] += data[0] * right;
stream[2] += data[1] * left;
stream[3] += data[1] * right;
stream += 4;
data += 2;
mix_float32_c1_neon(panning, data + 2, stream + 2, mixframes - 2);
} else if ( (((size_t)stream) % 16) || (((size_t)data) % 16) ) {
/* unaligned, do scalar version. */
mix_float32_c1_scalar(panning, data, stream, mixframes);
} else if ((left == 1.0f) && (right == 1.0f)) {
for (i = 0; i < unrolled; i++, data += 8, stream += 16) {
const float32x4_t vdataload1 = vld1q_f32(data);
const float32x4_t vdataload2 = vld1q_f32(data+4);
const float32x4_t vstream1 = vld1q_f32(stream);
const float32x4_t vstream2 = vld1q_f32(stream+4);
const float32x4_t vstream3 = vld1q_f32(stream+8);
const float32x4_t vstream4 = vld1q_f32(stream+12);
const float32x4x2_t vzipped1 = vzipq_f32(vdataload1, vdataload1);
const float32x4x2_t vzipped2 = vzipq_f32(vdataload2, vdataload2);
vst1q_f32(stream, vaddq_f32(vstream1, vzipped1.val[0]));
vst1q_f32(stream+4, vaddq_f32(vstream2, vzipped1.val[1]));
vst1q_f32(stream+8, vaddq_f32(vstream3, vzipped2.val[0]));
vst1q_f32(stream+12, vaddq_f32(vstream4, vzipped2.val[1]));
}
for (i = 0; i < leftover; i++, stream += 2) {
const float samp = *(data++);
stream[0] += samp;
stream[1] += samp;
}
} else {
const float32x4_t vleftright = { left, right, left, right };
for (i = 0; i < unrolled; i++, data += 8, stream += 16) {
const float32x4_t vdataload1 = vld1q_f32(data);
const float32x4_t vdataload2 = vld1q_f32(data+4);
const float32x4_t vstream1 = vld1q_f32(stream);
const float32x4_t vstream2 = vld1q_f32(stream+4);
const float32x4_t vstream3 = vld1q_f32(stream+8);
const float32x4_t vstream4 = vld1q_f32(stream+12);
const float32x4x2_t vzipped1 = vzipq_f32(vdataload1, vdataload1);
const float32x4x2_t vzipped2 = vzipq_f32(vdataload2, vdataload2);
vst1q_f32(stream, vmlaq_f32(vstream1, vzipped1.val[0], vleftright));
vst1q_f32(stream+4, vmlaq_f32(vstream2, vzipped1.val[1], vleftright));
vst1q_f32(stream+8, vmlaq_f32(vstream3, vzipped2.val[0], vleftright));
vst1q_f32(stream+12, vmlaq_f32(vstream4, vzipped2.val[1], vleftright));
}
for (i = 0; i < leftover; i++, stream += 2) {
const float samp = *(data++);
stream[0] += samp * left;
stream[1] += samp * right;
}
}
}
static void mix_float32_c2_neon(const ALfloat * restrict panning, const float * restrict data, float * restrict stream, const ALsizei mixframes)
{
const ALfloat left = panning[0];
const ALfloat right = panning[1];
const int unrolled = mixframes / 8;
const int leftover = mixframes % 8;
ALsizei i;
/* We can align this to 16 in one special case. */
if ( ((((size_t)stream) % 16) == 8) && ((((size_t)data) % 16) == 8) && mixframes ) {
stream[0] += data[0] * left;
stream[1] += data[1] * right;
stream += 2;
data += 2;
mix_float32_c2_neon(panning, data + 2, stream + 2, mixframes - 1);
} else if ( (((size_t)stream) % 16) || (((size_t)data) % 16) ) {
/* unaligned, do scalar version. */
mix_float32_c2_scalar(panning, data, stream, mixframes);
} else if ((left == 1.0f) && (right == 1.0f)) {