forked from WohlSoft/SDL-Mixer-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.c
3924 lines (3408 loc) · 99 KB
/
music.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
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_timer.h"
#include "SDL_mixer.h"
#include "mixer.h"
#include "music.h"
#include "music_cmd.h"
#include "music_wav.h"
#include "music_modplug.h"
#include "music_xmp.h"
#include "music_nativemidi.h"
#include "music_fluidsynth.h"
#include "music_timidity.h"
#include "music_ogg.h"
#include "music_opus.h"
#include "music_drmp3.h"
#include "music_mpg123.h"
#include "music_drflac.h"
#include "music_flac.h"
#include "music_wavpack.h"
#include "music_gme.h"
#include "native_midi/native_midi.h"
#include "music_midi_adl.h"
#include "music_midi_opn.h"
#include "music_midi_edmidi.h"
#include "music_ffmpeg.h"
#include "music_pxtone.h"
#include "utils.h"
#include "mp3utils.h"
/* Check to make sure we are building with a new enough SDL */
#if SDL_COMPILEDVERSION < SDL_VERSIONNUM(2, 0, 7)
#error You need SDL 2.0.7 or newer from http://www.libsdl.org
#endif
/* Set this hint to true if you want verbose logging of music interfaces */
#define SDL_MIXER_HINT_DEBUG_MUSIC_INTERFACES \
"SDL_MIXER_DEBUG_MUSIC_INTERFACES"
char *music_cmd = NULL;
static SDL_bool music_active = SDL_TRUE;
static int music_volume = MIX_MAX_VOLUME;
static Mix_Music * volatile music_playing = NULL;
SDL_AudioSpec music_spec;
/* ========== Multi-Music ========== */
static int num_streams = 0;
static Mix_Music **mix_streams = NULL;
static Uint8 *mix_streams_buffer = NULL;
static int num_streams_capacity = 0;
static int music_general_volume = MIX_MAX_VOLUME;
typedef struct _Mix_effectinfo
{
Mix_MusicEffectFunc_t callback;
Mix_MusicEffectDone_t done_callback;
void *udata;
struct _Mix_effectinfo *next;
} mus_effect_info;
/* Add music into the chain of playing songs, reject duplicated songs */
static SDL_bool _Mix_MultiMusic_Add(Mix_Music *mus)
{
const size_t inc = 10;
int i;
if (music_playing == mus) {
Mix_SetError("Music stream is already playing through old Music API");
return SDL_FALSE;
}
if (!mix_streams) {
mix_streams = SDL_calloc(1, sizeof(Mix_Music *) * inc);
if (!mix_streams) {
SDL_OutOfMemory();
return SDL_FALSE;
}
num_streams_capacity = inc;
SDL_memset(mix_streams, 0, sizeof(Mix_Music*) * num_streams_capacity);
mix_streams_buffer = SDL_calloc(1, music_spec.size);
if (!mix_streams_buffer) {
if (mix_streams) {
SDL_free(mix_streams);
mix_streams = NULL;
}
SDL_OutOfMemory();
return SDL_FALSE;
}
} else if (num_streams >= num_streams_capacity) {
mix_streams = SDL_realloc(mix_streams, sizeof(Mix_Music *) * (num_streams_capacity + inc));
SDL_memset(mix_streams + num_streams_capacity, 0, sizeof(Mix_Music*) * inc);
num_streams_capacity += inc;
}
for (i = 0; i < num_streams; ++i) {
if (mix_streams[i] == mus) {
Mix_SetError("Music stream is already playing");
return SDL_FALSE;
}
}
mix_streams[num_streams++] = mus;
return SDL_TRUE;
}
/* Check if song is already playing */
static SDL_bool _Mix_MultiMusic_InPlayQueue(Mix_Music *mus)
{
int i;
if (!mix_streams) {
return SDL_FALSE;
}
for (i = 0; i < num_streams; ++i) {
if (mix_streams[i] == mus) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
/* Remove music from the chain of playing songs */
static SDL_bool _Mix_MultiMusic_Remove(Mix_Music *mus)
{
int i = 0;
SDL_bool found = SDL_FALSE;
if (num_streams == 0) {
Mix_SetError("There is no playing music streams");
return SDL_FALSE;
}
for (i = 0; i < num_streams; ++i) {
if (!found) {
if (mix_streams[i] == mus) {
num_streams--;
found = SDL_TRUE;
}
}
if (found) {
mix_streams[i] = mix_streams[i + 1];
}
}
return found;
}
static void _Mix_MultiMusic_CloseAndFree(void)
{
int i;
if (!mix_streams) {
return;
}
for (i = 0; i < num_streams; ++i) {
Mix_FreeMusic(mix_streams[i]);
}
num_streams = 0;
num_streams_capacity = 0;
SDL_free(mix_streams);
mix_streams = NULL;
if (!mix_streams_buffer) {
SDL_free(mix_streams_buffer);
mix_streams_buffer = NULL;
}
}
static void _Mix_MultiMusic_HaltAll(void)
{
int i;
if (!mix_streams) {
return;
}
for (i = 0; i < num_streams; ++i) {
Mix_HaltMusicStream(mix_streams[i]);
}
}
static void _Mix_MultiMusic_PauseAll(void)
{
int i;
if (!mix_streams) {
return;
}
for (i = 0; i < num_streams; ++i) {
Mix_PauseMusicStream(mix_streams[i]);
}
}
static void _Mix_MultiMusic_ResumeAll(void)
{
int i;
if (!mix_streams) {
return;
}
for (i = 0; i < num_streams; ++i) {
Mix_ResumeMusicStream(mix_streams[i]);
}
}
/* ========== Multi-Music =END====== */
typedef struct _Eff_positionargs position_args;
struct _Mix_Music {
Mix_MusicInterface *interface;
void *context;
SDL_bool playing;
Mix_Fading fading;
int fade_step;
int fade_steps;
void (SDLCALL *music_finished_hook)(Mix_Music*, void*);
void *music_finished_hook_user_data;
mus_effect_info *effects;
position_args *pos_args;
int is_multimusic;
int music_active;
int music_volume;
int music_halted;
int free_on_stop;
char filename[1024];
};
void _Mix_SetMusicPositionArgs(Mix_Music *mus, position_args *args)
{
mus->pos_args = args;
}
position_args *_Mix_GetMusicPositionArgs(Mix_Music *mus)
{
return mus->pos_args;
}
/* ========== Multi-Music effects ========== */
/*
* rcg06122001 The special effects exportable API.
* Please see effect_*.c for internally-implemented effects, such
* as Mix_SetPanning().
*/
/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
static int _Mix_register_mus_effect(mus_effect_info **e, Mix_MusicEffectFunc_t f,
Mix_MusicEffectDone_t d, void *arg)
{
mus_effect_info *new_e;
if (!e) {
Mix_SetError("Internal error");
return(0);
}
if (f == NULL) {
Mix_SetError("NULL effect callback");
return(0);
}
new_e = SDL_malloc(sizeof (mus_effect_info));
if (new_e == NULL) {
Mix_SetError("Out of memory");
return(0);
}
new_e->callback = f;
new_e->done_callback = d;
new_e->udata = arg;
new_e->next = NULL;
/* add new effect to end of linked list... */
if (*e == NULL) {
*e = new_e;
} else {
mus_effect_info *cur = *e;
while (1) {
if (cur->next == NULL) {
cur->next = new_e;
break;
}
cur = cur->next;
}
}
return(1);
}
/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
static int _Mix_remove_mus_effect(Mix_Music *mus, mus_effect_info **e, Mix_MusicEffectFunc_t f)
{
mus_effect_info *cur;
mus_effect_info *prev = NULL;
mus_effect_info *next = NULL;
if (!e) {
Mix_SetError("Internal error");
return(0);
}
for (cur = *e; cur != NULL; cur = cur->next) {
if (cur->callback == f) {
next = cur->next;
if (cur->done_callback != NULL) {
cur->done_callback(mus, cur->udata);
}
SDL_free(cur);
if (prev == NULL) { /* removing first item of list? */
*e = next;
} else {
prev->next = next;
}
return(1);
}
prev = cur;
}
Mix_SetError("No such effect registered");
return(0);
}
/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
static int _Mix_remove_all_mus_effects(Mix_Music *mus, mus_effect_info **e)
{
mus_effect_info *cur;
mus_effect_info *next;
if (!e) {
Mix_SetError("Internal error");
return(0);
}
for (cur = *e; cur != NULL; cur = next) {
next = cur->next;
if (cur->done_callback != NULL) {
cur->done_callback(mus, cur->udata);
}
SDL_free(cur);
}
*e = NULL;
return(1);
}
/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
int _Mix_RegisterMusicEffect_locked(Mix_Music *mus, Mix_MusicEffectFunc_t f,
Mix_MusicEffectDone_t d, void *arg)
{
mus_effect_info **e = NULL;
if (!mus) {
Mix_SetError("Invalid music");
return(0);
}
e = &mus->effects;
return _Mix_register_mus_effect(e, f, d, arg);
}
int MIXCALLCC Mix_RegisterMusicEffect(Mix_Music *mus, Mix_MusicEffectFunc_t f,
Mix_MusicEffectDone_t d, void *arg)
{
int retval;
Mix_LockAudio();
retval = _Mix_RegisterMusicEffect_locked(mus, f, d, arg);
Mix_UnlockAudio();
return retval;
}
/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
int _Mix_UnregisterMusicEffect_locked(Mix_Music *mus, Mix_MusicEffectFunc_t f)
{
mus_effect_info **e = NULL;
if (!mus) {
Mix_SetError("Invalid music");
return(0);
}
e = &mus->effects;
return _Mix_remove_mus_effect(mus, e, f);
}
int MIXCALLCC Mix_UnregisterMusicEffect(Mix_Music *mus, Mix_MusicEffectFunc_t f)
{
int retval;
Mix_LockAudio();
retval = _Mix_UnregisterMusicEffect_locked(mus, f);
Mix_UnlockAudio();
return(retval);
}
/* MAKE SURE you hold the audio lock (Mix_LockAudio()) before calling this! */
int _Mix_UnregisterAllMusicEffects_locked(Mix_Music *mus)
{
mus_effect_info **e = NULL;
if (!mus) {
Mix_SetError("Invalid music");
return(0);
}
e = &mus->effects;
return _Mix_remove_all_mus_effects(mus, e);
}
int MIXCALLCC Mix_UnregisterAllMusicEffects(Mix_Music *mus)
{
int retval;
Mix_LockAudio();
retval = _Mix_UnregisterAllMusicEffects_locked(mus);
Mix_UnlockAudio();
return(retval);
}
static void Mix_Music_DoEffects(Mix_Music *mus, void *snd, int len)
{
mus_effect_info *e = mus->effects;
if (e != NULL) { /* are there any registered effects? */
for (; e != NULL; e = e->next) {
if (e->callback != NULL) {
e->callback(mus, snd, len, e->udata);
}
}
}
}
/* ========== Multi-Music effects =END====== */
/* Used to calculate fading steps */
static int ms_per_step;
/* rcg06042009 report available decoders at runtime. */
static const char **music_decoders = NULL;
static int num_decoders = 0;
/* Semicolon-separated SoundFont paths */
static char* soundfont_paths = NULL;
/* full path of timidity config file */
static char* timidity_cfg = NULL;
/* ======== MIDI toggler ======== */
/* MIDI player currently in use */
int midiplayer_current = MIDI_ANY;
/* Denies MIDI arguments */
static int midiplayer_args_lock = 0;
/* ======== MIDI toggler END ==== */
/* Meta-Tags utility */
void meta_tags_init(Mix_MusicMetaTags *tags)
{
SDL_memset(tags, 0, sizeof(Mix_MusicMetaTags));
}
void meta_tags_clear(Mix_MusicMetaTags *tags)
{
size_t i;
for (i = 0; i < MIX_META_LAST; i++) {
if (tags->tags[i]) {
SDL_free(tags->tags[i]);
tags->tags[i] = NULL;
}
}
}
void meta_tags_set(Mix_MusicMetaTags *tags, Mix_MusicMetaTag type, const char *value)
{
char *out;
size_t len;
if (!value) {
return;
}
if (type >= MIX_META_LAST) {
return;
}
len = SDL_strlen(value);
out = (char *)SDL_malloc(sizeof(char) * len + 1);
SDL_strlcpy(out, value, len +1);
if (tags->tags[type]) {
SDL_free(tags->tags[type]);
}
tags->tags[type] = out;
}
const char *meta_tags_get(Mix_MusicMetaTags *tags, Mix_MusicMetaTag type)
{
switch (type) {
case MIX_META_TITLE:
case MIX_META_ARTIST:
case MIX_META_ALBUM:
case MIX_META_COPYRIGHT:
return tags->tags[type] ? tags->tags[type] : "";
case MIX_META_LAST:
default:
break;
}
return "";
}
/* for music->filename */
#if defined(__WIN32__)||defined(__OS2__)
static SDL_INLINE const char *get_last_dirsep (const char *p) {
const char *p1 = SDL_strrchr(p, '/');
const char *p2 = SDL_strrchr(p, '\\');
if (!p1) return p2;
if (!p2) return p1;
return (p1 > p2)? p1 : p2;
}
#else /* unix */
static SDL_INLINE const char *get_last_dirsep (const char *p) {
return SDL_strrchr(p, '/');
}
#endif
/* Interfaces for the various music interfaces, ordered by priority */
static Mix_MusicInterface *s_music_interfaces[] =
{
#ifdef MUSIC_CMD
&Mix_MusicInterface_CMD,
#endif
#ifdef MUSIC_WAV
&Mix_MusicInterface_WAV,
#endif
#ifdef MUSIC_FLAC_DRFLAC
&Mix_MusicInterface_DRFLAC,
#endif
#ifdef MUSIC_FLAC_LIBFLAC
&Mix_MusicInterface_FLAC,
#endif
#ifdef MUSIC_WAVPACK
&Mix_MusicInterface_WAVPACK,
#endif
#ifdef MUSIC_OGG
&Mix_MusicInterface_OGG,
#endif
#ifdef MUSIC_OPUS
&Mix_MusicInterface_Opus,
#endif
#ifdef MUSIC_MID_ADLMIDI
&Mix_MusicInterface_ADLMIDI,
&Mix_MusicInterface_ADLIMF,
#endif
#ifdef MUSIC_MID_OPNMIDI
&Mix_MusicInterface_OPNMIDI,
&Mix_MusicInterface_OPNXMI,
#endif
#ifdef MUSIC_MP3_DRMP3
&Mix_MusicInterface_DRMP3,
#endif
#ifdef MUSIC_MP3_MPG123
&Mix_MusicInterface_MPG123,
#endif
#ifdef MUSIC_FFMPEG
&Mix_MusicInterface_FFMPEG,
#endif
#ifdef MUSIC_PXTONE
&Mix_MusicInterface_PXTONE,
#endif
#ifdef MUSIC_MOD_XMP
&Mix_MusicInterface_XMP,
#endif
#ifdef MUSIC_MOD_MODPLUG
&Mix_MusicInterface_MODPLUG,
#endif
#ifdef MUSIC_MID_FLUIDSYNTH
&Mix_MusicInterface_FLUIDSYNTH,
#endif
#ifdef MUSIC_MID_FLUIDLITE
&Mix_MusicInterface_FLUIDXMI,
#endif
#ifdef MUSIC_MID_EDMIDI
&Mix_MusicInterface_EDMIDI,
#endif
#ifdef MUSIC_MID_TIMIDITY
&Mix_MusicInterface_TIMIDITY,
#endif
#ifdef MUSIC_MID_NATIVE
&Mix_MusicInterface_NATIVEMIDI,
#endif
#ifdef MUSIC_MID_NATIVE_ALT
&Mix_MusicInterface_NATIVEXMI,
#endif
#ifdef MUSIC_GME
&Mix_MusicInterface_GME,
#endif
NULL
};
int get_num_music_interfaces(void)
{
return SDL_arraysize(s_music_interfaces) - 1;
}
Mix_MusicInterface *get_music_interface(int index)
{
return s_music_interfaces[index];
}
int MIXCALLCC Mix_GetNumMusicDecoders(void)
{
return(num_decoders);
}
const char *MIXCALLCC Mix_GetMusicDecoder(int index)
{
if ((index < 0) || (index >= num_decoders)) {
return NULL;
}
return(music_decoders[index]);
}
SDL_bool MIXCALLCC Mix_HasMusicDecoder(const char *name)
{
int index;
for (index = 0; index < num_decoders; ++index) {
if (SDL_strcasecmp(name, music_decoders[index]) == 0) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static void add_music_decoder(const char *decoder)
{
void *ptr;
int i;
/* Check to see if we already have this decoder */
for (i = 0; i < num_decoders; ++i) {
if (SDL_strcmp(music_decoders[i], decoder) == 0) {
return;
}
}
ptr = SDL_realloc((void *)music_decoders, ((size_t)num_decoders + 1) * sizeof (const char *));
if (ptr == NULL) {
return; /* oh well, go on without it. */
}
music_decoders = (const char **) ptr;
music_decoders[num_decoders++] = decoder;
}
/* Local low-level functions prototypes */
static void music_internal_initialize_volume(void);
static void music_internal_initialize_volume_stream(Mix_Music *music);
static void music_internal_volume(Mix_Music *music, int volume);
static int music_internal_play(Mix_Music *music, int play_count, double position);
static int music_internal_position(Mix_Music *music, double position);
static SDL_bool music_internal_playing(Mix_Music *music);
static void music_internal_halt(Mix_Music *music);
/* Support for hooking when the music has finished */
static void (SDLCALL *music_finished_hook)(void) = NULL;
/* Support for hooking when the any multi-music has finished */
static void (SDLCALL *music_finished_hook_mm)(void) = NULL;
void MIXCALLCC Mix_HookMusicFinished(void (SDLCALL *music_finished)(void))
{
Mix_LockAudio();
music_finished_hook = music_finished;
Mix_UnlockAudio();
}
void MIXCALLCC Mix_HookMusicStreamFinishedAny(void (SDLCALL *music_finished)(void))
{
Mix_LockAudio();
music_finished_hook_mm = music_finished;
Mix_UnlockAudio();
}
void MIXCALLCC Mix_HookMusicStreamFinished(Mix_Music *music, void (SDLCALL *music_finished)(Mix_Music*, void*), void *user_data)
{
Mix_LockAudio();
music->music_finished_hook = music_finished;
music->music_finished_hook_user_data = user_data;
Mix_UnlockAudio();
}
/* Convenience function to fill audio and mix at the specified volume
This is called from many music player's GetAudio callback.
*/
int music_pcm_getaudio(void *context, void *data, int bytes, int volume,
int (*GetSome)(void *context, void *data, int bytes, SDL_bool *done))
{
Uint8 *snd = (Uint8 *)data;
Uint8 *dst;
int len = bytes;
int zero_cycles = 0;
const int MAX_ZERO_CYCLES = 10; /* just try to catch infinite loops */
SDL_bool done = SDL_FALSE;
if (volume == MIX_MAX_VOLUME) {
dst = snd;
} else {
dst = SDL_stack_alloc(Uint8, (size_t)bytes);
}
while (len > 0 && !done) {
int consumed = GetSome(context, dst, len, &done);
if (consumed < 0) {
break;
}
if (consumed == 0) {
++zero_cycles;
if (zero_cycles > MAX_ZERO_CYCLES) {
/* We went too many cycles with no data, we're done */
done = SDL_TRUE;
}
continue;
}
zero_cycles = 0;
if (volume == MIX_MAX_VOLUME) {
dst += consumed;
} else {
SDL_MixAudioFormat(snd, dst, music_spec.format, (Uint32)consumed, volume);
snd += consumed;
}
len -= consumed;
}
if (volume != MIX_MAX_VOLUME) {
SDL_stack_free(dst);
}
return len;
}
/* Mixing function */
static SDL_INLINE int music_mix_stream(Mix_Music *music, void *udata, Uint8 *stream, int len)
{
SDL_bool done = SDL_FALSE;
(void)udata;
while (music && music->music_active && len > 0 && !done) {
/* Handle fading */
if (music->fading != MIX_NO_FADING) {
if (music->fade_step++ < music->fade_steps) {
int volume;
int fade_step = music->fade_step;
int fade_steps = music->fade_steps;
if (music->fading == MIX_FADING_OUT) {
volume = (music->music_volume * (fade_steps-fade_step)) / fade_steps;
} else { /* Fading in */
volume = (music->music_volume * fade_step) / fade_steps;
}
music_internal_volume(music, volume);
} else {
if (music->fading == MIX_FADING_OUT) {
music_internal_halt(music);
if (music->music_finished_hook) {
music->music_finished_hook(music, music->music_finished_hook_user_data);
}
if (music_finished_hook_mm) {
music_finished_hook_mm();
}
return -1;
}
music->fading = MIX_NO_FADING;
}
}
if (music->interface->GetAudio) {
int left = music->interface->GetAudio(music->context, stream, len);
if (left != 0) {
/* Either an error or finished playing with data left */
music->playing = SDL_FALSE;
done = SDL_TRUE;
}
if (left > 0) {
stream += (len - left);
len = left;
} else {
len = 0;
}
} else {
len = 0;
}
if (!music_internal_playing(music)) {
music_internal_halt(music);
if (music->music_finished_hook) {
music->music_finished_hook(music, music->music_finished_hook_user_data);
}
if (music_finished_hook_mm) {
music_finished_hook_mm();
}
}
}
return 0;
}
void SDLCALL multi_music_mixer(void *udata, Uint8 *stream, int len)
{
int i;
Mix_Music *m;
if (!mix_streams) {
return; /* Nothing to process */
}
/* Mix currently working streams */
for (i = 0; i < num_streams; ++i) {
m = mix_streams[i];
if (m && m->music_active) {
SDL_memset(mix_streams_buffer, music_spec.silence, (size_t)len);
music_mix_stream(m, udata, mix_streams_buffer, len);
Mix_Music_DoEffects(m, mix_streams_buffer, len);
SDL_MixAudioFormat(stream, mix_streams_buffer, music_spec.format, len, music_general_volume);
}
}
/* Clean-up halted streams */
for (i = 0; i < num_streams; ++i) {
m = mix_streams[i];
if (!m || m->music_halted) {
_Mix_MultiMusic_Remove(m);
if (m && m->free_on_stop) {
_Mix_remove_all_mus_effects(m, &m->effects);
m->interface->Delete(m->context);
SDL_free(m);
}
i--;
}
}
}
void SDLCALL music_mixer(void *udata, Uint8 *stream, int len)
{
Mix_Music *music;
SDL_bool done = SDL_FALSE;
Uint8 *src_stream = stream;
int src_len = len;
(void)udata;
while (music_playing && music_active && len > 0 && !done) {
/* Handle fading */
if (music_playing->fading != MIX_NO_FADING) {
if (music_playing->fade_step++ < music_playing->fade_steps) {
int volume;
int fade_step = music_playing->fade_step;
int fade_steps = music_playing->fade_steps;
if (music_playing->fading == MIX_FADING_OUT) {
volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
} else { /* Fading in */
volume = (music_volume * fade_step) / fade_steps;
}
music_internal_volume(music_playing, volume);
} else {
if (music_playing->fading == MIX_FADING_OUT) {
music = music_playing;
music_internal_halt(music_playing);
if (music && music->music_finished_hook) {
music->music_finished_hook(music, music->music_finished_hook_user_data);
}
if (music_finished_hook) {
music_finished_hook();
}
return;
}
music_playing->fading = MIX_NO_FADING;
}
}
if (music_playing->interface->GetAudio) {
int left = music_playing->interface->GetAudio(music_playing->context, stream, len);
if (left != 0) {
/* Either an error or finished playing with data left */
music_playing->playing = SDL_FALSE;
done = SDL_TRUE;
}
if (left > 0) {
stream += (len - left);
len = left;
} else {
len = 0;
}
} else {
len = 0;
}
if (!music_internal_playing(music_playing)) {
music = music_playing;
music_internal_halt(music_playing);
if (music->music_finished_hook) {
music->music_finished_hook(music, music->music_finished_hook_user_data);
}
if (music_finished_hook) {
music_finished_hook();
}
}
}
if (music_playing) {
Mix_Music_DoEffects(music_playing, src_stream, src_len);
}
}
void pause_async_music(int pause_on)
{
if (!music_active || !music_playing || !music_playing->interface) {
return;
}
if (pause_on) {
if (music_playing->interface->Pause) {
music_playing->interface->Pause(music_playing->context);
}
} else {
if (music_playing->interface->Resume) {
music_playing->interface->Resume(music_playing->context);
}
}
}
/* Load the music interface libraries for a given music type */
SDL_bool load_music_type(Mix_MusicType type)
{
int i;
int loaded = 0;
for (i = 0; i < get_num_music_interfaces(); ++i) {
Mix_MusicInterface *interface = s_music_interfaces[i];
if (interface->type != type) {
continue;
}
if (!interface->loaded) {
char hint[64];
SDL_snprintf(hint, sizeof(hint), "SDL_MIXER_DISABLE_%s", interface->tag);
if (SDL_GetHintBoolean(hint, SDL_FALSE)) {
continue;
}
if (interface->Load && interface->Load() < 0) {
if (SDL_GetHintBoolean(SDL_MIXER_HINT_DEBUG_MUSIC_INTERFACES, SDL_FALSE)) {
SDL_Log("Couldn't load %s: %s\n", interface->tag, Mix_GetError());
}
continue;
}
interface->loaded = SDL_TRUE;
}
++loaded;