-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathcute_sound.h
More file actions
2479 lines (2151 loc) · 78.5 KB
/
Copy pathcute_sound.h
File metadata and controls
2479 lines (2151 loc) · 78.5 KB
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
cute_sound.h - v3.00
To create implementation (the function definitions)
#define CUTE_SOUND_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
SUMMARY
cute_sound is a C API for loading, playing, looping, panning and fading mono
and stereo sounds. It requires SDL3 for audio output, and optionally
stb_vorbis.c for ogg file loading.
REVISION HISTORY
1.0 (06/04/2016) Initial release.
1.01 (06/06/2016) Load WAV from memory.
* Separate portable and OS-specific code in cs_mix.
* Fixed bug causing audio glitches when sounds ended.
* Added stb_vorbis loaders + demo example.
1.02 (06/08/2016) error checking + strings in vorbis loaders
* SSE2 implementation of mixer.
* Fix typos on docs/comments.
* Corrected volume bug introduced in 1.01.
1.03 (07/05/2016) size calculation helper (to know size of sound in
bytes on the heap) cs_sound_size
1.04 (12/06/2016) Merged in Aaron Balint's contributions.
* SFFT and pitch functions from Stephan M. Bernsee
* cs_mix can run on its own thread with cs_spawn_mix_thread
* Updated documentation, typo fixes.
* Fixed typo in cs_malloc16 that caused heap corruption.
1.05 (12/08/2016) cs_stop_all_sounds, suggested by Aaron Balint
1.06 (02/17/2017) Port to CoreAudio for Apple machines.
1.07 (06/18/2017) SIMD the pitch shift code; swapped out old Bernsee
code for a new re-write, updated docs as necessary,
support for compiling as .c and .cpp on Windows,
port for SDL (for Linux, or any other platform).
* Special thanks to DeXP (Dmitry Hrabrov) for 90% of
the work on the SDL port!
1.08 (09/06/2017) SDL_RWops support by RobLoach
1.09 (05/20/2018) Load wav funcs can skip all irrelevant chunks
Ref counting for playing sounds
1.10 (08/24/2019) Introduced plugin interface, reimplemented pitch shifting
as a plugin, added optional `ctx` to alloc functions
1.11 (04/23/2020) scalar SIMD mode and various compiler warning/error fixes
1.12 (10/20/2021) removed old and broken assert if driver requested non-
power of two sample size for mixing updates
2.00 (05/21/2022) redesigned the entire API for v2.00, music support, broke
the pitch/plugin interface (to be fixed), CoreAudio is not
yet tested, but the SDL2 implementation is well tested,
* ALSA support is dropped entirely
2.01 (11/02/2022) Compilation fixes for clang/llvm, added #include <stddef.h>
to have size_t defined. Correctly finalize the thread when
cs_shutdown is called for all platforms that spawned it
(this fixes segmentation fault on cs_shutdown).
2.02 (11/10/2022) Removed plugin API -- It was clunky and just not worth the
maintenence effort. If you're reading this Matt, I haven't
forgotten all the cool work you did! And will use it in the
future for sure :) -- Unfortunately this removes pitch.
* Fixed a bug where dsound mixing could run too fast.
2.03 (11/12/2022) Added internal queue for freeing audio sources to avoid the
need for refcount polling.
2.04 (02/04/2024) Added `cs_cull_duplicates` helper for removing extra plays
to the same sound on the exact same update tick.
2.05 (03/27/2023) Added cs_get_global_context and friends, and extra accessors
for panning and similar.
2.06 (06/23/2024) Looping sounds play seamlessly.
2.07 (06/23/2024) Added pitch shifting support, removed delay support.
2.08 (08/07/2024) Added sample_index to sound params, removed unnecessary asserts
for stopping music, added callbacks sounds/music ending
2.09 (08/10/2024) Upgrade to SDL3.
3.00 (02/01/2026) Removed platform-specific code, now SDL3 only.
* Simplified hashtable and removed redundant circular buffer.
* Fixed SIMD sample loading bugs causing audio popping.
* Fixed pitch shift bug at end of tracks (interpolate beyond last sample).
* Refactored mixer to be simpler.
* Replaced sample_index API with time values in seconds.
* Fixed looped/reverse playback pitch shifting (audio crackling).
* Removed cs_cull_duplicates (better handled user-side).
* Fixed music fade to incorporate user volume.
* WAV loader now supports 8/16/24/32-bit PCM and 32/64-bit float.
CONTRIBUTORS
Aaron Balint 1.04 - real time pitch
1.04 - separate thread for cs_mix
1.04 - bugfix, removed extra cs_free16 call for second channel
DeXP 1.07 - initial work on SDL port
RobLoach 1.08 - SDL_RWops support
Matt Rosen 1.10 - Initial experiments with cute_dsp to figure out plugin
interface needs and use-cases
fluffrabbit 1.11 - scalar SIMD mode and various compiler warning/error fixes
Daniel Guzman 2.01 - compilation fixes for clang/llvm on MAC.
Brie 2.06 - Looping sound rollover
ogam x.xx - Lots of bugfixes over time, including support negative pitch
DOCUMENTATION (very quick intro)
Steps to play audio:
1. create context (call cs_init)
2. load sounds from disk into memory (call cs_load_wav, or cs_load_ogg with stb_vorbis.c)
3. play sounds (cs_play_sound), or music (cs_music_play)
DISABLE SSE/NEON SIMD ACCELERATION
If for whatever reason you don't want to use SIMD intrinsics and instead would prefer
plain C (for example if your platform does not support SSE/NEON) then define
CUTE_SOUND_SCALAR_MODE before including cute_sound.h while also defining the
symbol definitions. Here's an example:
#define CUTE_SOUND_SCALAR_MODE
#define CUTE_SOUND_IMPLEMENTATION
#include <cute_sound.h>
CUSTOMIZATION
A few different macros can be overriden to provide custom functionality. Simply define any of these
macros before including this file to override them.
CUTE_SOUND_MINIMUM_BUFFERED_SAMPLES
CUTE_SOUND_ASSERT
CUTE_SOUND_ALLOC
CUTE_SOUND_FREE
CUTE_SOUND_MEMCPY
CUTE_SOUND_MEMSET
CUTE_SOUND_MEMCMP
CUTE_SOUND_SEEK_SET
CUTE_SOUND_SEEK_END
CUTE_SOUND_FILE
CUTE_SOUND_FOPEN
CUTE_SOUND_FSEEK
CUTE_SOUND_FREAD
CUTE_SOUND_FTELL
CUTE_SOUND_FCLOSE
KNOWN LIMITATIONS
* PCM mono/stereo format is the only formats the LoadWAV function supports. I don't
guarantee it will work for all kinds of wav files, but it certainly does for the common
kind (and can be changed fairly easily if someone wanted to extend it).
* Only supports 16 bits per sample.
* Mixer does not do any fancy clipping. The algorithm is to convert all 16 bit samples
to float, mix all samples, and write back to audio API as 16 bit integers. In
practice this works very well and clipping is not often a big problem.
*/
#if !defined(CUTE_SOUND_H)
#if defined(_WIN32)
#if !defined _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#if !defined _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
// -------------------------------------------------------------------------------------------------
// Error handling.
typedef enum cs_error_t
{
CUTE_SOUND_ERROR_NONE,
CUTE_SOUND_ERROR_FILE_NOT_FOUND,
CUTE_SOUND_ERROR_INVALID_SOUND,
CUTE_SOUND_ERROR_CANT_OPEN_AUDIO_DEVICE,
CUTE_SOUND_ERROR_CANT_INIT_SDL_AUDIO,
CUTE_SOUND_ERROR_THE_FILE_IS_NOT_A_WAV_FILE,
CUTE_SOUND_ERROR_WAV_FILE_FORMAT_CHUNK_NOT_FOUND,
CUTE_SOUND_ERROR_WAV_DATA_CHUNK_NOT_FOUND,
CUTE_SOUND_ERROR_WAV_ONLY_MONO_OR_STEREO_IS_SUPPORTED,
CUTE_SOUND_ERROR_WAV_UNSUPPORTED_FORMAT,
CUTE_SOUND_ERROR_CANNOT_SWITCH_MUSIC_WHILE_PAUSED,
CUTE_SOUND_ERROR_CANNOT_CROSSFADE_WHILE_MUSIC_IS_PAUSED,
CUTE_SOUND_ERROR_CANNOT_FADEOUT_WHILE_MUSIC_IS_PAUSED,
CUTE_SOUND_ERROR_TRIED_TO_SET_SAMPLE_INDEX_BEYOND_THE_AUDIO_SOURCES_SAMPLE_COUNT,
CUTE_SOUND_ERROR_STB_VORBIS_DECODE_FAILED,
CUTE_SOUND_ERROR_OGG_UNSUPPORTED_CHANNEL_COUNT,
CUTE_SOUND_ERROR_IMPLEMENTATION_ERROR_PLEASE_REPORT_THIS_ON_GITHUB, // https://github.com/RandyGaul/cute_headers/blob/master/cute_sound.h
} cs_error_t;
const char* cs_error_as_string(cs_error_t error);
// -------------------------------------------------------------------------------------------------
// Cute sound context functions.
/**
* play_frequency_in_Hz depends on your audio files, 44100 is typical.
*/
cs_error_t cs_init(unsigned play_frequency_in_Hz, void* user_allocator_context /* = NULL */);
void cs_shutdown();
/**
* Call this function once per game-tick.
*/
void cs_update(float dt);
void cs_set_global_volume(float volume_0_to_1);
void cs_set_global_pan(float pan_0_to_1);
void cs_set_global_pause(bool true_for_paused);
/**
* Sometimes useful for dynamic library shenanigans.
*/
void* cs_get_context_ptr();
void cs_set_context_ptr(void* ctx);
// -------------------------------------------------------------------------------------------------
// Loaded sounds.
typedef struct cs_audio_source_t cs_audio_source_t;
/**
* Load a WAV file from disk or memory.
*
* Supported WAV formats:
* - PCM 8-bit unsigned
* - PCM 16-bit signed
* - PCM 24-bit signed
* - PCM 32-bit signed
* - IEEE float 32-bit
* - IEEE float 64-bit
*
* Only mono and stereo channel configurations are supported.
*/
cs_audio_source_t* cs_load_wav(const char* path, cs_error_t* err /* = NULL */);
cs_audio_source_t* cs_read_mem_wav(const void* memory, size_t size, cs_error_t* err /* = NULL */);
void cs_free_audio_source(cs_audio_source_t* audio);
// If stb_vorbis was included *before* cute_sound go ahead and create
// some functions for dealing with OGG files.
#ifdef STB_VORBIS_INCLUDE_STB_VORBIS_H
cs_audio_source_t* cs_load_ogg(const char* path, cs_error_t* err /* = NULL */);
cs_audio_source_t* cs_read_mem_ogg(const void* memory, size_t size, cs_error_t* err /* = NULL */);
#endif
// SDL_RWops specific functions
#if defined(SDL_rwops_h_) && defined(CUTE_SOUND_SDL_RWOPS)
// Provides the ability to use cs_load_wav with an SDL_RWops object.
cs_audio_source_t* cs_load_wav_rw(SDL_RWops* context, cs_error_t* err /* = NULL */);
#ifdef STB_VORBIS_INCLUDE_STB_VORBIS_H
// Provides the ability to use cs_load_ogg with an SDL_RWops object.
cs_audio_source_t* cs_load_ogg_rw(SDL_RWops* rw, cs_error_t* err /* = NULL */);
#endif
#endif // SDL_rwops_h_
// -------------------------------------------------------------------------------------------------
// Audio source accessors.
int cs_get_sample_rate(const cs_audio_source_t* audio);
int cs_get_sample_count(const cs_audio_source_t* audio);
int cs_get_channel_count(const cs_audio_source_t* audio);
// -------------------------------------------------------------------------------------------------
// Music sounds.
void cs_music_play(cs_audio_source_t* audio, float fade_in_time /* = 0 */);
void cs_music_stop(float fade_out_time /* = 0 */);
void cs_music_pause();
void cs_music_resume();
void cs_music_set_volume(float volume_0_to_1);
void cs_music_set_pitch(float pitch /* = 1.0f */);
void cs_music_set_loop(bool true_to_loop);
void cs_music_switch_to(cs_audio_source_t* audio, float fade_out_time /* = 0 */, float fade_in_time /* = 0 */);
void cs_music_crossfade(cs_audio_source_t* audio, float cross_fade_time /* = 0 */);
double cs_music_get_time();
cs_error_t cs_music_set_time(double time_in_seconds);
// -------------------------------------------------------------------------------------------------
// Playing sounds.
typedef struct cs_playing_sound_t { uint64_t id; } cs_playing_sound_t;
#define CUTE_PLAYING_SOUND_INVALID (cs_playing_sound_t){ 0 }
typedef struct cs_sound_params_t
{
bool paused /* = false */;
bool looped /* = false */;
float volume /* = 1.0f */;
float pan /* = 0.5f */; // Can be from 0 to 1.
float pitch /* = 1.0f */;
double start_time /* = 0.0 */; // Start time in seconds.
} cs_sound_params_t;
cs_sound_params_t cs_sound_params_default();
cs_playing_sound_t cs_play_sound(cs_audio_source_t* audio, cs_sound_params_t params);
/**
* Setup a callback for whenever a sound finishes playing. This will get called from the
* mixer thread, which means you'll need to deal with a multithreaded callback if you've
* spawned a separate mixing thread.
*/
void cs_on_sound_finished_callback(void (*on_finish)(cs_playing_sound_t, void*), void* udata);
/**
* Setup a callback for whenever the current song finishes playing. This will get called from the
* mixer thread, which means you'll need to deal with a multithreaded callback if you've
* spawned a separate mixing thread.
*/
void cs_on_music_finished_callback(void (*on_finish)(void*), void* udata);
bool cs_sound_is_active(cs_playing_sound_t sound);
bool cs_sound_get_is_paused(cs_playing_sound_t sound);
bool cs_sound_get_is_looped(cs_playing_sound_t sound);
float cs_sound_get_volume(cs_playing_sound_t sound);
float cs_sound_get_pitch(cs_playing_sound_t sound);
float cs_sound_get_pan(cs_playing_sound_t sound);
double cs_sound_get_time(cs_playing_sound_t sound);
void cs_sound_set_is_paused(cs_playing_sound_t sound, bool true_for_paused);
void cs_sound_set_is_looped(cs_playing_sound_t sound, bool true_for_looped);
void cs_sound_set_volume(cs_playing_sound_t sound, float volume_0_to_1);
void cs_sound_set_pan(cs_playing_sound_t sound, float pan_0_to_1);
void cs_sound_set_pitch(cs_playing_sound_t sound, float pitch);
cs_error_t cs_sound_set_time(cs_playing_sound_t sound, double time_in_seconds);
void cs_sound_stop(cs_playing_sound_t sound);
void cs_set_playing_sounds_volume(float volume_0_to_1);
void cs_stop_all_playing_sounds();
// -------------------------------------------------------------------------------------------------
// Global context.
void* cs_get_global_context();
void cs_set_global_context(void* context);
void* cs_get_global_user_allocator_context();
void cs_set_global_user_allocator_context(void* user_allocator_context);
#define CUTE_SOUND_H
#endif
#ifdef CUTE_SOUND_IMPLEMENTATION
#ifndef CUTE_SOUND_IMPLEMENTATION_ONCE
#define CUTE_SOUND_IMPLEMENTATION_ONCE
// Internal mixer buffer size in samples. 4096 samples at 44100Hz is ~93ms.
#ifndef CUTE_SOUND_MIXER_BUFFER_SIZE
# define CUTE_SOUND_MIXER_BUFFER_SIZE 4096
#endif
// Bytes per sample frame (16-bit stereo = 4 bytes).
#define CUTE_SOUND_BYTES_PER_SAMPLE_FRAME 4
#if !defined(CUTE_SOUND_ASSERT)
# include <assert.h>
# define CUTE_SOUND_ASSERT assert
#endif
#if !defined(CUTE_SOUND_ALLOC)
#include <stdlib.h>
#define CUTE_SOUND_ALLOC(size, ctx) malloc(size)
#endif
#if !defined(CUTE_SOUND_FREE)
#include <stdlib.h>
#define CUTE_SOUND_FREE(mem, ctx) free(mem)
#endif
#ifndef CUTE_SOUND_MEMCPY
# include <string.h>
# define CUTE_SOUND_MEMCPY memcpy
#endif
#ifndef CUTE_SOUND_MEMSET
# include <string.h>
# define CUTE_SOUND_MEMSET memset
#endif
#ifndef CUTE_SOUND_MEMCMP
# include <string.h>
# define CUTE_SOUND_MEMCMP memcmp
#endif
#ifndef CUTE_SOUND_SEEK_SET
# include <stdio.h>
# define CUTE_SOUND_SEEK_SET SEEK_SET
#endif
#ifndef CUTE_SOUND_SEEK_END
# include <stdio.h>
# define CUTE_SOUND_SEEK_END SEEK_END
#endif
#ifndef CUTE_SOUND_FILE
# include <stdio.h>
# define CUTE_SOUND_FILE FILE
#endif
#ifndef CUTE_SOUND_FOPEN
# include <stdio.h>
# define CUTE_SOUND_FOPEN fopen
#endif
#ifndef CUTE_SOUND_FSEEK
# include <stdio.h>
# define CUTE_SOUND_FSEEK fseek
#endif
#ifndef CUTE_SOUND_FREAD
# include <stdio.h>
# define CUTE_SOUND_FREAD fread
#endif
#ifndef CUTE_SOUND_FTELL
# include <stdio.h>
# define CUTE_SOUND_FTELL ftell
#endif
#ifndef CUTE_SOUND_FCLOSE
# include <stdio.h>
# define CUTE_SOUND_FCLOSE fclose
#endif
// SDL3 is required.
#ifndef SDL_h_
#ifndef CUTE_SOUND_SDL_H
#define CUTE_SOUND_SDL_H <SDL3/SDL.h>
#endif
#include CUTE_SOUND_SDL_H
#endif
// Automatically select SSE/NEON, or default to scalar if `CUTE_SOUND_SCALAR_MODE` is defined.
#if !defined(CUTE_SOUND_SCALAR_MODE) && defined(__ARM_NEON) || defined(__ARM_NEON__)
#include <arm_neon.h>
#define cs__m128 float32x4_t
#define cs__m128i int32x4_t
#define cs_mm_set_ps(e3, e2, e1, e0) vsetq_lane_f32(e3, vsetq_lane_f32(e2, vsetq_lane_f32(e1, vsetq_lane_f32(e0, vdupq_n_f32(0), 0), 1), 2), 3)
#define cs_mm_set1_ps(e) vdupq_n_f32(e)
#define cs_mm_add_ps(a, b) vaddq_f32(a, b)
#define cs_mm_sub_ps(a, b) vsubq_f32(a, b)
#define cs_mm_mul_ps(a, b) vmulq_f32(a, b)
#define cs_mm_cvtps_epi32(a) vcvtq_s32_f32(a)
#define cs_mm_unpacklo_epi32(a, b) vzipq_s32(a, b).val[0]
#define cs_mm_unpackhi_epi32(a, b) vzipq_s32(a, b).val[1]
#define cs_mm_packs_epi32(a, b) vcombine_s16(vqmovn_s32(a), vqmovn_s32(b))
#define cs_mm_cvttps_epi32(a) vcvtq_s32_f32(a)
#define cs_mm_cvtepi32_ps(a) vcvtq_f32_s32(a)
#define cs_mm_extract_epi32(a, imm8) vgetq_lane_s32(a, imm8)
#define cs_mm_set1_epi32(a) vdupq_n_s32(a)
#define cs_mm_sub_epi32(a, b) vsubq_s32(a, b)
#define cs_mm_and_si128(a, b) vandq_s32(a, b)
#define cs_mm_cmplt_ps(a, b) vreinterpretq_f32_u32(vcltq_f32(a, b))
#define cs_mm_castps_si128(a) vreinterpretq_s32_f32(a)
#elif !defined(CUTE_SOUND_SCALAR_MODE) && defined(__SSE__) || defined(__SSE2__) || defined(__SSE3__) || defined(__SSE4_1__) || defined(__SSE4_2__)
#include <immintrin.h>
#define cs__m128 __m128
#define cs__m128i __m128i
#define cs_mm_set_ps _mm_set_ps
#define cs_mm_set1_ps _mm_set1_ps
#define cs_mm_add_ps _mm_add_ps
#define cs_mm_sub_ps _mm_sub_ps
#define cs_mm_mul_ps _mm_mul_ps
#define cs_mm_cvtps_epi32 _mm_cvtps_epi32
#define cs_mm_unpacklo_epi32 _mm_unpacklo_epi32
#define cs_mm_unpackhi_epi32 _mm_unpackhi_epi32
#define cs_mm_packs_epi32 _mm_packs_epi32
#define cs_mm_cvttps_epi32 _mm_cvttps_epi32
#define cs_mm_cvtepi32_ps _mm_cvtepi32_ps
#define cs_mm_extract_epi32 _mm_extract_epi32
#define cs_mm_set1_epi32 _mm_set1_epi32
#define cs_mm_sub_epi32 _mm_sub_epi32
#define cs_mm_and_si128 _mm_and_si128
#define cs_mm_cmplt_ps _mm_cmplt_ps
#define cs_mm_castps_si128 _mm_castps_si128
#else // Scalar mode as fallback.
#include <limits.h>
#define CUTE_SOUND_SATURATE16(X) (int16_t)((X) > SHRT_MAX ? SHRT_MAX : ((X) < SHRT_MIN ? SHRT_MIN : (X)))
typedef struct cs__m128
{
float a, b, c, d;
} cs__m128;
typedef struct cs__m128i
{
int32_t a, b, c, d;
} cs__m128i;
cs__m128 cs_mm_set_ps(float e3, float e2, float e1, float e0)
{
cs__m128 a;
a.a = e0;
a.b = e1;
a.c = e2;
a.d = e3;
return a;
}
cs__m128 cs_mm_set1_ps(float e)
{
cs__m128 a;
a.a = e;
a.b = e;
a.c = e;
a.d = e;
return a;
}
cs__m128 cs_mm_add_ps(cs__m128 a, cs__m128 b)
{
cs__m128 c;
c.a = a.a + b.a;
c.b = a.b + b.b;
c.c = a.c + b.c;
c.d = a.d + b.d;
return c;
}
cs__m128 cs_mm_sub_ps(cs__m128 a, cs__m128 b)
{
cs__m128 c;
c.a = a.a - b.a;
c.b = a.b - b.b;
c.c = a.c - b.c;
c.d = a.d - b.d;
return c;
}
cs__m128 cs_mm_mul_ps(cs__m128 a, cs__m128 b)
{
cs__m128 c;
c.a = a.a * b.a;
c.b = a.b * b.b;
c.c = a.c * b.c;
c.d = a.d * b.d;
return c;
}
cs__m128i cs_mm_cvtps_epi32(cs__m128 a)
{
cs__m128i b;
b.a = (int32_t)a.a;
b.b = (int32_t)a.b;
b.c = (int32_t)a.c;
b.d = (int32_t)a.d;
return b;
}
cs__m128i cs_mm_unpacklo_epi32(cs__m128i a, cs__m128i b)
{
cs__m128i c;
c.a = a.a;
c.b = b.a;
c.c = a.b;
c.d = b.b;
return c;
}
cs__m128i cs_mm_unpackhi_epi32(cs__m128i a, cs__m128i b)
{
cs__m128i c;
c.a = a.c;
c.b = b.c;
c.c = a.d;
c.d = b.d;
return c;
}
cs__m128i cs_mm_packs_epi32(cs__m128i a, cs__m128i b)
{
union {
int16_t c[8];
cs__m128i m;
} dst;
dst.c[0] = CUTE_SOUND_SATURATE16(a.a);
dst.c[1] = CUTE_SOUND_SATURATE16(a.b);
dst.c[2] = CUTE_SOUND_SATURATE16(a.c);
dst.c[3] = CUTE_SOUND_SATURATE16(a.d);
dst.c[4] = CUTE_SOUND_SATURATE16(b.a);
dst.c[5] = CUTE_SOUND_SATURATE16(b.b);
dst.c[6] = CUTE_SOUND_SATURATE16(b.c);
dst.c[7] = CUTE_SOUND_SATURATE16(b.d);
return dst.m;
}
cs__m128i cs_mm_cvttps_epi32(cs__m128 a)
{
cs__m128i b;
b.a = (int32_t)a.a;
b.b = (int32_t)a.b;
b.c = (int32_t)a.c;
b.d = (int32_t)a.d;
return b;
}
cs__m128 cs_mm_cvtepi32_ps(cs__m128i a)
{
cs__m128 b;
b.a = (float)a.a;
b.b = (float)a.b;
b.c = (float)a.c;
b.d = (float)a.d;
return b;
}
int32_t cs_mm_extract_epi32(cs__m128i a, const int imm8)
{
switch (imm8) {
case 0: return a.a;
case 1: return a.b;
case 2: return a.c;
case 3: return a.d;
default: return 0;
}
}
cs__m128i cs_mm_set1_epi32(int32_t e)
{
cs__m128i a;
a.a = e;
a.b = e;
a.c = e;
a.d = e;
return a;
}
cs__m128i cs_mm_sub_epi32(cs__m128i a, cs__m128i b)
{
cs__m128i c;
c.a = a.a - b.a;
c.b = a.b - b.b;
c.c = a.c - b.c;
c.d = a.d - b.d;
return c;
}
cs__m128i cs_mm_and_si128(cs__m128i a, cs__m128i b)
{
cs__m128i c;
c.a = a.a & b.a;
c.b = a.b & b.b;
c.c = a.c & b.c;
c.d = a.d & b.d;
return c;
}
cs__m128 cs_mm_cmplt_ps(cs__m128 a, cs__m128 b)
{
cs__m128 c;
union { float f; uint32_t u; } mask;
mask.u = 0xFFFFFFFF;
c.a = a.a < b.a ? mask.f : 0.0f;
c.b = a.b < b.b ? mask.f : 0.0f;
c.c = a.c < b.c ? mask.f : 0.0f;
c.d = a.d < b.d ? mask.f : 0.0f;
return c;
}
cs__m128i cs_mm_castps_si128(cs__m128 a)
{
union { cs__m128 f; cs__m128i i; } u;
u.f = a;
return u.i;
}
#endif // End of SIMD wrappers.
#define CUTE_SOUND_ALIGN(X, Y) ((((size_t)X) + ((Y) - 1)) & ~((Y) - 1))
#define CUTE_SOUND_TRUNC(X, Y) ((size_t)(X) & ~((Y) - 1))
// -------------------------------------------------------------------------------------------------
// Simple hash map for uint64_t keys to void* values.
typedef struct cs_map_slot_t
{
uint64_t key;
void* val;
} cs_map_slot_t;
typedef struct cs_map_t
{
void* memctx;
cs_map_slot_t* slots;
int capacity;
int count;
} cs_map_t;
static uint32_t cs_map_hash(uint64_t key)
{
key = (~key) + (key << 18);
key = key ^ (key >> 31);
key = key * 21;
key = key ^ (key >> 11);
key = key + (key << 6);
key = key ^ (key >> 22);
return (uint32_t)key;
}
static void cs_map_init(cs_map_t* map, int capacity, void* memctx)
{
map->memctx = memctx;
map->capacity = capacity;
map->count = 0;
size_t size = (size_t)capacity * sizeof(cs_map_slot_t);
map->slots = (cs_map_slot_t*)CUTE_SOUND_ALLOC(size, memctx);
CUTE_SOUND_MEMSET(map->slots, 0, size);
}
static void cs_map_term(cs_map_t* map)
{
CUTE_SOUND_FREE(map->slots, map->memctx);
}
static void cs_map_grow(cs_map_t* map)
{
int old_capacity = map->capacity;
cs_map_slot_t* old_slots = map->slots;
map->capacity *= 2;
size_t size = (size_t)map->capacity * sizeof(cs_map_slot_t);
map->slots = (cs_map_slot_t*)CUTE_SOUND_ALLOC(size, map->memctx);
CUTE_SOUND_MEMSET(map->slots, 0, size);
int mask = map->capacity - 1;
for (int i = 0; i < old_capacity; ++i) {
if (old_slots[i].key) {
uint32_t h = cs_map_hash(old_slots[i].key);
int idx = h & mask;
while (map->slots[idx].key) {
idx = (idx + 1) & mask;
}
map->slots[idx] = old_slots[i];
}
}
CUTE_SOUND_FREE(old_slots, map->memctx);
}
static void cs_map_insert(cs_map_t* map, uint64_t key, void* val)
{
CUTE_SOUND_ASSERT(key != 0);
if (map->count >= map->capacity / 2) {
cs_map_grow(map);
}
int mask = map->capacity - 1;
uint32_t h = cs_map_hash(key);
int idx = h & mask;
while (map->slots[idx].key && map->slots[idx].key != key) {
idx = (idx + 1) & mask;
}
if (!map->slots[idx].key) {
++map->count;
}
map->slots[idx].key = key;
map->slots[idx].val = val;
}
static void* cs_map_find(cs_map_t* map, uint64_t key)
{
if (!key) return NULL;
int mask = map->capacity - 1;
uint32_t h = cs_map_hash(key);
int idx = h & mask;
while (map->slots[idx].key) {
if (map->slots[idx].key == key) {
return map->slots[idx].val;
}
idx = (idx + 1) & mask;
}
return NULL;
}
static void cs_map_remove(cs_map_t* map, uint64_t key)
{
if (!key) return;
int mask = map->capacity - 1;
uint32_t h = cs_map_hash(key);
int idx = h & mask;
while (map->slots[idx].key) {
if (map->slots[idx].key == key) {
map->slots[idx].key = 0;
map->slots[idx].val = NULL;
--map->count;
// Reinsert subsequent entries to maintain probe chain.
int next = (idx + 1) & mask;
while (map->slots[next].key) {
cs_map_slot_t slot = map->slots[next];
map->slots[next].key = 0;
map->slots[next].val = NULL;
--map->count;
cs_map_insert(map, slot.key, slot.val);
next = (next + 1) & mask;
}
return;
}
idx = (idx + 1) & mask;
}
}
static void cs_map_clear(cs_map_t* map)
{
CUTE_SOUND_MEMSET(map->slots, 0, (size_t)map->capacity * sizeof(cs_map_slot_t));
map->count = 0;
}
// -------------------------------------------------------------------------------------------------
const char* cs_error_as_string(cs_error_t error) {
switch (error) {
case CUTE_SOUND_ERROR_NONE: return "CUTE_SOUND_ERROR_NONE";
case CUTE_SOUND_ERROR_FILE_NOT_FOUND: return "CUTE_SOUND_ERROR_FILE_NOT_FOUND";
case CUTE_SOUND_ERROR_INVALID_SOUND: return "CUTE_SOUND_ERROR_INVALID_SOUND";
case CUTE_SOUND_ERROR_CANT_OPEN_AUDIO_DEVICE: return "CUTE_SOUND_ERROR_CANT_OPEN_AUDIO_DEVICE";
case CUTE_SOUND_ERROR_CANT_INIT_SDL_AUDIO: return "CUTE_SOUND_ERROR_CANT_INIT_SDL_AUDIO";
case CUTE_SOUND_ERROR_THE_FILE_IS_NOT_A_WAV_FILE: return "CUTE_SOUND_ERROR_THE_FILE_IS_NOT_A_WAV_FILE";
case CUTE_SOUND_ERROR_WAV_FILE_FORMAT_CHUNK_NOT_FOUND: return "CUTE_SOUND_ERROR_WAV_FILE_FORMAT_CHUNK_NOT_FOUND";
case CUTE_SOUND_ERROR_WAV_DATA_CHUNK_NOT_FOUND: return "CUTE_SOUND_ERROR_WAV_DATA_CHUNK_NOT_FOUND";
case CUTE_SOUND_ERROR_WAV_ONLY_MONO_OR_STEREO_IS_SUPPORTED: return "CUTE_SOUND_ERROR_WAV_ONLY_MONO_OR_STEREO_IS_SUPPORTED";
case CUTE_SOUND_ERROR_WAV_UNSUPPORTED_FORMAT: return "CUTE_SOUND_ERROR_WAV_UNSUPPORTED_FORMAT";
case CUTE_SOUND_ERROR_CANNOT_SWITCH_MUSIC_WHILE_PAUSED: return "CUTE_SOUND_ERROR_CANNOT_SWITCH_MUSIC_WHILE_PAUSED";
case CUTE_SOUND_ERROR_CANNOT_CROSSFADE_WHILE_MUSIC_IS_PAUSED: return "CUTE_SOUND_ERROR_CANNOT_CROSSFADE_WHILE_MUSIC_IS_PAUSED";
case CUTE_SOUND_ERROR_CANNOT_FADEOUT_WHILE_MUSIC_IS_PAUSED: return "CUTE_SOUND_ERROR_CANNOT_FADEOUT_WHILE_MUSIC_IS_PAUSED";
case CUTE_SOUND_ERROR_TRIED_TO_SET_SAMPLE_INDEX_BEYOND_THE_AUDIO_SOURCES_SAMPLE_COUNT: return "CUTE_SOUND_ERROR_TRIED_TO_SET_SAMPLE_INDEX_BEYOND_THE_AUDIO_SOURCES_SAMPLE_COUNT";
case CUTE_SOUND_ERROR_STB_VORBIS_DECODE_FAILED: return "CUTE_SOUND_ERROR_STB_VORBIS_DECODE_FAILED";
case CUTE_SOUND_ERROR_OGG_UNSUPPORTED_CHANNEL_COUNT: return "CUTE_SOUND_ERROR_OGG_UNSUPPORTED_CHANNEL_COUNT";
case CUTE_SOUND_ERROR_IMPLEMENTATION_ERROR_PLEASE_REPORT_THIS_ON_GITHUB: return "CUTE_SOUND_ERROR_IMPLEMENTATION_ERROR_PLEASE_REPORT_THIS_ON_GITHUB";
default: return "UNKNOWN";
}
}
// Cute sound context functions.
typedef struct cs_audio_source_t
{
int sample_rate;
int sample_count;
int channel_count;
// Number of instances currently referencing this audio. Must be zero
// in order to safely delete the audio. References are automatically
// updated whenever playing instances are inserted into the context.
int playing_count;
// The actual raw audio samples in memory.
void* channels[2];
} cs_audio_source_t;
typedef struct cs_sound_inst_t
{
uint64_t id;
bool is_music;
bool active;
bool paused;
bool looped;
float volume;
float pan0;
float pan1;
float pitch;
double sample_index;
cs_audio_source_t* audio;
struct cs_sound_inst_t* next;
struct cs_sound_inst_t* prev;
} cs_sound_inst_t;
typedef enum cs_music_state_t
{
CUTE_SOUND_MUSIC_STATE_NONE,
CUTE_SOUND_MUSIC_STATE_PLAYING,
CUTE_SOUND_MUSIC_STATE_FADE_OUT,
CUTE_SOUND_MUSIC_STATE_FADE_IN,
CUTE_SOUND_MUSIC_STATE_SWITCH_TO_0,
CUTE_SOUND_MUSIC_STATE_SWITCH_TO_1,
CUTE_SOUND_MUSIC_STATE_CROSSFADE,
CUTE_SOUND_MUSIC_STATE_PAUSED
} cs_music_state_t;
#define CUTE_SOUND_PAGE_INSTANCE_COUNT 1024
typedef struct cs_inst_page_t
{
struct cs_inst_page_t* next;
cs_sound_inst_t instances[CUTE_SOUND_PAGE_INSTANCE_COUNT];
} cs_inst_page_t;
typedef struct cs_context_t
{
float global_pan /* = 0.5f */;
float global_volume /* = 1.0f */;
bool global_pause /* = false */;
float music_volume /* = 1.0f */;
float music_pitch /* = 1.0f */;
float sound_volume /* = 1.0f */;
void (*on_finish)(cs_playing_sound_t, void*); /* = NULL */;
void* on_finish_udata /* = NULL */;
void (*on_music_finish)(void*); /* = NULL */;
void* on_music_finish_udata /* = NULL */;
bool music_paused /* = false */;
bool music_looped /* = true */;
float t /* = 0 */;
float fade /* = 0 */;
float fade_switch_1 /* = 0 */;
float fade_start_volume /* = 0 */;
cs_music_state_t music_state /* = MUSIC_STATE_NONE */;
cs_music_state_t music_state_to_resume_from_paused /* = MUSIC_STATE_NONE */;
cs_sound_inst_t* music_playing /* = NULL */;
cs_sound_inst_t* music_next /* = NULL */;
int audio_sources_to_free_capacity /* = 0 */;
int audio_sources_to_free_size /* = 0 */;
cs_audio_source_t** audio_sources_to_free /* = NULL */;
uint64_t instance_id_gen /* = 1 */;
cs_map_t instance_map;
cs_inst_page_t* pages /* = NULL */;
cs_sound_inst_t* playing_sounds /* = NULL */;
cs_sound_inst_t* free_sounds /* = NULL */;
int wide_count;
cs__m128* floatA;
cs__m128* floatB;
cs__m128i* samples;
bool running;
SDL_AudioStream* stream;
SDL_Mutex* mutex;
} cs_context_t;
void* s_mem_ctx;
cs_context_t* s_ctx = NULL;
static void* cs_malloc16(size_t size)
{
void* p = CUTE_SOUND_ALLOC(size + 16, s_mem_ctx);
if (!p) return 0;
unsigned char offset = (size_t)p & 15;
p = (void*)CUTE_SOUND_ALIGN(p + 1, 16);
*((char*)p - 1) = 16 - offset;
CUTE_SOUND_ASSERT(!((size_t)p & 15));
return p;
}
static void cs_free16(void* p)
{
if (!p) return;
CUTE_SOUND_FREE((char*)p - (((size_t)*((char*)p - 1)) & 0xFF), s_mem_ctx);
}
static void cs_mix(int bytes_to_write);
static void cs_sdl_audio_callback(void* udata, SDL_AudioStream* stream, int additional_amount, int total_amount)
{
if (additional_amount > 0) {
cs_mix(additional_amount);
SDL_PutAudioStreamData(stream, s_ctx->samples, additional_amount);
}
}
static void s_add_page()
{
cs_inst_page_t* page = (cs_inst_page_t*)CUTE_SOUND_ALLOC(sizeof(cs_inst_page_t), user_allocator_context);
for (int i = 0; i < CUTE_SOUND_PAGE_INSTANCE_COUNT; ++i) {
cs_sound_inst_t* inst = &page->instances[i];
inst->next = s_ctx->free_sounds;
inst->prev = NULL;
if (s_ctx->free_sounds) s_ctx->free_sounds->prev = inst;
s_ctx->free_sounds = inst;
}
page->next = s_ctx->pages;
s_ctx->pages = page;
}
cs_error_t cs_init(unsigned play_frequency_in_Hz, void* user_allocator_context /* = NULL */)
{
int wide_count = (int)CUTE_SOUND_ALIGN(CUTE_SOUND_MIXER_BUFFER_SIZE, 4);
SDL_AudioSpec wanted = { SDL_AUDIO_S16, 2, (int)play_frequency_in_Hz };