music.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 17 Oct 2017 02:33:47 -0700
changeset 777 92882ef2ab81
parent 770 74a0ac03a448
child 779 a2b494c054d5
permissions -rw-r--r--
Rewrote music.c to support any number of decode libraries using a compiled-in plugin interface
Mix_LoadWAV_RW() can now load sound formats that were previously available only as music.

This is still work in progress. Testing and project updates need to happen on other platforms.
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@42
    46
int volatile music_active = 1;
slouken@42
    47
static Mix_Music * volatile music_playing = NULL;
slouken@777
    48
int music_loops = 0;
slouken@777
    49
int music_volume = MIX_MAX_VOLUME;
slouken@777
    50
SDL_AudioSpec music_spec;
megastep@4
    51
slouken@0
    52
struct _Mix_Music {
slouken@777
    53
    Mix_MusicInterface *interface;
slouken@777
    54
    void *context;
slouken@777
    55
slouken@777
    56
    SDL_bool playing;
slouken@616
    57
    Mix_Fading fading;
slouken@616
    58
    int fade_step;
slouken@616
    59
    int fade_steps;
slouken@616
    60
    int error;
slouken@0
    61
};
slouken@0
    62
slouken@17
    63
/* Used to calculate fading steps */
slouken@17
    64
static int ms_per_step;
slouken@17
    65
icculus@390
    66
/* rcg06042009 report available decoders at runtime. */
icculus@390
    67
static const char **music_decoders = NULL;
icculus@390
    68
static int num_decoders = 0;
icculus@390
    69
chewi@506
    70
/* Semicolon-separated SoundFont paths */
chewi@506
    71
char* soundfont_paths = NULL;
chewi@506
    72
slouken@777
    73
/* Interfaces for the various music interfaces, ordered by priority */
slouken@777
    74
static Mix_MusicInterface *s_music_interfaces[] =
slouken@777
    75
{
slouken@777
    76
#ifdef MUSIC_CMD
slouken@777
    77
    &Mix_MusicInterface_CMD,
slouken@777
    78
#endif
slouken@777
    79
#ifdef MUSIC_WAV
slouken@777
    80
    &Mix_MusicInterface_WAV,
slouken@777
    81
#endif
slouken@777
    82
#ifdef MUSIC_MOD_MODPLUG
slouken@777
    83
    &Mix_MusicInterface_MODPLUG,
slouken@777
    84
#endif
slouken@777
    85
#ifdef MUSIC_MOD_MIKMOD
slouken@777
    86
    &Mix_MusicInterface_MIKMOD,
slouken@777
    87
#endif
slouken@777
    88
#ifdef MUSIC_MID_FLUIDSYNTH
slouken@777
    89
    &Mix_MusicInterface_FLUIDSYNTH,
slouken@777
    90
#endif
slouken@777
    91
#ifdef MUSIC_MID_TIMIDITY
slouken@777
    92
    &Mix_MusicInterface_TIMIDITY,
slouken@777
    93
#endif
slouken@777
    94
#ifdef MUSIC_MID_NATIVE
slouken@777
    95
    &Mix_MusicInterface_NATIVEMIDI,
slouken@777
    96
#endif
slouken@777
    97
#ifdef MUSIC_OGG
slouken@777
    98
    &Mix_MusicInterface_OGG,
slouken@777
    99
#endif
slouken@777
   100
#ifdef MUSIC_MP3_MPG123
slouken@777
   101
    &Mix_MusicInterface_MPG123,
slouken@777
   102
#endif
slouken@777
   103
#ifdef MUSIC_MP3_MAD
slouken@777
   104
    &Mix_MusicInterface_MAD,
slouken@777
   105
#endif
slouken@777
   106
#ifdef MUSIC_MP3_SMPEG
slouken@777
   107
    &Mix_MusicInterface_SMPEG,
slouken@777
   108
#endif
slouken@777
   109
#ifdef MUSIC_FLAC
slouken@777
   110
    &Mix_MusicInterface_FLAC,
slouken@777
   111
#endif
slouken@777
   112
};
slouken@777
   113
slouken@777
   114
int get_num_music_interfaces(void)
slouken@777
   115
{
slouken@777
   116
    return SDL_arraysize(s_music_interfaces);
slouken@777
   117
}
slouken@777
   118
slouken@777
   119
Mix_MusicInterface *get_music_interface(int index)
slouken@777
   120
{
slouken@777
   121
    return s_music_interfaces[index];
slouken@777
   122
}
slouken@777
   123
slouken@466
   124
int Mix_GetNumMusicDecoders(void)
icculus@390
   125
{
slouken@616
   126
    return(num_decoders);
icculus@390
   127
}
icculus@390
   128
icculus@390
   129
const char *Mix_GetMusicDecoder(int index)
icculus@390
   130
{
slouken@616
   131
    if ((index < 0) || (index >= num_decoders)) {
slouken@616
   132
        return NULL;
slouken@616
   133
    }
slouken@616
   134
    return(music_decoders[index]);
icculus@390
   135
}
icculus@390
   136
icculus@390
   137
static void add_music_decoder(const char *decoder)
icculus@390
   138
{
slouken@644
   139
    void *ptr = SDL_realloc((void *)music_decoders, (num_decoders + 1) * sizeof (const char *));
slouken@616
   140
    if (ptr == NULL) {
slouken@616
   141
        return;  /* oh well, go on without it. */
slouken@616
   142
    }
slouken@616
   143
    music_decoders = (const char **) ptr;
slouken@616
   144
    music_decoders[num_decoders++] = decoder;
icculus@390
   145
}
icculus@390
   146
megastep@10
   147
/* Local low-level functions prototypes */
icculus@237
   148
static void music_internal_initialize_volume(void);
slouken@173
   149
static void music_internal_volume(int volume);
slouken@173
   150
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   151
static int  music_internal_position(double position);
slouken@777
   152
static SDL_bool music_internal_playing();
slouken@173
   153
static void music_internal_halt(void);
megastep@7
   154
slouken@29
   155
slouken@29
   156
/* Support for hooking when the music has finished */
slouken@29
   157
static void (*music_finished_hook)(void) = NULL;
slouken@29
   158
slouken@29
   159
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   160
{
slouken@718
   161
    Mix_LockAudio();
slouken@616
   162
    music_finished_hook = music_finished;
slouken@718
   163
    Mix_UnlockAudio();
slouken@29
   164
}
slouken@29
   165
slouken@29
   166
icculus@281
   167
/* If music isn't playing, halt it if no looping is required, restart it */
slouken@777
   168
/* otherwise. NOP if the music is playing */
slouken@777
   169
static int music_halt_or_loop(void)
icculus@281
   170
{
slouken@616
   171
    /* Restart music if it has to loop */
slouken@616
   172
slouken@616
   173
    if (!music_internal_playing())
slouken@616
   174
    {
slouken@616
   175
        /* Native MIDI handles looping internally */
slouken@777
   176
        if (music_playing->interface->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   177
            music_loops = 0;
slouken@616
   178
        }
slouken@533
   179
slouken@616
   180
        /* Restart music if it has to loop at a high level */
slouken@616
   181
        if (music_loops)
slouken@616
   182
        {
slouken@616
   183
            Mix_Fading current_fade;
slouken@648
   184
            if (music_loops > 0) {
slouken@648
   185
                --music_loops;
slouken@648
   186
            }
slouken@616
   187
            current_fade = music_playing->fading;
slouken@616
   188
            music_internal_play(music_playing, 0.0);
slouken@616
   189
            music_playing->fading = current_fade;
slouken@616
   190
        }
slouken@616
   191
        else
slouken@616
   192
        {
slouken@616
   193
            music_internal_halt();
slouken@616
   194
            if (music_finished_hook)
slouken@616
   195
                music_finished_hook();
slouken@616
   196
slouken@616
   197
            return 0;
slouken@616
   198
        }
slouken@616
   199
    }
slouken@616
   200
slouken@616
   201
    return 1;
icculus@281
   202
}
icculus@281
   203
icculus@281
   204
slouken@0
   205
/* Mixing function */
slouken@0
   206
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   207
{
slouken@777
   208
    if (music_playing && music_active) {
slouken@616
   209
        /* Handle fading */
slouken@777
   210
        if (music_playing->fading != MIX_NO_FADING) {
slouken@777
   211
            if (music_playing->fade_step++ < music_playing->fade_steps) {
slouken@616
   212
                int volume;
slouken@616
   213
                int fade_step = music_playing->fade_step;
slouken@616
   214
                int fade_steps = music_playing->fade_steps;
slouken@17
   215
slouken@777
   216
                if (music_playing->fading == MIX_FADING_OUT) {
slouken@616
   217
                    volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@616
   218
                } else { /* Fading in */
slouken@616
   219
                    volume = (music_volume * fade_step) / fade_steps;
slouken@616
   220
                }
slouken@616
   221
                music_internal_volume(volume);
slouken@616
   222
            } else {
slouken@777
   223
                if (music_playing->fading == MIX_FADING_OUT) {
slouken@616
   224
                    music_internal_halt();
slouken@777
   225
                    if (music_finished_hook) {
slouken@616
   226
                        music_finished_hook();
slouken@616
   227
                    }
slouken@616
   228
                    return;
slouken@616
   229
                }
slouken@616
   230
                music_playing->fading = MIX_NO_FADING;
slouken@616
   231
            }
slouken@616
   232
        }
slouken@528
   233
slouken@616
   234
        music_halt_or_loop();
slouken@777
   235
        if (!music_internal_playing()) {
slouken@616
   236
            return;
slouken@777
   237
        }
slouken@616
   238
slouken@777
   239
        if (music_playing->interface->GetAudio) {
slouken@777
   240
            int left = music_playing->interface->GetAudio(music_playing->context, stream, len);
slouken@777
   241
            if (left > 0) {
slouken@777
   242
                music_playing->playing = SDL_FALSE;
slouken@777
   243
            }
slouken@777
   244
slouken@777
   245
            /* Handle seamless music looping */
slouken@777
   246
            if (left > 0 && left < len) {
slouken@777
   247
                music_halt_or_loop();
slouken@777
   248
                if (music_internal_playing()) {
slouken@777
   249
                    music_mixer(udata, stream+(len-left), left);
slouken@616
   250
                }
slouken@777
   251
            }
slouken@777
   252
        }
slouken@777
   253
    }
slouken@777
   254
}
slouken@616
   255
slouken@777
   256
/* Load the music interface libraries */
slouken@777
   257
int load_music(void)
slouken@777
   258
{
slouken@777
   259
    char hint[128];
slouken@777
   260
slouken@777
   261
    int i;
slouken@777
   262
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   263
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   264
        if (interface->loaded) {
slouken@777
   265
            continue;
slouken@777
   266
        }
slouken@777
   267
slouken@777
   268
        SDL_snprintf(hint, sizeof(hint), "SDL_MIXER_DISABLE_%s", interface->tag);
slouken@777
   269
        if (SDL_GetHintBoolean(hint, SDL_FALSE)) {
slouken@777
   270
            continue;
slouken@777
   271
        }
slouken@777
   272
slouken@777
   273
        if (!interface->Load || interface->Load() == 0) {
slouken@777
   274
            interface->loaded = SDL_TRUE;
slouken@777
   275
        }
slouken@777
   276
    }
slouken@777
   277
    return 0;
slouken@777
   278
}
slouken@777
   279
slouken@777
   280
/* Return SDL_TRUE if the music type is available */
slouken@777
   281
SDL_bool has_music(Mix_MusicType type)
slouken@777
   282
{
slouken@777
   283
    int i;
slouken@777
   284
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   285
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   286
        if (interface->type != type) {
slouken@777
   287
            continue;
slouken@777
   288
        }
slouken@777
   289
        if (interface->opened) {
slouken@777
   290
            return SDL_TRUE;
slouken@777
   291
        }
slouken@777
   292
    }
slouken@777
   293
    return SDL_FALSE;
slouken@777
   294
}
slouken@777
   295
slouken@777
   296
/* Initialize the music interfaces with a certain desired audio format */
slouken@777
   297
int open_music(const SDL_AudioSpec *spec)
slouken@777
   298
{
slouken@777
   299
    int i;
slouken@777
   300
    SDL_bool use_native_midi = SDL_FALSE;
slouken@777
   301
slouken@777
   302
    if (SDL_GetHintBoolean("SDL_NATIVE_MUSIC", SDL_FALSE) && native_midi_detect()) {
slouken@777
   303
        use_native_midi = SDL_TRUE;
slouken@777
   304
    }
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@777
   373
    char 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
        if (interface->api == MIX_MUSIC_SMPEG) {
slouken@777
   525
            /* Uh oh, if SMPEG couldn't create a context, it freed the src */
slouken@777
   526
            freesrc = SDL_FALSE;
slouken@616
   527
            break;
slouken@616
   528
        }
slouken@542
   529
slouken@777
   530
        /* Reset the stream for the next decoder */
slouken@777
   531
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   532
    }
chewi@506
   533
slouken@777
   534
    if (!*Mix_GetError()) {
slouken@777
   535
        Mix_SetError("Unrecognized audio format");
slouken@616
   536
    }
slouken@777
   537
    if (freesrc) {
slouken@777
   538
        SDL_RWclose(src);
slouken@777
   539
    } else {
slouken@777
   540
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   541
    }
slouken@777
   542
    return NULL;
slouken@0
   543
}
slouken@0
   544
slouken@0
   545
/* Free a music chunk previously loaded */
slouken@0
   546
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   547
{
slouken@777
   548
    if (music) {
slouken@616
   549
        /* Stop the music if it's currently playing */
slouken@718
   550
        Mix_LockAudio();
slouken@777
   551
        if (music == music_playing) {
slouken@616
   552
            /* Wait for any fade out to finish */
slouken@777
   553
            while (music->fading == MIX_FADING_OUT) {
slouken@718
   554
                Mix_UnlockAudio();
slouken@616
   555
                SDL_Delay(100);
slouken@718
   556
                Mix_LockAudio();
slouken@616
   557
            }
slouken@777
   558
            if (music == music_playing) {
slouken@616
   559
                music_internal_halt();
slouken@616
   560
            }
slouken@616
   561
        }
slouken@718
   562
        Mix_UnlockAudio();
chewi@506
   563
slouken@777
   564
        music->interface->Delete(music->context);
slouken@616
   565
        SDL_free(music);
slouken@616
   566
    }
slouken@0
   567
}
slouken@0
   568
slouken@177
   569
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   570
   music, if 'music' is NULL.
slouken@177
   571
*/
slouken@177
   572
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   573
{
slouken@616
   574
    Mix_MusicType type = MUS_NONE;
slouken@177
   575
slouken@777
   576
    if (music) {
slouken@777
   577
        type = music->interface->type;
slouken@616
   578
    } else {
slouken@718
   579
        Mix_LockAudio();
slouken@777
   580
        if (music_playing) {
slouken@777
   581
            type = music_playing->interface->type;
slouken@616
   582
        }
slouken@718
   583
        Mix_UnlockAudio();
slouken@616
   584
    }
slouken@616
   585
    return(type);
slouken@177
   586
}
slouken@177
   587
slouken@173
   588
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   589
 */
slouken@173
   590
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   591
{
slouken@616
   592
    int retval = 0;
megastep@10
   593
slouken@777
   594
#if defined(__MACOSX__) && defined(MID_MUSIC_NATIVE)
slouken@616
   595
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   596
       can't really stop and restart MIDI from the audio callback.
slouken@616
   597
    */
slouken@777
   598
    if (music == music_playing && music->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   599
        /* Just a seek suffices to restart playing */
slouken@616
   600
        music_internal_position(position);
slouken@616
   601
        return 0;
slouken@616
   602
    }
slouken@531
   603
#endif
slouken@529
   604
slouken@616
   605
    /* Note the music we're playing */
slouken@777
   606
    if (music_playing) {
slouken@616
   607
        music_internal_halt();
slouken@616
   608
    }
slouken@616
   609
    music_playing = music;
slouken@777
   610
    music_playing->playing = SDL_TRUE;
slouken@173
   611
slouken@616
   612
    /* Set the initial volume */
slouken@777
   613
    music_internal_initialize_volume();
slouken@173
   614
slouken@616
   615
    /* Set up for playback */
slouken@777
   616
    retval = music->interface->Play(music->context);
slouken@173
   617
slouken@616
   618
    /* Set the playback position, note any errors if an offset is used */
slouken@777
   619
    if (retval == 0) {
slouken@777
   620
        if (position > 0.0) {
slouken@777
   621
            if (music_internal_position(position) < 0) {
slouken@616
   622
                Mix_SetError("Position not implemented for music type");
slouken@616
   623
                retval = -1;
slouken@616
   624
            }
slouken@616
   625
        } else {
slouken@616
   626
            music_internal_position(0.0);
slouken@616
   627
        }
slouken@616
   628
    }
slouken@173
   629
slouken@616
   630
    /* If the setup failed, we're not playing any music anymore */
slouken@777
   631
    if (retval < 0) {
slouken@616
   632
        music_playing = NULL;
slouken@616
   633
    }
slouken@616
   634
    return(retval);
slouken@173
   635
}
slouken@777
   636
slouken@173
   637
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   638
{
slouken@616
   639
    int retval;
slouken@173
   640
slouken@777
   641
    if (ms_per_step == 0) {
slouken@616
   642
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   643
        return(-1);
slouken@616
   644
    }
slouken@499
   645
slouken@616
   646
    /* Don't play null pointers :-) */
slouken@777
   647
    if (music == NULL) {
slouken@616
   648
        Mix_SetError("music parameter was NULL");
slouken@616
   649
        return(-1);
slouken@616
   650
    }
slouken@173
   651
slouken@616
   652
    /* Setup the data */
slouken@777
   653
    if (ms) {
slouken@616
   654
        music->fading = MIX_FADING_IN;
slouken@616
   655
    } else {
slouken@616
   656
        music->fading = MIX_NO_FADING;
slouken@616
   657
    }
slouken@616
   658
    music->fade_step = 0;
slouken@616
   659
    music->fade_steps = ms/ms_per_step;
slouken@173
   660
slouken@616
   661
    /* Play the puppy */
slouken@718
   662
    Mix_LockAudio();
slouken@616
   663
    /* If the current music is fading out, wait for the fade to complete */
slouken@777
   664
    while (music_playing && (music_playing->fading == MIX_FADING_OUT)) {
slouken@718
   665
        Mix_UnlockAudio();
slouken@616
   666
        SDL_Delay(100);
slouken@718
   667
        Mix_LockAudio();
slouken@616
   668
    }
slouken@616
   669
    music_active = 1;
slouken@616
   670
    if (loops == 1) {
slouken@616
   671
        /* Loop is the number of times to play the audio */
slouken@616
   672
        loops = 0;
slouken@616
   673
    }
slouken@616
   674
    music_loops = loops;
slouken@616
   675
    retval = music_internal_play(music, position);
slouken@718
   676
    Mix_UnlockAudio();
slouken@173
   677
slouken@616
   678
    return(retval);
slouken@173
   679
}
slouken@173
   680
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   681
{
slouken@616
   682
    return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   683
}
slouken@173
   684
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   685
{
slouken@616
   686
    return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   687
}
megastep@10
   688
slouken@173
   689
/* Set the playing music position */
slouken@173
   690
int music_internal_position(double position)
megastep@10
   691
{
slouken@777
   692
    if (music_playing->interface->Seek) {
slouken@777
   693
        return music_playing->interface->Seek(music_playing->context, position);
slouken@616
   694
    }
slouken@777
   695
    return -1;
megastep@4
   696
}
slouken@155
   697
int Mix_SetMusicPosition(double position)
slouken@154
   698
{
slouken@616
   699
    int retval;
slouken@173
   700
slouken@718
   701
    Mix_LockAudio();
slouken@777
   702
    if (music_playing) {
slouken@616
   703
        retval = music_internal_position(position);
slouken@777
   704
        if (retval < 0) {
slouken@616
   705
            Mix_SetError("Position not implemented for music type");
slouken@616
   706
        }
slouken@616
   707
    } else {
slouken@616
   708
        Mix_SetError("Music isn't playing");
slouken@616
   709
        retval = -1;
slouken@616
   710
    }
slouken@718
   711
    Mix_UnlockAudio();
slouken@154
   712
slouken@616
   713
    return(retval);
slouken@154
   714
}
slouken@154
   715
icculus@237
   716
/* Set the music's initial volume */
icculus@237
   717
static void music_internal_initialize_volume(void)
icculus@237
   718
{
slouken@777
   719
    if (music_playing->fading == MIX_FADING_IN) {
slouken@616
   720
        music_internal_volume(0);
slouken@616
   721
    } else {
slouken@616
   722
        music_internal_volume(music_volume);
slouken@616
   723
    }
icculus@237
   724
}
icculus@237
   725
slouken@0
   726
/* Set the music volume */
slouken@173
   727
static void music_internal_volume(int volume)
slouken@173
   728
{
slouken@777
   729
    if (music_playing->interface->SetVolume) {
slouken@777
   730
        music_playing->interface->SetVolume(music_playing->context, volume);
slouken@616
   731
    }
slouken@173
   732
}
slouken@0
   733
int Mix_VolumeMusic(int volume)
slouken@0
   734
{
slouken@616
   735
    int prev_volume;
slouken@0
   736
slouken@616
   737
    prev_volume = music_volume;
slouken@777
   738
    if (volume < 0) {
slouken@616
   739
        return prev_volume;
slouken@616
   740
    }
slouken@777
   741
    if (volume > SDL_MIX_MAXVOLUME) {
slouken@616
   742
        volume = SDL_MIX_MAXVOLUME;
slouken@616
   743
    }
slouken@616
   744
    music_volume = volume;
slouken@718
   745
    Mix_LockAudio();
slouken@777
   746
    if (music_playing) {
slouken@616
   747
        music_internal_volume(music_volume);
slouken@616
   748
    }
slouken@718
   749
    Mix_UnlockAudio();
slouken@616
   750
    return(prev_volume);
slouken@0
   751
}
slouken@0
   752
slouken@173
   753
/* Halt playing of music */
slouken@173
   754
static void music_internal_halt(void)
megastep@7
   755
{
slouken@777
   756
    if (music_playing->interface->Stop) {
slouken@777
   757
        music_playing->interface->Stop(music_playing->context);
slouken@616
   758
    }
chewi@506
   759
slouken@777
   760
    music_playing->playing = SDL_FALSE;
slouken@616
   761
    music_playing->fading = MIX_NO_FADING;
slouken@616
   762
    music_playing = NULL;
megastep@7
   763
}
slouken@0
   764
int Mix_HaltMusic(void)
slouken@0
   765
{
slouken@718
   766
    Mix_LockAudio();
slouken@777
   767
    if (music_playing) {
slouken@616
   768
        music_internal_halt();
slouken@777
   769
        if (music_finished_hook) {
slouken@616
   770
            music_finished_hook();
slouken@616
   771
        }
slouken@616
   772
    }
slouken@718
   773
    Mix_UnlockAudio();
slouken@173
   774
slouken@616
   775
    return(0);
slouken@0
   776
}
slouken@0
   777
megastep@4
   778
/* Progressively stop the music */
megastep@4
   779
int Mix_FadeOutMusic(int ms)
megastep@4
   780
{
slouken@616
   781
    int retval = 0;
slouken@173
   782
slouken@777
   783
    if (ms_per_step == 0) {
slouken@616
   784
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   785
        return 0;
slouken@616
   786
    }
slouken@499
   787
slouken@616
   788
    if (ms <= 0) {  /* just halt immediately. */
slouken@616
   789
        Mix_HaltMusic();
slouken@616
   790
        return 1;
slouken@616
   791
    }
icculus@280
   792
slouken@718
   793
    Mix_LockAudio();
slouken@777
   794
    if (music_playing) {
slouken@777
   795
        int fade_steps = (ms + ms_per_step - 1) / ms_per_step;
slouken@777
   796
        if (music_playing->fading == MIX_NO_FADING) {
slouken@777
   797
            music_playing->fade_step = 0;
slouken@777
   798
        } else {
slouken@777
   799
            int step;
slouken@777
   800
            int old_fade_steps = music_playing->fade_steps;
slouken@777
   801
            if (music_playing->fading == MIX_FADING_OUT) {
slouken@777
   802
                step = music_playing->fade_step;
slouken@777
   803
            } else {
slouken@777
   804
                step = old_fade_steps - music_playing->fade_step + 1;
slouken@777
   805
            }
slouken@777
   806
            music_playing->fade_step = (step * fade_steps) / old_fade_steps;
slouken@777
   807
        }
slouken@616
   808
        music_playing->fading = MIX_FADING_OUT;
slouken@616
   809
        music_playing->fade_steps = fade_steps;
slouken@616
   810
        retval = 1;
slouken@616
   811
    }
slouken@718
   812
    Mix_UnlockAudio();
slouken@173
   813
slouken@616
   814
    return(retval);
megastep@4
   815
}
megastep@4
   816
megastep@4
   817
Mix_Fading Mix_FadingMusic(void)
megastep@4
   818
{
slouken@616
   819
    Mix_Fading fading = MIX_NO_FADING;
slouken@173
   820
slouken@718
   821
    Mix_LockAudio();
slouken@777
   822
    if (music_playing) {
slouken@616
   823
        fading = music_playing->fading;
slouken@616
   824
    }
slouken@718
   825
    Mix_UnlockAudio();
slouken@173
   826
slouken@616
   827
    return(fading);
megastep@4
   828
}
megastep@4
   829
slouken@0
   830
/* Pause/Resume the music stream */
slouken@0
   831
void Mix_PauseMusic(void)
slouken@0
   832
{
slouken@777
   833
    Mix_LockAudio();
slouken@777
   834
    if (music_playing && music_playing->interface->Pause) {
slouken@777
   835
        music_playing->interface->Pause(music_playing->context);
slouken@777
   836
    }
slouken@616
   837
    music_active = 0;
slouken@777
   838
    Mix_UnlockAudio();
slouken@0
   839
}
megastep@4
   840
slouken@0
   841
void Mix_ResumeMusic(void)
slouken@0
   842
{
slouken@777
   843
    Mix_LockAudio();
slouken@616
   844
    music_active = 1;
slouken@777
   845
    if (music_playing && music_playing->interface->Resume) {
slouken@777
   846
        music_playing->interface->Resume(music_playing->context);
slouken@777
   847
    }
slouken@777
   848
    Mix_UnlockAudio();
slouken@0
   849
}
slouken@0
   850
slouken@0
   851
void Mix_RewindMusic(void)
slouken@0
   852
{
slouken@616
   853
    Mix_SetMusicPosition(0.0);
slouken@0
   854
}
slouken@0
   855
megastep@13
   856
int Mix_PausedMusic(void)
megastep@13
   857
{
slouken@616
   858
    return (music_active == 0);
megastep@13
   859
}
megastep@13
   860
slouken@0
   861
/* Check the status of the music */
slouken@777
   862
static SDL_bool music_internal_playing()
slouken@173
   863
{
slouken@616
   864
    if (music_playing == NULL) {
slouken@777
   865
        return SDL_FALSE;
slouken@616
   866
    }
icculus@510
   867
slouken@777
   868
    if (music_playing->interface->IsPlaying) {
slouken@777
   869
        return music_playing->interface->IsPlaying(music_playing->context);
slouken@616
   870
    }
slouken@777
   871
    return music_playing->playing;
slouken@173
   872
}
megastep@13
   873
int Mix_PlayingMusic(void)
slouken@0
   874
{
slouken@616
   875
    int playing = 0;
slouken@173
   876
slouken@718
   877
    Mix_LockAudio();
slouken@777
   878
    if (music_playing) {
slouken@616
   879
        playing = music_loops || music_internal_playing();
slouken@616
   880
    }
slouken@718
   881
    Mix_UnlockAudio();
slouken@173
   882
slouken@616
   883
    return(playing);
slouken@0
   884
}
slouken@0
   885
slouken@0
   886
/* Set the external music playback command */
slouken@0
   887
int Mix_SetMusicCMD(const char *command)
slouken@0
   888
{
slouken@616
   889
    Mix_HaltMusic();
slouken@777
   890
    if (music_cmd) {
slouken@616
   891
        SDL_free(music_cmd);
slouken@616
   892
        music_cmd = NULL;
slouken@616
   893
    }
slouken@777
   894
    if (command) {
slouken@777
   895
        size_t length = SDL_strlen(command) + 1;
slouken@777
   896
        music_cmd = (char *)SDL_malloc(length);
slouken@777
   897
        if (music_cmd == NULL) {
slouken@777
   898
            return SDL_OutOfMemory();
slouken@616
   899
        }
slouken@777
   900
        SDL_memcpy(music_cmd, command, length);
slouken@616
   901
    }
slouken@777
   902
    return 0;
slouken@0
   903
}
slouken@0
   904
slouken@154
   905
int Mix_SetSynchroValue(int i)
slouken@154
   906
{
slouken@616
   907
    /* Not supported by any players at this time */
slouken@616
   908
    return(-1);
slouken@154
   909
}
slouken@154
   910
slouken@154
   911
int Mix_GetSynchroValue(void)
slouken@154
   912
{
slouken@616
   913
    /* Not supported by any players at this time */
slouken@616
   914
    return(-1);
slouken@154
   915
}
slouken@154
   916
slouken@154
   917
slouken@777
   918
/* Uninitialize the music interfaces */
slouken@0
   919
void close_music(void)
slouken@0
   920
{
slouken@777
   921
    int i;
slouken@777
   922
slouken@616
   923
    Mix_HaltMusic();
slouken@777
   924
slouken@777
   925
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   926
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   927
        if (!interface || !interface->opened) {
slouken@777
   928
            continue;
slouken@777
   929
        }
slouken@777
   930
slouken@777
   931
        if (interface->Close) {
slouken@777
   932
            interface->Close();
slouken@777
   933
        }
slouken@777
   934
        interface->opened = SDL_FALSE;
slouken@777
   935
    }
icculus@390
   936
slouken@616
   937
    /* rcg06042009 report available decoders at runtime. */
slouken@621
   938
    SDL_free((void *)music_decoders);
slouken@616
   939
    music_decoders = NULL;
slouken@616
   940
    num_decoders = 0;
slouken@499
   941
slouken@616
   942
    ms_per_step = 0;
slouken@0
   943
}
slouken@0
   944
slouken@777
   945
/* Unload the music interface libraries */
slouken@777
   946
void unload_music(void)
slouken@777
   947
{
slouken@777
   948
    int i;
slouken@777
   949
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   950
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   951
        if (!interface || !interface->loaded) {
slouken@777
   952
            continue;
slouken@777
   953
        }
slouken@777
   954
slouken@777
   955
        if (interface->Unload) {
slouken@777
   956
            interface->Unload();
slouken@777
   957
        }
slouken@777
   958
        interface->loaded = SDL_FALSE;
slouken@777
   959
    }
slouken@777
   960
}
slouken@777
   961
chewi@506
   962
int Mix_SetSoundFonts(const char *paths)
chewi@506
   963
{
slouken@616
   964
    if (soundfont_paths) {
slouken@616
   965
        SDL_free(soundfont_paths);
slouken@616
   966
        soundfont_paths = NULL;
slouken@616
   967
    }
chewi@506
   968
slouken@616
   969
    if (paths) {
slouken@616
   970
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
   971
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
   972
            return 0;
slouken@616
   973
        }
slouken@616
   974
    }
slouken@616
   975
    return 1;
chewi@506
   976
}
chewi@506
   977
dimitris@508
   978
const char* Mix_GetSoundFonts(void)
chewi@506
   979
{
slouken@777
   980
    if (!soundfont_paths || SDL_GetHintBoolean("SDL_FORCE_SOUNDFONTS", SDL_FALSE)) {
slouken@777
   981
        return SDL_getenv("SDL_SOUNDFONTS");
slouken@616
   982
    } else {
slouken@616
   983
        return soundfont_paths;
slouken@616
   984
    }
chewi@506
   985
}
chewi@506
   986
chewi@506
   987
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
   988
{
slouken@616
   989
    char *context, *path, *paths;
slouken@616
   990
    const char* cpaths = Mix_GetSoundFonts();
slouken@762
   991
    int soundfonts_found = 0;
chewi@506
   992
slouken@616
   993
    if (!cpaths) {
slouken@616
   994
        Mix_SetError("No SoundFonts have been requested");
slouken@616
   995
        return 0;
slouken@616
   996
    }
chewi@506
   997
slouken@616
   998
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
   999
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
  1000
        return 0;
slouken@616
  1001
    }
chewi@506
  1002
slouken@770
  1003
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
slouken@616
  1004
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1005
#elif defined(_WIN32)
slouken@616
  1006
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1007
#else
slouken@616
  1008
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1009
#endif
slouken@616
  1010
        if (!function(path, data)) {
slouken@762
  1011
            continue;
slouken@762
  1012
        } else {
slouken@762
  1013
            soundfonts_found++;
slouken@616
  1014
        }
slouken@616
  1015
    }
chewi@506
  1016
slouken@616
  1017
    SDL_free(paths);
slouken@762
  1018
    if (soundfonts_found > 0)
slouken@762
  1019
        return 1;
slouken@762
  1020
    else
slouken@762
  1021
        return 0;
chewi@506
  1022
}
slouken@777
  1023
slouken@777
  1024
/* vi: set ts=4 sw=4 expandtab: */