music.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 17 Oct 2017 16:43:23 -0700
changeset 780 74df2aa47195
parent 779 a2b494c054d5
child 797 b4b6adff699a
permissions -rw-r--r--
Fixed various issues with the new SDL_mixer code (thanks Ozkan!)
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@779
   152
static SDL_bool music_internal_playing(void);
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@780
   302
#ifdef MUSIC_MID_NATIVE
slouken@777
   303
    if (SDL_GetHintBoolean("SDL_NATIVE_MUSIC", SDL_FALSE) && native_midi_detect()) {
slouken@777
   304
        use_native_midi = SDL_TRUE;
slouken@777
   305
    }
slouken@780
   306
#endif
slouken@777
   307
slouken@777
   308
    music_spec = *spec;
slouken@777
   309
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   310
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   311
        if (!interface->loaded) {
slouken@777
   312
            continue;
slouken@777
   313
        }
slouken@777
   314
slouken@777
   315
        if (interface->type == MUS_MID && use_native_midi && interface->api != MIX_MUSIC_NATIVEMIDI) {
slouken@777
   316
            continue;
slouken@777
   317
        }
slouken@777
   318
slouken@777
   319
        if (!interface->Open || interface->Open(spec) == 0) {
slouken@777
   320
            interface->opened = SDL_TRUE;
slouken@777
   321
            add_music_decoder(interface->tag);
slouken@616
   322
        }
slouken@616
   323
    }
slouken@407
   324
slouken@777
   325
    if (has_music(MUS_MOD)) {
slouken@777
   326
        add_music_decoder("MOD");
slouken@616
   327
    }
slouken@777
   328
    if (has_music(MUS_MID)) {
slouken@777
   329
        add_music_decoder("MIDI");
slouken@777
   330
    }
slouken@777
   331
    if (has_music(MUS_MP3)) {
slouken@777
   332
        add_music_decoder("MP3");
slouken@777
   333
    }
slouken@777
   334
slouken@777
   335
    Mix_VolumeMusic(MIX_MAX_VOLUME);
slouken@777
   336
slouken@777
   337
    /* Calculate the number of ms for each callback */
slouken@777
   338
    ms_per_step = (int) (((float)spec->samples * 1000.0) / spec->freq);
slouken@777
   339
slouken@777
   340
    return 0;
slouken@0
   341
}
slouken@0
   342
slouken@777
   343
Mix_MusicType detect_music_type_from_magic(const Uint8 *magic)
slouken@0
   344
{
slouken@616
   345
    /* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@777
   346
    if (SDL_memcmp(magic, "OggS", 4) == 0) {
slouken@616
   347
        return MUS_OGG;
slouken@616
   348
    }
slouken@542
   349
slouken@616
   350
    /* FLAC files have the magic four bytes "fLaC" */
slouken@777
   351
    if (SDL_memcmp(magic, "fLaC", 4) == 0) {
slouken@616
   352
        return MUS_FLAC;
slouken@616
   353
    }
slouken@542
   354
slouken@616
   355
    /* MIDI files have the magic four bytes "MThd" */
slouken@777
   356
    if (SDL_memcmp(magic, "MThd", 4) == 0) {
slouken@616
   357
        return MUS_MID;
slouken@616
   358
    }
slouken@542
   359
slouken@777
   360
    if (SDL_memcmp(magic, "ID3", 3) == 0 ||
slouken@777
   361
        (magic[0] == 0xFF && (magic[1] & 0xFE) == 0xFA)) {
slouken@616
   362
        return MUS_MP3;
slouken@616
   363
    }
slouken@542
   364
slouken@616
   365
    /* Assume MOD format.
slouken@616
   366
     *
slouken@616
   367
     * Apparently there is no way to check if the file is really a MOD,
slouken@616
   368
     * or there are too many formats supported by MikMod/ModPlug, or
slouken@616
   369
     * MikMod/ModPlug does this check by itself. */
slouken@616
   370
    return MUS_MOD;
slouken@542
   371
}
slouken@542
   372
slouken@777
   373
static Mix_MusicType detect_music_type(SDL_RWops *src)
slouken@777
   374
{
slouken@779
   375
    Uint8 magic[12];
slouken@777
   376
slouken@777
   377
    if (SDL_RWread(src, magic, 1, 12) != 12) {
slouken@777
   378
        Mix_SetError("Couldn't read first 12 bytes of audio data");
slouken@777
   379
        return MUS_NONE;
slouken@777
   380
    }
slouken@777
   381
    SDL_RWseek(src, -12, RW_SEEK_CUR);
slouken@777
   382
slouken@777
   383
    /* WAVE files have the magic four bytes "RIFF"
slouken@777
   384
       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
slouken@777
   385
    if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) ||
slouken@777
   386
        (SDL_memcmp(magic, "FORM", 4) == 0)) {
slouken@777
   387
        return MUS_WAV;
slouken@777
   388
    }
slouken@777
   389
slouken@777
   390
    return detect_music_type_from_magic(magic);
slouken@777
   391
}
slouken@777
   392
slouken@0
   393
/* Load a music file */
slouken@0
   394
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   395
{
slouken@777
   396
    int i;
slouken@777
   397
    void *context;
slouken@777
   398
    char *ext;
slouken@777
   399
    Mix_MusicType type;
slouken@625
   400
    SDL_RWops *src;
slouken@542
   401
slouken@777
   402
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   403
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   404
        if (!interface->opened || !interface->CreateFromFile) {
slouken@777
   405
            continue;
slouken@616
   406
        }
slouken@777
   407
slouken@777
   408
        context = interface->CreateFromFile(file);
slouken@777
   409
        if (context) {
slouken@777
   410
            /* Allocate memory for the music structure */
slouken@777
   411
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   412
            if (music == NULL) {
slouken@777
   413
                Mix_SetError("Out of memory");
slouken@777
   414
                return NULL;
slouken@777
   415
            }
slouken@777
   416
            music->interface = interface;
slouken@777
   417
            music->context = context;
slouken@777
   418
            return music;
slouken@616
   419
        }
slouken@616
   420
    }
slouken@542
   421
slouken@625
   422
    src = SDL_RWFromFile(file, "rb");
slouken@777
   423
    if (src == NULL) {
slouken@616
   424
        Mix_SetError("Couldn't open '%s'", file);
slouken@616
   425
        return NULL;
slouken@616
   426
    }
slouken@542
   427
slouken@616
   428
    /* Use the extension as a first guess on the file type */
slouken@616
   429
    type = MUS_NONE;
slouken@616
   430
    ext = strrchr(file, '.');
slouken@777
   431
    if (ext) {
slouken@616
   432
        ++ext; /* skip the dot in the extension */
slouken@777
   433
        if (SDL_strcasecmp(ext, "WAV") == 0) {
slouken@616
   434
            type = MUS_WAV;
slouken@777
   435
        } else if (SDL_strcasecmp(ext, "MID") == 0 ||
slouken@777
   436
                    SDL_strcasecmp(ext, "MIDI") == 0 ||
slouken@777
   437
                    SDL_strcasecmp(ext, "KAR") == 0) {
slouken@616
   438
            type = MUS_MID;
slouken@777
   439
        } else if (SDL_strcasecmp(ext, "OGG") == 0) {
slouken@616
   440
            type = MUS_OGG;
slouken@777
   441
        } else if (SDL_strcasecmp(ext, "FLAC") == 0) {
slouken@616
   442
            type = MUS_FLAC;
slouken@777
   443
        } else  if (SDL_strcasecmp(ext, "MPG") == 0 ||
slouken@777
   444
                     SDL_strcasecmp(ext, "MPEG") == 0 ||
slouken@777
   445
                     SDL_strcasecmp(ext, "MP3") == 0 ||
slouken@777
   446
                     SDL_strcasecmp(ext, "MAD") == 0) {
slouken@616
   447
            type = MUS_MP3;
slouken@777
   448
        } else if (SDL_strcasecmp(ext, "669") == 0 ||
slouken@777
   449
                    SDL_strcasecmp(ext, "AMF") == 0 ||
slouken@777
   450
                    SDL_strcasecmp(ext, "AMS") == 0 ||
slouken@777
   451
                    SDL_strcasecmp(ext, "DBM") == 0 ||
slouken@777
   452
                    SDL_strcasecmp(ext, "DSM") == 0 ||
slouken@777
   453
                    SDL_strcasecmp(ext, "FAR") == 0 ||
slouken@777
   454
                    SDL_strcasecmp(ext, "IT") == 0 ||
slouken@777
   455
                    SDL_strcasecmp(ext, "MED") == 0 ||
slouken@777
   456
                    SDL_strcasecmp(ext, "MDL") == 0 ||
slouken@777
   457
                    SDL_strcasecmp(ext, "MOD") == 0 ||
slouken@777
   458
                    SDL_strcasecmp(ext, "MOL") == 0 ||
slouken@777
   459
                    SDL_strcasecmp(ext, "MTM") == 0 ||
slouken@777
   460
                    SDL_strcasecmp(ext, "NST") == 0 ||
slouken@777
   461
                    SDL_strcasecmp(ext, "OKT") == 0 ||
slouken@777
   462
                    SDL_strcasecmp(ext, "PTM") == 0 ||
slouken@777
   463
                    SDL_strcasecmp(ext, "S3M") == 0 ||
slouken@777
   464
                    SDL_strcasecmp(ext, "STM") == 0 ||
slouken@777
   465
                    SDL_strcasecmp(ext, "ULT") == 0 ||
slouken@777
   466
                    SDL_strcasecmp(ext, "UMX") == 0 ||
slouken@777
   467
                    SDL_strcasecmp(ext, "WOW") == 0 ||
slouken@777
   468
                    SDL_strcasecmp(ext, "XM") == 0) {
slouken@777
   469
            type = MUS_MOD;
slouken@616
   470
        }
slouken@616
   471
    }
slouken@777
   472
    return Mix_LoadMUSType_RW(src, type, SDL_TRUE);
slouken@542
   473
}
slouken@542
   474
slouken@625
   475
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *src, int freesrc)
slouken@542
   476
{
slouken@625
   477
    return Mix_LoadMUSType_RW(src, MUS_NONE, freesrc);
slouken@542
   478
}
slouken@542
   479
slouken@625
   480
Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
slouken@542
   481
{
slouken@777
   482
    int i;
slouken@777
   483
    void *context;
slouken@625
   484
    Sint64 start;
slouken@0
   485
slouken@625
   486
    if (!src) {
slouken@616
   487
        Mix_SetError("RWops pointer is NULL");
slouken@616
   488
        return NULL;
slouken@616
   489
    }
slouken@625
   490
    start = SDL_RWtell(src);
slouken@542
   491
slouken@616
   492
    /* If the caller wants auto-detection, figure out what kind of file
slouken@616
   493
     * this is. */
slouken@616
   494
    if (type == MUS_NONE) {
slouken@625
   495
        if ((type = detect_music_type(src)) == MUS_NONE) {
slouken@777
   496
            /* Don't call Mix_SetError() since detect_music_type() does that. */
slouken@625
   497
            if (freesrc) {
slouken@625
   498
                SDL_RWclose(src);
slouken@625
   499
            }
slouken@616
   500
            return NULL;
slouken@616
   501
        }
slouken@616
   502
    }
slouken@125
   503
slouken@777
   504
    Mix_ClearError();
slouken@777
   505
slouken@777
   506
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   507
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   508
        if (!interface->opened || type != interface->type || !interface->CreateFromRW) {
slouken@777
   509
            continue;
slouken@625
   510
        }
slouken@0
   511
slouken@777
   512
        context = interface->CreateFromRW(src, freesrc);
slouken@777
   513
        if (context) {
slouken@777
   514
            /* Allocate memory for the music structure */
slouken@777
   515
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   516
            if (music == NULL) {
slouken@777
   517
                interface->Delete(context);
slouken@777
   518
                Mix_SetError("Out of memory");
slouken@777
   519
                return NULL;
slouken@777
   520
            }
slouken@777
   521
            music->interface = interface;
slouken@777
   522
            music->context = context;
slouken@777
   523
            return music;
slouken@616
   524
        }
slouken@777
   525
slouken@777
   526
        if (interface->api == MIX_MUSIC_SMPEG) {
slouken@777
   527
            /* Uh oh, if SMPEG couldn't create a context, it freed the src */
slouken@777
   528
            freesrc = SDL_FALSE;
slouken@616
   529
            break;
slouken@616
   530
        }
slouken@542
   531
slouken@777
   532
        /* Reset the stream for the next decoder */
slouken@777
   533
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   534
    }
chewi@506
   535
slouken@777
   536
    if (!*Mix_GetError()) {
slouken@777
   537
        Mix_SetError("Unrecognized audio format");
slouken@616
   538
    }
slouken@777
   539
    if (freesrc) {
slouken@777
   540
        SDL_RWclose(src);
slouken@777
   541
    } else {
slouken@777
   542
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   543
    }
slouken@777
   544
    return NULL;
slouken@0
   545
}
slouken@0
   546
slouken@0
   547
/* Free a music chunk previously loaded */
slouken@0
   548
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   549
{
slouken@777
   550
    if (music) {
slouken@616
   551
        /* Stop the music if it's currently playing */
slouken@718
   552
        Mix_LockAudio();
slouken@777
   553
        if (music == music_playing) {
slouken@616
   554
            /* Wait for any fade out to finish */
slouken@777
   555
            while (music->fading == MIX_FADING_OUT) {
slouken@718
   556
                Mix_UnlockAudio();
slouken@616
   557
                SDL_Delay(100);
slouken@718
   558
                Mix_LockAudio();
slouken@616
   559
            }
slouken@777
   560
            if (music == music_playing) {
slouken@616
   561
                music_internal_halt();
slouken@616
   562
            }
slouken@616
   563
        }
slouken@718
   564
        Mix_UnlockAudio();
chewi@506
   565
slouken@777
   566
        music->interface->Delete(music->context);
slouken@616
   567
        SDL_free(music);
slouken@616
   568
    }
slouken@0
   569
}
slouken@0
   570
slouken@177
   571
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   572
   music, if 'music' is NULL.
slouken@177
   573
*/
slouken@177
   574
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   575
{
slouken@616
   576
    Mix_MusicType type = MUS_NONE;
slouken@177
   577
slouken@777
   578
    if (music) {
slouken@777
   579
        type = music->interface->type;
slouken@616
   580
    } else {
slouken@718
   581
        Mix_LockAudio();
slouken@777
   582
        if (music_playing) {
slouken@777
   583
            type = music_playing->interface->type;
slouken@616
   584
        }
slouken@718
   585
        Mix_UnlockAudio();
slouken@616
   586
    }
slouken@616
   587
    return(type);
slouken@177
   588
}
slouken@177
   589
slouken@173
   590
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   591
 */
slouken@173
   592
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   593
{
slouken@616
   594
    int retval = 0;
megastep@10
   595
slouken@777
   596
#if defined(__MACOSX__) && defined(MID_MUSIC_NATIVE)
slouken@616
   597
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   598
       can't really stop and restart MIDI from the audio callback.
slouken@616
   599
    */
slouken@777
   600
    if (music == music_playing && music->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   601
        /* Just a seek suffices to restart playing */
slouken@616
   602
        music_internal_position(position);
slouken@616
   603
        return 0;
slouken@616
   604
    }
slouken@531
   605
#endif
slouken@529
   606
slouken@616
   607
    /* Note the music we're playing */
slouken@777
   608
    if (music_playing) {
slouken@616
   609
        music_internal_halt();
slouken@616
   610
    }
slouken@616
   611
    music_playing = music;
slouken@777
   612
    music_playing->playing = SDL_TRUE;
slouken@173
   613
slouken@616
   614
    /* Set the initial volume */
slouken@777
   615
    music_internal_initialize_volume();
slouken@173
   616
slouken@616
   617
    /* Set up for playback */
slouken@777
   618
    retval = music->interface->Play(music->context);
slouken@173
   619
slouken@616
   620
    /* Set the playback position, note any errors if an offset is used */
slouken@777
   621
    if (retval == 0) {
slouken@777
   622
        if (position > 0.0) {
slouken@777
   623
            if (music_internal_position(position) < 0) {
slouken@616
   624
                Mix_SetError("Position not implemented for music type");
slouken@616
   625
                retval = -1;
slouken@616
   626
            }
slouken@616
   627
        } else {
slouken@616
   628
            music_internal_position(0.0);
slouken@616
   629
        }
slouken@616
   630
    }
slouken@173
   631
slouken@616
   632
    /* If the setup failed, we're not playing any music anymore */
slouken@777
   633
    if (retval < 0) {
slouken@616
   634
        music_playing = NULL;
slouken@616
   635
    }
slouken@616
   636
    return(retval);
slouken@173
   637
}
slouken@777
   638
slouken@173
   639
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   640
{
slouken@616
   641
    int retval;
slouken@173
   642
slouken@777
   643
    if (ms_per_step == 0) {
slouken@616
   644
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   645
        return(-1);
slouken@616
   646
    }
slouken@499
   647
slouken@616
   648
    /* Don't play null pointers :-) */
slouken@777
   649
    if (music == NULL) {
slouken@616
   650
        Mix_SetError("music parameter was NULL");
slouken@616
   651
        return(-1);
slouken@616
   652
    }
slouken@173
   653
slouken@616
   654
    /* Setup the data */
slouken@777
   655
    if (ms) {
slouken@616
   656
        music->fading = MIX_FADING_IN;
slouken@616
   657
    } else {
slouken@616
   658
        music->fading = MIX_NO_FADING;
slouken@616
   659
    }
slouken@616
   660
    music->fade_step = 0;
slouken@616
   661
    music->fade_steps = ms/ms_per_step;
slouken@173
   662
slouken@616
   663
    /* Play the puppy */
slouken@718
   664
    Mix_LockAudio();
slouken@616
   665
    /* If the current music is fading out, wait for the fade to complete */
slouken@777
   666
    while (music_playing && (music_playing->fading == MIX_FADING_OUT)) {
slouken@718
   667
        Mix_UnlockAudio();
slouken@616
   668
        SDL_Delay(100);
slouken@718
   669
        Mix_LockAudio();
slouken@616
   670
    }
slouken@616
   671
    music_active = 1;
slouken@616
   672
    if (loops == 1) {
slouken@616
   673
        /* Loop is the number of times to play the audio */
slouken@616
   674
        loops = 0;
slouken@616
   675
    }
slouken@616
   676
    music_loops = loops;
slouken@616
   677
    retval = music_internal_play(music, position);
slouken@718
   678
    Mix_UnlockAudio();
slouken@173
   679
slouken@616
   680
    return(retval);
slouken@173
   681
}
slouken@173
   682
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   683
{
slouken@616
   684
    return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   685
}
slouken@173
   686
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   687
{
slouken@616
   688
    return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   689
}
megastep@10
   690
slouken@173
   691
/* Set the playing music position */
slouken@173
   692
int music_internal_position(double position)
megastep@10
   693
{
slouken@777
   694
    if (music_playing->interface->Seek) {
slouken@777
   695
        return music_playing->interface->Seek(music_playing->context, position);
slouken@616
   696
    }
slouken@777
   697
    return -1;
megastep@4
   698
}
slouken@155
   699
int Mix_SetMusicPosition(double position)
slouken@154
   700
{
slouken@616
   701
    int retval;
slouken@173
   702
slouken@718
   703
    Mix_LockAudio();
slouken@777
   704
    if (music_playing) {
slouken@616
   705
        retval = music_internal_position(position);
slouken@777
   706
        if (retval < 0) {
slouken@616
   707
            Mix_SetError("Position not implemented for music type");
slouken@616
   708
        }
slouken@616
   709
    } else {
slouken@616
   710
        Mix_SetError("Music isn't playing");
slouken@616
   711
        retval = -1;
slouken@616
   712
    }
slouken@718
   713
    Mix_UnlockAudio();
slouken@154
   714
slouken@616
   715
    return(retval);
slouken@154
   716
}
slouken@154
   717
icculus@237
   718
/* Set the music's initial volume */
icculus@237
   719
static void music_internal_initialize_volume(void)
icculus@237
   720
{
slouken@777
   721
    if (music_playing->fading == MIX_FADING_IN) {
slouken@616
   722
        music_internal_volume(0);
slouken@616
   723
    } else {
slouken@616
   724
        music_internal_volume(music_volume);
slouken@616
   725
    }
icculus@237
   726
}
icculus@237
   727
slouken@0
   728
/* Set the music volume */
slouken@173
   729
static void music_internal_volume(int volume)
slouken@173
   730
{
slouken@777
   731
    if (music_playing->interface->SetVolume) {
slouken@777
   732
        music_playing->interface->SetVolume(music_playing->context, volume);
slouken@616
   733
    }
slouken@173
   734
}
slouken@0
   735
int Mix_VolumeMusic(int volume)
slouken@0
   736
{
slouken@616
   737
    int prev_volume;
slouken@0
   738
slouken@616
   739
    prev_volume = music_volume;
slouken@777
   740
    if (volume < 0) {
slouken@616
   741
        return prev_volume;
slouken@616
   742
    }
slouken@777
   743
    if (volume > SDL_MIX_MAXVOLUME) {
slouken@616
   744
        volume = SDL_MIX_MAXVOLUME;
slouken@616
   745
    }
slouken@616
   746
    music_volume = volume;
slouken@718
   747
    Mix_LockAudio();
slouken@777
   748
    if (music_playing) {
slouken@616
   749
        music_internal_volume(music_volume);
slouken@616
   750
    }
slouken@718
   751
    Mix_UnlockAudio();
slouken@616
   752
    return(prev_volume);
slouken@0
   753
}
slouken@0
   754
slouken@173
   755
/* Halt playing of music */
slouken@173
   756
static void music_internal_halt(void)
megastep@7
   757
{
slouken@777
   758
    if (music_playing->interface->Stop) {
slouken@777
   759
        music_playing->interface->Stop(music_playing->context);
slouken@616
   760
    }
chewi@506
   761
slouken@777
   762
    music_playing->playing = SDL_FALSE;
slouken@616
   763
    music_playing->fading = MIX_NO_FADING;
slouken@616
   764
    music_playing = NULL;
megastep@7
   765
}
slouken@0
   766
int Mix_HaltMusic(void)
slouken@0
   767
{
slouken@718
   768
    Mix_LockAudio();
slouken@777
   769
    if (music_playing) {
slouken@616
   770
        music_internal_halt();
slouken@777
   771
        if (music_finished_hook) {
slouken@616
   772
            music_finished_hook();
slouken@616
   773
        }
slouken@616
   774
    }
slouken@718
   775
    Mix_UnlockAudio();
slouken@173
   776
slouken@616
   777
    return(0);
slouken@0
   778
}
slouken@0
   779
megastep@4
   780
/* Progressively stop the music */
megastep@4
   781
int Mix_FadeOutMusic(int ms)
megastep@4
   782
{
slouken@616
   783
    int retval = 0;
slouken@173
   784
slouken@777
   785
    if (ms_per_step == 0) {
slouken@616
   786
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   787
        return 0;
slouken@616
   788
    }
slouken@499
   789
slouken@616
   790
    if (ms <= 0) {  /* just halt immediately. */
slouken@616
   791
        Mix_HaltMusic();
slouken@616
   792
        return 1;
slouken@616
   793
    }
icculus@280
   794
slouken@718
   795
    Mix_LockAudio();
slouken@777
   796
    if (music_playing) {
slouken@777
   797
        int fade_steps = (ms + ms_per_step - 1) / ms_per_step;
slouken@777
   798
        if (music_playing->fading == MIX_NO_FADING) {
slouken@777
   799
            music_playing->fade_step = 0;
slouken@777
   800
        } else {
slouken@777
   801
            int step;
slouken@777
   802
            int old_fade_steps = music_playing->fade_steps;
slouken@777
   803
            if (music_playing->fading == MIX_FADING_OUT) {
slouken@777
   804
                step = music_playing->fade_step;
slouken@777
   805
            } else {
slouken@777
   806
                step = old_fade_steps - music_playing->fade_step + 1;
slouken@777
   807
            }
slouken@777
   808
            music_playing->fade_step = (step * fade_steps) / old_fade_steps;
slouken@777
   809
        }
slouken@616
   810
        music_playing->fading = MIX_FADING_OUT;
slouken@616
   811
        music_playing->fade_steps = fade_steps;
slouken@616
   812
        retval = 1;
slouken@616
   813
    }
slouken@718
   814
    Mix_UnlockAudio();
slouken@173
   815
slouken@616
   816
    return(retval);
megastep@4
   817
}
megastep@4
   818
megastep@4
   819
Mix_Fading Mix_FadingMusic(void)
megastep@4
   820
{
slouken@616
   821
    Mix_Fading fading = MIX_NO_FADING;
slouken@173
   822
slouken@718
   823
    Mix_LockAudio();
slouken@777
   824
    if (music_playing) {
slouken@616
   825
        fading = music_playing->fading;
slouken@616
   826
    }
slouken@718
   827
    Mix_UnlockAudio();
slouken@173
   828
slouken@616
   829
    return(fading);
megastep@4
   830
}
megastep@4
   831
slouken@0
   832
/* Pause/Resume the music stream */
slouken@0
   833
void Mix_PauseMusic(void)
slouken@0
   834
{
slouken@777
   835
    Mix_LockAudio();
slouken@777
   836
    if (music_playing && music_playing->interface->Pause) {
slouken@777
   837
        music_playing->interface->Pause(music_playing->context);
slouken@777
   838
    }
slouken@616
   839
    music_active = 0;
slouken@777
   840
    Mix_UnlockAudio();
slouken@0
   841
}
megastep@4
   842
slouken@0
   843
void Mix_ResumeMusic(void)
slouken@0
   844
{
slouken@777
   845
    Mix_LockAudio();
slouken@616
   846
    music_active = 1;
slouken@777
   847
    if (music_playing && music_playing->interface->Resume) {
slouken@777
   848
        music_playing->interface->Resume(music_playing->context);
slouken@777
   849
    }
slouken@777
   850
    Mix_UnlockAudio();
slouken@0
   851
}
slouken@0
   852
slouken@0
   853
void Mix_RewindMusic(void)
slouken@0
   854
{
slouken@616
   855
    Mix_SetMusicPosition(0.0);
slouken@0
   856
}
slouken@0
   857
megastep@13
   858
int Mix_PausedMusic(void)
megastep@13
   859
{
slouken@616
   860
    return (music_active == 0);
megastep@13
   861
}
megastep@13
   862
slouken@0
   863
/* Check the status of the music */
slouken@779
   864
static SDL_bool music_internal_playing(void)
slouken@173
   865
{
slouken@616
   866
    if (music_playing == NULL) {
slouken@777
   867
        return SDL_FALSE;
slouken@616
   868
    }
icculus@510
   869
slouken@777
   870
    if (music_playing->interface->IsPlaying) {
slouken@777
   871
        return music_playing->interface->IsPlaying(music_playing->context);
slouken@616
   872
    }
slouken@777
   873
    return music_playing->playing;
slouken@173
   874
}
megastep@13
   875
int Mix_PlayingMusic(void)
slouken@0
   876
{
slouken@616
   877
    int playing = 0;
slouken@173
   878
slouken@718
   879
    Mix_LockAudio();
slouken@777
   880
    if (music_playing) {
slouken@616
   881
        playing = music_loops || music_internal_playing();
slouken@616
   882
    }
slouken@718
   883
    Mix_UnlockAudio();
slouken@173
   884
slouken@616
   885
    return(playing);
slouken@0
   886
}
slouken@0
   887
slouken@0
   888
/* Set the external music playback command */
slouken@0
   889
int Mix_SetMusicCMD(const char *command)
slouken@0
   890
{
slouken@616
   891
    Mix_HaltMusic();
slouken@777
   892
    if (music_cmd) {
slouken@616
   893
        SDL_free(music_cmd);
slouken@616
   894
        music_cmd = NULL;
slouken@616
   895
    }
slouken@777
   896
    if (command) {
slouken@777
   897
        size_t length = SDL_strlen(command) + 1;
slouken@777
   898
        music_cmd = (char *)SDL_malloc(length);
slouken@777
   899
        if (music_cmd == NULL) {
slouken@777
   900
            return SDL_OutOfMemory();
slouken@616
   901
        }
slouken@777
   902
        SDL_memcpy(music_cmd, command, length);
slouken@616
   903
    }
slouken@777
   904
    return 0;
slouken@0
   905
}
slouken@0
   906
slouken@154
   907
int Mix_SetSynchroValue(int i)
slouken@154
   908
{
slouken@616
   909
    /* Not supported by any players at this time */
slouken@616
   910
    return(-1);
slouken@154
   911
}
slouken@154
   912
slouken@154
   913
int Mix_GetSynchroValue(void)
slouken@154
   914
{
slouken@616
   915
    /* Not supported by any players at this time */
slouken@616
   916
    return(-1);
slouken@154
   917
}
slouken@154
   918
slouken@154
   919
slouken@777
   920
/* Uninitialize the music interfaces */
slouken@0
   921
void close_music(void)
slouken@0
   922
{
slouken@777
   923
    int i;
slouken@777
   924
slouken@616
   925
    Mix_HaltMusic();
slouken@777
   926
slouken@777
   927
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   928
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   929
        if (!interface || !interface->opened) {
slouken@777
   930
            continue;
slouken@777
   931
        }
slouken@777
   932
slouken@777
   933
        if (interface->Close) {
slouken@777
   934
            interface->Close();
slouken@777
   935
        }
slouken@777
   936
        interface->opened = SDL_FALSE;
slouken@777
   937
    }
icculus@390
   938
slouken@616
   939
    /* rcg06042009 report available decoders at runtime. */
slouken@621
   940
    SDL_free((void *)music_decoders);
slouken@616
   941
    music_decoders = NULL;
slouken@616
   942
    num_decoders = 0;
slouken@499
   943
slouken@616
   944
    ms_per_step = 0;
slouken@0
   945
}
slouken@0
   946
slouken@777
   947
/* Unload the music interface libraries */
slouken@777
   948
void unload_music(void)
slouken@777
   949
{
slouken@777
   950
    int i;
slouken@777
   951
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   952
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   953
        if (!interface || !interface->loaded) {
slouken@777
   954
            continue;
slouken@777
   955
        }
slouken@777
   956
slouken@777
   957
        if (interface->Unload) {
slouken@777
   958
            interface->Unload();
slouken@777
   959
        }
slouken@777
   960
        interface->loaded = SDL_FALSE;
slouken@777
   961
    }
slouken@777
   962
}
slouken@777
   963
chewi@506
   964
int Mix_SetSoundFonts(const char *paths)
chewi@506
   965
{
slouken@616
   966
    if (soundfont_paths) {
slouken@616
   967
        SDL_free(soundfont_paths);
slouken@616
   968
        soundfont_paths = NULL;
slouken@616
   969
    }
chewi@506
   970
slouken@616
   971
    if (paths) {
slouken@616
   972
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
   973
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
   974
            return 0;
slouken@616
   975
        }
slouken@616
   976
    }
slouken@616
   977
    return 1;
chewi@506
   978
}
chewi@506
   979
dimitris@508
   980
const char* Mix_GetSoundFonts(void)
chewi@506
   981
{
slouken@777
   982
    if (!soundfont_paths || SDL_GetHintBoolean("SDL_FORCE_SOUNDFONTS", SDL_FALSE)) {
slouken@777
   983
        return SDL_getenv("SDL_SOUNDFONTS");
slouken@616
   984
    } else {
slouken@616
   985
        return soundfont_paths;
slouken@616
   986
    }
chewi@506
   987
}
chewi@506
   988
chewi@506
   989
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
   990
{
slouken@616
   991
    char *context, *path, *paths;
slouken@616
   992
    const char* cpaths = Mix_GetSoundFonts();
slouken@762
   993
    int soundfonts_found = 0;
chewi@506
   994
slouken@616
   995
    if (!cpaths) {
slouken@616
   996
        Mix_SetError("No SoundFonts have been requested");
slouken@616
   997
        return 0;
slouken@616
   998
    }
chewi@506
   999
slouken@616
  1000
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
  1001
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
  1002
        return 0;
slouken@616
  1003
    }
chewi@506
  1004
slouken@770
  1005
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
slouken@616
  1006
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1007
#elif defined(_WIN32)
slouken@616
  1008
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1009
#else
slouken@616
  1010
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1011
#endif
slouken@616
  1012
        if (!function(path, data)) {
slouken@762
  1013
            continue;
slouken@762
  1014
        } else {
slouken@762
  1015
            soundfonts_found++;
slouken@616
  1016
        }
slouken@616
  1017
    }
chewi@506
  1018
slouken@616
  1019
    SDL_free(paths);
slouken@762
  1020
    if (soundfonts_found > 0)
slouken@762
  1021
        return 1;
slouken@762
  1022
    else
slouken@762
  1023
        return 0;
chewi@506
  1024
}
slouken@777
  1025
slouken@777
  1026
/* vi: set ts=4 sw=4 expandtab: */