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