forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cute_sound.h
3231 lines (2685 loc) · 101 KB
/
cute_sound.h
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 - v2.04
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, without any external dependencies other than things that ship
with standard OSs, or SDL2 for more uncommon OSs.
While platform detection is done automatically, you are able to explicitly
specify which to use by defining one of the following:
#define CUTE_SOUND_PLATFORM_WINDOWS
#define CUTE_SOUND_PLATFORM_APPLE
#define CUTE_SOUND_PLATFORM_SDL
For Windows cute_sound uses DirectSound. Due to the use of SSE intrinsics, MinGW
builds must be made with the compiler option: -march=native, or optionally SSE
can be disabled with CUTE_SOUND_SCALAR_MODE. More on this mode written below.
For Apple machines cute_sound uses CoreAudio.
For Linux builds cute_sound uses SDL2.
An SDL2 implementation of cute_sound is available on platforms SDL2 is available,
which is pretty much everywhere. To use the SDL2 implementation of cute_sound
define CUTE_SOUND_FORCE_SDL before placing the CUTE_SOUND_IMPLEMENTATION into a
translation unit in order to force the use of SDL. Here is an example:
#define CUTE_SOUND_FORCE_SDL
#define CUTE_SOUND_IMPLEMENTATION
#include <cute_sound.h>
If you want to use cute_sound with SDL_RWops, you must enable it by putting this
before you #include cute_sound.h:
#define CUTE_SOUND_SDL_RWOPS
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 (03/27/2023) Added cs_get_global_context and friends.
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.
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 SIMD ACCELERATION
If for whatever reason you don't want to use SSE intrinsics and instead would prefer
plain C (for example if your platform does not support SSE) then define
CUTE_SOUND_SCALAR_MODE before including cute_sound.h while also defining the
symbol definitions. Here's an example:
#define CUTE_SOUND_IMPLEMENTATION
#define CUTE_SOUND_SCALAR_MODE
#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.
FAQ
Q : I would like to use my own memory management, how can I achieve this?
A : This header makes a couple uses of malloc/free, and cs_malloc16/cs_free16. Simply find these bits
and replace them with your own memory allocation routines. They can be wrapped up into a macro,
or call your own functions directly -- it's up to you. Generally these functions allocate fairly
large chunks of memory, and not very often (if at all).
Q : Does this library support audio streaming? Something like System::createStream in FMOD.
A : No. Typically music files aren't that large (in the megabytes). Compare this to a typical
DXT texture of 1024x1024, at 0.5MB of memory. Now say an average music file for a game is three
minutes long. Loading this file into memory and storing it as raw 16bit samples with two channels,
would be:
num_samples = 3 * 60 * 44100 * 2
num_bits = num_samples * 16
num_bytes = num_bits / 8
num_megabytes = num_bytes / (1024 * 1024)
or 30.3mb
So say the user has 2gb of spare RAM. That means we could fit 67 different three minute length
music files in there simultaneously. That is a ridiculous amount of spare memory. 30mb is nothing
nowadays. Just load your music file into memory all at once and then play it.
Q : But I really need streaming of audio files from disk to save memory! Also loading my audio
files (like .OGG) takes a long time (multiple seconds).
A : It is recommended to either A) load up your music files before they are needed, perhaps during
a loading screen, or B) stream in the entire audio into memory on another thread. cs_read_mem_ogg is
a great candidate function to throw onto a job pool. Streaming is more a remnant of older machines
(like in the 90's or early 2000's) where decoding speed and RAM were real nasty bottlenecks. For
more modern machines, these aren't really concerns, even with mobile devices. If even after reading
this Q/A section you still want to stream your audio, you can try mini_al as an alternative:
https://github.com/dr-soft/mini_al
*/
#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_IMPLEMENTATION_ERROR_PLEASE_REPORT_THIS_ON_GITHUB, // https://github.com/RandyGaul/cute_headers/issues
CUTE_SOUND_ERROR_FILE_NOT_FOUND,
CUTE_SOUND_ERROR_INVALID_SOUND,
CUTE_SOUND_ERROR_HWND_IS_NULL,
CUTE_SOUND_ERROR_DIRECTSOUND_CREATE_FAILED,
CUTE_SOUND_ERROR_CREATESOUNDBUFFER_FAILED,
CUTE_SOUND_ERROR_SETFORMAT_FAILED,
CUTE_SOUND_ERROR_AUDIOCOMPONENTFINDNEXT_FAILED,
CUTE_SOUND_ERROR_AUDIOCOMPONENTINSTANCENEW_FAILED,
CUTE_SOUND_ERROR_FAILED_TO_SET_STREAM_FORMAT,
CUTE_SOUND_ERROR_FAILED_TO_SET_RENDER_CALLBACK,
CUTE_SOUND_ERROR_AUDIOUNITINITIALIZE_FAILED,
CUTE_SOUND_ERROR_AUDIOUNITSTART_FAILED,
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_ONLY_PCM_WAV_FILES_ARE_SUPPORTED,
CUTE_SOUND_ERROR_WAV_ONLY_MONO_OR_STEREO_IS_SUPPORTED,
CUTE_SOUND_ERROR_WAV_ONLY_16_BITS_PER_SAMPLE_SUPPORTED,
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,
} cs_error_t;
const char* cs_error_as_string(cs_error_t error);
// -------------------------------------------------------------------------------------------------
// Cute sound context functions.
/**
* Pass in NULL for `os_handle`, except for the DirectSound backend this should be hwnd.
* play_frequency_in_Hz depends on your audio file, 44100 seems to be fine.
* buffered_samples is clamped to be at least 1024.
*/
cs_error_t cs_init(void* os_handle, unsigned play_frequency_in_Hz, int buffered_samples, 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);
/**
* Spawns a mixing thread dedicated to mixing audio in the background.
* If you don't call this function mixing will happen on the main-thread when you call `cs_update`.
*/
void cs_spawn_mix_thread();
/**
* In cases where the mixing thread takes up extra CPU attention doing nothing, you can force
* it to sleep manually. You can tune this as necessary, but it's probably not necessary for you.
*/
void cs_mix_thread_sleep_delay(int milliseconds);
/**
* 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;
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_
// -------------------------------------------------------------------------------------------------
// 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_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 */);
uint64_t cs_music_get_sample_index();
cs_error_t cs_music_set_sample_index(uint64_t sample_index);
// -------------------------------------------------------------------------------------------------
// Playing sounds.
typedef struct cs_playing_sound_t { uint64_t id; } cs_playing_sound_t;
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 delay /* = 0 */;
} 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);
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);
uint64_t cs_sound_get_sample_index(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);
cs_error_t cs_sound_set_sample_index(cs_playing_sound_t sound, uint64_t sample_index);
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
#ifndef CUTE_SOUND_MINIMUM_BUFFERED_SAMPLES
# define CUTE_SOUND_MINIMUM_BUFFERED_SAMPLES 1024
#endif
#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
// Platform detection.
#define CUTE_SOUND_WINDOWS 1
#define CUTE_SOUND_APPLE 2
#define CUTE_SOUND_SDL 3
// Use CUTE_SOUND_FORCE_SDL as a way to force CUTE_SOUND_PLATFORM_SDL.
#ifdef CUTE_SOUND_FORCE_SDL
#define CUTE_SOUND_PLATFORM_SDL
#endif
#ifndef CUTE_SOUND_PLATFORM
// Check the specific platform defines.
#ifdef CUTE_SOUND_PLATFORM_WINDOWS
#define CUTE_SOUND_PLATFORM CUTE_SOUND_WINDOWS
#elif defined(CUTE_SOUND_PLATFORM_APPLE)
#define CUTE_SOUND_PLATFORM CUTE_SOUND_APPLE
#elif defined(CUTE_SOUND_PLATFORM_SDL)
#define CUTE_SOUND_PLATFORM CUTE_SOUND_SDL
#else
// Detect the platform automatically.
#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
#define CUTE_SOUND_PLATFORM CUTE_SOUND_WINDOWS
#elif defined(__APPLE__)
#define CUTE_SOUND_PLATFORM CUTE_SOUND_APPLE
#else
// Just use SDL on other esoteric platforms.
#define CUTE_SOUND_PLATFORM CUTE_SOUND_SDL
#endif
#endif
#endif
// Platform specific file inclusions.
#if CUTE_SOUND_PLATFORM == CUTE_SOUND_WINDOWS
#ifndef _WINDOWS_
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
#ifndef _WAVEFORMATEX_
#include <mmreg.h>
#include <mmsystem.h>
#endif
#include <dsound.h>
#undef PlaySound
#ifdef _MSC_VER
#pragma comment(lib, "dsound.lib")
#endif
#elif CUTE_SOUND_PLATFORM == CUTE_SOUND_APPLE
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include <pthread.h>
#include <mach/mach_time.h>
#elif CUTE_SOUND_PLATFORM == CUTE_SOUND_SDL
#ifndef SDL_h_
// Define CUTE_SOUND_SDL_H to allow changing the SDL.h path.
#ifndef CUTE_SOUND_SDL_H
#define CUTE_SOUND_SDL_H <SDL.h>
#endif
#include CUTE_SOUND_SDL_H
#endif
#ifndef _WIN32
#include <alloca.h>
#endif
#else
#error Unsupported platform - please choose one of CUTE_SOUND_WINDOWS, CUTE_SOUND_APPLE, CUTE_SOUND_SDL.
#endif
#ifdef CUTE_SOUND_SCALAR_MODE
#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_load_ps(float const* mem_addr)
{
cs__m128 a;
a.a = mem_addr[0];
a.b = mem_addr[1];
a.c = mem_addr[2];
a.d = mem_addr[3];
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_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 = a.a;
b.b = a.b;
b.c = a.c;
b.d = 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;
}
#else // CUTE_SOUND_SCALAR_MODE
#include <xmmintrin.h>
#include <emmintrin.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_load_ps _mm_load_ps
#define cs_mm_add_ps _mm_add_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
#endif // CUTE_SOUND_SCALAR_MODE
#define CUTE_SOUND_ALIGN(X, Y) ((((size_t)X) + ((Y) - 1)) & ~((Y) - 1))
#define CUTE_SOUND_TRUNC(X, Y) ((size_t)(X) & ~((Y) - 1))
// -------------------------------------------------------------------------------------------------
// hashtable.h implementation by Mattias Gustavsson
// See: http://www.mattiasgustavsson.com/ and https://github.com/mattiasgustavsson/libs/blob/master/hashtable.h
// begin hashtable.h
#ifndef HASHTABLE_MEMSET
#define HASHTABLE_MEMSET(ptr, val, n) CUTE_SOUND_MEMSET(ptr, val, n)
#endif
#ifndef HASHTABLE_MEMCPY
#define HASHTABLE_MEMCPY(dst, src, n) CUTE_SOUND_MEMCPY(dst, src, n)
#endif
#ifndef HASHTABLE_MALLOC
#define HASHTABLE_MALLOC(ctx, size) CUTE_SOUND_ALLOC(size, ctx)
#endif
#ifndef HASHTABLE_FREE
#define HASHTABLE_FREE(ctx, ptr) CUTE_SOUND_FREE(ptr, ctx)
#endif
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
hashtable.h - v1.1 - Cache efficient hash table implementation for C/C++.
Do this:
#define HASHTABLE_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef hashtable_h
#define hashtable_h
#ifndef HASHTABLE_U64
#define HASHTABLE_U64 unsigned long long
#endif
typedef struct hashtable_t hashtable_t;
void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx );
void hashtable_term( hashtable_t* table );
void* hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item );
void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key );
void hashtable_clear( hashtable_t* table );
void* hashtable_find( hashtable_t const* table, HASHTABLE_U64 key );
int hashtable_count( hashtable_t const* table );
void* hashtable_items( hashtable_t const* table );
HASHTABLE_U64 const* hashtable_keys( hashtable_t const* table );
void hashtable_swap( hashtable_t* table, int index_a, int index_b );
#endif /* hashtable_h */
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifndef hashtable_t_h
#define hashtable_t_h
#ifndef HASHTABLE_U32
#define HASHTABLE_U32 unsigned int
#endif
struct hashtable_internal_slot_t
{
HASHTABLE_U32 key_hash;
int item_index;
int base_count;
};
struct hashtable_t
{
void* memctx;
int count;
int item_size;
struct hashtable_internal_slot_t* slots;
int slot_capacity;
HASHTABLE_U64* items_key;
int* items_slot;
void* items_data;
int item_capacity;
void* swap_temp;
};
#endif /* hashtable_t_h */
// end hashtable.h
#define HASHTABLE_IMPLEMENTATION
#ifdef HASHTABLE_IMPLEMENTATION
#ifndef HASHTABLE_IMPLEMENTATION_ONCE
#define HASHTABLE_IMPLEMENTATION_ONCE
// hashtable.h implementation by Mattias Gustavsson
// See: http://www.mattiasgustavsson.com/ and https://github.com/mattiasgustavsson/libs/blob/master/hashtable.h
// begin hashtable.h (continuing from first time)
#ifndef HASHTABLE_SIZE_T
#include <stddef.h>
#define HASHTABLE_SIZE_T size_t
#endif
#ifndef HASHTABLE_ASSERT
#include <assert.h>
#define HASHTABLE_ASSERT( x ) assert( x )
#endif
#ifndef HASHTABLE_MEMSET
#include <string.h>
#define HASHTABLE_MEMSET( ptr, val, cnt ) ( memset( ptr, val, cnt ) )
#endif
#ifndef HASHTABLE_MEMCPY
#include <string.h>
#define HASHTABLE_MEMCPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) )
#endif
#ifndef HASHTABLE_MALLOC
#include <stdlib.h>
#define HASHTABLE_MALLOC( ctx, size ) ( malloc( size ) )
#define HASHTABLE_FREE( ctx, ptr ) ( free( ptr ) )
#endif
static HASHTABLE_U32 hashtable_internal_pow2ceil( HASHTABLE_U32 v )
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
++v;
v += ( v == 0 );
return v;
}
void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx )
{
initial_capacity = (int)hashtable_internal_pow2ceil( initial_capacity >=0 ? (HASHTABLE_U32) initial_capacity : 32U );
table->memctx = memctx;
table->count = 0;
table->item_size = item_size;
table->slot_capacity = (int) hashtable_internal_pow2ceil( (HASHTABLE_U32) ( initial_capacity + initial_capacity / 2 ) );
int slots_size = (int)( table->slot_capacity * sizeof( *table->slots ) );
table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) slots_size );
HASHTABLE_ASSERT( table->slots );
HASHTABLE_MEMSET( table->slots, 0, (HASHTABLE_SIZE_T) slots_size );
table->item_capacity = (int) hashtable_internal_pow2ceil( (HASHTABLE_U32) initial_capacity );
table->items_key = (HASHTABLE_U64*) HASHTABLE_MALLOC( table->memctx,
table->item_capacity * ( sizeof( *table->items_key ) + sizeof( *table->items_slot ) + table->item_size ) + table->item_size );
HASHTABLE_ASSERT( table->items_key );
table->items_slot = (int*)( table->items_key + table->item_capacity );
table->items_data = (void*)( table->items_slot + table->item_capacity );
table->swap_temp = (void*)( ( (uintptr_t) table->items_data ) + table->item_size * table->item_capacity );
}
void hashtable_term( hashtable_t* table )
{
HASHTABLE_FREE( table->memctx, table->items_key );
HASHTABLE_FREE( table->memctx, table->slots );
}
// from https://gist.github.com/badboy/6267743
static HASHTABLE_U32 hashtable_internal_calculate_hash( HASHTABLE_U64 key )
{
key = ( ~key ) + ( key << 18 );
key = key ^ ( key >> 31 );
key = key * 21;
key = key ^ ( key >> 11 );
key = key + ( key << 6 );
key = key ^ ( key >> 22 );
HASHTABLE_ASSERT( key );
return (HASHTABLE_U32) key;
}
static int hashtable_internal_find_slot( hashtable_t const* table, HASHTABLE_U64 key )
{
int const slot_mask = table->slot_capacity - 1;
HASHTABLE_U32 const hash = hashtable_internal_calculate_hash( key );
int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask );
int base_count = table->slots[ base_slot ].base_count;
int slot = base_slot;
while( base_count > 0 )
{
HASHTABLE_U32 slot_hash = table->slots[ slot ].key_hash;
if( slot_hash )
{
int slot_base = (int)( slot_hash & (HASHTABLE_U32)slot_mask );
if( slot_base == base_slot )
{
HASHTABLE_ASSERT( base_count > 0 );
--base_count;
if( slot_hash == hash && table->items_key[ table->slots[ slot ].item_index ] == key )
return slot;
}
}
slot = ( slot + 1 ) & slot_mask;
}
return -1;
}
static void hashtable_internal_expand_slots( hashtable_t* table )
{
int const old_capacity = table->slot_capacity;
struct hashtable_internal_slot_t* old_slots = table->slots;
table->slot_capacity *= 2;
int const slot_mask = table->slot_capacity - 1;
int const size = (int)( table->slot_capacity * sizeof( *table->slots ) );
table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) size );
HASHTABLE_ASSERT( table->slots );
HASHTABLE_MEMSET( table->slots, 0, (HASHTABLE_SIZE_T) size );
for( int i = 0; i < old_capacity; ++i )
{
HASHTABLE_U32 const hash = old_slots[ i ].key_hash;
if( hash )
{
int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask );
int slot = base_slot;
while( table->slots[ slot ].key_hash )
slot = ( slot + 1 ) & slot_mask;
table->slots[ slot ].key_hash = hash;
int item_index = old_slots[ i ].item_index;
table->slots[ slot ].item_index = item_index;
table->items_slot[ item_index ] = slot;
++table->slots[ base_slot ].base_count;
}
}
HASHTABLE_FREE( table->memctx, old_slots );
}
static void hashtable_internal_expand_items( hashtable_t* table )
{
table->item_capacity *= 2;
HASHTABLE_U64* const new_items_key = (HASHTABLE_U64*) HASHTABLE_MALLOC( table->memctx,
table->item_capacity * ( sizeof( *table->items_key ) + sizeof( *table->items_slot ) + table->item_size ) + table->item_size);
HASHTABLE_ASSERT( new_items_key );
int* const new_items_slot = (int*)( new_items_key + table->item_capacity );
void* const new_items_data = (void*)( new_items_slot + table->item_capacity );
void* const new_swap_temp = (void*)( ( (uintptr_t) new_items_data ) + table->item_size * table->item_capacity );
HASHTABLE_MEMCPY( new_items_key, table->items_key, table->count * sizeof( *table->items_key ) );
HASHTABLE_MEMCPY( new_items_slot, table->items_slot, table->count * sizeof( *table->items_key ) );
HASHTABLE_MEMCPY( new_items_data, table->items_data, (HASHTABLE_SIZE_T) table->count * table->item_size );
HASHTABLE_FREE( table->memctx, table->items_key );
table->items_key = new_items_key;
table->items_slot = new_items_slot;
table->items_data = new_items_data;
table->swap_temp = new_swap_temp;
}
void* hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item )
{
HASHTABLE_ASSERT( hashtable_internal_find_slot( table, key ) < 0 );
if( table->count >= ( table->slot_capacity - table->slot_capacity / 3 ) )
hashtable_internal_expand_slots( table );
int const slot_mask = table->slot_capacity - 1;
HASHTABLE_U32 const hash = hashtable_internal_calculate_hash( key );
int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask );
int base_count = table->slots[ base_slot ].base_count;
int slot = base_slot;
int first_free = slot;
while( base_count )
{
HASHTABLE_U32 const slot_hash = table->slots[ slot ].key_hash;
if( slot_hash == 0 && table->slots[ first_free ].key_hash != 0 ) first_free = slot;
int slot_base = (int)( slot_hash & (HASHTABLE_U32)slot_mask );
if( slot_base == base_slot )
--base_count;
slot = ( slot + 1 ) & slot_mask;
}
slot = first_free;
while( table->slots[ slot ].key_hash )
slot = ( slot + 1 ) & slot_mask;
if( table->count >= table->item_capacity )
hashtable_internal_expand_items( table );
HASHTABLE_ASSERT( !table->slots[ slot ].key_hash && ( hash & (HASHTABLE_U32) slot_mask ) == (HASHTABLE_U32) base_slot );
HASHTABLE_ASSERT( hash );
table->slots[ slot ].key_hash = hash;
table->slots[ slot ].item_index = table->count;
++table->slots[ base_slot ].base_count;
void* dest_item = (void*)( ( (uintptr_t) table->items_data ) + table->count * table->item_size );
HASHTABLE_MEMCPY( dest_item, item, (HASHTABLE_SIZE_T) table->item_size );
table->items_key[ table->count ] = key;