music.c
author Ozkan Sezer <sezeroz@gmail.com>
Wed, 10 Oct 2018 17:56:10 +0300
changeset 901 253f50984a9a
parent 861 671a669e1065
child 926 d6c9518fb5ee
permissions -rw-r--r--
removed smpeg support completely.
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"
sezeroz@855
    39
#include "music_opus.h"
slouken@777
    40
#include "music_mpg123.h"
slouken@357
    41
#include "music_mad.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
sezeroz@855
    95
#ifdef MUSIC_OPUS
sezeroz@855
    96
    &Mix_MusicInterface_Opus,
sezeroz@855
    97
#endif
slouken@830
    98
#ifdef MUSIC_MP3_MPG123
slouken@830
    99
    &Mix_MusicInterface_MPG123,
slouken@830
   100
#endif
slouken@830
   101
#ifdef MUSIC_MP3_MAD
slouken@830
   102
    &Mix_MusicInterface_MAD,
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
    }
sezeroz@855
   357
    if (has_music(MUS_OPUS)) {
sezeroz@855
   358
        add_music_decoder("OPUS");
sezeroz@855
   359
        add_chunk_decoder("OPUS");
sezeroz@855
   360
    }
slouken@839
   361
    if (has_music(MUS_MP3)) {
slouken@839
   362
        add_music_decoder("MP3");
slouken@839
   363
        add_chunk_decoder("MP3");
slouken@839
   364
    }
slouken@839
   365
    if (has_music(MUS_FLAC)) {
slouken@839
   366
        add_music_decoder("FLAC");
slouken@839
   367
        add_chunk_decoder("FLAC");
slouken@839
   368
    }
slouken@839
   369
slouken@839
   370
    return (opened > 0) ? SDL_TRUE : SDL_FALSE;
slouken@839
   371
}
slouken@839
   372
slouken@839
   373
/* Initialize the music interfaces with a certain desired audio format */
slouken@839
   374
void open_music(const SDL_AudioSpec *spec)
slouken@839
   375
{
slouken@839
   376
#ifdef MIX_INIT_SOUNDFONT_PATHS
slouken@839
   377
    if (!soundfont_paths) {
slouken@839
   378
        soundfont_paths = SDL_strdup(MIX_INIT_SOUNDFONT_PATHS);
slouken@839
   379
    }
slouken@839
   380
#endif
slouken@839
   381
slouken@839
   382
    /* Load the music interfaces that don't have explicit initialization */
slouken@839
   383
    load_music_type(MUS_CMD);
slouken@839
   384
    load_music_type(MUS_WAV);
slouken@839
   385
slouken@839
   386
    /* Open all the interfaces that are loaded */
slouken@839
   387
    music_spec = *spec;
slouken@839
   388
    open_music_type(MUS_NONE);
slouken@839
   389
slouken@839
   390
    Mix_VolumeMusic(MIX_MAX_VOLUME);
slouken@839
   391
slouken@839
   392
    /* Calculate the number of ms for each callback */
slouken@839
   393
    ms_per_step = (int) (((float)spec->samples * 1000.0) / spec->freq);
slouken@777
   394
}
slouken@777
   395
slouken@777
   396
/* Return SDL_TRUE if the music type is available */
slouken@777
   397
SDL_bool has_music(Mix_MusicType type)
slouken@777
   398
{
slouken@777
   399
    int i;
slouken@777
   400
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   401
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   402
        if (interface->type != type) {
slouken@777
   403
            continue;
slouken@777
   404
        }
slouken@777
   405
        if (interface->opened) {
slouken@777
   406
            return SDL_TRUE;
slouken@777
   407
        }
slouken@777
   408
    }
slouken@777
   409
    return SDL_FALSE;
slouken@777
   410
}
slouken@777
   411
slouken@777
   412
Mix_MusicType detect_music_type_from_magic(const Uint8 *magic)
slouken@0
   413
{
slouken@616
   414
    /* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@777
   415
    if (SDL_memcmp(magic, "OggS", 4) == 0) {
slouken@616
   416
        return MUS_OGG;
slouken@616
   417
    }
slouken@542
   418
slouken@616
   419
    /* FLAC files have the magic four bytes "fLaC" */
slouken@777
   420
    if (SDL_memcmp(magic, "fLaC", 4) == 0) {
slouken@616
   421
        return MUS_FLAC;
slouken@616
   422
    }
slouken@542
   423
slouken@616
   424
    /* MIDI files have the magic four bytes "MThd" */
slouken@777
   425
    if (SDL_memcmp(magic, "MThd", 4) == 0) {
slouken@616
   426
        return MUS_MID;
slouken@616
   427
    }
slouken@542
   428
slouken@777
   429
    if (SDL_memcmp(magic, "ID3", 3) == 0 ||
slouken@777
   430
        (magic[0] == 0xFF && (magic[1] & 0xFE) == 0xFA)) {
slouken@616
   431
        return MUS_MP3;
slouken@616
   432
    }
slouken@542
   433
slouken@616
   434
    /* Assume MOD format.
slouken@616
   435
     *
slouken@616
   436
     * Apparently there is no way to check if the file is really a MOD,
slouken@616
   437
     * or there are too many formats supported by MikMod/ModPlug, or
slouken@616
   438
     * MikMod/ModPlug does this check by itself. */
slouken@616
   439
    return MUS_MOD;
slouken@542
   440
}
slouken@542
   441
slouken@777
   442
static Mix_MusicType detect_music_type(SDL_RWops *src)
slouken@777
   443
{
slouken@779
   444
    Uint8 magic[12];
sezeroz@855
   445
    Mix_MusicType t;
slouken@777
   446
slouken@777
   447
    if (SDL_RWread(src, magic, 1, 12) != 12) {
slouken@777
   448
        Mix_SetError("Couldn't read first 12 bytes of audio data");
slouken@777
   449
        return MUS_NONE;
slouken@777
   450
    }
slouken@777
   451
    SDL_RWseek(src, -12, RW_SEEK_CUR);
slouken@777
   452
slouken@777
   453
    /* WAVE files have the magic four bytes "RIFF"
slouken@777
   454
       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
slouken@777
   455
    if (((SDL_memcmp(magic, "RIFF", 4) == 0) && (SDL_memcmp((magic+8), "WAVE", 4) == 0)) ||
slouken@777
   456
        (SDL_memcmp(magic, "FORM", 4) == 0)) {
slouken@777
   457
        return MUS_WAV;
slouken@777
   458
    }
sezeroz@855
   459
    t = detect_music_type_from_magic(magic);
sezeroz@855
   460
    if (t == MUS_OGG) {
sezeroz@855
   461
        Sint64 pos = SDL_RWtell(src);
sezeroz@855
   462
        SDL_RWseek(src, 28, RW_SEEK_CUR);
sezeroz@855
   463
        SDL_RWread(src, magic, 1, 8);
sezeroz@855
   464
        SDL_RWseek(src, pos, RW_SEEK_SET);
sezeroz@855
   465
        if (SDL_memcmp(magic, "OpusHead", 8) == 0) {
sezeroz@855
   466
            return MUS_OPUS;
sezeroz@855
   467
        }
sezeroz@855
   468
    }
sezeroz@855
   469
    return t;
slouken@777
   470
}
slouken@777
   471
slouken@0
   472
/* Load a music file */
slouken@0
   473
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   474
{
slouken@777
   475
    int i;
slouken@777
   476
    void *context;
slouken@777
   477
    char *ext;
slouken@777
   478
    Mix_MusicType type;
slouken@625
   479
    SDL_RWops *src;
slouken@542
   480
slouken@777
   481
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
   482
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
   483
        if (!interface->opened || !interface->CreateFromFile) {
slouken@777
   484
            continue;
slouken@616
   485
        }
slouken@777
   486
slouken@777
   487
        context = interface->CreateFromFile(file);
slouken@777
   488
        if (context) {
slouken@777
   489
            /* Allocate memory for the music structure */
slouken@777
   490
            Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@777
   491
            if (music == NULL) {
slouken@777
   492
                Mix_SetError("Out of memory");
slouken@777
   493
                return NULL;
slouken@777
   494
            }
slouken@777
   495
            music->interface = interface;
slouken@777
   496
            music->context = context;
slouken@777
   497
            return music;
slouken@616
   498
        }
slouken@616
   499
    }
slouken@542
   500
slouken@625
   501
    src = SDL_RWFromFile(file, "rb");
slouken@777
   502
    if (src == NULL) {
slouken@616
   503
        Mix_SetError("Couldn't open '%s'", file);
slouken@616
   504
        return NULL;
slouken@616
   505
    }
slouken@542
   506
slouken@616
   507
    /* Use the extension as a first guess on the file type */
slouken@616
   508
    type = MUS_NONE;
slouken@616
   509
    ext = strrchr(file, '.');
slouken@777
   510
    if (ext) {
slouken@616
   511
        ++ext; /* skip the dot in the extension */
slouken@777
   512
        if (SDL_strcasecmp(ext, "WAV") == 0) {
slouken@616
   513
            type = MUS_WAV;
slouken@777
   514
        } else if (SDL_strcasecmp(ext, "MID") == 0 ||
slouken@777
   515
                    SDL_strcasecmp(ext, "MIDI") == 0 ||
slouken@777
   516
                    SDL_strcasecmp(ext, "KAR") == 0) {
slouken@616
   517
            type = MUS_MID;
slouken@777
   518
        } else if (SDL_strcasecmp(ext, "OGG") == 0) {
slouken@616
   519
            type = MUS_OGG;
sezeroz@855
   520
        } else if (SDL_strcasecmp(ext, "OPUS") == 0) {
sezeroz@855
   521
            type = MUS_OPUS;
slouken@777
   522
        } else if (SDL_strcasecmp(ext, "FLAC") == 0) {
slouken@616
   523
            type = MUS_FLAC;
slouken@777
   524
        } else  if (SDL_strcasecmp(ext, "MPG") == 0 ||
slouken@777
   525
                     SDL_strcasecmp(ext, "MPEG") == 0 ||
slouken@777
   526
                     SDL_strcasecmp(ext, "MP3") == 0 ||
slouken@777
   527
                     SDL_strcasecmp(ext, "MAD") == 0) {
slouken@616
   528
            type = MUS_MP3;
slouken@777
   529
        } else if (SDL_strcasecmp(ext, "669") == 0 ||
slouken@777
   530
                    SDL_strcasecmp(ext, "AMF") == 0 ||
slouken@777
   531
                    SDL_strcasecmp(ext, "AMS") == 0 ||
slouken@777
   532
                    SDL_strcasecmp(ext, "DBM") == 0 ||
slouken@777
   533
                    SDL_strcasecmp(ext, "DSM") == 0 ||
slouken@777
   534
                    SDL_strcasecmp(ext, "FAR") == 0 ||
slouken@777
   535
                    SDL_strcasecmp(ext, "IT") == 0 ||
slouken@777
   536
                    SDL_strcasecmp(ext, "MED") == 0 ||
slouken@777
   537
                    SDL_strcasecmp(ext, "MDL") == 0 ||
slouken@777
   538
                    SDL_strcasecmp(ext, "MOD") == 0 ||
slouken@777
   539
                    SDL_strcasecmp(ext, "MOL") == 0 ||
slouken@777
   540
                    SDL_strcasecmp(ext, "MTM") == 0 ||
slouken@777
   541
                    SDL_strcasecmp(ext, "NST") == 0 ||
slouken@777
   542
                    SDL_strcasecmp(ext, "OKT") == 0 ||
slouken@777
   543
                    SDL_strcasecmp(ext, "PTM") == 0 ||
slouken@777
   544
                    SDL_strcasecmp(ext, "S3M") == 0 ||
slouken@777
   545
                    SDL_strcasecmp(ext, "STM") == 0 ||
slouken@777
   546
                    SDL_strcasecmp(ext, "ULT") == 0 ||
slouken@777
   547
                    SDL_strcasecmp(ext, "UMX") == 0 ||
slouken@777
   548
                    SDL_strcasecmp(ext, "WOW") == 0 ||
slouken@777
   549
                    SDL_strcasecmp(ext, "XM") == 0) {
slouken@777
   550
            type = MUS_MOD;
slouken@616
   551
        }
slouken@616
   552
    }
slouken@777
   553
    return Mix_LoadMUSType_RW(src, type, SDL_TRUE);
slouken@542
   554
}
slouken@542
   555
slouken@625
   556
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *src, int freesrc)
slouken@542
   557
{
slouken@625
   558
    return Mix_LoadMUSType_RW(src, MUS_NONE, freesrc);
slouken@542
   559
}
slouken@542
   560
slouken@625
   561
Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
slouken@542
   562
{
slouken@777
   563
    int i;
slouken@777
   564
    void *context;
slouken@625
   565
    Sint64 start;
slouken@0
   566
slouken@625
   567
    if (!src) {
slouken@616
   568
        Mix_SetError("RWops pointer is NULL");
slouken@616
   569
        return NULL;
slouken@616
   570
    }
slouken@625
   571
    start = SDL_RWtell(src);
slouken@542
   572
slouken@616
   573
    /* If the caller wants auto-detection, figure out what kind of file
slouken@616
   574
     * this is. */
slouken@616
   575
    if (type == MUS_NONE) {
slouken@625
   576
        if ((type = detect_music_type(src)) == MUS_NONE) {
slouken@777
   577
            /* Don't call Mix_SetError() since detect_music_type() does that. */
slouken@625
   578
            if (freesrc) {
slouken@625
   579
                SDL_RWclose(src);
slouken@625
   580
            }
slouken@616
   581
            return NULL;
slouken@616
   582
        }
slouken@616
   583
    }
slouken@125
   584
slouken@777
   585
    Mix_ClearError();
slouken@777
   586
slouken@839
   587
    if (load_music_type(type) && open_music_type(type)) {
slouken@839
   588
        for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@839
   589
            Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@839
   590
            if (!interface->opened || type != interface->type || !interface->CreateFromRW) {
slouken@839
   591
                continue;
slouken@839
   592
            }
slouken@839
   593
slouken@839
   594
            context = interface->CreateFromRW(src, freesrc);
slouken@839
   595
            if (context) {
slouken@839
   596
                /* Allocate memory for the music structure */
slouken@839
   597
                Mix_Music *music = (Mix_Music *)SDL_calloc(1, sizeof(Mix_Music));
slouken@839
   598
                if (music == NULL) {
slouken@839
   599
                    interface->Delete(context);
slouken@839
   600
                    Mix_SetError("Out of memory");
slouken@839
   601
                    return NULL;
slouken@839
   602
                }
slouken@839
   603
                music->interface = interface;
slouken@839
   604
                music->context = context;
slouken@839
   605
slouken@839
   606
                if (SDL_GetHintBoolean(SDL_MIXER_HINT_DEBUG_MUSIC_INTERFACES, SDL_FALSE)) {
slouken@839
   607
                    SDL_Log("Loaded music with %s\n", interface->tag);
slouken@839
   608
                }
slouken@839
   609
                return music;
slouken@839
   610
            }
slouken@839
   611
slouken@839
   612
            /* Reset the stream for the next decoder */
slouken@839
   613
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@625
   614
        }
slouken@777
   615
    }
chewi@506
   616
slouken@777
   617
    if (!*Mix_GetError()) {
slouken@777
   618
        Mix_SetError("Unrecognized audio format");
slouken@616
   619
    }
slouken@777
   620
    if (freesrc) {
slouken@777
   621
        SDL_RWclose(src);
slouken@777
   622
    } else {
slouken@777
   623
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@777
   624
    }
slouken@777
   625
    return NULL;
slouken@0
   626
}
slouken@0
   627
slouken@0
   628
/* Free a music chunk previously loaded */
slouken@0
   629
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   630
{
slouken@777
   631
    if (music) {
slouken@616
   632
        /* Stop the music if it's currently playing */
slouken@718
   633
        Mix_LockAudio();
slouken@777
   634
        if (music == music_playing) {
slouken@616
   635
            /* Wait for any fade out to finish */
slouken@777
   636
            while (music->fading == MIX_FADING_OUT) {
slouken@718
   637
                Mix_UnlockAudio();
slouken@616
   638
                SDL_Delay(100);
slouken@718
   639
                Mix_LockAudio();
slouken@616
   640
            }
slouken@777
   641
            if (music == music_playing) {
slouken@616
   642
                music_internal_halt();
slouken@616
   643
            }
slouken@616
   644
        }
slouken@718
   645
        Mix_UnlockAudio();
chewi@506
   646
slouken@777
   647
        music->interface->Delete(music->context);
slouken@616
   648
        SDL_free(music);
slouken@616
   649
    }
slouken@0
   650
}
slouken@0
   651
slouken@177
   652
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   653
   music, if 'music' is NULL.
slouken@177
   654
*/
slouken@177
   655
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   656
{
slouken@616
   657
    Mix_MusicType type = MUS_NONE;
slouken@177
   658
slouken@777
   659
    if (music) {
slouken@777
   660
        type = music->interface->type;
slouken@616
   661
    } else {
slouken@718
   662
        Mix_LockAudio();
slouken@777
   663
        if (music_playing) {
slouken@777
   664
            type = music_playing->interface->type;
slouken@616
   665
        }
slouken@718
   666
        Mix_UnlockAudio();
slouken@616
   667
    }
slouken@616
   668
    return(type);
slouken@177
   669
}
slouken@177
   670
slouken@173
   671
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   672
 */
slouken@797
   673
static int music_internal_play(Mix_Music *music, int play_count, double position)
slouken@0
   674
{
slouken@616
   675
    int retval = 0;
megastep@10
   676
slouken@777
   677
#if defined(__MACOSX__) && defined(MID_MUSIC_NATIVE)
slouken@616
   678
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   679
       can't really stop and restart MIDI from the audio callback.
slouken@616
   680
    */
slouken@777
   681
    if (music == music_playing && music->api == MIX_MUSIC_NATIVEMIDI) {
slouken@616
   682
        /* Just a seek suffices to restart playing */
slouken@616
   683
        music_internal_position(position);
slouken@616
   684
        return 0;
slouken@616
   685
    }
slouken@531
   686
#endif
slouken@529
   687
slouken@616
   688
    /* Note the music we're playing */
slouken@777
   689
    if (music_playing) {
slouken@616
   690
        music_internal_halt();
slouken@616
   691
    }
slouken@616
   692
    music_playing = music;
slouken@777
   693
    music_playing->playing = SDL_TRUE;
slouken@173
   694
slouken@616
   695
    /* Set the initial volume */
slouken@777
   696
    music_internal_initialize_volume();
slouken@173
   697
slouken@616
   698
    /* Set up for playback */
slouken@797
   699
    retval = music->interface->Play(music->context, play_count);
slouken@173
   700
slouken@616
   701
    /* Set the playback position, note any errors if an offset is used */
slouken@777
   702
    if (retval == 0) {
slouken@777
   703
        if (position > 0.0) {
slouken@777
   704
            if (music_internal_position(position) < 0) {
slouken@616
   705
                Mix_SetError("Position not implemented for music type");
slouken@616
   706
                retval = -1;
slouken@616
   707
            }
slouken@616
   708
        } else {
slouken@616
   709
            music_internal_position(0.0);
slouken@616
   710
        }
slouken@616
   711
    }
slouken@173
   712
slouken@616
   713
    /* If the setup failed, we're not playing any music anymore */
slouken@777
   714
    if (retval < 0) {
slouken@797
   715
        music->playing = SDL_FALSE;
slouken@616
   716
        music_playing = NULL;
slouken@616
   717
    }
slouken@616
   718
    return(retval);
slouken@173
   719
}
slouken@777
   720
slouken@173
   721
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   722
{
slouken@616
   723
    int retval;
slouken@173
   724
slouken@777
   725
    if (ms_per_step == 0) {
slouken@616
   726
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   727
        return(-1);
slouken@616
   728
    }
slouken@499
   729
slouken@616
   730
    /* Don't play null pointers :-) */
slouken@777
   731
    if (music == NULL) {
slouken@616
   732
        Mix_SetError("music parameter was NULL");
slouken@616
   733
        return(-1);
slouken@616
   734
    }
slouken@173
   735
slouken@616
   736
    /* Setup the data */
slouken@777
   737
    if (ms) {
slouken@616
   738
        music->fading = MIX_FADING_IN;
slouken@616
   739
    } else {
slouken@616
   740
        music->fading = MIX_NO_FADING;
slouken@616
   741
    }
slouken@616
   742
    music->fade_step = 0;
slouken@616
   743
    music->fade_steps = ms/ms_per_step;
slouken@173
   744
slouken@616
   745
    /* Play the puppy */
slouken@718
   746
    Mix_LockAudio();
slouken@616
   747
    /* If the current music is fading out, wait for the fade to complete */
slouken@777
   748
    while (music_playing && (music_playing->fading == MIX_FADING_OUT)) {
slouken@718
   749
        Mix_UnlockAudio();
slouken@616
   750
        SDL_Delay(100);
slouken@718
   751
        Mix_LockAudio();
slouken@616
   752
    }
slouken@797
   753
    if (loops == 0) {
slouken@616
   754
        /* Loop is the number of times to play the audio */
slouken@797
   755
        loops = 1;
slouken@616
   756
    }
slouken@797
   757
    retval = music_internal_play(music, loops, position);
sezeroz@861
   758
    music_active = (retval == 0);
slouken@718
   759
    Mix_UnlockAudio();
slouken@173
   760
slouken@616
   761
    return(retval);
slouken@173
   762
}
slouken@173
   763
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   764
{
slouken@616
   765
    return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   766
}
slouken@173
   767
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   768
{
slouken@616
   769
    return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   770
}
megastep@10
   771
slouken@173
   772
/* Set the playing music position */
slouken@173
   773
int music_internal_position(double position)
megastep@10
   774
{
slouken@777
   775
    if (music_playing->interface->Seek) {
slouken@777
   776
        return music_playing->interface->Seek(music_playing->context, position);
slouken@616
   777
    }
slouken@777
   778
    return -1;
megastep@4
   779
}
slouken@155
   780
int Mix_SetMusicPosition(double position)
slouken@154
   781
{
slouken@616
   782
    int retval;
slouken@173
   783
slouken@718
   784
    Mix_LockAudio();
slouken@777
   785
    if (music_playing) {
slouken@616
   786
        retval = music_internal_position(position);
slouken@777
   787
        if (retval < 0) {
slouken@616
   788
            Mix_SetError("Position not implemented for music type");
slouken@616
   789
        }
slouken@616
   790
    } else {
slouken@616
   791
        Mix_SetError("Music isn't playing");
slouken@616
   792
        retval = -1;
slouken@616
   793
    }
slouken@718
   794
    Mix_UnlockAudio();
slouken@154
   795
slouken@616
   796
    return(retval);
slouken@154
   797
}
slouken@154
   798
icculus@237
   799
/* Set the music's initial volume */
icculus@237
   800
static void music_internal_initialize_volume(void)
icculus@237
   801
{
slouken@777
   802
    if (music_playing->fading == MIX_FADING_IN) {
slouken@616
   803
        music_internal_volume(0);
slouken@616
   804
    } else {
slouken@616
   805
        music_internal_volume(music_volume);
slouken@616
   806
    }
icculus@237
   807
}
icculus@237
   808
slouken@0
   809
/* Set the music volume */
slouken@173
   810
static void music_internal_volume(int volume)
slouken@173
   811
{
slouken@777
   812
    if (music_playing->interface->SetVolume) {
slouken@777
   813
        music_playing->interface->SetVolume(music_playing->context, volume);
slouken@616
   814
    }
slouken@173
   815
}
slouken@0
   816
int Mix_VolumeMusic(int volume)
slouken@0
   817
{
slouken@616
   818
    int prev_volume;
slouken@0
   819
slouken@616
   820
    prev_volume = music_volume;
slouken@777
   821
    if (volume < 0) {
slouken@616
   822
        return prev_volume;
slouken@616
   823
    }
slouken@777
   824
    if (volume > SDL_MIX_MAXVOLUME) {
slouken@616
   825
        volume = SDL_MIX_MAXVOLUME;
slouken@616
   826
    }
slouken@616
   827
    music_volume = volume;
slouken@718
   828
    Mix_LockAudio();
slouken@777
   829
    if (music_playing) {
slouken@616
   830
        music_internal_volume(music_volume);
slouken@616
   831
    }
slouken@718
   832
    Mix_UnlockAudio();
slouken@616
   833
    return(prev_volume);
slouken@0
   834
}
slouken@0
   835
slouken@173
   836
/* Halt playing of music */
slouken@173
   837
static void music_internal_halt(void)
megastep@7
   838
{
slouken@777
   839
    if (music_playing->interface->Stop) {
slouken@777
   840
        music_playing->interface->Stop(music_playing->context);
slouken@616
   841
    }
chewi@506
   842
slouken@777
   843
    music_playing->playing = SDL_FALSE;
slouken@616
   844
    music_playing->fading = MIX_NO_FADING;
slouken@616
   845
    music_playing = NULL;
megastep@7
   846
}
slouken@0
   847
int Mix_HaltMusic(void)
slouken@0
   848
{
slouken@718
   849
    Mix_LockAudio();
slouken@777
   850
    if (music_playing) {
slouken@616
   851
        music_internal_halt();
slouken@777
   852
        if (music_finished_hook) {
slouken@616
   853
            music_finished_hook();
slouken@616
   854
        }
slouken@616
   855
    }
slouken@718
   856
    Mix_UnlockAudio();
slouken@173
   857
slouken@616
   858
    return(0);
slouken@0
   859
}
slouken@0
   860
megastep@4
   861
/* Progressively stop the music */
megastep@4
   862
int Mix_FadeOutMusic(int ms)
megastep@4
   863
{
slouken@616
   864
    int retval = 0;
slouken@173
   865
slouken@777
   866
    if (ms_per_step == 0) {
slouken@616
   867
        SDL_SetError("Audio device hasn't been opened");
slouken@616
   868
        return 0;
slouken@616
   869
    }
slouken@499
   870
slouken@616
   871
    if (ms <= 0) {  /* just halt immediately. */
slouken@616
   872
        Mix_HaltMusic();
slouken@616
   873
        return 1;
slouken@616
   874
    }
icculus@280
   875
slouken@718
   876
    Mix_LockAudio();
slouken@777
   877
    if (music_playing) {
slouken@777
   878
        int fade_steps = (ms + ms_per_step - 1) / ms_per_step;
slouken@777
   879
        if (music_playing->fading == MIX_NO_FADING) {
slouken@777
   880
            music_playing->fade_step = 0;
slouken@777
   881
        } else {
slouken@777
   882
            int step;
slouken@777
   883
            int old_fade_steps = music_playing->fade_steps;
slouken@777
   884
            if (music_playing->fading == MIX_FADING_OUT) {
slouken@777
   885
                step = music_playing->fade_step;
slouken@777
   886
            } else {
slouken@777
   887
                step = old_fade_steps - music_playing->fade_step + 1;
slouken@777
   888
            }
slouken@777
   889
            music_playing->fade_step = (step * fade_steps) / old_fade_steps;
slouken@777
   890
        }
slouken@616
   891
        music_playing->fading = MIX_FADING_OUT;
slouken@616
   892
        music_playing->fade_steps = fade_steps;
slouken@616
   893
        retval = 1;
slouken@616
   894
    }
slouken@718
   895
    Mix_UnlockAudio();
slouken@173
   896
slouken@616
   897
    return(retval);
megastep@4
   898
}
megastep@4
   899
megastep@4
   900
Mix_Fading Mix_FadingMusic(void)
megastep@4
   901
{
slouken@616
   902
    Mix_Fading fading = MIX_NO_FADING;
slouken@173
   903
slouken@718
   904
    Mix_LockAudio();
slouken@777
   905
    if (music_playing) {
slouken@616
   906
        fading = music_playing->fading;
slouken@616
   907
    }
slouken@718
   908
    Mix_UnlockAudio();
slouken@173
   909
slouken@616
   910
    return(fading);
megastep@4
   911
}
megastep@4
   912
slouken@0
   913
/* Pause/Resume the music stream */
slouken@0
   914
void Mix_PauseMusic(void)
slouken@0
   915
{
slouken@777
   916
    Mix_LockAudio();
slouken@797
   917
    if (music_playing) {
slouken@797
   918
        if (music_playing->interface->Pause) {
slouken@797
   919
            music_playing->interface->Pause(music_playing->context);
slouken@797
   920
        }
slouken@777
   921
    }
slouken@797
   922
    music_active = SDL_FALSE;
slouken@777
   923
    Mix_UnlockAudio();
slouken@0
   924
}
megastep@4
   925
slouken@0
   926
void Mix_ResumeMusic(void)
slouken@0
   927
{
slouken@777
   928
    Mix_LockAudio();
slouken@797
   929
    if (music_playing) {
slouken@797
   930
        if (music_playing->interface->Resume) {
slouken@797
   931
            music_playing->interface->Resume(music_playing->context);
slouken@797
   932
        }
slouken@777
   933
    }
slouken@797
   934
    music_active = SDL_TRUE;
slouken@777
   935
    Mix_UnlockAudio();
slouken@0
   936
}
slouken@0
   937
slouken@0
   938
void Mix_RewindMusic(void)
slouken@0
   939
{
slouken@616
   940
    Mix_SetMusicPosition(0.0);
slouken@0
   941
}
slouken@0
   942
megastep@13
   943
int Mix_PausedMusic(void)
megastep@13
   944
{
slouken@797
   945
    return (music_active == SDL_FALSE);
megastep@13
   946
}
megastep@13
   947
slouken@0
   948
/* Check the status of the music */
slouken@779
   949
static SDL_bool music_internal_playing(void)
slouken@173
   950
{
slouken@797
   951
    if (!music_playing) {
slouken@777
   952
        return SDL_FALSE;
slouken@616
   953
    }
icculus@510
   954
slouken@777
   955
    if (music_playing->interface->IsPlaying) {
slouken@797
   956
        music_playing->playing = music_playing->interface->IsPlaying(music_playing->context);
slouken@616
   957
    }
slouken@777
   958
    return music_playing->playing;
slouken@173
   959
}
megastep@13
   960
int Mix_PlayingMusic(void)
slouken@0
   961
{
slouken@797
   962
    SDL_bool playing;
slouken@173
   963
slouken@718
   964
    Mix_LockAudio();
slouken@797
   965
    playing = music_internal_playing();
slouken@718
   966
    Mix_UnlockAudio();
slouken@173
   967
slouken@797
   968
    return playing ? 1 : 0;
slouken@0
   969
}
slouken@0
   970
slouken@0
   971
/* Set the external music playback command */
slouken@0
   972
int Mix_SetMusicCMD(const char *command)
slouken@0
   973
{
slouken@616
   974
    Mix_HaltMusic();
slouken@777
   975
    if (music_cmd) {
slouken@616
   976
        SDL_free(music_cmd);
slouken@616
   977
        music_cmd = NULL;
slouken@616
   978
    }
slouken@777
   979
    if (command) {
slouken@777
   980
        size_t length = SDL_strlen(command) + 1;
slouken@777
   981
        music_cmd = (char *)SDL_malloc(length);
slouken@777
   982
        if (music_cmd == NULL) {
slouken@777
   983
            return SDL_OutOfMemory();
slouken@616
   984
        }
slouken@777
   985
        SDL_memcpy(music_cmd, command, length);
slouken@616
   986
    }
slouken@777
   987
    return 0;
slouken@0
   988
}
slouken@0
   989
slouken@154
   990
int Mix_SetSynchroValue(int i)
slouken@154
   991
{
slouken@616
   992
    /* Not supported by any players at this time */
slouken@616
   993
    return(-1);
slouken@154
   994
}
slouken@154
   995
slouken@154
   996
int Mix_GetSynchroValue(void)
slouken@154
   997
{
slouken@616
   998
    /* Not supported by any players at this time */
slouken@616
   999
    return(-1);
slouken@154
  1000
}
slouken@154
  1001
slouken@154
  1002
slouken@777
  1003
/* Uninitialize the music interfaces */
slouken@0
  1004
void close_music(void)
slouken@0
  1005
{
slouken@777
  1006
    int i;
slouken@777
  1007
slouken@616
  1008
    Mix_HaltMusic();
slouken@777
  1009
slouken@777
  1010
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
  1011
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
  1012
        if (!interface || !interface->opened) {
slouken@777
  1013
            continue;
slouken@777
  1014
        }
slouken@777
  1015
slouken@777
  1016
        if (interface->Close) {
slouken@777
  1017
            interface->Close();
slouken@777
  1018
        }
slouken@777
  1019
        interface->opened = SDL_FALSE;
slouken@777
  1020
    }
icculus@390
  1021
slouken@801
  1022
    if (soundfont_paths) {
slouken@801
  1023
        SDL_free(soundfont_paths);
slouken@801
  1024
        soundfont_paths = NULL;
slouken@801
  1025
    }
slouken@801
  1026
slouken@616
  1027
    /* rcg06042009 report available decoders at runtime. */
slouken@801
  1028
    if (music_decoders) {
slouken@801
  1029
        SDL_free((void *)music_decoders);
slouken@801
  1030
        music_decoders = NULL;
slouken@801
  1031
    }
slouken@616
  1032
    num_decoders = 0;
slouken@499
  1033
slouken@616
  1034
    ms_per_step = 0;
slouken@0
  1035
}
slouken@0
  1036
slouken@777
  1037
/* Unload the music interface libraries */
slouken@777
  1038
void unload_music(void)
slouken@777
  1039
{
slouken@777
  1040
    int i;
slouken@777
  1041
    for (i = 0; i < SDL_arraysize(s_music_interfaces); ++i) {
slouken@777
  1042
        Mix_MusicInterface *interface = s_music_interfaces[i];
slouken@777
  1043
        if (!interface || !interface->loaded) {
slouken@777
  1044
            continue;
slouken@777
  1045
        }
slouken@777
  1046
slouken@777
  1047
        if (interface->Unload) {
slouken@777
  1048
            interface->Unload();
slouken@777
  1049
        }
slouken@777
  1050
        interface->loaded = SDL_FALSE;
slouken@777
  1051
    }
slouken@777
  1052
}
slouken@777
  1053
chewi@506
  1054
int Mix_SetSoundFonts(const char *paths)
chewi@506
  1055
{
slouken@616
  1056
    if (soundfont_paths) {
slouken@616
  1057
        SDL_free(soundfont_paths);
slouken@616
  1058
        soundfont_paths = NULL;
slouken@616
  1059
    }
chewi@506
  1060
slouken@616
  1061
    if (paths) {
slouken@616
  1062
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
  1063
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
  1064
            return 0;
slouken@616
  1065
        }
slouken@616
  1066
    }
slouken@616
  1067
    return 1;
chewi@506
  1068
}
chewi@506
  1069
dimitris@508
  1070
const char* Mix_GetSoundFonts(void)
chewi@506
  1071
{
slouken@801
  1072
    const char *env_paths = SDL_getenv("SDL_SOUNDFONTS");
slouken@801
  1073
    SDL_bool force_env_paths = SDL_GetHintBoolean("SDL_FORCE_SOUNDFONTS", SDL_FALSE);
slouken@801
  1074
    if (force_env_paths && (!env_paths || !*env_paths)) {
slouken@801
  1075
        force_env_paths = SDL_FALSE;
slouken@801
  1076
    }
slouken@801
  1077
    if (soundfont_paths && *soundfont_paths && !force_env_paths) {
slouken@616
  1078
        return soundfont_paths;
slouken@616
  1079
    }
slouken@801
  1080
    if (env_paths) {
slouken@801
  1081
        return env_paths;
slouken@801
  1082
    }
slouken@801
  1083
slouken@801
  1084
    /* We don't have any sound fonts set programmatically or in the environment
slouken@801
  1085
       Time to start guessing where they might be...
slouken@801
  1086
     */
slouken@801
  1087
    {
slouken@801
  1088
        static char *s_soundfont_paths[] = {
slouken@801
  1089
            "/usr/share/sounds/sf2/FluidR3_GM.sf2"  /* Remember to add ',' here */
slouken@801
  1090
        };
slouken@801
  1091
        unsigned i;
slouken@801
  1092
slouken@801
  1093
        for (i = 0; i < SDL_arraysize(s_soundfont_paths); ++i) {
slouken@801
  1094
            SDL_RWops *rwops = SDL_RWFromFile(s_soundfont_paths[i], "rb");
slouken@801
  1095
            if (rwops) {
slouken@801
  1096
                SDL_RWclose(rwops);
slouken@801
  1097
                return s_soundfont_paths[i];
slouken@801
  1098
            }
slouken@801
  1099
        }
slouken@801
  1100
    }
slouken@801
  1101
    return NULL;
chewi@506
  1102
}
chewi@506
  1103
sezero@817
  1104
int Mix_EachSoundFont(int (SDLCALL *function)(const char*, void*), void *data)
chewi@506
  1105
{
slouken@616
  1106
    char *context, *path, *paths;
slouken@616
  1107
    const char* cpaths = Mix_GetSoundFonts();
slouken@762
  1108
    int soundfonts_found = 0;
chewi@506
  1109
slouken@616
  1110
    if (!cpaths) {
slouken@616
  1111
        Mix_SetError("No SoundFonts have been requested");
slouken@616
  1112
        return 0;
slouken@616
  1113
    }
chewi@506
  1114
slouken@616
  1115
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
  1116
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
  1117
        return 0;
slouken@616
  1118
    }
chewi@506
  1119
slouken@770
  1120
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__)
slouken@616
  1121
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1122
#elif defined(_WIN32)
slouken@616
  1123
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1124
#else
slouken@616
  1125
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1126
#endif
slouken@616
  1127
        if (!function(path, data)) {
slouken@762
  1128
            continue;
slouken@762
  1129
        } else {
slouken@762
  1130
            soundfonts_found++;
slouken@616
  1131
        }
slouken@616
  1132
    }
chewi@506
  1133
slouken@616
  1134
    SDL_free(paths);
slouken@762
  1135
    if (soundfonts_found > 0)
slouken@762
  1136
        return 1;
slouken@762
  1137
    else
slouken@762
  1138
        return 0;
chewi@506
  1139
}
slouken@777
  1140
slouken@777
  1141
/* vi: set ts=4 sw=4 expandtab: */