music.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Oct 2017 02:35:44 -0700
changeset 802 98ee7bc3833a
parent 801 46acd70caa6a
child 813 e52b611d3a6f
permissions -rw-r--r--
Added some debugging to test float audio with playmus and show the music interfaces being used.
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@802
   528
#ifdef DEBUG_MUSIC
slouken@802
   529
            /* This would be useful to expose via an API */
slouken@802
   530
            SDL_Log("Music playing with %s\n", interface->tag);
slouken@802
   531
#endif
slouken@777
   532
            return music;
slouken@616
   533
        }
slouken@777
   534
slouken@777
   535
        /* Reset the stream for the next decoder */
slouken@777
   536
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   537
    }
chewi@506
   538
slouken@777
   539
    if (!*Mix_GetError()) {
slouken@777
   540
        Mix_SetError("Unrecognized audio format");
slouken@616
   541
    }
slouken@777
   542
    if (freesrc) {
slouken@777
   543
        SDL_RWclose(src);
slouken@777
   544
    } else {
slouken@777
   545
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   546
    }
slouken@777
   547
    return NULL;
slouken@0
   548
}
slouken@0
   549
slouken@0
   550
/* Free a music chunk previously loaded */
slouken@0
   551
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   552
{
slouken@777
   553
    if (music) {
slouken@616
   554
        /* Stop the music if it's currently playing */
slouken@718
   555
        Mix_LockAudio();
slouken@777
   556
        if (music == music_playing) {
slouken@616
   557
            /* Wait for any fade out to finish */
slouken@777
   558
            while (music->fading == MIX_FADING_OUT) {
slouken@718
   559
                Mix_UnlockAudio();
slouken@616
   560
                SDL_Delay(100);
slouken@718
   561
                Mix_LockAudio();
slouken@616
   562
            }
slouken@777
   563
            if (music == music_playing) {
slouken@616
   564
                music_internal_halt();
slouken@616
   565
            }
slouken@616
   566
        }
slouken@718
   567
        Mix_UnlockAudio();
chewi@506
   568
slouken@777
   569
        music->interface->Delete(music->context);
slouken@616
   570
        SDL_free(music);
slouken@616
   571
    }
slouken@0
   572
}
slouken@0
   573
slouken@177
   574
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   575
   music, if 'music' is NULL.
slouken@177
   576
*/
slouken@177
   577
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   578
{
slouken@616
   579
    Mix_MusicType type = MUS_NONE;
slouken@177
   580
slouken@777
   581
    if (music) {
slouken@777
   582
        type = music->interface->type;
slouken@616
   583
    } else {
slouken@718
   584
        Mix_LockAudio();
slouken@777
   585
        if (music_playing) {
slouken@777
   586
            type = music_playing->interface->type;
slouken@616
   587
        }
slouken@718
   588
        Mix_UnlockAudio();
slouken@616
   589
    }
slouken@616
   590
    return(type);
slouken@177
   591
}
slouken@177
   592
slouken@173
   593
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   594
 */
slouken@797
   595
static int music_internal_play(Mix_Music *music, int play_count, double position)
slouken@0
   596
{
slouken@616
   597
    int retval = 0;
megastep@10
   598
slouken@777
   599
#if defined(__MACOSX__) && defined(MID_MUSIC_NATIVE)
slouken@616
   600
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   601
       can't really stop and restart MIDI from the audio callback.
slouken@616
   602
    */
slouken@777
   603
    if (music == music_playing && music->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   604
        /* Just a seek suffices to restart playing */
slouken@616
   605
        music_internal_position(position);
slouken@616
   606
        return 0;
slouken@616
   607
    }
slouken@531
   608
#endif
slouken@529
   609
slouken@616
   610
    /* Note the music we're playing */
slouken@777
   611
    if (music_playing) {
slouken@616
   612
        music_internal_halt();
slouken@616
   613
    }
slouken@616
   614
    music_playing = music;
slouken@777
   615
    music_playing->playing = SDL_TRUE;
slouken@173
   616
slouken@616
   617
    /* Set the initial volume */
slouken@777
   618
    music_internal_initialize_volume();
slouken@173
   619
slouken@616
   620
    /* Set up for playback */
slouken@797
   621
    retval = music->interface->Play(music->context, play_count);
slouken@173
   622
slouken@616
   623
    /* Set the playback position, note any errors if an offset is used */
slouken@777
   624
    if (retval == 0) {
slouken@777
   625
        if (position > 0.0) {
slouken@777
   626
            if (music_internal_position(position) < 0) {
slouken@616
   627
                Mix_SetError("Position not implemented for music type");
slouken@616
   628
                retval = -1;
slouken@616
   629
            }
slouken@616
   630
        } else {
slouken@616
   631
            music_internal_position(0.0);
slouken@616
   632
        }
slouken@616
   633
    }
slouken@173
   634
slouken@616
   635
    /* If the setup failed, we're not playing any music anymore */
slouken@777
   636
    if (retval < 0) {
slouken@797
   637
        music->playing = SDL_FALSE;
slouken@616
   638
        music_playing = NULL;
slouken@616
   639
    }
slouken@616
   640
    return(retval);
slouken@173
   641
}
slouken@777
   642
slouken@173
   643
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   644
{
slouken@616
   645
    int retval;
slouken@173
   646
slouken@777
   647
    if (ms_per_step == 0) {
slouken@616
   648
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   649
        return(-1);
slouken@616
   650
    }
slouken@499
   651
slouken@616
   652
    /* Don't play null pointers :-) */
slouken@777
   653
    if (music == NULL) {
slouken@616
   654
        Mix_SetError("music parameter was NULL");
slouken@616
   655
        return(-1);
slouken@616
   656
    }
slouken@173
   657
slouken@616
   658
    /* Setup the data */
slouken@777
   659
    if (ms) {
slouken@616
   660
        music->fading = MIX_FADING_IN;
slouken@616
   661
    } else {
slouken@616
   662
        music->fading = MIX_NO_FADING;
slouken@616
   663
    }
slouken@616
   664
    music->fade_step = 0;
slouken@616
   665
    music->fade_steps = ms/ms_per_step;
slouken@173
   666
slouken@616
   667
    /* Play the puppy */
slouken@718
   668
    Mix_LockAudio();
slouken@616
   669
    /* If the current music is fading out, wait for the fade to complete */
slouken@777
   670
    while (music_playing && (music_playing->fading == MIX_FADING_OUT)) {
slouken@718
   671
        Mix_UnlockAudio();
slouken@616
   672
        SDL_Delay(100);
slouken@718
   673
        Mix_LockAudio();
slouken@616
   674
    }
slouken@797
   675
    if (loops == 0) {
slouken@616
   676
        /* Loop is the number of times to play the audio */
slouken@797
   677
        loops = 1;
slouken@616
   678
    }
slouken@797
   679
    retval = music_internal_play(music, loops, position);
slouken@718
   680
    Mix_UnlockAudio();
slouken@173
   681
slouken@616
   682
    return(retval);
slouken@173
   683
}
slouken@173
   684
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   685
{
slouken@616
   686
    return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   687
}
slouken@173
   688
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   689
{
slouken@616
   690
    return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   691
}
megastep@10
   692
slouken@173
   693
/* Set the playing music position */
slouken@173
   694
int music_internal_position(double position)
megastep@10
   695
{
slouken@777
   696
    if (music_playing->interface->Seek) {
slouken@777
   697
        return music_playing->interface->Seek(music_playing->context, position);
slouken@616
   698
    }
slouken@777
   699
    return -1;
megastep@4
   700
}
slouken@155
   701
int Mix_SetMusicPosition(double position)
slouken@154
   702
{
slouken@616
   703
    int retval;
slouken@173
   704
slouken@718
   705
    Mix_LockAudio();
slouken@777
   706
    if (music_playing) {
slouken@616
   707
        retval = music_internal_position(position);
slouken@777
   708
        if (retval < 0) {
slouken@616
   709
            Mix_SetError("Position not implemented for music type");
slouken@616
   710
        }
slouken@616
   711
    } else {
slouken@616
   712
        Mix_SetError("Music isn't playing");
slouken@616
   713
        retval = -1;
slouken@616
   714
    }
slouken@718
   715
    Mix_UnlockAudio();
slouken@154
   716
slouken@616
   717
    return(retval);
slouken@154
   718
}
slouken@154
   719
icculus@237
   720
/* Set the music's initial volume */
icculus@237
   721
static void music_internal_initialize_volume(void)
icculus@237
   722
{
slouken@777
   723
    if (music_playing->fading == MIX_FADING_IN) {
slouken@616
   724
        music_internal_volume(0);
slouken@616
   725
    } else {
slouken@616
   726
        music_internal_volume(music_volume);
slouken@616
   727
    }
icculus@237
   728
}
icculus@237
   729
slouken@0
   730
/* Set the music volume */
slouken@173
   731
static void music_internal_volume(int volume)
slouken@173
   732
{
slouken@777
   733
    if (music_playing->interface->SetVolume) {
slouken@777
   734
        music_playing->interface->SetVolume(music_playing->context, volume);
slouken@616
   735
    }
slouken@173
   736
}
slouken@0
   737
int Mix_VolumeMusic(int volume)
slouken@0
   738
{
slouken@616
   739
    int prev_volume;
slouken@0
   740
slouken@616
   741
    prev_volume = music_volume;
slouken@777
   742
    if (volume < 0) {
slouken@616
   743
        return prev_volume;
slouken@616
   744
    }
slouken@777
   745
    if (volume > SDL_MIX_MAXVOLUME) {
slouken@616
   746
        volume = SDL_MIX_MAXVOLUME;
slouken@616
   747
    }
slouken@616
   748
    music_volume = volume;
slouken@718
   749
    Mix_LockAudio();
slouken@777
   750
    if (music_playing) {
slouken@616
   751
        music_internal_volume(music_volume);
slouken@616
   752
    }
slouken@718
   753
    Mix_UnlockAudio();
slouken@616
   754
    return(prev_volume);
slouken@0
   755
}
slouken@0
   756
slouken@173
   757
/* Halt playing of music */
slouken@173
   758
static void music_internal_halt(void)
megastep@7
   759
{
slouken@777
   760
    if (music_playing->interface->Stop) {
slouken@777
   761
        music_playing->interface->Stop(music_playing->context);
slouken@616
   762
    }
chewi@506
   763
slouken@777
   764
    music_playing->playing = SDL_FALSE;
slouken@616
   765
    music_playing->fading = MIX_NO_FADING;
slouken@616
   766
    music_playing = NULL;
megastep@7
   767
}
slouken@0
   768
int Mix_HaltMusic(void)
slouken@0
   769
{
slouken@718
   770
    Mix_LockAudio();
slouken@777
   771
    if (music_playing) {
slouken@616
   772
        music_internal_halt();
slouken@777
   773
        if (music_finished_hook) {
slouken@616
   774
            music_finished_hook();
slouken@616
   775
        }
slouken@616
   776
    }
slouken@718
   777
    Mix_UnlockAudio();
slouken@173
   778
slouken@616
   779
    return(0);
slouken@0
   780
}
slouken@0
   781
megastep@4
   782
/* Progressively stop the music */
megastep@4
   783
int Mix_FadeOutMusic(int ms)
megastep@4
   784
{
slouken@616
   785
    int retval = 0;
slouken@173
   786
slouken@777
   787
    if (ms_per_step == 0) {
slouken@616
   788
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   789
        return 0;
slouken@616
   790
    }
slouken@499
   791
slouken@616
   792
    if (ms <= 0) {  /* just halt immediately. */
slouken@616
   793
        Mix_HaltMusic();
slouken@616
   794
        return 1;
slouken@616
   795
    }
icculus@280
   796
slouken@718
   797
    Mix_LockAudio();
slouken@777
   798
    if (music_playing) {
slouken@777
   799
        int fade_steps = (ms + ms_per_step - 1) / ms_per_step;
slouken@777
   800
        if (music_playing->fading == MIX_NO_FADING) {
slouken@777
   801
            music_playing->fade_step = 0;
slouken@777
   802
        } else {
slouken@777
   803
            int step;
slouken@777
   804
            int old_fade_steps = music_playing->fade_steps;
slouken@777
   805
            if (music_playing->fading == MIX_FADING_OUT) {
slouken@777
   806
                step = music_playing->fade_step;
slouken@777
   807
            } else {
slouken@777
   808
                step = old_fade_steps - music_playing->fade_step + 1;
slouken@777
   809
            }
slouken@777
   810
            music_playing->fade_step = (step * fade_steps) / old_fade_steps;
slouken@777
   811
        }
slouken@616
   812
        music_playing->fading = MIX_FADING_OUT;
slouken@616
   813
        music_playing->fade_steps = fade_steps;
slouken@616
   814
        retval = 1;
slouken@616
   815
    }
slouken@718
   816
    Mix_UnlockAudio();
slouken@173
   817
slouken@616
   818
    return(retval);
megastep@4
   819
}
megastep@4
   820
megastep@4
   821
Mix_Fading Mix_FadingMusic(void)
megastep@4
   822
{
slouken@616
   823
    Mix_Fading fading = MIX_NO_FADING;
slouken@173
   824
slouken@718
   825
    Mix_LockAudio();
slouken@777
   826
    if (music_playing) {
slouken@616
   827
        fading = music_playing->fading;
slouken@616
   828
    }
slouken@718
   829
    Mix_UnlockAudio();
slouken@173
   830
slouken@616
   831
    return(fading);
megastep@4
   832
}
megastep@4
   833
slouken@0
   834
/* Pause/Resume the music stream */
slouken@0
   835
void Mix_PauseMusic(void)
slouken@0
   836
{
slouken@777
   837
    Mix_LockAudio();
slouken@797
   838
    if (music_playing) {
slouken@797
   839
        if (music_playing->interface->Pause) {
slouken@797
   840
            music_playing->interface->Pause(music_playing->context);
slouken@797
   841
        }
slouken@777
   842
    }
slouken@797
   843
    music_active = SDL_FALSE;
slouken@777
   844
    Mix_UnlockAudio();
slouken@0
   845
}
megastep@4
   846
slouken@0
   847
void Mix_ResumeMusic(void)
slouken@0
   848
{
slouken@777
   849
    Mix_LockAudio();
slouken@797
   850
    if (music_playing) {
slouken@797
   851
        if (music_playing->interface->Resume) {
slouken@797
   852
            music_playing->interface->Resume(music_playing->context);
slouken@797
   853
        }
slouken@777
   854
    }
slouken@797
   855
    music_active = SDL_TRUE;
slouken@777
   856
    Mix_UnlockAudio();
slouken@0
   857
}
slouken@0
   858
slouken@0
   859
void Mix_RewindMusic(void)
slouken@0
   860
{
slouken@616
   861
    Mix_SetMusicPosition(0.0);
slouken@0
   862
}
slouken@0
   863
megastep@13
   864
int Mix_PausedMusic(void)
megastep@13
   865
{
slouken@797
   866
    return (music_active == SDL_FALSE);
megastep@13
   867
}
megastep@13
   868
slouken@0
   869
/* Check the status of the music */
slouken@779
   870
static SDL_bool music_internal_playing(void)
slouken@173
   871
{
slouken@797
   872
    if (!music_playing) {
slouken@777
   873
        return SDL_FALSE;
slouken@616
   874
    }
icculus@510
   875
slouken@777
   876
    if (music_playing->interface->IsPlaying) {
slouken@797
   877
        music_playing->playing = music_playing->interface->IsPlaying(music_playing->context);
slouken@616
   878
    }
slouken@777
   879
    return music_playing->playing;
slouken@173
   880
}
megastep@13
   881
int Mix_PlayingMusic(void)
slouken@0
   882
{
slouken@797
   883
    SDL_bool playing;
slouken@173
   884
slouken@718
   885
    Mix_LockAudio();
slouken@797
   886
    playing = music_internal_playing();
slouken@718
   887
    Mix_UnlockAudio();
slouken@173
   888
slouken@797
   889
    return playing ? 1 : 0;
slouken@0
   890
}
slouken@0
   891
slouken@0
   892
/* Set the external music playback command */
slouken@0
   893
int Mix_SetMusicCMD(const char *command)
slouken@0
   894
{
slouken@616
   895
    Mix_HaltMusic();
slouken@777
   896
    if (music_cmd) {
slouken@616
   897
        SDL_free(music_cmd);
slouken@616
   898
        music_cmd = NULL;
slouken@616
   899
    }
slouken@777
   900
    if (command) {
slouken@777
   901
        size_t length = SDL_strlen(command) + 1;
slouken@777
   902
        music_cmd = (char *)SDL_malloc(length);
slouken@777
   903
        if (music_cmd == NULL) {
slouken@777
   904
            return SDL_OutOfMemory();
slouken@616
   905
        }
slouken@777
   906
        SDL_memcpy(music_cmd, command, length);
slouken@616
   907
    }
slouken@777
   908
    return 0;
slouken@0
   909
}
slouken@0
   910
slouken@154
   911
int Mix_SetSynchroValue(int i)
slouken@154
   912
{
slouken@616
   913
    /* Not supported by any players at this time */
slouken@616
   914
    return(-1);
slouken@154
   915
}
slouken@154
   916
slouken@154
   917
int Mix_GetSynchroValue(void)
slouken@154
   918
{
slouken@616
   919
    /* Not supported by any players at this time */
slouken@616
   920
    return(-1);
slouken@154
   921
}
slouken@154
   922
slouken@154
   923
slouken@777
   924
/* Uninitialize the music interfaces */
slouken@0
   925
void close_music(void)
slouken@0
   926
{
slouken@777
   927
    int i;
slouken@777
   928
slouken@616
   929
    Mix_HaltMusic();
slouken@777
   930
slouken@777
   931
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   932
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   933
        if (!interface || !interface->opened) {
slouken@777
   934
            continue;
slouken@777
   935
        }
slouken@777
   936
slouken@777
   937
        if (interface->Close) {
slouken@777
   938
            interface->Close();
slouken@777
   939
        }
slouken@777
   940
        interface->opened = SDL_FALSE;
slouken@777
   941
    }
icculus@390
   942
slouken@801
   943
    if (soundfont_paths) {
slouken@801
   944
        SDL_free(soundfont_paths);
slouken@801
   945
        soundfont_paths = NULL;
slouken@801
   946
    }
slouken@801
   947
slouken@616
   948
    /* rcg06042009 report available decoders at runtime. */
slouken@801
   949
    if (music_decoders) {
slouken@801
   950
        SDL_free((void *)music_decoders);
slouken@801
   951
        music_decoders = NULL;
slouken@801
   952
    }
slouken@616
   953
    num_decoders = 0;
slouken@499
   954
slouken@616
   955
    ms_per_step = 0;
slouken@0
   956
}
slouken@0
   957
slouken@777
   958
/* Unload the music interface libraries */
slouken@777
   959
void unload_music(void)
slouken@777
   960
{
slouken@777
   961
    int i;
slouken@777
   962
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   963
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   964
        if (!interface || !interface->loaded) {
slouken@777
   965
            continue;
slouken@777
   966
        }
slouken@777
   967
slouken@777
   968
        if (interface->Unload) {
slouken@777
   969
            interface->Unload();
slouken@777
   970
        }
slouken@777
   971
        interface->loaded = SDL_FALSE;
slouken@777
   972
    }
slouken@777
   973
}
slouken@777
   974
chewi@506
   975
int Mix_SetSoundFonts(const char *paths)
chewi@506
   976
{
slouken@616
   977
    if (soundfont_paths) {
slouken@616
   978
        SDL_free(soundfont_paths);
slouken@616
   979
        soundfont_paths = NULL;
slouken@616
   980
    }
chewi@506
   981
slouken@616
   982
    if (paths) {
slouken@616
   983
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
   984
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
   985
            return 0;
slouken@616
   986
        }
slouken@616
   987
    }
slouken@616
   988
    return 1;
chewi@506
   989
}
chewi@506
   990
dimitris@508
   991
const char* Mix_GetSoundFonts(void)
chewi@506
   992
{
slouken@801
   993
    const char *env_paths = SDL_getenv("SDL_SOUNDFONTS");
slouken@801
   994
    SDL_bool force_env_paths = SDL_GetHintBoolean("SDL_FORCE_SOUNDFONTS", SDL_FALSE);
slouken@801
   995
    if (force_env_paths && (!env_paths || !*env_paths)) {
slouken@801
   996
        force_env_paths = SDL_FALSE;
slouken@801
   997
    }
slouken@801
   998
    if (soundfont_paths && *soundfont_paths && !force_env_paths) {
slouken@616
   999
        return soundfont_paths;
slouken@616
  1000
    }
slouken@801
  1001
    if (env_paths) {
slouken@801
  1002
        return env_paths;
slouken@801
  1003
    }
slouken@801
  1004
slouken@801
  1005
    /* We don't have any sound fonts set programmatically or in the environment
slouken@801
  1006
       Time to start guessing where they might be...
slouken@801
  1007
     */
slouken@801
  1008
    {
slouken@801
  1009
        static char *s_soundfont_paths[] = {
slouken@801
  1010
            "/usr/share/sounds/sf2/FluidR3_GM.sf2"  /* Remember to add ',' here */
slouken@801
  1011
        };
slouken@801
  1012
        unsigned i;
slouken@801
  1013
slouken@801
  1014
        for (i = 0; i < SDL_arraysize(s_soundfont_paths); ++i) {
slouken@801
  1015
            SDL_RWops *rwops = SDL_RWFromFile(s_soundfont_paths[i], "rb");
slouken@801
  1016
            if (rwops) {
slouken@801
  1017
                SDL_RWclose(rwops);
slouken@801
  1018
                return s_soundfont_paths[i];
slouken@801
  1019
            }
slouken@801
  1020
        }
slouken@801
  1021
    }
slouken@801
  1022
    return NULL;
chewi@506
  1023
}
chewi@506
  1024
chewi@506
  1025
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
  1026
{
slouken@616
  1027
    char *context, *path, *paths;
slouken@616
  1028
    const char* cpaths = Mix_GetSoundFonts();
slouken@762
  1029
    int soundfonts_found = 0;
chewi@506
  1030
slouken@616
  1031
    if (!cpaths) {
slouken@616
  1032
        Mix_SetError("No SoundFonts have been requested");
slouken@616
  1033
        return 0;
slouken@616
  1034
    }
chewi@506
  1035
slouken@616
  1036
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
  1037
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
  1038
        return 0;
slouken@616
  1039
    }
chewi@506
  1040
slouken@770
  1041
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
slouken@616
  1042
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1043
#elif defined(_WIN32)
slouken@616
  1044
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1045
#else
slouken@616
  1046
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1047
#endif
slouken@616
  1048
        if (!function(path, data)) {
slouken@762
  1049
            continue;
slouken@762
  1050
        } else {
slouken@762
  1051
            soundfonts_found++;
slouken@616
  1052
        }
slouken@616
  1053
    }
chewi@506
  1054
slouken@616
  1055
    SDL_free(paths);
slouken@762
  1056
    if (soundfonts_found > 0)
slouken@762
  1057
        return 1;
slouken@762
  1058
    else
slouken@762
  1059
        return 0;
chewi@506
  1060
}
slouken@777
  1061
slouken@777
  1062
/* vi: set ts=4 sw=4 expandtab: */