-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
mojoal.c
4903 lines (4259 loc) · 169 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 <stdlib.h> /* needed for alloca */
#include <math.h>
#include <float.h>
/* Unless compiling statically into another app, we want the public API
to export on Windows. Define these before including al.h, so we override
its attempt to mark these as `dllimport`. */
#if defined(_WIN32) && !defined(AL_LIBTYPE_STATIC)
#define AL_API __declspec(dllexport)
#define ALC_API __declspec(dllexport)
#endif
#ifndef M_PI
#define M_PI (3.14159265358979323846264338327950288)
#endif
#include "al.h"
#include "alc.h"
#include "SDL.h"
/* This is for debugging and/or pulling the fire alarm. */
#define FORCE_SCALAR_FALLBACK 0
#if FORCE_SCALAR_FALLBACK
# ifdef __SSE__
# undef __SSE__
# endif
# ifdef __ARM_NEON__
# undef __ARM_NEON__
# endif
#endif
#if defined(__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 && !FORCE_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"
/* 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
/* Number of sources to allocate at once when we need a new block during alGenSources(). */
#ifndef OPENAL_SOURCE_BLOCK_SIZE
#define OPENAL_SOURCE_BLOCK_SIZE 64
#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:
- The initial work on this implementation attempted to be completely
lock free, and it lead to fragile, overly-clever, and complicated code.
Attempt #2 is making more reasonable tradeoffs.
- All API entry points are protected by a global mutex, which means that
calls into the API are serialized, but we expect this to not be a
serious problem; most AL calls are likely to come from a single thread
and uncontended mutexes generally aren't very expensive. This mutex
is not shared with the mixer thread, so there is never a point where
an innocent "fast" call into the AL will block because of the bad luck
of a high mixing load and the wrong moment.
- In rare cases we'll lock the mixer thread for a brief time; when a playing
source is accessible to the mixer, it is flagged as such. The mixer has a
mutex that it holds when mixing a source, and if we need to touch a source
that is flagged as accessible, we'll grab that lock to make sure there isn't
a conflict. Not all source changes need to do this. The likelihood of
hitting this case is extremely small, and the lock hold time is pretty
short. Things that might do this, only on currently-playing sources:
alDeleteSources, alSourceStop, alSourceRewind. alSourcePlay and
alSourcePause never need to lock.
- Devices are expected to live for the entire life of your OpenAL
experience, so closing one while another thread is using it is your own
fault. Don't do that. Devices are allocated pointers, and the AL doesn't
know if you've deleted it, making the pointer invalid. Device open and
close are not meant to be "fast" calls.
- Creating or destroying a context will lock the mixer thread completely
(so it isn't running _at all_ during the lock), so we can add/remove the
context on the device's list without racing. So don't do this in
time-critical code.
- Generating an object (source, buffer, etc) might need to allocate
memory, which can always take longer than you would expect. We allocate in
blocks, so not every call will allocate more memory. Generating an object
does not lock the mixer thread.
- Deleting a buffer does not lock the mixer thread (in-use buffers can
not be deleted per API spec). Deleting a source will lock the mixer briefly
if the source is still visible to the mixer. We don't believe this will be
a serious issue in normal use cases. Deleted objects' memory is marked for
reuse, but no memory is free'd by deleting sources or buffers until the
context or device, respectively, are destroyed. A deleted source that's
still visible to the mixer will not be available for reallocation until
the mixer runs another iteration, where it will mark it as no longer
visible. If you call alGenSources() during this time, a different source
will be allocated.
- alBufferData needs to allocate memory to copy new audio data. Often,
you can avoid doing these things in time-critical code. You can't set
a buffer's data when it's attached to a source (either with AL_BUFFER
or buffer queueing), so there's never a chance of contention with the
mixer thread here.
- Buffers and sources are allocated in blocks of OPENAL_BUFFER_BLOCK_SIZE
(or OPENAL_SOURCE_BLOCK_SIZE). These blocks are never deallocated as long
as the device (for buffers) or context (for sources) lives, so they don't
need a lock to access as the pointers are immutable once they're wired in.
We don't keep a ALuint name index array, but rather an array of block
pointers, which lets us find the right offset in the correct block without
iteration. The mixer thread never references the blocks directly, as they
get buffer and source pointers to objects within those blocks. Sources keep
a pointer to their specifically-bound buffer, and the mixer keeps a list of
pointers to playing sources. Since the API is serialized and the mixer
doesn't touch them, we don't need to tapdance to add new blocks.
- Buffer data is owned by the AL, and it's illegal to delete a buffer or
alBufferData() its contents while attached to 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. Refcounts only change when
changing a source's AL_BUFFER or altering its buffer queue, both of which
are protected by the api lock. The mixer thread doesn't touch the
refcount, as a buffer moving from AL_PENDING to AL_PROCESSED is still
attached to a source.
- alSource(Stop|Pause|Rewind)v with > 1 source used will always lock the
mixer thread to guarantee that all sources change in sync (!!! FIXME?).
The non-v version of these functions do not lock the mixer thread.
alSourcePlayv never locks the mixer thread (it atomically appends to a
linked list of sources to be played, which the mixer will pick up all
at once).
- 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_queued 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.
- 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
/* no threads in Emscripten (at the moment...!) */
#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
#define init_api_lock() 1
#define grab_api_lock()
#define ungrab_api_lock()
#else
static SDL_mutex *api_lock = NULL;
static int init_api_lock(void)
{
if (!api_lock) {
api_lock = SDL_CreateMutex();
if (!api_lock) {
return 0;
}
}
return 1;
}
static void grab_api_lock(void)
{
if (!api_lock) {
if (!init_api_lock()) {
return;
}
}
const int rc = SDL_LockMutex(api_lock);
SDL_assert(rc == 0);
}
static void ungrab_api_lock(void)
{
if (!api_lock) {
init_api_lock();
return;
}
const int rc = SDL_UnlockMutex(api_lock);
SDL_assert(rc == 0);
}
#endif
#define ENTRYPOINT(rettype,fn,params,args) \
rettype fn params { rettype retval; grab_api_lock(); retval = _##fn args ; ungrab_api_lock(); return retval; }
#define ENTRYPOINTVOID(fn,params,args) \
void fn params { grab_api_lock(); _##fn args ; ungrab_api_lock(); }
/* 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
{
ALboolean 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;
/* !!! FIXME: buffers and sources use almost identical code for blocks */
typedef struct BufferBlock
{
ALbuffer buffers[OPENAL_BUFFER_BLOCK_SIZE]; /* allocate these in blocks so we can step through faster. */
ALuint used;
ALuint tmp; /* only touch under api_lock, assume it'll be gone later. */
} 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;
#define pitch_framesize 1024
#define pitch_framesize2 512
typedef struct PitchState
{
/* !!! FIXME: this is a wild amount of memory for pitch-shifting! */
ALfloat infifo[pitch_framesize];
ALfloat outfifo[pitch_framesize];
ALfloat workspace[2*pitch_framesize];
ALfloat lastphase[pitch_framesize2+1];
ALfloat sumphase[pitch_framesize2+1];
ALfloat outputaccum[2*pitch_framesize];
ALfloat synmagn[pitch_framesize2+1];
ALfloat synfreq[pitch_framesize2+1];
ALint rover;
} PitchState;
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 mixer_accessible;
SDL_atomic_t state; /* initial, playing, paused, stopped */
ALuint name;
ALboolean allocated;
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. */
SDL_atomic_t total_queued_buffers; /* everything queued, playing and processed. AL_BUFFERS_QUEUED value. */
BufferQueue buffer_queue;
BufferQueue buffer_queue_processed;
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;
PitchState *pitchstate;
ALsource *playlist_next; /* linked list that contains currently-playing sources! Only touched by mixer thread! */
};
/* !!! FIXME: buffers and sources use almost identical code for blocks */
typedef struct SourceBlock
{
ALsource sources[OPENAL_SOURCE_BLOCK_SIZE]; /* allocate these in blocks so we can step through faster. */
ALuint used;
ALuint tmp; /* only touch under api_lock, assume it'll be gone later. */
} SourceBlock;
typedef struct SourcePlayTodo
{
ALsource *source;
struct SourcePlayTodo *next;
} SourcePlayTodo;
struct ALCdevice_struct
{
char *name;
ALCenum error;
SDL_atomic_t connected;
ALCboolean iscapture;
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. */
ALCsizei num_buffer_blocks;
BufferQueueItem *buffer_queue_pool; /* mixer thread doesn't touch this. */
void *source_todo_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 */
SourceBlock **source_blocks;
ALsizei num_source_blocks;
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_mutex *source_lock;
void *playlist_todo; /* void* so we can AtomicCASPtr it. Transmits new play commands from api thread to mixer thread */
ALsource *playlist; /* linked list of currently-playing sources. Mixer thread only! */
ALsource *playlist_tail; /* end of playlist so we know if last item is being readded. Mixer thread only! */
ALCcontext *prev; /* contexts are in a double-linked list */
ALCcontext *next;
};
/* forward declarations */
static float source_get_offset(ALsource *src, ALenum param);
static void source_set_offset(ALsource *src, ALenum param, ALfloat value);
/* 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, (BufferQueueItem*)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! :) */
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 = (BufferQueueItem*)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;
}
static void source_release_buffer_queue(ALCcontext *ctx, ALsource *src)
{
/* 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) {
BufferQueueItem *i;
for (i = src->buffer_queue.head; i; i = (BufferQueueItem*)i->next) {
(void) SDL_AtomicDecRef(&i->buffer->refcount);
}
src->buffer_queue.tail->next = ctx->device->playback.buffer_queue_pool;
ctx->device->playback.buffer_queue_pool = src->buffer_queue.head;
}
src->buffer_queue.head = src->buffer_queue.tail = NULL;
SDL_AtomicSet(&src->buffer_queue.num_items, 0);
obtain_newly_queued_buffers(&src->buffer_queue_processed);
if (src->buffer_queue_processed.tail != NULL) {
BufferQueueItem *i;
for (i = src->buffer_queue_processed.head; i; i = (BufferQueueItem*)i->next) {
(void) SDL_AtomicDecRef(&i->buffer->refcount);
}
src->buffer_queue_processed.tail->next = ctx->device->playback.buffer_queue_pool;
ctx->device->playback.buffer_queue_pool = src->buffer_queue_processed.head;
}
src->buffer_queue_processed.head = src->buffer_queue_processed.tail = NULL;
SDL_AtomicSet(&src->buffer_queue_processed.num_items, 0);
}
/* 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;
static ALCdevice *prep_alc_device(const char *devicename, const ALCboolean iscapture)
{
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
if (!init_api_lock()) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL;
}
dev = (ALCdevice *) SDL_calloc(1, sizeof (ALCdevice));
if (!dev) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL;
}
dev->name = SDL_strdup(devicename);
if (!dev->name) {
SDL_free(dev);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return NULL;
}
SDL_AtomicSet(&dev->connected, ALC_TRUE);
dev->iscapture = iscapture;
return dev;
}
/* no api lock; this creates it and otherwise doesn't have any state that can race */
ALCdevice *alcOpenDevice(const ALCchar *devicename)
{
if (!devicename) {
devicename = DEFAULT_PLAYBACK_DEVICE; /* so ALC_DEVICE_SPECIFIER is meaningful */
}
return prep_alc_device(devicename, ALC_FALSE);
/* we don't open an SDL audio device until the first context is
created, so we can attempt to match audio formats. */
}
/* no api lock; this requires you to not destroy a device that's still in use */
ALCboolean alcCloseDevice(ALCdevice *device)
{
BufferQueueItem *item;
SourcePlayTodo *todo;
ALCsizei i;
if (!device || device->iscapture) {
return ALC_FALSE;
}
/* spec: "Failure will occur if all the device's contexts and buffers have not been destroyed." */
if (device->playback.contexts) {
return ALC_FALSE;
}
for (i = 0; i <device->playback.num_buffer_blocks; i++) {
if (device->playback.buffer_blocks[i]->used > 0) {
return ALC_FALSE; /* still buffers allocated. */
}
}
if (device->sdldevice) {
SDL_CloseAudioDevice(device->sdldevice);
}
for (i = 0; i < device->playback.num_buffer_blocks; i++) {
SDL_free(device->playback.buffer_blocks[i]);
}
SDL_free(device->playback.buffer_blocks);
item = device->playback.buffer_queue_pool;
while (item) {
BufferQueueItem *next = (BufferQueueItem*)item->next;
SDL_free(item);
item = next;
}
todo = (SourcePlayTodo *) device->playback.source_todo_pool;
while (todo) {
SourcePlayTodo *next = todo->next;
SDL_free(todo);
todo = 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;
mix_float32_c1_sse(panning, data + 2, stream + 4, 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];