music.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Oct 2017 02:10:33 -0700
changeset 801 46acd70caa6a
parent 797 b4b6adff699a
child 802 98ee7bc3833a
permissions -rw-r--r--
Fixed FluidSynth support and added a default soundfont path on Linux
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@725
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@777
    21
#include <string.h> /* for strtok() and strtok_s() */
slouken@0
    22
slouken@777
    23
#include "SDL_hints.h"
slouken@29
    24
#include "SDL_timer.h"
slouken@0
    25
slouken@34
    26
#include "SDL_mixer.h"
slouken@718
    27
#include "mixer.h"
slouken@777
    28
#include "music.h"
slouken@0
    29
slouken@0
    30
#include "music_cmd.h"
slouken@777
    31
#include "music_wav.h"
slouken@777
    32
#include "music_mikmod.h"
slouken@481
    33
#include "music_modplug.h"
slouken@777
    34
#include "music_nativemidi.h"
slouken@777
    35
#include "music_fluidsynth.h"
slouken@777
    36
#include "music_timidity.h"
slouken@63
    37
#include "music_ogg.h"
slouken@777
    38
#include "music_mpg123.h"
slouken@357
    39
#include "music_mad.h"
slouken@777
    40
#include "music_smpeg.h"
slouken@382
    41
#include "music_flac.h"
slouken@777
    42
#include "native_midi/native_midi.h"
slouken@0
    43
slouken@0
    44
slouken@777
    45
char *music_cmd = NULL;
slouken@797
    46
static SDL_bool music_active = SDL_TRUE;
slouken@797
    47
static int music_volume = MIX_MAX_VOLUME;
slouken@42
    48
static Mix_Music * volatile music_playing = NULL;
slouken@777
    49
SDL_AudioSpec music_spec;
megastep@4
    50
slouken@0
    51
struct _Mix_Music {
slouken@777
    52
    Mix_MusicInterface *interface;
slouken@777
    53
    void *context;
slouken@777
    54
slouken@777
    55
    SDL_bool playing;
slouken@616
    56
    Mix_Fading fading;
slouken@616
    57
    int fade_step;
slouken@616
    58
    int fade_steps;
slouken@0
    59
};
slouken@0
    60
slouken@17
    61
/* Used to calculate fading steps */
slouken@17
    62
static int ms_per_step;
slouken@17
    63
icculus@390
    64
/* rcg06042009 report available decoders at runtime. */
icculus@390
    65
static const char **music_decoders = NULL;
icculus@390
    66
static int num_decoders = 0;
icculus@390
    67
chewi@506
    68
/* Semicolon-separated SoundFont paths */
slouken@801
    69
static char* soundfont_paths = NULL;
chewi@506
    70
slouken@777
    71
/* Interfaces for the various music interfaces, ordered by priority */
slouken@777
    72
static Mix_MusicInterface *s_music_interfaces[] =
slouken@777
    73
{
slouken@777
    74
#ifdef MUSIC_CMD
slouken@777
    75
    &Mix_MusicInterface_CMD,
slouken@777
    76
#endif
slouken@777
    77
#ifdef MUSIC_WAV
slouken@777
    78
    &Mix_MusicInterface_WAV,
slouken@777
    79
#endif
slouken@777
    80
#ifdef MUSIC_MOD_MODPLUG
slouken@777
    81
    &Mix_MusicInterface_MODPLUG,
slouken@777
    82
#endif
slouken@777
    83
#ifdef MUSIC_MOD_MIKMOD
slouken@777
    84
    &Mix_MusicInterface_MIKMOD,
slouken@777
    85
#endif
slouken@777
    86
#ifdef MUSIC_MID_FLUIDSYNTH
slouken@777
    87
    &Mix_MusicInterface_FLUIDSYNTH,
slouken@777
    88
#endif
slouken@777
    89
#ifdef MUSIC_MID_TIMIDITY
slouken@777
    90
    &Mix_MusicInterface_TIMIDITY,
slouken@777
    91
#endif
slouken@777
    92
#ifdef MUSIC_MID_NATIVE
slouken@777
    93
    &Mix_MusicInterface_NATIVEMIDI,
slouken@777
    94
#endif
slouken@777
    95
#ifdef MUSIC_OGG
slouken@777
    96
    &Mix_MusicInterface_OGG,
slouken@777
    97
#endif
slouken@777
    98
#ifdef MUSIC_MP3_MPG123
slouken@777
    99
    &Mix_MusicInterface_MPG123,
slouken@777
   100
#endif
slouken@777
   101
#ifdef MUSIC_MP3_MAD
slouken@777
   102
    &Mix_MusicInterface_MAD,
slouken@777
   103
#endif
slouken@777
   104
#ifdef MUSIC_MP3_SMPEG
slouken@777
   105
    &Mix_MusicInterface_SMPEG,
slouken@777
   106
#endif
slouken@777
   107
#ifdef MUSIC_FLAC
slouken@777
   108
    &Mix_MusicInterface_FLAC,
slouken@777
   109
#endif
slouken@777
   110
};
slouken@777
   111
slouken@777
   112
int get_num_music_interfaces(void)
slouken@777
   113
{
slouken@777
   114
    return SDL_arraysize(s_music_interfaces);
slouken@777
   115
}
slouken@777
   116
slouken@777
   117
Mix_MusicInterface *get_music_interface(int index)
slouken@777
   118
{
slouken@777
   119
    return s_music_interfaces[index];
slouken@777
   120
}
slouken@777
   121
slouken@466
   122
int Mix_GetNumMusicDecoders(void)
icculus@390
   123
{
slouken@616
   124
    return(num_decoders);
icculus@390
   125
}
icculus@390
   126
icculus@390
   127
const char *Mix_GetMusicDecoder(int index)
icculus@390
   128
{
slouken@616
   129
    if ((index < 0) || (index >= num_decoders)) {
slouken@616
   130
        return NULL;
slouken@616
   131
    }
slouken@616
   132
    return(music_decoders[index]);
icculus@390
   133
}
icculus@390
   134
icculus@390
   135
static void add_music_decoder(const char *decoder)
icculus@390
   136
{
slouken@644
   137
    void *ptr = SDL_realloc((void *)music_decoders, (num_decoders + 1) * sizeof (const char *));
slouken@616
   138
    if (ptr == NULL) {
slouken@616
   139
        return;  /* oh well, go on without it. */
slouken@616
   140
    }
slouken@616
   141
    music_decoders = (const char **) ptr;
slouken@616
   142
    music_decoders[num_decoders++] = decoder;
icculus@390
   143
}
icculus@390
   144
megastep@10
   145
/* Local low-level functions prototypes */
icculus@237
   146
static void music_internal_initialize_volume(void);
slouken@173
   147
static void music_internal_volume(int volume);
slouken@797
   148
static int  music_internal_play(Mix_Music *music, int play_count, double position);
slouken@173
   149
static int  music_internal_position(double position);
slouken@779
   150
static SDL_bool music_internal_playing(void);
slouken@173
   151
static void music_internal_halt(void);
megastep@7
   152
slouken@29
   153
slouken@29
   154
/* Support for hooking when the music has finished */
slouken@29
   155
static void (*music_finished_hook)(void) = NULL;
slouken@29
   156
slouken@29
   157
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   158
{
slouken@718
   159
    Mix_LockAudio();
slouken@616
   160
    music_finished_hook = music_finished;
slouken@718
   161
    Mix_UnlockAudio();
slouken@29
   162
}
slouken@29
   163
slouken@797
   164
/* Convenience function to fill audio and mix at the specified volume
slouken@797
   165
   This is called from many music player's GetAudio callback.
slouken@797
   166
 */
slouken@797
   167
int music_pcm_getaudio(void *context, void *data, int bytes, int volume,
slouken@797
   168
                       int (*GetSome)(void *context, void *data, int bytes, SDL_bool *done))
slouken@797
   169
{
slouken@797
   170
    Uint8 *snd = (Uint8 *)data;
slouken@797
   171
    Uint8 *dst;
slouken@797
   172
    int len = bytes;
slouken@797
   173
    SDL_bool done = SDL_FALSE;
slouken@29
   174
slouken@797
   175
    if (volume == MIX_MAX_VOLUME) {
slouken@797
   176
        dst = snd;
slouken@797
   177
    } else {
slouken@797
   178
        dst = SDL_stack_alloc(Uint8, bytes);
slouken@797
   179
    }
slouken@797
   180
    while (len > 0 && !done) {
slouken@797
   181
        int consumed = GetSome(context, dst, len, &done);
slouken@797
   182
        if (consumed < 0) {
slouken@797
   183
            break;
slouken@616
   184
        }
slouken@533
   185
slouken@797
   186
        if (volume == MIX_MAX_VOLUME) {
slouken@797
   187
            dst += consumed;
slouken@797
   188
        } else {
slouken@797
   189
            SDL_MixAudioFormat(snd, dst, music_spec.format, (Uint32)consumed, volume);
slouken@797
   190
            snd += consumed;
slouken@616
   191
        }
slouken@797
   192
        len -= consumed;
slouken@616
   193
    }
slouken@797
   194
    if (volume != MIX_MAX_VOLUME) {
slouken@797
   195
        SDL_stack_free(dst);
slouken@797
   196
    }
slouken@797
   197
    return len;
icculus@281
   198
}
icculus@281
   199
slouken@0
   200
/* Mixing function */
slouken@0
   201
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   202
{
slouken@797
   203
    while (music_playing && music_active && len > 0) {
slouken@616
   204
        /* Handle fading */
slouken@777
   205
        if (music_playing->fading != MIX_NO_FADING) {
slouken@777
   206
            if (music_playing->fade_step++ < music_playing->fade_steps) {
slouken@616
   207
                int volume;
slouken@616
   208
                int fade_step = music_playing->fade_step;
slouken@616
   209
                int fade_steps = music_playing->fade_steps;
slouken@17
   210
slouken@777
   211
                if (music_playing->fading == MIX_FADING_OUT) {
slouken@616
   212
                    volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@616
   213
                } else { /* Fading in */
slouken@616
   214
                    volume = (music_volume * fade_step) / fade_steps;
slouken@616
   215
                }
slouken@616
   216
                music_internal_volume(volume);
slouken@616
   217
            } else {
slouken@777
   218
                if (music_playing->fading == MIX_FADING_OUT) {
slouken@616
   219
                    music_internal_halt();
slouken@777
   220
                    if (music_finished_hook) {
slouken@616
   221
                        music_finished_hook();
slouken@616
   222
                    }
slouken@616
   223
                    return;
slouken@616
   224
                }
slouken@616
   225
                music_playing->fading = MIX_NO_FADING;
slouken@616
   226
            }
slouken@616
   227
        }
slouken@528
   228
slouken@797
   229
        if (music_playing->interface->GetAudio) {
slouken@797
   230
            int left = music_playing->interface->GetAudio(music_playing->context, stream, len);
slouken@797
   231
            if (left != 0) {
slouken@797
   232
                /* Either an error or finished playing with data left */
slouken@797
   233
                music_playing->playing = SDL_FALSE;
slouken@797
   234
            }
slouken@797
   235
            if (left > 0) {
slouken@797
   236
                stream += (len - left);
slouken@797
   237
                len = left;
slouken@797
   238
            } else {
slouken@797
   239
                len = 0;
slouken@797
   240
            }
slouken@797
   241
        } else {
slouken@797
   242
            len = 0;
slouken@777
   243
        }
slouken@616
   244
slouken@797
   245
        if (!music_internal_playing()) {
slouken@797
   246
            music_internal_halt();
slouken@797
   247
            if (music_finished_hook) {
slouken@797
   248
                music_finished_hook();
slouken@777
   249
            }
slouken@777
   250
        }
slouken@777
   251
    }
slouken@777
   252
}
slouken@616
   253
slouken@777
   254
/* Load the music interface libraries */
slouken@777
   255
int load_music(void)
slouken@777
   256
{
slouken@777
   257
    char hint[128];
slouken@777
   258
slouken@777
   259
    int i;
slouken@777
   260
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   261
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   262
        if (interface->loaded) {
slouken@777
   263
            continue;
slouken@777
   264
        }
slouken@777
   265
slouken@777
   266
        SDL_snprintf(hint, sizeof(hint), "SDL_MIXER_DISABLE_%s", interface->tag);
slouken@777
   267
        if (SDL_GetHintBoolean(hint, SDL_FALSE)) {
slouken@777
   268
            continue;
slouken@777
   269
        }
slouken@777
   270
slouken@777
   271
        if (!interface->Load || interface->Load() == 0) {
slouken@777
   272
            interface->loaded = SDL_TRUE;
slouken@777
   273
        }
slouken@777
   274
    }
slouken@777
   275
    return 0;
slouken@777
   276
}
slouken@777
   277
slouken@777
   278
/* Return SDL_TRUE if the music type is available */
slouken@777
   279
SDL_bool has_music(Mix_MusicType type)
slouken@777
   280
{
slouken@777
   281
    int i;
slouken@777
   282
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   283
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   284
        if (interface->type != type) {
slouken@777
   285
            continue;
slouken@777
   286
        }
slouken@777
   287
        if (interface->opened) {
slouken@777
   288
            return SDL_TRUE;
slouken@777
   289
        }
slouken@777
   290
    }
slouken@777
   291
    return SDL_FALSE;
slouken@777
   292
}
slouken@777
   293
slouken@777
   294
/* Initialize the music interfaces with a certain desired audio format */
slouken@777
   295
int open_music(const SDL_AudioSpec *spec)
slouken@777
   296
{
slouken@777
   297
    int i;
slouken@777
   298
    SDL_bool use_native_midi = SDL_FALSE;
slouken@777
   299
slouken@801
   300
#ifdef MIX_INIT_SOUNDFONT_PATHS
slouken@801
   301
    if (!soundfont_paths) {
slouken@801
   302
        soundfont_paths = SDL_strdup(MIX_INIT_SOUNDFONT_PATHS);
slouken@801
   303
    }
slouken@801
   304
#endif
slouken@801
   305
slouken@801
   306
slouken@780
   307
#ifdef MUSIC_MID_NATIVE
slouken@777
   308
    if (SDL_GetHintBoolean("SDL_NATIVE_MUSIC", SDL_FALSE) && native_midi_detect()) {
slouken@777
   309
        use_native_midi = SDL_TRUE;
slouken@777
   310
    }
slouken@780
   311
#endif
slouken@777
   312
slouken@777
   313
    music_spec = *spec;
slouken@777
   314
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   315
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   316
        if (!interface->loaded) {
slouken@777
   317
            continue;
slouken@777
   318
        }
slouken@777
   319
slouken@777
   320
        if (interface->type == MUS_MID && use_native_midi && interface->api != MIX_MUSIC_NATIVEMIDI) {
slouken@777
   321
            continue;
slouken@777
   322
        }
slouken@777
   323
slouken@777
   324
        if (!interface->Open || interface->Open(spec) == 0) {
slouken@777
   325
            interface->opened = SDL_TRUE;
slouken@777
   326
            add_music_decoder(interface->tag);
slouken@616
   327
        }
slouken@616
   328
    }
slouken@407
   329
slouken@777
   330
    if (has_music(MUS_MOD)) {
slouken@777
   331
        add_music_decoder("MOD");
slouken@616
   332
    }
slouken@777
   333
    if (has_music(MUS_MID)) {
slouken@777
   334
        add_music_decoder("MIDI");
slouken@777
   335
    }
slouken@777
   336
    if (has_music(MUS_MP3)) {
slouken@777
   337
        add_music_decoder("MP3");
slouken@777
   338
    }
slouken@777
   339
slouken@777
   340
    Mix_VolumeMusic(MIX_MAX_VOLUME);
slouken@777
   341
slouken@777
   342
    /* Calculate the number of ms for each callback */
slouken@777
   343
    ms_per_step = (int) (((float)spec->samples * 1000.0) / spec->freq);
slouken@777
   344
slouken@777
   345
    return 0;
slouken@0
   346
}
slouken@0
   347
slouken@777
   348
Mix_MusicType detect_music_type_from_magic(const Uint8 *magic)
slouken@0
   349
{
slouken@616
   350
    /* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@777
   351
    if (SDL_memcmp(magic, "OggS", 4) == 0) {
slouken@616
   352
        return MUS_OGG;
slouken@616
   353
    }
slouken@542
   354
slouken@616
   355
    /* FLAC files have the magic four bytes "fLaC" */
slouken@777
   356
    if (SDL_memcmp(magic, "fLaC", 4) == 0) {
slouken@616
   357
        return MUS_FLAC;
slouken@616
   358
    }
slouken@542
   359
slouken@616
   360
    /* MIDI files have the magic four bytes "MThd" */
slouken@777
   361
    if (SDL_memcmp(magic, "MThd", 4) == 0) {
slouken@616
   362
        return MUS_MID;
slouken@616
   363
    }
slouken@542
   364
slouken@777
   365
    if (SDL_memcmp(magic, "ID3", 3) == 0 ||
slouken@777
   366
        (magic[0] == 0xFF && (magic[1] & 0xFE) == 0xFA)) {
slouken@616
   367
        return MUS_MP3;
slouken@616
   368
    }
slouken@542
   369
slouken@616
   370
    /* Assume MOD format.
slouken@616
   371
     *
slouken@616
   372
     * Apparently there is no way to check if the file is really a MOD,
slouken@616
   373
     * or there are too many formats supported by MikMod/ModPlug, or
slouken@616
   374
     * MikMod/ModPlug does this check by itself. */
slouken@616
   375
    return MUS_MOD;
slouken@542
   376
}
slouken@542
   377
slouken@777
   378
static Mix_MusicType detect_music_type(SDL_RWops *src)
slouken@777
   379
{
slouken@779
   380
    Uint8 magic[12];
slouken@777
   381
slouken@777
   382
    if (SDL_RWread(src, magic, 1, 12) != 12) {
slouken@777
   383
        Mix_SetError("Couldn't read first 12 bytes of audio data");
slouken@777
   384
        return MUS_NONE;
slouken@777
   385
    }
slouken@777
   386
    SDL_RWseek(src, -12, RW_SEEK_CUR);
slouken@777
   387
slouken@777
   388
    /* WAVE files have the magic four bytes "RIFF"
slouken@777
   389
       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
slouken@777
   390
    if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) ||
slouken@777
   391
        (SDL_memcmp(magic, "FORM", 4) == 0)) {
slouken@777
   392
        return MUS_WAV;
slouken@777
   393
    }
slouken@777
   394
slouken@777
   395
    return detect_music_type_from_magic(magic);
slouken@777
   396
}
slouken@777
   397
slouken@0
   398
/* Load a music file */
slouken@0
   399
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   400
{
slouken@777
   401
    int i;
slouken@777
   402
    void *context;
slouken@777
   403
    char *ext;
slouken@777
   404
    Mix_MusicType type;
slouken@625
   405
    SDL_RWops *src;
slouken@542
   406
slouken@777
   407
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   408
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   409
        if (!interface->opened || !interface->CreateFromFile) {
slouken@777
   410
            continue;
slouken@616
   411
        }
slouken@777
   412
slouken@777
   413
        context = interface->CreateFromFile(file);
slouken@777
   414
        if (context) {
slouken@777
   415
            /* Allocate memory for the music structure */
slouken@777
   416
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   417
            if (music == NULL) {
slouken@777
   418
                Mix_SetError("Out of memory");
slouken@777
   419
                return NULL;
slouken@777
   420
            }
slouken@777
   421
            music->interface = interface;
slouken@777
   422
            music->context = context;
slouken@777
   423
            return music;
slouken@616
   424
        }
slouken@616
   425
    }
slouken@542
   426
slouken@625
   427
    src = SDL_RWFromFile(file, "rb");
slouken@777
   428
    if (src == NULL) {
slouken@616
   429
        Mix_SetError("Couldn't open '%s'", file);
slouken@616
   430
        return NULL;
slouken@616
   431
    }
slouken@542
   432
slouken@616
   433
    /* Use the extension as a first guess on the file type */
slouken@616
   434
    type = MUS_NONE;
slouken@616
   435
    ext = strrchr(file, '.');
slouken@777
   436
    if (ext) {
slouken@616
   437
        ++ext; /* skip the dot in the extension */
slouken@777
   438
        if (SDL_strcasecmp(ext, "WAV") == 0) {
slouken@616
   439
            type = MUS_WAV;
slouken@777
   440
        } else if (SDL_strcasecmp(ext, "MID") == 0 ||
slouken@777
   441
                    SDL_strcasecmp(ext, "MIDI") == 0 ||
slouken@777
   442
                    SDL_strcasecmp(ext, "KAR") == 0) {
slouken@616
   443
            type = MUS_MID;
slouken@777
   444
        } else if (SDL_strcasecmp(ext, "OGG") == 0) {
slouken@616
   445
            type = MUS_OGG;
slouken@777
   446
        } else if (SDL_strcasecmp(ext, "FLAC") == 0) {
slouken@616
   447
            type = MUS_FLAC;
slouken@777
   448
        } else  if (SDL_strcasecmp(ext, "MPG") == 0 ||
slouken@777
   449
                     SDL_strcasecmp(ext, "MPEG") == 0 ||
slouken@777
   450
                     SDL_strcasecmp(ext, "MP3") == 0 ||
slouken@777
   451
                     SDL_strcasecmp(ext, "MAD") == 0) {
slouken@616
   452
            type = MUS_MP3;
slouken@777
   453
        } else if (SDL_strcasecmp(ext, "669") == 0 ||
slouken@777
   454
                    SDL_strcasecmp(ext, "AMF") == 0 ||
slouken@777
   455
                    SDL_strcasecmp(ext, "AMS") == 0 ||
slouken@777
   456
                    SDL_strcasecmp(ext, "DBM") == 0 ||
slouken@777
   457
                    SDL_strcasecmp(ext, "DSM") == 0 ||
slouken@777
   458
                    SDL_strcasecmp(ext, "FAR") == 0 ||
slouken@777
   459
                    SDL_strcasecmp(ext, "IT") == 0 ||
slouken@777
   460
                    SDL_strcasecmp(ext, "MED") == 0 ||
slouken@777
   461
                    SDL_strcasecmp(ext, "MDL") == 0 ||
slouken@777
   462
                    SDL_strcasecmp(ext, "MOD") == 0 ||
slouken@777
   463
                    SDL_strcasecmp(ext, "MOL") == 0 ||
slouken@777
   464
                    SDL_strcasecmp(ext, "MTM") == 0 ||
slouken@777
   465
                    SDL_strcasecmp(ext, "NST") == 0 ||
slouken@777
   466
                    SDL_strcasecmp(ext, "OKT") == 0 ||
slouken@777
   467
                    SDL_strcasecmp(ext, "PTM") == 0 ||
slouken@777
   468
                    SDL_strcasecmp(ext, "S3M") == 0 ||
slouken@777
   469
                    SDL_strcasecmp(ext, "STM") == 0 ||
slouken@777
   470
                    SDL_strcasecmp(ext, "ULT") == 0 ||
slouken@777
   471
                    SDL_strcasecmp(ext, "UMX") == 0 ||
slouken@777
   472
                    SDL_strcasecmp(ext, "WOW") == 0 ||
slouken@777
   473
                    SDL_strcasecmp(ext, "XM") == 0) {
slouken@777
   474
            type = MUS_MOD;
slouken@616
   475
        }
slouken@616
   476
    }
slouken@777
   477
    return Mix_LoadMUSType_RW(src, type, SDL_TRUE);
slouken@542
   478
}
slouken@542
   479
slouken@625
   480
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *src, int freesrc)
slouken@542
   481
{
slouken@625
   482
    return Mix_LoadMUSType_RW(src, MUS_NONE, freesrc);
slouken@542
   483
}
slouken@542
   484
slouken@625
   485
Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
slouken@542
   486
{
slouken@777
   487
    int i;
slouken@777
   488
    void *context;
slouken@625
   489
    Sint64 start;
slouken@0
   490
slouken@625
   491
    if (!src) {
slouken@616
   492
        Mix_SetError("RWops pointer is NULL");
slouken@616
   493
        return NULL;
slouken@616
   494
    }
slouken@625
   495
    start = SDL_RWtell(src);
slouken@542
   496
slouken@616
   497
    /* If the caller wants auto-detection, figure out what kind of file
slouken@616
   498
     * this is. */
slouken@616
   499
    if (type == MUS_NONE) {
slouken@625
   500
        if ((type = detect_music_type(src)) == MUS_NONE) {
slouken@777
   501
            /* Don't call Mix_SetError() since detect_music_type() does that. */
slouken@625
   502
            if (freesrc) {
slouken@625
   503
                SDL_RWclose(src);
slouken@625
   504
            }
slouken@616
   505
            return NULL;
slouken@616
   506
        }
slouken@616
   507
    }
slouken@125
   508
slouken@777
   509
    Mix_ClearError();
slouken@777
   510
slouken@777
   511
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   512
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   513
        if (!interface->opened || type != interface->type || !interface->CreateFromRW) {
slouken@777
   514
            continue;
slouken@625
   515
        }
slouken@0
   516
slouken@777
   517
        context = interface->CreateFromRW(src, freesrc);
slouken@777
   518
        if (context) {
slouken@777
   519
            /* Allocate memory for the music structure */
slouken@777
   520
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   521
            if (music == NULL) {
slouken@777
   522
                interface->Delete(context);
slouken@777
   523
                Mix_SetError("Out of memory");
slouken@777
   524
                return NULL;
slouken@777
   525
            }
slouken@777
   526
            music->interface = interface;
slouken@777
   527
            music->context = context;
slouken@777
   528
            return music;
slouken@616
   529
        }
slouken@777
   530
slouken@777
   531
        /* Reset the stream for the next decoder */
slouken@777
   532
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   533
    }
chewi@506
   534
slouken@777
   535
    if (!*Mix_GetError()) {
slouken@777
   536
        Mix_SetError("Unrecognized audio format");
slouken@616
   537
    }
slouken@777
   538
    if (freesrc) {
slouken@777
   539
        SDL_RWclose(src);
slouken@777
   540
    } else {
slouken@777
   541
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   542
    }
slouken@777
   543
    return NULL;
slouken@0
   544
}
slouken@0
   545
slouken@0
   546
/* Free a music chunk previously loaded */
slouken@0
   547
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   548
{
slouken@777
   549
    if (music) {
slouken@616
   550
        /* Stop the music if it's currently playing */
slouken@718
   551
        Mix_LockAudio();
slouken@777
   552
        if (music == music_playing) {
slouken@616
   553
            /* Wait for any fade out to finish */
slouken@777
   554
            while (music->fading == MIX_FADING_OUT) {
slouken@718
   555
                Mix_UnlockAudio();
slouken@616
   556
                SDL_Delay(100);
slouken@718
   557
                Mix_LockAudio();
slouken@616
   558
            }
slouken@777
   559
            if (music == music_playing) {
slouken@616
   560
                music_internal_halt();
slouken@616
   561
            }
slouken@616
   562
        }
slouken@718
   563
        Mix_UnlockAudio();
chewi@506
   564
slouken@777
   565
        music->interface->Delete(music->context);
slouken@616
   566
        SDL_free(music);
slouken@616
   567
    }
slouken@0
   568
}
slouken@0
   569
slouken@177
   570
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   571
   music, if 'music' is NULL.
slouken@177
   572
*/
slouken@177
   573
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   574
{
slouken@616
   575
    Mix_MusicType type = MUS_NONE;
slouken@177
   576
slouken@777
   577
    if (music) {
slouken@777
   578
        type = music->interface->type;
slouken@616
   579
    } else {
slouken@718
   580
        Mix_LockAudio();
slouken@777
   581
        if (music_playing) {
slouken@777
   582
            type = music_playing->interface->type;
slouken@616
   583
        }
slouken@718
   584
        Mix_UnlockAudio();
slouken@616
   585
    }
slouken@616
   586
    return(type);
slouken@177
   587
}
slouken@177
   588
slouken@173
   589
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   590
 */
slouken@797
   591
static int music_internal_play(Mix_Music *music, int play_count, double position)
slouken@0
   592
{
slouken@616
   593
    int retval = 0;
megastep@10
   594
slouken@777
   595
#if defined(__MACOSX__) && defined(MID_MUSIC_NATIVE)
slouken@616
   596
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   597
       can't really stop and restart MIDI from the audio callback.
slouken@616
   598
    */
slouken@777
   599
    if (music == music_playing && music->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   600
        /* Just a seek suffices to restart playing */
slouken@616
   601
        music_internal_position(position);
slouken@616
   602
        return 0;
slouken@616
   603
    }
slouken@531
   604
#endif
slouken@529
   605
slouken@616
   606
    /* Note the music we're playing */
slouken@777
   607
    if (music_playing) {
slouken@616
   608
        music_internal_halt();
slouken@616
   609
    }
slouken@616
   610
    music_playing = music;
slouken@777
   611
    music_playing->playing = SDL_TRUE;
slouken@173
   612
slouken@616
   613
    /* Set the initial volume */
slouken@777
   614
    music_internal_initialize_volume();
slouken@173
   615
slouken@616
   616
    /* Set up for playback */
slouken@797
   617
    retval = music->interface->Play(music->context, play_count);
slouken@173
   618
slouken@616
   619
    /* Set the playback position, note any errors if an offset is used */
slouken@777
   620
    if (retval == 0) {
slouken@777
   621
        if (position > 0.0) {
slouken@777
   622
            if (music_internal_position(position) < 0) {
slouken@616
   623
                Mix_SetError("Position not implemented for music type");
slouken@616
   624
                retval = -1;
slouken@616
   625
            }
slouken@616
   626
        } else {
slouken@616
   627
            music_internal_position(0.0);
slouken@616
   628
        }
slouken@616
   629
    }
slouken@173
   630
slouken@616
   631
    /* If the setup failed, we're not playing any music anymore */
slouken@777
   632
    if (retval < 0) {
slouken@797
   633
        music->playing = SDL_FALSE;
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@797
   671
    if (loops == 0) {
slouken@616
   672
        /* Loop is the number of times to play the audio */
slouken@797
   673
        loops = 1;
slouken@616
   674
    }
slouken@797
   675
    retval = music_internal_play(music, loops, 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@797
   834
    if (music_playing) {
slouken@797
   835
        if (music_playing->interface->Pause) {
slouken@797
   836
            music_playing->interface->Pause(music_playing->context);
slouken@797
   837
        }
slouken@777
   838
    }
slouken@797
   839
    music_active = SDL_FALSE;
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@797
   846
    if (music_playing) {
slouken@797
   847
        if (music_playing->interface->Resume) {
slouken@797
   848
            music_playing->interface->Resume(music_playing->context);
slouken@797
   849
        }
slouken@777
   850
    }
slouken@797
   851
    music_active = SDL_TRUE;
slouken@777
   852
    Mix_UnlockAudio();
slouken@0
   853
}
slouken@0
   854
slouken@0
   855
void Mix_RewindMusic(void)
slouken@0
   856
{
slouken@616
   857
    Mix_SetMusicPosition(0.0);
slouken@0
   858
}
slouken@0
   859
megastep@13
   860
int Mix_PausedMusic(void)
megastep@13
   861
{
slouken@797
   862
    return (music_active == SDL_FALSE);
megastep@13
   863
}
megastep@13
   864
slouken@0
   865
/* Check the status of the music */
slouken@779
   866
static SDL_bool music_internal_playing(void)
slouken@173
   867
{
slouken@797
   868
    if (!music_playing) {
slouken@777
   869
        return SDL_FALSE;
slouken@616
   870
    }
icculus@510
   871
slouken@777
   872
    if (music_playing->interface->IsPlaying) {
slouken@797
   873
        music_playing->playing = music_playing->interface->IsPlaying(music_playing->context);
slouken@616
   874
    }
slouken@777
   875
    return music_playing->playing;
slouken@173
   876
}
megastep@13
   877
int Mix_PlayingMusic(void)
slouken@0
   878
{
slouken@797
   879
    SDL_bool playing;
slouken@173
   880
slouken@718
   881
    Mix_LockAudio();
slouken@797
   882
    playing = music_internal_playing();
slouken@718
   883
    Mix_UnlockAudio();
slouken@173
   884
slouken@797
   885
    return playing ? 1 : 0;
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@801
   939
    if (soundfont_paths) {
slouken@801
   940
        SDL_free(soundfont_paths);
slouken@801
   941
        soundfont_paths = NULL;
slouken@801
   942
    }
slouken@801
   943
slouken@616
   944
    /* rcg06042009 report available decoders at runtime. */
slouken@801
   945
    if (music_decoders) {
slouken@801
   946
        SDL_free((void *)music_decoders);
slouken@801
   947
        music_decoders = NULL;
slouken@801
   948
    }
slouken@616
   949
    num_decoders = 0;
slouken@499
   950
slouken@616
   951
    ms_per_step = 0;
slouken@0
   952
}
slouken@0
   953
slouken@777
   954
/* Unload the music interface libraries */
slouken@777
   955
void unload_music(void)
slouken@777
   956
{
slouken@777
   957
    int i;
slouken@777
   958
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   959
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   960
        if (!interface || !interface->loaded) {
slouken@777
   961
            continue;
slouken@777
   962
        }
slouken@777
   963
slouken@777
   964
        if (interface->Unload) {
slouken@777
   965
            interface->Unload();
slouken@777
   966
        }
slouken@777
   967
        interface->loaded = SDL_FALSE;
slouken@777
   968
    }
slouken@777
   969
}
slouken@777
   970
chewi@506
   971
int Mix_SetSoundFonts(const char *paths)
chewi@506
   972
{
slouken@616
   973
    if (soundfont_paths) {
slouken@616
   974
        SDL_free(soundfont_paths);
slouken@616
   975
        soundfont_paths = NULL;
slouken@616
   976
    }
chewi@506
   977
slouken@616
   978
    if (paths) {
slouken@616
   979
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
   980
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
   981
            return 0;
slouken@616
   982
        }
slouken@616
   983
    }
slouken@616
   984
    return 1;
chewi@506
   985
}
chewi@506
   986
dimitris@508
   987
const char* Mix_GetSoundFonts(void)
chewi@506
   988
{
slouken@801
   989
    const char *env_paths = SDL_getenv("SDL_SOUNDFONTS");
slouken@801
   990
    SDL_bool force_env_paths = SDL_GetHintBoolean("SDL_FORCE_SOUNDFONTS", SDL_FALSE);
slouken@801
   991
    if (force_env_paths && (!env_paths || !*env_paths)) {
slouken@801
   992
        force_env_paths = SDL_FALSE;
slouken@801
   993
    }
slouken@801
   994
    if (soundfont_paths && *soundfont_paths && !force_env_paths) {
slouken@616
   995
        return soundfont_paths;
slouken@616
   996
    }
slouken@801
   997
    if (env_paths) {
slouken@801
   998
        return env_paths;
slouken@801
   999
    }
slouken@801
  1000
slouken@801
  1001
    /* We don't have any sound fonts set programmatically or in the environment
slouken@801
  1002
       Time to start guessing where they might be...
slouken@801
  1003
     */
slouken@801
  1004
    {
slouken@801
  1005
        static char *s_soundfont_paths[] = {
slouken@801
  1006
            "/usr/share/sounds/sf2/FluidR3_GM.sf2"  /* Remember to add ',' here */
slouken@801
  1007
        };
slouken@801
  1008
        unsigned i;
slouken@801
  1009
slouken@801
  1010
        for (i = 0; i < SDL_arraysize(s_soundfont_paths); ++i) {
slouken@801
  1011
            SDL_RWops *rwops = SDL_RWFromFile(s_soundfont_paths[i], "rb");
slouken@801
  1012
            if (rwops) {
slouken@801
  1013
                SDL_RWclose(rwops);
slouken@801
  1014
                return s_soundfont_paths[i];
slouken@801
  1015
            }
slouken@801
  1016
        }
slouken@801
  1017
    }
slouken@801
  1018
    return NULL;
chewi@506
  1019
}
chewi@506
  1020
chewi@506
  1021
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
  1022
{
slouken@616
  1023
    char *context, *path, *paths;
slouken@616
  1024
    const char* cpaths = Mix_GetSoundFonts();
slouken@762
  1025
    int soundfonts_found = 0;
chewi@506
  1026
slouken@616
  1027
    if (!cpaths) {
slouken@616
  1028
        Mix_SetError("No SoundFonts have been requested");
slouken@616
  1029
        return 0;
slouken@616
  1030
    }
chewi@506
  1031
slouken@616
  1032
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
  1033
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
  1034
        return 0;
slouken@616
  1035
    }
chewi@506
  1036
slouken@770
  1037
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
slouken@616
  1038
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1039
#elif defined(_WIN32)
slouken@616
  1040
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1041
#else
slouken@616
  1042
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1043
#endif
slouken@616
  1044
        if (!function(path, data)) {
slouken@762
  1045
            continue;
slouken@762
  1046
        } else {
slouken@762
  1047
            soundfonts_found++;
slouken@616
  1048
        }
slouken@616
  1049
    }
chewi@506
  1050
slouken@616
  1051
    SDL_free(paths);
slouken@762
  1052
    if (soundfonts_found > 0)
slouken@762
  1053
        return 1;
slouken@762
  1054
    else
slouken@762
  1055
        return 0;
chewi@506
  1056
}
slouken@777
  1057
slouken@777
  1058
/* vi: set ts=4 sw=4 expandtab: */