music.c
author Ozkan Sezer <sezeroz@gmail.com>
Tue, 07 Aug 2018 10:30:55 +0300
changeset 861 671a669e1065
parent 855 a7dee77dd60f
child 901 253f50984a9a
permissions -rw-r--r--
Mix_FadeInMusicPos(): fix bug #4216:

set music_active according to error returned by music_internal_play().

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