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