music.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 20 Oct 2017 23:39:04 -0700
changeset 797 b4b6adff699a
parent 780 74df2aa47195
child 801 46acd70caa6a
permissions -rw-r--r--
Switched to using SDL_AudioStream in SDL 2.0.7 for better streaming resampling support
Also made the following changes:
* Moved looping support into the music interfaces for better seamless looping when resampling
* Music interfaces always start play from the beginning of the song
* Implemented 24-bit and surround sound support for FLAC audio files
* Implemented sample dithering in MAD music interface using GPL code
* Improved error checking in music interfaces
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@725
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@777
    21
#include <string.h> /* for strtok() and strtok_s() */
slouken@0
    22
slouken@777
    23
#include "SDL_hints.h"
slouken@29
    24
#include "SDL_timer.h"
slouken@0
    25
slouken@34
    26
#include "SDL_mixer.h"
slouken@718
    27
#include "mixer.h"
slouken@777
    28
#include "music.h"
slouken@0
    29
slouken@0
    30
#include "music_cmd.h"
slouken@777
    31
#include "music_wav.h"
slouken@777
    32
#include "music_mikmod.h"
slouken@481
    33
#include "music_modplug.h"
slouken@777
    34
#include "music_nativemidi.h"
slouken@777
    35
#include "music_fluidsynth.h"
slouken@777
    36
#include "music_timidity.h"
slouken@63
    37
#include "music_ogg.h"
slouken@777
    38
#include "music_mpg123.h"
slouken@357
    39
#include "music_mad.h"
slouken@777
    40
#include "music_smpeg.h"
slouken@382
    41
#include "music_flac.h"
slouken@777
    42
#include "native_midi/native_midi.h"
slouken@0
    43
slouken@0
    44
slouken@777
    45
char *music_cmd = NULL;
slouken@797
    46
static SDL_bool music_active = SDL_TRUE;
slouken@797
    47
static int music_volume = MIX_MAX_VOLUME;
slouken@42
    48
static Mix_Music * volatile music_playing = NULL;
slouken@777
    49
SDL_AudioSpec music_spec;
megastep@4
    50
slouken@0
    51
struct _Mix_Music {
slouken@777
    52
    Mix_MusicInterface *interface;
slouken@777
    53
    void *context;
slouken@777
    54
slouken@777
    55
    SDL_bool playing;
slouken@616
    56
    Mix_Fading fading;
slouken@616
    57
    int fade_step;
slouken@616
    58
    int fade_steps;
slouken@0
    59
};
slouken@0
    60
slouken@17
    61
/* Used to calculate fading steps */
slouken@17
    62
static int ms_per_step;
slouken@17
    63
icculus@390
    64
/* rcg06042009 report available decoders at runtime. */
icculus@390
    65
static const char **music_decoders = NULL;
icculus@390
    66
static int num_decoders = 0;
icculus@390
    67
chewi@506
    68
/* Semicolon-separated SoundFont paths */
chewi@506
    69
char* soundfont_paths = NULL;
chewi@506
    70
slouken@777
    71
/* Interfaces for the various music interfaces, ordered by priority */
slouken@777
    72
static Mix_MusicInterface *s_music_interfaces[] =
slouken@777
    73
{
slouken@777
    74
#ifdef MUSIC_CMD
slouken@777
    75
    &Mix_MusicInterface_CMD,
slouken@777
    76
#endif
slouken@777
    77
#ifdef MUSIC_WAV
slouken@777
    78
    &Mix_MusicInterface_WAV,
slouken@777
    79
#endif
slouken@777
    80
#ifdef MUSIC_MOD_MODPLUG
slouken@777
    81
    &Mix_MusicInterface_MODPLUG,
slouken@777
    82
#endif
slouken@777
    83
#ifdef MUSIC_MOD_MIKMOD
slouken@777
    84
    &Mix_MusicInterface_MIKMOD,
slouken@777
    85
#endif
slouken@777
    86
#ifdef MUSIC_MID_FLUIDSYNTH
slouken@777
    87
    &Mix_MusicInterface_FLUIDSYNTH,
slouken@777
    88
#endif
slouken@777
    89
#ifdef MUSIC_MID_TIMIDITY
slouken@777
    90
    &Mix_MusicInterface_TIMIDITY,
slouken@777
    91
#endif
slouken@777
    92
#ifdef MUSIC_MID_NATIVE
slouken@777
    93
    &Mix_MusicInterface_NATIVEMIDI,
slouken@777
    94
#endif
slouken@777
    95
#ifdef MUSIC_OGG
slouken@777
    96
    &Mix_MusicInterface_OGG,
slouken@777
    97
#endif
slouken@777
    98
#ifdef MUSIC_MP3_MPG123
slouken@777
    99
    &Mix_MusicInterface_MPG123,
slouken@777
   100
#endif
slouken@777
   101
#ifdef MUSIC_MP3_MAD
slouken@777
   102
    &Mix_MusicInterface_MAD,
slouken@777
   103
#endif
slouken@777
   104
#ifdef MUSIC_MP3_SMPEG
slouken@777
   105
    &Mix_MusicInterface_SMPEG,
slouken@777
   106
#endif
slouken@777
   107
#ifdef MUSIC_FLAC
slouken@777
   108
    &Mix_MusicInterface_FLAC,
slouken@777
   109
#endif
slouken@777
   110
};
slouken@777
   111
slouken@777
   112
int get_num_music_interfaces(void)
slouken@777
   113
{
slouken@777
   114
    return SDL_arraysize(s_music_interfaces);
slouken@777
   115
}
slouken@777
   116
slouken@777
   117
Mix_MusicInterface *get_music_interface(int index)
slouken@777
   118
{
slouken@777
   119
    return s_music_interfaces[index];
slouken@777
   120
}
slouken@777
   121
slouken@466
   122
int Mix_GetNumMusicDecoders(void)
icculus@390
   123
{
slouken@616
   124
    return(num_decoders);
icculus@390
   125
}
icculus@390
   126
icculus@390
   127
const char *Mix_GetMusicDecoder(int index)
icculus@390
   128
{
slouken@616
   129
    if ((index < 0) || (index >= num_decoders)) {
slouken@616
   130
        return NULL;
slouken@616
   131
    }
slouken@616
   132
    return(music_decoders[index]);
icculus@390
   133
}
icculus@390
   134
icculus@390
   135
static void add_music_decoder(const char *decoder)
icculus@390
   136
{
slouken@644
   137
    void *ptr = SDL_realloc((void *)music_decoders, (num_decoders + 1) * sizeof (const char *));
slouken@616
   138
    if (ptr == NULL) {
slouken@616
   139
        return;  /* oh well, go on without it. */
slouken@616
   140
    }
slouken@616
   141
    music_decoders = (const char **) ptr;
slouken@616
   142
    music_decoders[num_decoders++] = decoder;
icculus@390
   143
}
icculus@390
   144
megastep@10
   145
/* Local low-level functions prototypes */
icculus@237
   146
static void music_internal_initialize_volume(void);
slouken@173
   147
static void music_internal_volume(int volume);
slouken@797
   148
static int  music_internal_play(Mix_Music *music, int play_count, double position);
slouken@173
   149
static int  music_internal_position(double position);
slouken@779
   150
static SDL_bool music_internal_playing(void);
slouken@173
   151
static void music_internal_halt(void);
megastep@7
   152
slouken@29
   153
slouken@29
   154
/* Support for hooking when the music has finished */
slouken@29
   155
static void (*music_finished_hook)(void) = NULL;
slouken@29
   156
slouken@29
   157
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   158
{
slouken@718
   159
    Mix_LockAudio();
slouken@616
   160
    music_finished_hook = music_finished;
slouken@718
   161
    Mix_UnlockAudio();
slouken@29
   162
}
slouken@29
   163
slouken@797
   164
/* Convenience function to fill audio and mix at the specified volume
slouken@797
   165
   This is called from many music player's GetAudio callback.
slouken@797
   166
 */
slouken@797
   167
int music_pcm_getaudio(void *context, void *data, int bytes, int volume,
slouken@797
   168
                       int (*GetSome)(void *context, void *data, int bytes, SDL_bool *done))
slouken@797
   169
{
slouken@797
   170
    Uint8 *snd = (Uint8 *)data;
slouken@797
   171
    Uint8 *dst;
slouken@797
   172
    int len = bytes;
slouken@797
   173
    SDL_bool done = SDL_FALSE;
slouken@29
   174
slouken@797
   175
    if (volume == MIX_MAX_VOLUME) {
slouken@797
   176
        dst = snd;
slouken@797
   177
    } else {
slouken@797
   178
        dst = SDL_stack_alloc(Uint8, bytes);
slouken@797
   179
    }
slouken@797
   180
    while (len > 0 && !done) {
slouken@797
   181
        int consumed = GetSome(context, dst, len, &done);
slouken@797
   182
        if (consumed < 0) {
slouken@797
   183
            break;
slouken@616
   184
        }
slouken@533
   185
slouken@797
   186
        if (volume == MIX_MAX_VOLUME) {
slouken@797
   187
            dst += consumed;
slouken@797
   188
        } else {
slouken@797
   189
            SDL_MixAudioFormat(snd, dst, music_spec.format, (Uint32)consumed, volume);
slouken@797
   190
            snd += consumed;
slouken@616
   191
        }
slouken@797
   192
        len -= consumed;
slouken@616
   193
    }
slouken@797
   194
    if (volume != MIX_MAX_VOLUME) {
slouken@797
   195
        SDL_stack_free(dst);
slouken@797
   196
    }
slouken@797
   197
    return len;
icculus@281
   198
}
icculus@281
   199
slouken@0
   200
/* Mixing function */
slouken@0
   201
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   202
{
slouken@797
   203
    while (music_playing && music_active && len > 0) {
slouken@616
   204
        /* Handle fading */
slouken@777
   205
        if (music_playing->fading != MIX_NO_FADING) {
slouken@777
   206
            if (music_playing->fade_step++ < music_playing->fade_steps) {
slouken@616
   207
                int volume;
slouken@616
   208
                int fade_step = music_playing->fade_step;
slouken@616
   209
                int fade_steps = music_playing->fade_steps;
slouken@17
   210
slouken@777
   211
                if (music_playing->fading == MIX_FADING_OUT) {
slouken@616
   212
                    volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@616
   213
                } else { /* Fading in */
slouken@616
   214
                    volume = (music_volume * fade_step) / fade_steps;
slouken@616
   215
                }
slouken@616
   216
                music_internal_volume(volume);
slouken@616
   217
            } else {
slouken@777
   218
                if (music_playing->fading == MIX_FADING_OUT) {
slouken@616
   219
                    music_internal_halt();
slouken@777
   220
                    if (music_finished_hook) {
slouken@616
   221
                        music_finished_hook();
slouken@616
   222
                    }
slouken@616
   223
                    return;
slouken@616
   224
                }
slouken@616
   225
                music_playing->fading = MIX_NO_FADING;
slouken@616
   226
            }
slouken@616
   227
        }
slouken@528
   228
slouken@797
   229
        if (music_playing->interface->GetAudio) {
slouken@797
   230
            int left = music_playing->interface->GetAudio(music_playing->context, stream, len);
slouken@797
   231
            if (left != 0) {
slouken@797
   232
                /* Either an error or finished playing with data left */
slouken@797
   233
                music_playing->playing = SDL_FALSE;
slouken@797
   234
            }
slouken@797
   235
            if (left > 0) {
slouken@797
   236
                stream += (len - left);
slouken@797
   237
                len = left;
slouken@797
   238
            } else {
slouken@797
   239
                len = 0;
slouken@797
   240
            }
slouken@797
   241
        } else {
slouken@797
   242
            len = 0;
slouken@777
   243
        }
slouken@616
   244
slouken@797
   245
        if (!music_internal_playing()) {
slouken@797
   246
            music_internal_halt();
slouken@797
   247
            if (music_finished_hook) {
slouken@797
   248
                music_finished_hook();
slouken@777
   249
            }
slouken@777
   250
        }
slouken@777
   251
    }
slouken@777
   252
}
slouken@616
   253
slouken@777
   254
/* Load the music interface libraries */
slouken@777
   255
int load_music(void)
slouken@777
   256
{
slouken@777
   257
    char hint[128];
slouken@777
   258
slouken@777
   259
    int i;
slouken@777
   260
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   261
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   262
        if (interface->loaded) {
slouken@777
   263
            continue;
slouken@777
   264
        }
slouken@777
   265
slouken@777
   266
        SDL_snprintf(hint, sizeof(hint), "SDL_MIXER_DISABLE_%s", interface->tag);
slouken@777
   267
        if (SDL_GetHintBoolean(hint, SDL_FALSE)) {
slouken@777
   268
            continue;
slouken@777
   269
        }
slouken@777
   270
slouken@777
   271
        if (!interface->Load || interface->Load() == 0) {
slouken@777
   272
            interface->loaded = SDL_TRUE;
slouken@777
   273
        }
slouken@777
   274
    }
slouken@777
   275
    return 0;
slouken@777
   276
}
slouken@777
   277
slouken@777
   278
/* Return SDL_TRUE if the music type is available */
slouken@777
   279
SDL_bool has_music(Mix_MusicType type)
slouken@777
   280
{
slouken@777
   281
    int i;
slouken@777
   282
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   283
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   284
        if (interface->type != type) {
slouken@777
   285
            continue;
slouken@777
   286
        }
slouken@777
   287
        if (interface->opened) {
slouken@777
   288
            return SDL_TRUE;
slouken@777
   289
        }
slouken@777
   290
    }
slouken@777
   291
    return SDL_FALSE;
slouken@777
   292
}
slouken@777
   293
slouken@777
   294
/* Initialize the music interfaces with a certain desired audio format */
slouken@777
   295
int open_music(const SDL_AudioSpec *spec)
slouken@777
   296
{
slouken@777
   297
    int i;
slouken@777
   298
    SDL_bool use_native_midi = SDL_FALSE;
slouken@777
   299
slouken@780
   300
#ifdef MUSIC_MID_NATIVE
slouken@777
   301
    if (SDL_GetHintBoolean("SDL_NATIVE_MUSIC", SDL_FALSE) && native_midi_detect()) {
slouken@777
   302
        use_native_midi = SDL_TRUE;
slouken@777
   303
    }
slouken@780
   304
#endif
slouken@777
   305
slouken@777
   306
    music_spec = *spec;
slouken@777
   307
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   308
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   309
        if (!interface->loaded) {
slouken@777
   310
            continue;
slouken@777
   311
        }
slouken@777
   312
slouken@777
   313
        if (interface->type == MUS_MID && use_native_midi && interface->api != MIX_MUSIC_NATIVEMIDI) {
slouken@777
   314
            continue;
slouken@777
   315
        }
slouken@777
   316
slouken@777
   317
        if (!interface->Open || interface->Open(spec) == 0) {
slouken@777
   318
            interface->opened = SDL_TRUE;
slouken@777
   319
            add_music_decoder(interface->tag);
slouken@616
   320
        }
slouken@616
   321
    }
slouken@407
   322
slouken@777
   323
    if (has_music(MUS_MOD)) {
slouken@777
   324
        add_music_decoder("MOD");
slouken@616
   325
    }
slouken@777
   326
    if (has_music(MUS_MID)) {
slouken@777
   327
        add_music_decoder("MIDI");
slouken@777
   328
    }
slouken@777
   329
    if (has_music(MUS_MP3)) {
slouken@777
   330
        add_music_decoder("MP3");
slouken@777
   331
    }
slouken@777
   332
slouken@777
   333
    Mix_VolumeMusic(MIX_MAX_VOLUME);
slouken@777
   334
slouken@777
   335
    /* Calculate the number of ms for each callback */
slouken@777
   336
    ms_per_step = (int) (((float)spec->samples * 1000.0) / spec->freq);
slouken@777
   337
slouken@777
   338
    return 0;
slouken@0
   339
}
slouken@0
   340
slouken@777
   341
Mix_MusicType detect_music_type_from_magic(const Uint8 *magic)
slouken@0
   342
{
slouken@616
   343
    /* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@777
   344
    if (SDL_memcmp(magic, "OggS", 4) == 0) {
slouken@616
   345
        return MUS_OGG;
slouken@616
   346
    }
slouken@542
   347
slouken@616
   348
    /* FLAC files have the magic four bytes "fLaC" */
slouken@777
   349
    if (SDL_memcmp(magic, "fLaC", 4) == 0) {
slouken@616
   350
        return MUS_FLAC;
slouken@616
   351
    }
slouken@542
   352
slouken@616
   353
    /* MIDI files have the magic four bytes "MThd" */
slouken@777
   354
    if (SDL_memcmp(magic, "MThd", 4) == 0) {
slouken@616
   355
        return MUS_MID;
slouken@616
   356
    }
slouken@542
   357
slouken@777
   358
    if (SDL_memcmp(magic, "ID3", 3) == 0 ||
slouken@777
   359
        (magic[0] == 0xFF && (magic[1] & 0xFE) == 0xFA)) {
slouken@616
   360
        return MUS_MP3;
slouken@616
   361
    }
slouken@542
   362
slouken@616
   363
    /* Assume MOD format.
slouken@616
   364
     *
slouken@616
   365
     * Apparently there is no way to check if the file is really a MOD,
slouken@616
   366
     * or there are too many formats supported by MikMod/ModPlug, or
slouken@616
   367
     * MikMod/ModPlug does this check by itself. */
slouken@616
   368
    return MUS_MOD;
slouken@542
   369
}
slouken@542
   370
slouken@777
   371
static Mix_MusicType detect_music_type(SDL_RWops *src)
slouken@777
   372
{
slouken@779
   373
    Uint8 magic[12];
slouken@777
   374
slouken@777
   375
    if (SDL_RWread(src, magic, 1, 12) != 12) {
slouken@777
   376
        Mix_SetError("Couldn't read first 12 bytes of audio data");
slouken@777
   377
        return MUS_NONE;
slouken@777
   378
    }
slouken@777
   379
    SDL_RWseek(src, -12, RW_SEEK_CUR);
slouken@777
   380
slouken@777
   381
    /* WAVE files have the magic four bytes "RIFF"
slouken@777
   382
       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
slouken@777
   383
    if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) ||
slouken@777
   384
        (SDL_memcmp(magic, "FORM", 4) == 0)) {
slouken@777
   385
        return MUS_WAV;
slouken@777
   386
    }
slouken@777
   387
slouken@777
   388
    return detect_music_type_from_magic(magic);
slouken@777
   389
}
slouken@777
   390
slouken@0
   391
/* Load a music file */
slouken@0
   392
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   393
{
slouken@777
   394
    int i;
slouken@777
   395
    void *context;
slouken@777
   396
    char *ext;
slouken@777
   397
    Mix_MusicType type;
slouken@625
   398
    SDL_RWops *src;
slouken@542
   399
slouken@777
   400
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   401
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   402
        if (!interface->opened || !interface->CreateFromFile) {
slouken@777
   403
            continue;
slouken@616
   404
        }
slouken@777
   405
slouken@777
   406
        context = interface->CreateFromFile(file);
slouken@777
   407
        if (context) {
slouken@777
   408
            /* Allocate memory for the music structure */
slouken@777
   409
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   410
            if (music == NULL) {
slouken@777
   411
                Mix_SetError("Out of memory");
slouken@777
   412
                return NULL;
slouken@777
   413
            }
slouken@777
   414
            music->interface = interface;
slouken@777
   415
            music->context = context;
slouken@777
   416
            return music;
slouken@616
   417
        }
slouken@616
   418
    }
slouken@542
   419
slouken@625
   420
    src = SDL_RWFromFile(file, "rb");
slouken@777
   421
    if (src == NULL) {
slouken@616
   422
        Mix_SetError("Couldn't open '%s'", file);
slouken@616
   423
        return NULL;
slouken@616
   424
    }
slouken@542
   425
slouken@616
   426
    /* Use the extension as a first guess on the file type */
slouken@616
   427
    type = MUS_NONE;
slouken@616
   428
    ext = strrchr(file, '.');
slouken@777
   429
    if (ext) {
slouken@616
   430
        ++ext; /* skip the dot in the extension */
slouken@777
   431
        if (SDL_strcasecmp(ext, "WAV") == 0) {
slouken@616
   432
            type = MUS_WAV;
slouken@777
   433
        } else if (SDL_strcasecmp(ext, "MID") == 0 ||
slouken@777
   434
                    SDL_strcasecmp(ext, "MIDI") == 0 ||
slouken@777
   435
                    SDL_strcasecmp(ext, "KAR") == 0) {
slouken@616
   436
            type = MUS_MID;
slouken@777
   437
        } else if (SDL_strcasecmp(ext, "OGG") == 0) {
slouken@616
   438
            type = MUS_OGG;
slouken@777
   439
        } else if (SDL_strcasecmp(ext, "FLAC") == 0) {
slouken@616
   440
            type = MUS_FLAC;
slouken@777
   441
        } else  if (SDL_strcasecmp(ext, "MPG") == 0 ||
slouken@777
   442
                     SDL_strcasecmp(ext, "MPEG") == 0 ||
slouken@777
   443
                     SDL_strcasecmp(ext, "MP3") == 0 ||
slouken@777
   444
                     SDL_strcasecmp(ext, "MAD") == 0) {
slouken@616
   445
            type = MUS_MP3;
slouken@777
   446
        } else if (SDL_strcasecmp(ext, "669") == 0 ||
slouken@777
   447
                    SDL_strcasecmp(ext, "AMF") == 0 ||
slouken@777
   448
                    SDL_strcasecmp(ext, "AMS") == 0 ||
slouken@777
   449
                    SDL_strcasecmp(ext, "DBM") == 0 ||
slouken@777
   450
                    SDL_strcasecmp(ext, "DSM") == 0 ||
slouken@777
   451
                    SDL_strcasecmp(ext, "FAR") == 0 ||
slouken@777
   452
                    SDL_strcasecmp(ext, "IT") == 0 ||
slouken@777
   453
                    SDL_strcasecmp(ext, "MED") == 0 ||
slouken@777
   454
                    SDL_strcasecmp(ext, "MDL") == 0 ||
slouken@777
   455
                    SDL_strcasecmp(ext, "MOD") == 0 ||
slouken@777
   456
                    SDL_strcasecmp(ext, "MOL") == 0 ||
slouken@777
   457
                    SDL_strcasecmp(ext, "MTM") == 0 ||
slouken@777
   458
                    SDL_strcasecmp(ext, "NST") == 0 ||
slouken@777
   459
                    SDL_strcasecmp(ext, "OKT") == 0 ||
slouken@777
   460
                    SDL_strcasecmp(ext, "PTM") == 0 ||
slouken@777
   461
                    SDL_strcasecmp(ext, "S3M") == 0 ||
slouken@777
   462
                    SDL_strcasecmp(ext, "STM") == 0 ||
slouken@777
   463
                    SDL_strcasecmp(ext, "ULT") == 0 ||
slouken@777
   464
                    SDL_strcasecmp(ext, "UMX") == 0 ||
slouken@777
   465
                    SDL_strcasecmp(ext, "WOW") == 0 ||
slouken@777
   466
                    SDL_strcasecmp(ext, "XM") == 0) {
slouken@777
   467
            type = MUS_MOD;
slouken@616
   468
        }
slouken@616
   469
    }
slouken@777
   470
    return Mix_LoadMUSType_RW(src, type, SDL_TRUE);
slouken@542
   471
}
slouken@542
   472
slouken@625
   473
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *src, int freesrc)
slouken@542
   474
{
slouken@625
   475
    return Mix_LoadMUSType_RW(src, MUS_NONE, freesrc);
slouken@542
   476
}
slouken@542
   477
slouken@625
   478
Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
slouken@542
   479
{
slouken@777
   480
    int i;
slouken@777
   481
    void *context;
slouken@625
   482
    Sint64 start;
slouken@0
   483
slouken@625
   484
    if (!src) {
slouken@616
   485
        Mix_SetError("RWops pointer is NULL");
slouken@616
   486
        return NULL;
slouken@616
   487
    }
slouken@625
   488
    start = SDL_RWtell(src);
slouken@542
   489
slouken@616
   490
    /* If the caller wants auto-detection, figure out what kind of file
slouken@616
   491
     * this is. */
slouken@616
   492
    if (type == MUS_NONE) {
slouken@625
   493
        if ((type = detect_music_type(src)) == MUS_NONE) {
slouken@777
   494
            /* Don't call Mix_SetError() since detect_music_type() does that. */
slouken@625
   495
            if (freesrc) {
slouken@625
   496
                SDL_RWclose(src);
slouken@625
   497
            }
slouken@616
   498
            return NULL;
slouken@616
   499
        }
slouken@616
   500
    }
slouken@125
   501
slouken@777
   502
    Mix_ClearError();
slouken@777
   503
slouken@777
   504
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   505
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   506
        if (!interface->opened || type != interface->type || !interface->CreateFromRW) {
slouken@777
   507
            continue;
slouken@625
   508
        }
slouken@0
   509
slouken@777
   510
        context = interface->CreateFromRW(src, freesrc);
slouken@777
   511
        if (context) {
slouken@777
   512
            /* Allocate memory for the music structure */
slouken@777
   513
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   514
            if (music == NULL) {
slouken@777
   515
                interface->Delete(context);
slouken@777
   516
                Mix_SetError("Out of memory");
slouken@777
   517
                return NULL;
slouken@777
   518
            }
slouken@777
   519
            music->interface = interface;
slouken@777
   520
            music->context = context;
slouken@777
   521
            return music;
slouken@616
   522
        }
slouken@777
   523
slouken@777
   524
        /* Reset the stream for the next decoder */
slouken@777
   525
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   526
    }
chewi@506
   527
slouken@777
   528
    if (!*Mix_GetError()) {
slouken@777
   529
        Mix_SetError("Unrecognized audio format");
slouken@616
   530
    }
slouken@777
   531
    if (freesrc) {
slouken@777
   532
        SDL_RWclose(src);
slouken@777
   533
    } else {
slouken@777
   534
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   535
    }
slouken@777
   536
    return NULL;
slouken@0
   537
}
slouken@0
   538
slouken@0
   539
/* Free a music chunk previously loaded */
slouken@0
   540
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   541
{
slouken@777
   542
    if (music) {
slouken@616
   543
        /* Stop the music if it's currently playing */
slouken@718
   544
        Mix_LockAudio();
slouken@777
   545
        if (music == music_playing) {
slouken@616
   546
            /* Wait for any fade out to finish */
slouken@777
   547
            while (music->fading == MIX_FADING_OUT) {
slouken@718
   548
                Mix_UnlockAudio();
slouken@616
   549
                SDL_Delay(100);
slouken@718
   550
                Mix_LockAudio();
slouken@616
   551
            }
slouken@777
   552
            if (music == music_playing) {
slouken@616
   553
                music_internal_halt();
slouken@616
   554
            }
slouken@616
   555
        }
slouken@718
   556
        Mix_UnlockAudio();
chewi@506
   557
slouken@777
   558
        music->interface->Delete(music->context);
slouken@616
   559
        SDL_free(music);
slouken@616
   560
    }
slouken@0
   561
}
slouken@0
   562
slouken@177
   563
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   564
   music, if 'music' is NULL.
slouken@177
   565
*/
slouken@177
   566
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   567
{
slouken@616
   568
    Mix_MusicType type = MUS_NONE;
slouken@177
   569
slouken@777
   570
    if (music) {
slouken@777
   571
        type = music->interface->type;
slouken@616
   572
    } else {
slouken@718
   573
        Mix_LockAudio();
slouken@777
   574
        if (music_playing) {
slouken@777
   575
            type = music_playing->interface->type;
slouken@616
   576
        }
slouken@718
   577
        Mix_UnlockAudio();
slouken@616
   578
    }
slouken@616
   579
    return(type);
slouken@177
   580
}
slouken@177
   581
slouken@173
   582
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   583
 */
slouken@797
   584
static int music_internal_play(Mix_Music *music, int play_count, double position)
slouken@0
   585
{
slouken@616
   586
    int retval = 0;
megastep@10
   587
slouken@777
   588
#if defined(__MACOSX__) && defined(MID_MUSIC_NATIVE)
slouken@616
   589
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   590
       can't really stop and restart MIDI from the audio callback.
slouken@616
   591
    */
slouken@777
   592
    if (music == music_playing && music->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   593
        /* Just a seek suffices to restart playing */
slouken@616
   594
        music_internal_position(position);
slouken@616
   595
        return 0;
slouken@616
   596
    }
slouken@531
   597
#endif
slouken@529
   598
slouken@616
   599
    /* Note the music we're playing */
slouken@777
   600
    if (music_playing) {
slouken@616
   601
        music_internal_halt();
slouken@616
   602
    }
slouken@616
   603
    music_playing = music;
slouken@777
   604
    music_playing->playing = SDL_TRUE;
slouken@173
   605
slouken@616
   606
    /* Set the initial volume */
slouken@777
   607
    music_internal_initialize_volume();
slouken@173
   608
slouken@616
   609
    /* Set up for playback */
slouken@797
   610
    retval = music->interface->Play(music->context, play_count);
slouken@173
   611
slouken@616
   612
    /* Set the playback position, note any errors if an offset is used */
slouken@777
   613
    if (retval == 0) {
slouken@777
   614
        if (position > 0.0) {
slouken@777
   615
            if (music_internal_position(position) < 0) {
slouken@616
   616
                Mix_SetError("Position not implemented for music type");
slouken@616
   617
                retval = -1;
slouken@616
   618
            }
slouken@616
   619
        } else {
slouken@616
   620
            music_internal_position(0.0);
slouken@616
   621
        }
slouken@616
   622
    }
slouken@173
   623
slouken@616
   624
    /* If the setup failed, we're not playing any music anymore */
slouken@777
   625
    if (retval < 0) {
slouken@797
   626
        music->playing = SDL_FALSE;
slouken@616
   627
        music_playing = NULL;
slouken@616
   628
    }
slouken@616
   629
    return(retval);
slouken@173
   630
}
slouken@777
   631
slouken@173
   632
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   633
{
slouken@616
   634
    int retval;
slouken@173
   635
slouken@777
   636
    if (ms_per_step == 0) {
slouken@616
   637
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   638
        return(-1);
slouken@616
   639
    }
slouken@499
   640
slouken@616
   641
    /* Don't play null pointers :-) */
slouken@777
   642
    if (music == NULL) {
slouken@616
   643
        Mix_SetError("music parameter was NULL");
slouken@616
   644
        return(-1);
slouken@616
   645
    }
slouken@173
   646
slouken@616
   647
    /* Setup the data */
slouken@777
   648
    if (ms) {
slouken@616
   649
        music->fading = MIX_FADING_IN;
slouken@616
   650
    } else {
slouken@616
   651
        music->fading = MIX_NO_FADING;
slouken@616
   652
    }
slouken@616
   653
    music->fade_step = 0;
slouken@616
   654
    music->fade_steps = ms/ms_per_step;
slouken@173
   655
slouken@616
   656
    /* Play the puppy */
slouken@718
   657
    Mix_LockAudio();
slouken@616
   658
    /* If the current music is fading out, wait for the fade to complete */
slouken@777
   659
    while (music_playing && (music_playing->fading == MIX_FADING_OUT)) {
slouken@718
   660
        Mix_UnlockAudio();
slouken@616
   661
        SDL_Delay(100);
slouken@718
   662
        Mix_LockAudio();
slouken@616
   663
    }
slouken@797
   664
    if (loops == 0) {
slouken@616
   665
        /* Loop is the number of times to play the audio */
slouken@797
   666
        loops = 1;
slouken@616
   667
    }
slouken@797
   668
    retval = music_internal_play(music, loops, position);
slouken@718
   669
    Mix_UnlockAudio();
slouken@173
   670
slouken@616
   671
    return(retval);
slouken@173
   672
}
slouken@173
   673
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   674
{
slouken@616
   675
    return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   676
}
slouken@173
   677
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   678
{
slouken@616
   679
    return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   680
}
megastep@10
   681
slouken@173
   682
/* Set the playing music position */
slouken@173
   683
int music_internal_position(double position)
megastep@10
   684
{
slouken@777
   685
    if (music_playing->interface->Seek) {
slouken@777
   686
        return music_playing->interface->Seek(music_playing->context, position);
slouken@616
   687
    }
slouken@777
   688
    return -1;
megastep@4
   689
}
slouken@155
   690
int Mix_SetMusicPosition(double position)
slouken@154
   691
{
slouken@616
   692
    int retval;
slouken@173
   693
slouken@718
   694
    Mix_LockAudio();
slouken@777
   695
    if (music_playing) {
slouken@616
   696
        retval = music_internal_position(position);
slouken@777
   697
        if (retval < 0) {
slouken@616
   698
            Mix_SetError("Position not implemented for music type");
slouken@616
   699
        }
slouken@616
   700
    } else {
slouken@616
   701
        Mix_SetError("Music isn't playing");
slouken@616
   702
        retval = -1;
slouken@616
   703
    }
slouken@718
   704
    Mix_UnlockAudio();
slouken@154
   705
slouken@616
   706
    return(retval);
slouken@154
   707
}
slouken@154
   708
icculus@237
   709
/* Set the music's initial volume */
icculus@237
   710
static void music_internal_initialize_volume(void)
icculus@237
   711
{
slouken@777
   712
    if (music_playing->fading == MIX_FADING_IN) {
slouken@616
   713
        music_internal_volume(0);
slouken@616
   714
    } else {
slouken@616
   715
        music_internal_volume(music_volume);
slouken@616
   716
    }
icculus@237
   717
}
icculus@237
   718
slouken@0
   719
/* Set the music volume */
slouken@173
   720
static void music_internal_volume(int volume)
slouken@173
   721
{
slouken@777
   722
    if (music_playing->interface->SetVolume) {
slouken@777
   723
        music_playing->interface->SetVolume(music_playing->context, volume);
slouken@616
   724
    }
slouken@173
   725
}
slouken@0
   726
int Mix_VolumeMusic(int volume)
slouken@0
   727
{
slouken@616
   728
    int prev_volume;
slouken@0
   729
slouken@616
   730
    prev_volume = music_volume;
slouken@777
   731
    if (volume < 0) {
slouken@616
   732
        return prev_volume;
slouken@616
   733
    }
slouken@777
   734
    if (volume > SDL_MIX_MAXVOLUME) {
slouken@616
   735
        volume = SDL_MIX_MAXVOLUME;
slouken@616
   736
    }
slouken@616
   737
    music_volume = volume;
slouken@718
   738
    Mix_LockAudio();
slouken@777
   739
    if (music_playing) {
slouken@616
   740
        music_internal_volume(music_volume);
slouken@616
   741
    }
slouken@718
   742
    Mix_UnlockAudio();
slouken@616
   743
    return(prev_volume);
slouken@0
   744
}
slouken@0
   745
slouken@173
   746
/* Halt playing of music */
slouken@173
   747
static void music_internal_halt(void)
megastep@7
   748
{
slouken@777
   749
    if (music_playing->interface->Stop) {
slouken@777
   750
        music_playing->interface->Stop(music_playing->context);
slouken@616
   751
    }
chewi@506
   752
slouken@777
   753
    music_playing->playing = SDL_FALSE;
slouken@616
   754
    music_playing->fading = MIX_NO_FADING;
slouken@616
   755
    music_playing = NULL;
megastep@7
   756
}
slouken@0
   757
int Mix_HaltMusic(void)
slouken@0
   758
{
slouken@718
   759
    Mix_LockAudio();
slouken@777
   760
    if (music_playing) {
slouken@616
   761
        music_internal_halt();
slouken@777
   762
        if (music_finished_hook) {
slouken@616
   763
            music_finished_hook();
slouken@616
   764
        }
slouken@616
   765
    }
slouken@718
   766
    Mix_UnlockAudio();
slouken@173
   767
slouken@616
   768
    return(0);
slouken@0
   769
}
slouken@0
   770
megastep@4
   771
/* Progressively stop the music */
megastep@4
   772
int Mix_FadeOutMusic(int ms)
megastep@4
   773
{
slouken@616
   774
    int retval = 0;
slouken@173
   775
slouken@777
   776
    if (ms_per_step == 0) {
slouken@616
   777
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   778
        return 0;
slouken@616
   779
    }
slouken@499
   780
slouken@616
   781
    if (ms <= 0) {  /* just halt immediately. */
slouken@616
   782
        Mix_HaltMusic();
slouken@616
   783
        return 1;
slouken@616
   784
    }
icculus@280
   785
slouken@718
   786
    Mix_LockAudio();
slouken@777
   787
    if (music_playing) {
slouken@777
   788
        int fade_steps = (ms + ms_per_step - 1) / ms_per_step;
slouken@777
   789
        if (music_playing->fading == MIX_NO_FADING) {
slouken@777
   790
            music_playing->fade_step = 0;
slouken@777
   791
        } else {
slouken@777
   792
            int step;
slouken@777
   793
            int old_fade_steps = music_playing->fade_steps;
slouken@777
   794
            if (music_playing->fading == MIX_FADING_OUT) {
slouken@777
   795
                step = music_playing->fade_step;
slouken@777
   796
            } else {
slouken@777
   797
                step = old_fade_steps - music_playing->fade_step + 1;
slouken@777
   798
            }
slouken@777
   799
            music_playing->fade_step = (step * fade_steps) / old_fade_steps;
slouken@777
   800
        }
slouken@616
   801
        music_playing->fading = MIX_FADING_OUT;
slouken@616
   802
        music_playing->fade_steps = fade_steps;
slouken@616
   803
        retval = 1;
slouken@616
   804
    }
slouken@718
   805
    Mix_UnlockAudio();
slouken@173
   806
slouken@616
   807
    return(retval);
megastep@4
   808
}
megastep@4
   809
megastep@4
   810
Mix_Fading Mix_FadingMusic(void)
megastep@4
   811
{
slouken@616
   812
    Mix_Fading fading = MIX_NO_FADING;
slouken@173
   813
slouken@718
   814
    Mix_LockAudio();
slouken@777
   815
    if (music_playing) {
slouken@616
   816
        fading = music_playing->fading;
slouken@616
   817
    }
slouken@718
   818
    Mix_UnlockAudio();
slouken@173
   819
slouken@616
   820
    return(fading);
megastep@4
   821
}
megastep@4
   822
slouken@0
   823
/* Pause/Resume the music stream */
slouken@0
   824
void Mix_PauseMusic(void)
slouken@0
   825
{
slouken@777
   826
    Mix_LockAudio();
slouken@797
   827
    if (music_playing) {
slouken@797
   828
        if (music_playing->interface->Pause) {
slouken@797
   829
            music_playing->interface->Pause(music_playing->context);
slouken@797
   830
        }
slouken@777
   831
    }
slouken@797
   832
    music_active = SDL_FALSE;
slouken@777
   833
    Mix_UnlockAudio();
slouken@0
   834
}
megastep@4
   835
slouken@0
   836
void Mix_ResumeMusic(void)
slouken@0
   837
{
slouken@777
   838
    Mix_LockAudio();
slouken@797
   839
    if (music_playing) {
slouken@797
   840
        if (music_playing->interface->Resume) {
slouken@797
   841
            music_playing->interface->Resume(music_playing->context);
slouken@797
   842
        }
slouken@777
   843
    }
slouken@797
   844
    music_active = SDL_TRUE;
slouken@777
   845
    Mix_UnlockAudio();
slouken@0
   846
}
slouken@0
   847
slouken@0
   848
void Mix_RewindMusic(void)
slouken@0
   849
{
slouken@616
   850
    Mix_SetMusicPosition(0.0);
slouken@0
   851
}
slouken@0
   852
megastep@13
   853
int Mix_PausedMusic(void)
megastep@13
   854
{
slouken@797
   855
    return (music_active == SDL_FALSE);
megastep@13
   856
}
megastep@13
   857
slouken@0
   858
/* Check the status of the music */
slouken@779
   859
static SDL_bool music_internal_playing(void)
slouken@173
   860
{
slouken@797
   861
    if (!music_playing) {
slouken@777
   862
        return SDL_FALSE;
slouken@616
   863
    }
icculus@510
   864
slouken@777
   865
    if (music_playing->interface->IsPlaying) {
slouken@797
   866
        music_playing->playing = music_playing->interface->IsPlaying(music_playing->context);
slouken@616
   867
    }
slouken@777
   868
    return music_playing->playing;
slouken@173
   869
}
megastep@13
   870
int Mix_PlayingMusic(void)
slouken@0
   871
{
slouken@797
   872
    SDL_bool playing;
slouken@173
   873
slouken@718
   874
    Mix_LockAudio();
slouken@797
   875
    playing = music_internal_playing();
slouken@718
   876
    Mix_UnlockAudio();
slouken@173
   877
slouken@797
   878
    return playing ? 1 : 0;
slouken@0
   879
}
slouken@0
   880
slouken@0
   881
/* Set the external music playback command */
slouken@0
   882
int Mix_SetMusicCMD(const char *command)
slouken@0
   883
{
slouken@616
   884
    Mix_HaltMusic();
slouken@777
   885
    if (music_cmd) {
slouken@616
   886
        SDL_free(music_cmd);
slouken@616
   887
        music_cmd = NULL;
slouken@616
   888
    }
slouken@777
   889
    if (command) {
slouken@777
   890
        size_t length = SDL_strlen(command) + 1;
slouken@777
   891
        music_cmd = (char *)SDL_malloc(length);
slouken@777
   892
        if (music_cmd == NULL) {
slouken@777
   893
            return SDL_OutOfMemory();
slouken@616
   894
        }
slouken@777
   895
        SDL_memcpy(music_cmd, command, length);
slouken@616
   896
    }
slouken@777
   897
    return 0;
slouken@0
   898
}
slouken@0
   899
slouken@154
   900
int Mix_SetSynchroValue(int i)
slouken@154
   901
{
slouken@616
   902
    /* Not supported by any players at this time */
slouken@616
   903
    return(-1);
slouken@154
   904
}
slouken@154
   905
slouken@154
   906
int Mix_GetSynchroValue(void)
slouken@154
   907
{
slouken@616
   908
    /* Not supported by any players at this time */
slouken@616
   909
    return(-1);
slouken@154
   910
}
slouken@154
   911
slouken@154
   912
slouken@777
   913
/* Uninitialize the music interfaces */
slouken@0
   914
void close_music(void)
slouken@0
   915
{
slouken@777
   916
    int i;
slouken@777
   917
slouken@616
   918
    Mix_HaltMusic();
slouken@777
   919
slouken@777
   920
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   921
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   922
        if (!interface || !interface->opened) {
slouken@777
   923
            continue;
slouken@777
   924
        }
slouken@777
   925
slouken@777
   926
        if (interface->Close) {
slouken@777
   927
            interface->Close();
slouken@777
   928
        }
slouken@777
   929
        interface->opened = SDL_FALSE;
slouken@777
   930
    }
icculus@390
   931
slouken@616
   932
    /* rcg06042009 report available decoders at runtime. */
slouken@621
   933
    SDL_free((void *)music_decoders);
slouken@616
   934
    music_decoders = NULL;
slouken@616
   935
    num_decoders = 0;
slouken@499
   936
slouken@616
   937
    ms_per_step = 0;
slouken@0
   938
}
slouken@0
   939
slouken@777
   940
/* Unload the music interface libraries */
slouken@777
   941
void unload_music(void)
slouken@777
   942
{
slouken@777
   943
    int i;
slouken@777
   944
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   945
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   946
        if (!interface || !interface->loaded) {
slouken@777
   947
            continue;
slouken@777
   948
        }
slouken@777
   949
slouken@777
   950
        if (interface->Unload) {
slouken@777
   951
            interface->Unload();
slouken@777
   952
        }
slouken@777
   953
        interface->loaded = SDL_FALSE;
slouken@777
   954
    }
slouken@777
   955
}
slouken@777
   956
chewi@506
   957
int Mix_SetSoundFonts(const char *paths)
chewi@506
   958
{
slouken@616
   959
    if (soundfont_paths) {
slouken@616
   960
        SDL_free(soundfont_paths);
slouken@616
   961
        soundfont_paths = NULL;
slouken@616
   962
    }
chewi@506
   963
slouken@616
   964
    if (paths) {
slouken@616
   965
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
   966
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
   967
            return 0;
slouken@616
   968
        }
slouken@616
   969
    }
slouken@616
   970
    return 1;
chewi@506
   971
}
chewi@506
   972
dimitris@508
   973
const char* Mix_GetSoundFonts(void)
chewi@506
   974
{
slouken@777
   975
    if (!soundfont_paths || SDL_GetHintBoolean("SDL_FORCE_SOUNDFONTS", SDL_FALSE)) {
slouken@777
   976
        return SDL_getenv("SDL_SOUNDFONTS");
slouken@616
   977
    } else {
slouken@616
   978
        return soundfont_paths;
slouken@616
   979
    }
chewi@506
   980
}
chewi@506
   981
chewi@506
   982
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
   983
{
slouken@616
   984
    char *context, *path, *paths;
slouken@616
   985
    const char* cpaths = Mix_GetSoundFonts();
slouken@762
   986
    int soundfonts_found = 0;
chewi@506
   987
slouken@616
   988
    if (!cpaths) {
slouken@616
   989
        Mix_SetError("No SoundFonts have been requested");
slouken@616
   990
        return 0;
slouken@616
   991
    }
chewi@506
   992
slouken@616
   993
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
   994
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
   995
        return 0;
slouken@616
   996
    }
chewi@506
   997
slouken@770
   998
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
slouken@616
   999
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1000
#elif defined(_WIN32)
slouken@616
  1001
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1002
#else
slouken@616
  1003
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1004
#endif
slouken@616
  1005
        if (!function(path, data)) {
slouken@762
  1006
            continue;
slouken@762
  1007
        } else {
slouken@762
  1008
            soundfonts_found++;
slouken@616
  1009
        }
slouken@616
  1010
    }
chewi@506
  1011
slouken@616
  1012
    SDL_free(paths);
slouken@762
  1013
    if (soundfonts_found > 0)
slouken@762
  1014
        return 1;
slouken@762
  1015
    else
slouken@762
  1016
        return 0;
chewi@506
  1017
}
slouken@777
  1018
slouken@777
  1019
/* vi: set ts=4 sw=4 expandtab: */