music.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 29 Jan 2016 12:44:13 -0800
changeset 718 fb0562cc1559
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
permissions -rw-r--r--
Added Mix_OpenAudioDevice() so you can specify the audio device to open
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@711
     3
  Copyright (C) 1997-2016 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@0
    21
slouken@140
    22
/* $Id$ */
slouken@138
    23
slouken@3
    24
#include <stdlib.h>
slouken@3
    25
#include <string.h>
slouken@125
    26
#include <ctype.h>
icculus@280
    27
#include <assert.h>
slouken@24
    28
#include "SDL_endian.h"
slouken@24
    29
#include "SDL_audio.h"
slouken@29
    30
#include "SDL_timer.h"
slouken@0
    31
slouken@34
    32
#include "SDL_mixer.h"
slouken@718
    33
#include "mixer.h"
slouken@0
    34
slouken@0
    35
#ifdef CMD_MUSIC
slouken@0
    36
#include "music_cmd.h"
slouken@0
    37
#endif
slouken@0
    38
#ifdef WAV_MUSIC
slouken@0
    39
#include "wavestream.h"
slouken@0
    40
#endif
slouken@481
    41
#ifdef MODPLUG_MUSIC
slouken@481
    42
#include "music_modplug.h"
slouken@481
    43
#endif
slouken@411
    44
#ifdef MOD_MUSIC
slouken@411
    45
#include "music_mod.h"
slouken@0
    46
#endif
slouken@0
    47
#ifdef MID_MUSIC
slouken@106
    48
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    49
#    include "timidity.h"
slouken@106
    50
#  endif
chewi@506
    51
#  ifdef USE_FLUIDSYNTH_MIDI
chewi@506
    52
#    include "fluidsynth.h"
chewi@506
    53
#  endif
slouken@106
    54
#  ifdef USE_NATIVE_MIDI
slouken@106
    55
#    include "native_midi.h"
slouken@106
    56
#  endif
slouken@0
    57
#endif
slouken@63
    58
#ifdef OGG_MUSIC
slouken@63
    59
#include "music_ogg.h"
slouken@63
    60
#endif
slouken@0
    61
#ifdef MP3_MUSIC
slouken@314
    62
#include "dynamic_mp3.h"
slouken@357
    63
#endif
slouken@357
    64
#ifdef MP3_MAD_MUSIC
slouken@357
    65
#include "music_mad.h"
slouken@357
    66
#endif
slouken@382
    67
#ifdef FLAC_MUSIC
slouken@382
    68
#include "music_flac.h"
slouken@382
    69
#endif
slouken@0
    70
slouken@357
    71
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
    72
static SDL_AudioSpec used_mixer;
slouken@0
    73
#endif
slouken@0
    74
slouken@357
    75
slouken@42
    76
int volatile music_active = 1;
slouken@42
    77
static int volatile music_stopped = 0;
slouken@0
    78
static int music_loops = 0;
slouken@0
    79
static char *music_cmd = NULL;
slouken@42
    80
static Mix_Music * volatile music_playing = NULL;
slouken@17
    81
static int music_volume = MIX_MAX_VOLUME;
megastep@4
    82
slouken@0
    83
struct _Mix_Music {
slouken@616
    84
    Mix_MusicType type;
slouken@616
    85
    union {
slouken@0
    86
#ifdef CMD_MUSIC
slouken@616
    87
        MusicCMD *cmd;
slouken@0
    88
#endif
slouken@0
    89
#ifdef WAV_MUSIC
slouken@616
    90
        WAVStream *wave;
slouken@0
    91
#endif
slouken@481
    92
#ifdef MODPLUG_MUSIC
slouken@616
    93
        modplug_data *modplug;
slouken@481
    94
#endif
slouken@411
    95
#ifdef MOD_MUSIC
slouken@616
    96
        struct MODULE *module;
slouken@0
    97
#endif
slouken@0
    98
#ifdef MID_MUSIC
slouken@106
    99
#ifdef USE_TIMIDITY_MIDI
slouken@616
   100
        MidiSong *midi;
slouken@106
   101
#endif
chewi@506
   102
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
   103
        FluidSynthMidiSong *fluidsynthmidi;
chewi@506
   104
#endif
slouken@98
   105
#ifdef USE_NATIVE_MIDI
slouken@616
   106
        NativeMidiSong *nativemidi;
slouken@98
   107
#endif
slouken@0
   108
#endif
slouken@63
   109
#ifdef OGG_MUSIC
slouken@616
   110
        OGG_music *ogg;
slouken@63
   111
#endif
slouken@0
   112
#ifdef MP3_MUSIC
slouken@616
   113
        SMPEG *mp3;
slouken@0
   114
#endif
slouken@357
   115
#ifdef MP3_MAD_MUSIC
slouken@616
   116
        mad_data *mp3_mad;
slouken@357
   117
#endif
slouken@382
   118
#ifdef FLAC_MUSIC
slouken@616
   119
        FLAC_music *flac;
slouken@382
   120
#endif
slouken@616
   121
    } data;
slouken@616
   122
    Mix_Fading fading;
slouken@616
   123
    int fade_step;
slouken@616
   124
    int fade_steps;
slouken@616
   125
    int error;
slouken@0
   126
};
slouken@29
   127
#ifdef MID_MUSIC
slouken@106
   128
#ifdef USE_TIMIDITY_MIDI
slouken@0
   129
static int timidity_ok;
slouken@142
   130
static int samplesize;
slouken@106
   131
#endif
chewi@506
   132
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   133
static int fluidsynth_ok;
chewi@506
   134
#endif
slouken@98
   135
#ifdef USE_NATIVE_MIDI
slouken@98
   136
static int native_midi_ok;
slouken@98
   137
#endif
slouken@29
   138
#endif
slouken@0
   139
slouken@17
   140
/* Used to calculate fading steps */
slouken@17
   141
static int ms_per_step;
slouken@17
   142
icculus@390
   143
/* rcg06042009 report available decoders at runtime. */
icculus@390
   144
static const char **music_decoders = NULL;
icculus@390
   145
static int num_decoders = 0;
icculus@390
   146
chewi@506
   147
/* Semicolon-separated SoundFont paths */
chewi@506
   148
#ifdef MID_MUSIC
chewi@506
   149
char* soundfont_paths = NULL;
chewi@506
   150
#endif
chewi@506
   151
slouken@466
   152
int Mix_GetNumMusicDecoders(void)
icculus@390
   153
{
slouken@616
   154
    return(num_decoders);
icculus@390
   155
}
icculus@390
   156
icculus@390
   157
const char *Mix_GetMusicDecoder(int index)
icculus@390
   158
{
slouken@616
   159
    if ((index < 0) || (index >= num_decoders)) {
slouken@616
   160
        return NULL;
slouken@616
   161
    }
slouken@616
   162
    return(music_decoders[index]);
icculus@390
   163
}
icculus@390
   164
icculus@390
   165
static void add_music_decoder(const char *decoder)
icculus@390
   166
{
slouken@644
   167
    void *ptr = SDL_realloc((void *)music_decoders, (num_decoders + 1) * sizeof (const char *));
slouken@616
   168
    if (ptr == NULL) {
slouken@616
   169
        return;  /* oh well, go on without it. */
slouken@616
   170
    }
slouken@616
   171
    music_decoders = (const char **) ptr;
slouken@616
   172
    music_decoders[num_decoders++] = decoder;
icculus@390
   173
}
icculus@390
   174
megastep@10
   175
/* Local low-level functions prototypes */
icculus@237
   176
static void music_internal_initialize_volume(void);
slouken@173
   177
static void music_internal_volume(int volume);
slouken@173
   178
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   179
static int  music_internal_position(double position);
slouken@173
   180
static int  music_internal_playing();
slouken@173
   181
static void music_internal_halt(void);
megastep@7
   182
slouken@29
   183
slouken@29
   184
/* Support for hooking when the music has finished */
slouken@29
   185
static void (*music_finished_hook)(void) = NULL;
slouken@29
   186
slouken@29
   187
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   188
{
slouken@718
   189
    Mix_LockAudio();
slouken@616
   190
    music_finished_hook = music_finished;
slouken@718
   191
    Mix_UnlockAudio();
slouken@29
   192
}
slouken@29
   193
slouken@29
   194
icculus@281
   195
/* If music isn't playing, halt it if no looping is required, restart it */
slouken@625
   196
/* othesrchise. NOP if the music is playing */
icculus@281
   197
static int music_halt_or_loop (void)
icculus@281
   198
{
slouken@616
   199
    /* Restart music if it has to loop */
slouken@616
   200
slouken@616
   201
    if (!music_internal_playing())
slouken@616
   202
    {
slouken@541
   203
#ifdef USE_NATIVE_MIDI
slouken@616
   204
        /* Native MIDI handles looping internally */
slouken@616
   205
        if (music_playing->type == MUS_MID && native_midi_ok) {
slouken@616
   206
            music_loops = 0;
slouken@616
   207
        }
slouken@541
   208
#endif
slouken@533
   209
slouken@616
   210
        /* Restart music if it has to loop at a high level */
slouken@616
   211
        if (music_loops)
slouken@616
   212
        {
slouken@616
   213
            Mix_Fading current_fade;
slouken@648
   214
            if (music_loops > 0) {
slouken@648
   215
                --music_loops;
slouken@648
   216
            }
slouken@616
   217
            current_fade = music_playing->fading;
slouken@616
   218
            music_internal_play(music_playing, 0.0);
slouken@616
   219
            music_playing->fading = current_fade;
slouken@616
   220
        }
slouken@616
   221
        else
slouken@616
   222
        {
slouken@616
   223
            music_internal_halt();
slouken@616
   224
            if (music_finished_hook)
slouken@616
   225
                music_finished_hook();
slouken@616
   226
slouken@616
   227
            return 0;
slouken@616
   228
        }
slouken@616
   229
    }
slouken@616
   230
slouken@616
   231
    return 1;
icculus@281
   232
}
icculus@281
   233
icculus@281
   234
icculus@281
   235
slouken@0
   236
/* Mixing function */
slouken@0
   237
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   238
{
slouken@616
   239
    int left = 0;
slouken@407
   240
slouken@616
   241
    if ( music_playing && music_active ) {
slouken@616
   242
        /* Handle fading */
slouken@616
   243
        if ( music_playing->fading != MIX_NO_FADING ) {
slouken@616
   244
            if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@616
   245
                int volume;
slouken@616
   246
                int fade_step = music_playing->fade_step;
slouken@616
   247
                int fade_steps = music_playing->fade_steps;
slouken@17
   248
slouken@616
   249
                if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@616
   250
                    volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@616
   251
                } else { /* Fading in */
slouken@616
   252
                    volume = (music_volume * fade_step) / fade_steps;
slouken@616
   253
                }
slouken@616
   254
                music_internal_volume(volume);
slouken@616
   255
            } else {
slouken@616
   256
                if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@616
   257
                    music_internal_halt();
slouken@616
   258
                    if ( music_finished_hook ) {
slouken@616
   259
                        music_finished_hook();
slouken@616
   260
                    }
slouken@616
   261
                    return;
slouken@616
   262
                }
slouken@616
   263
                music_playing->fading = MIX_NO_FADING;
slouken@616
   264
            }
slouken@616
   265
        }
slouken@528
   266
slouken@616
   267
        music_halt_or_loop();
slouken@616
   268
        if (!music_internal_playing())
slouken@616
   269
            return;
slouken@616
   270
slouken@616
   271
        switch (music_playing->type) {
slouken@0
   272
#ifdef CMD_MUSIC
slouken@616
   273
            case MUS_CMD:
slouken@616
   274
                /* The playing is done externally */
slouken@616
   275
                break;
slouken@0
   276
#endif
slouken@0
   277
#ifdef WAV_MUSIC
slouken@616
   278
            case MUS_WAV:
slouken@616
   279
                left = WAVStream_PlaySome(stream, len);
slouken@616
   280
                break;
slouken@0
   281
#endif
slouken@481
   282
#ifdef MODPLUG_MUSIC
slouken@616
   283
            case MUS_MODPLUG:
slouken@616
   284
                left = modplug_playAudio(music_playing->data.modplug, stream, len);
slouken@616
   285
                break;
slouken@481
   286
#endif
slouken@411
   287
#ifdef MOD_MUSIC
slouken@616
   288
            case MUS_MOD:
slouken@616
   289
                left = MOD_playAudio(music_playing->data.module, stream, len);
slouken@616
   290
                break;
slouken@0
   291
#endif
slouken@0
   292
#ifdef MID_MUSIC
slouken@616
   293
            case MUS_MID:
slouken@529
   294
#ifdef USE_NATIVE_MIDI
slouken@616
   295
                if ( native_midi_ok ) {
slouken@616
   296
                    /* Native midi is handled asynchronously */
slouken@616
   297
                    goto skip;
slouken@616
   298
                }
slouken@529
   299
#endif
chewi@506
   300
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
   301
                if ( fluidsynth_ok ) {
slouken@616
   302
                    fluidsynth_playsome(music_playing->data.fluidsynthmidi, stream, len);
slouken@616
   303
                    goto skip;
slouken@616
   304
                }
chewi@506
   305
#endif
slouken@106
   306
#ifdef USE_TIMIDITY_MIDI
slouken@616
   307
                if ( timidity_ok ) {
slouken@616
   308
                    int samples = len / samplesize;
slouken@616
   309
                    Timidity_PlaySome(stream, samples);
slouken@616
   310
                    goto skip;
slouken@616
   311
                }
chewi@506
   312
#endif
slouken@616
   313
                break;
slouken@0
   314
#endif
slouken@63
   315
#ifdef OGG_MUSIC
slouken@616
   316
            case MUS_OGG:
slouken@616
   317
slouken@616
   318
                left = OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@616
   319
                break;
slouken@63
   320
#endif
slouken@382
   321
#ifdef FLAC_MUSIC
slouken@616
   322
            case MUS_FLAC:
slouken@616
   323
                left = FLAC_playAudio(music_playing->data.flac, stream, len);
slouken@616
   324
                break;
slouken@382
   325
#endif
slouken@0
   326
#ifdef MP3_MUSIC
slouken@616
   327
            case MUS_MP3:
slouken@616
   328
                left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
slouken@616
   329
                break;
slouken@0
   330
#endif
slouken@357
   331
#ifdef MP3_MAD_MUSIC
slouken@616
   332
            case MUS_MP3_MAD:
slouken@616
   333
                left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
slouken@616
   334
                break;
slouken@357
   335
#endif
slouken@616
   336
            default:
slouken@616
   337
                /* Unknown music type?? */
slouken@616
   338
                break;
slouken@616
   339
        }
slouken@616
   340
    }
slouken@407
   341
slouken@528
   342
skip:
slouken@616
   343
    /* Handle seamless music looping */
slouken@616
   344
    if (left > 0 && left < len) {
slouken@616
   345
        music_halt_or_loop();
slouken@616
   346
        if (music_internal_playing())
slouken@616
   347
            music_mixer(udata, stream+(len-left), left);
slouken@616
   348
    }
slouken@0
   349
}
slouken@0
   350
slouken@0
   351
/* Initialize the music players with a certain desired audio format */
slouken@0
   352
int open_music(SDL_AudioSpec *mixer)
slouken@0
   353
{
slouken@0
   354
#ifdef WAV_MUSIC
slouken@616
   355
    if ( WAVStream_Init(mixer) == 0 ) {
slouken@616
   356
        add_music_decoder("WAVE");
slouken@616
   357
    }
slouken@0
   358
#endif
slouken@481
   359
#ifdef MODPLUG_MUSIC
slouken@616
   360
    if ( modplug_init(mixer) == 0 ) {
slouken@616
   361
        add_music_decoder("MODPLUG");
slouken@616
   362
    }
slouken@481
   363
#endif
slouken@411
   364
#ifdef MOD_MUSIC
slouken@616
   365
    if ( MOD_init(mixer) == 0 ) {
slouken@616
   366
        add_music_decoder("MIKMOD");
slouken@616
   367
    }
slouken@0
   368
#endif
slouken@0
   369
#ifdef MID_MUSIC
slouken@106
   370
#ifdef USE_TIMIDITY_MIDI
slouken@616
   371
    samplesize = mixer->size / mixer->samples;
slouken@616
   372
    if ( Timidity_Init(mixer->freq, mixer->format,
slouken@616
   373
                        mixer->channels, mixer->samples) == 0 ) {
slouken@616
   374
        timidity_ok = 1;
slouken@616
   375
        add_music_decoder("TIMIDITY");
slouken@616
   376
    } else {
slouken@616
   377
        timidity_ok = 0;
slouken@616
   378
    }
slouken@106
   379
#endif
chewi@506
   380
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
   381
    if ( fluidsynth_init(mixer) == 0 ) {
slouken@616
   382
        fluidsynth_ok = 1;
slouken@616
   383
        add_music_decoder("FLUIDSYNTH");
slouken@616
   384
    } else {
slouken@616
   385
        fluidsynth_ok = 0;
slouken@616
   386
    }
chewi@506
   387
#endif
slouken@100
   388
#ifdef USE_NATIVE_MIDI
chewi@506
   389
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
   390
    native_midi_ok = !fluidsynth_ok;
slouken@616
   391
    if ( native_midi_ok )
chewi@506
   392
#endif
slouken@106
   393
#ifdef USE_TIMIDITY_MIDI
slouken@616
   394
        native_midi_ok = !timidity_ok;
slouken@616
   395
    if ( !native_midi_ok ) {
slouken@616
   396
        native_midi_ok = (getenv("SDL_NATIVE_MUSIC") != NULL);
slouken@616
   397
    }
slouken@616
   398
    if ( native_midi_ok )
slouken@106
   399
#endif
slouken@616
   400
        native_midi_ok = native_midi_detect();
slouken@616
   401
    if ( native_midi_ok )
slouken@616
   402
        add_music_decoder("NATIVEMIDI");
slouken@100
   403
#endif
slouken@0
   404
#endif
slouken@63
   405
#ifdef OGG_MUSIC
slouken@616
   406
    if ( OGG_init(mixer) == 0 ) {
slouken@616
   407
        add_music_decoder("OGG");
slouken@616
   408
    }
slouken@63
   409
#endif
slouken@382
   410
#ifdef FLAC_MUSIC
slouken@616
   411
    if ( FLAC_init(mixer) == 0 ) {
slouken@616
   412
        add_music_decoder("FLAC");
slouken@616
   413
    }
slouken@382
   414
#endif
slouken@357
   415
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@616
   416
    /* Keep a copy of the mixer */
slouken@616
   417
    used_mixer = *mixer;
slouken@616
   418
    add_music_decoder("MP3");
slouken@0
   419
#endif
icculus@390
   420
slouken@616
   421
    music_playing = NULL;
slouken@616
   422
    music_stopped = 0;
slouken@616
   423
    Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   424
slouken@616
   425
    /* Calculate the number of ms for each callback */
slouken@616
   426
    ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   427
slouken@616
   428
    return(0);
slouken@0
   429
}
slouken@0
   430
slouken@125
   431
/* Portable case-insensitive string compare function */
slouken@125
   432
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   433
{
slouken@616
   434
    while ( *str1 && *str2 ) {
slouken@616
   435
        if ( toupper((unsigned char)*str1) !=
slouken@616
   436
             toupper((unsigned char)*str2) )
slouken@616
   437
            break;
slouken@616
   438
        ++str1;
slouken@616
   439
        ++str2;
slouken@616
   440
    }
slouken@616
   441
    return (!*str1 && !*str2);
slouken@125
   442
}
slouken@125
   443
slouken@542
   444
static int detect_mp3(Uint8 *magic)
slouken@542
   445
{
slouken@616
   446
    if ( strncmp((char *)magic, "ID3", 3) == 0 ) {
slouken@616
   447
        return 1;
slouken@616
   448
    }
slouken@542
   449
slouken@616
   450
    /* Detection code lifted from SMPEG */
slouken@616
   451
    if(((magic[0] & 0xff) != 0xff) || // No sync bits
slouken@616
   452
       ((magic[1] & 0xf0) != 0xf0) || //
slouken@616
   453
       ((magic[2] & 0xf0) == 0x00) || // Bitrate is 0
slouken@616
   454
       ((magic[2] & 0xf0) == 0xf0) || // Bitrate is 15
slouken@616
   455
       ((magic[2] & 0x0c) == 0x0c) || // Frequency is 3
slouken@616
   456
       ((magic[1] & 0x06) == 0x00)) { // Layer is 4
slouken@616
   457
        return(0);
slouken@616
   458
    }
slouken@616
   459
    return 1;
slouken@542
   460
}
slouken@542
   461
slouken@542
   462
/* MUS_MOD can't be auto-detected. If no other format was detected, MOD is
slouken@542
   463
 * assumed and MUS_MOD will be returned, meaning that the format might not
slouken@542
   464
 * actually be MOD-based.
slouken@542
   465
 *
slouken@542
   466
 * Returns MUS_NONE in case of errors. */
slouken@625
   467
static Mix_MusicType detect_music_type(SDL_RWops *src)
slouken@542
   468
{
slouken@616
   469
    Uint8 magic[5];
slouken@616
   470
    Uint8 moremagic[9];
slouken@542
   471
slouken@625
   472
    Sint64 start = SDL_RWtell(src);
slouken@625
   473
    if (SDL_RWread(src, magic, 1, 4) != 4 || SDL_RWread(src, moremagic, 1, 8) != 8 ) {
slouken@616
   474
        Mix_SetError("Couldn't read from RWops");
slouken@616
   475
        return MUS_NONE;
slouken@616
   476
    }
slouken@625
   477
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@616
   478
    magic[4]='\0';
slouken@616
   479
    moremagic[8] = '\0';
slouken@542
   480
slouken@616
   481
    /* WAVE files have the magic four bytes "RIFF"
slouken@616
   482
       AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
slouken@616
   483
    if (((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@616
   484
        (strcmp((char *)magic, "FORM") == 0)) {
slouken@616
   485
        return MUS_WAV;
slouken@616
   486
    }
slouken@542
   487
slouken@616
   488
    /* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@616
   489
    if (strcmp((char *)magic, "OggS") == 0) {
slouken@616
   490
        return MUS_OGG;
slouken@616
   491
    }
slouken@542
   492
slouken@616
   493
    /* FLAC files have the magic four bytes "fLaC" */
slouken@616
   494
    if (strcmp((char *)magic, "fLaC") == 0) {
slouken@616
   495
        return MUS_FLAC;
slouken@616
   496
    }
slouken@542
   497
slouken@616
   498
    /* MIDI files have the magic four bytes "MThd" */
slouken@616
   499
    if (strcmp((char *)magic, "MThd") == 0) {
slouken@616
   500
        return MUS_MID;
slouken@616
   501
    }
slouken@542
   502
slouken@616
   503
    if (detect_mp3(magic)) {
slouken@616
   504
        return MUS_MP3;
slouken@616
   505
    }
slouken@542
   506
slouken@616
   507
    /* Assume MOD format.
slouken@616
   508
     *
slouken@616
   509
     * Apparently there is no way to check if the file is really a MOD,
slouken@616
   510
     * or there are too many formats supported by MikMod/ModPlug, or
slouken@616
   511
     * MikMod/ModPlug does this check by itself. */
slouken@616
   512
    return MUS_MOD;
slouken@542
   513
}
slouken@542
   514
slouken@0
   515
/* Load a music file */
slouken@0
   516
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   517
{
slouken@625
   518
    SDL_RWops *src;
slouken@616
   519
    Mix_Music *music;
slouken@616
   520
    Mix_MusicType type;
slouken@616
   521
    char *ext = strrchr(file, '.');
slouken@542
   522
slouken@542
   523
#ifdef CMD_MUSIC
slouken@616
   524
    if ( music_cmd ) {
slouken@616
   525
        /* Allocate memory for the music structure */
slouken@616
   526
        music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
slouken@616
   527
        if ( music == NULL ) {
slouken@616
   528
            Mix_SetError("Out of memory");
slouken@616
   529
            return(NULL);
slouken@616
   530
        }
slouken@616
   531
        music->error = 0;
slouken@616
   532
        music->type = MUS_CMD;
slouken@616
   533
        music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@616
   534
        if ( music->data.cmd == NULL ) {
slouken@616
   535
            SDL_free(music);
slouken@616
   536
            music = NULL;
slouken@616
   537
        }
slouken@616
   538
        return music;
slouken@616
   539
    }
slouken@542
   540
#endif
slouken@542
   541
slouken@625
   542
    src = SDL_RWFromFile(file, "rb");
slouken@625
   543
    if ( src == NULL ) {
slouken@616
   544
        Mix_SetError("Couldn't open '%s'", file);
slouken@616
   545
        return NULL;
slouken@616
   546
    }
slouken@542
   547
slouken@616
   548
    /* Use the extension as a first guess on the file type */
slouken@616
   549
    type = MUS_NONE;
slouken@616
   550
    ext = strrchr(file, '.');
slouken@616
   551
    /* No need to guard these with #ifdef *_MUSIC stuff,
slouken@616
   552
     * since we simply call Mix_LoadMUSType_RW() later */
slouken@616
   553
    if ( ext ) {
slouken@616
   554
        ++ext; /* skip the dot in the extension */
slouken@616
   555
        if ( MIX_string_equals(ext, "WAV") ) {
slouken@616
   556
            type = MUS_WAV;
slouken@616
   557
        } else if ( MIX_string_equals(ext, "MID") ||
slouken@616
   558
                    MIX_string_equals(ext, "MIDI") ||
slouken@616
   559
                    MIX_string_equals(ext, "KAR") ) {
slouken@616
   560
            type = MUS_MID;
slouken@616
   561
        } else if ( MIX_string_equals(ext, "OGG") ) {
slouken@616
   562
            type = MUS_OGG;
slouken@616
   563
        } else if ( MIX_string_equals(ext, "FLAC") ) {
slouken@616
   564
            type = MUS_FLAC;
slouken@616
   565
        } else  if ( MIX_string_equals(ext, "MPG") ||
slouken@616
   566
                     MIX_string_equals(ext, "MPEG") ||
slouken@616
   567
                     MIX_string_equals(ext, "MP3") ||
slouken@616
   568
                     MIX_string_equals(ext, "MAD") ) {
slouken@616
   569
            type = MUS_MP3;
slouken@616
   570
        }
slouken@616
   571
    }
slouken@616
   572
    if ( type == MUS_NONE ) {
slouken@625
   573
        type = detect_music_type(src);
slouken@616
   574
    }
slouken@542
   575
slouken@616
   576
    /* We need to know if a specific error occurs; if not, we'll set a
slouken@616
   577
     * generic one, so we clear the current one. */
slouken@687
   578
    SDL_ClearError();
slouken@625
   579
    music = Mix_LoadMUSType_RW(src, type, SDL_TRUE);
slouken@616
   580
    if ( music == NULL && Mix_GetError()[0] == '\0' ) {
slouken@629
   581
        Mix_SetError("Unrecognized music format");
slouken@616
   582
    }
slouken@616
   583
    return music;
slouken@542
   584
}
slouken@542
   585
slouken@625
   586
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *src, int freesrc)
slouken@542
   587
{
slouken@625
   588
    return Mix_LoadMUSType_RW(src, MUS_NONE, freesrc);
slouken@542
   589
}
slouken@542
   590
slouken@625
   591
Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *src, Mix_MusicType type, int freesrc)
slouken@542
   592
{
slouken@616
   593
    Mix_Music *music;
slouken@625
   594
    Sint64 start;
slouken@0
   595
slouken@625
   596
    if (!src) {
slouken@616
   597
        Mix_SetError("RWops pointer is NULL");
slouken@616
   598
        return NULL;
slouken@616
   599
    }
slouken@625
   600
    start = SDL_RWtell(src);
slouken@542
   601
slouken@616
   602
    /* If the caller wants auto-detection, figure out what kind of file
slouken@616
   603
     * this is. */
slouken@616
   604
    if (type == MUS_NONE) {
slouken@625
   605
        if ((type = detect_music_type(src)) == MUS_NONE) {
slouken@616
   606
            /* Don't call Mix_SetError() here since detect_music_type()
slouken@616
   607
             * does that. */
slouken@625
   608
            if (freesrc) {
slouken@625
   609
                SDL_RWclose(src);
slouken@625
   610
            }
slouken@616
   611
            return NULL;
slouken@616
   612
        }
slouken@616
   613
    }
slouken@125
   614
slouken@616
   615
    /* Allocate memory for the music structure */
slouken@616
   616
    music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
slouken@616
   617
    if (music == NULL ) {
slouken@616
   618
        Mix_SetError("Out of memory");
slouken@625
   619
        if (freesrc) {
slouken@625
   620
            SDL_RWclose(src);
slouken@625
   621
        }
slouken@616
   622
        return NULL;
slouken@616
   623
    }
slouken@625
   624
    music->error = 1;
slouken@0
   625
slouken@616
   626
    switch (type) {
slouken@542
   627
#ifdef WAV_MUSIC
slouken@616
   628
    case MUS_WAV:
slouken@625
   629
        music->type = MUS_WAV;
slouken@625
   630
        music->data.wave = WAVStream_LoadSong_RW(src, freesrc);
slouken@625
   631
        if (music->data.wave) {
slouken@625
   632
            music->error = 0;
slouken@616
   633
        }
slouken@616
   634
        break;
slouken@0
   635
#endif
slouken@542
   636
#ifdef OGG_MUSIC
slouken@616
   637
    case MUS_OGG:
slouken@616
   638
        music->type = MUS_OGG;
slouken@625
   639
        music->data.ogg = OGG_new_RW(src, freesrc);
slouken@625
   640
        if (music->data.ogg) {
slouken@625
   641
            music->error = 0;
slouken@616
   642
        }
slouken@616
   643
        break;
slouken@542
   644
#endif
slouken@542
   645
#ifdef FLAC_MUSIC
slouken@616
   646
    case MUS_FLAC:
slouken@616
   647
        music->type = MUS_FLAC;
slouken@625
   648
        music->data.flac = FLAC_new_RW(src, freesrc);
slouken@625
   649
        if (music->data.flac) {
slouken@625
   650
            music->error = 0;
slouken@616
   651
        }
slouken@616
   652
        break;
slouken@542
   653
#endif
slouken@542
   654
#ifdef MP3_MUSIC
slouken@616
   655
    case MUS_MP3:
slouken@625
   656
        if (Mix_Init(MIX_INIT_MP3)) {
slouken@616
   657
            SMPEG_Info info;
slouken@616
   658
            music->type = MUS_MP3;
slouken@625
   659
            music->data.mp3 = smpeg.SMPEG_new_rwops(src, &info, freesrc, 0);
slouken@625
   660
            if (!info.has_audio) {
slouken@616
   661
                Mix_SetError("MPEG file does not have any audio stream.");
slouken@625
   662
                smpeg.SMPEG_delete(music->data.mp3);
slouken@625
   663
                /* Deleting the MP3 closed the source if desired */
slouken@625
   664
                freesrc = SDL_FALSE;
slouken@616
   665
            } else {
slouken@616
   666
                smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@625
   667
                music->error = 0;
slouken@616
   668
            }
slouken@616
   669
        }
slouken@616
   670
        break;
slouken@542
   671
#elif defined(MP3_MAD_MUSIC)
slouken@616
   672
    case MUS_MP3:
slouken@616
   673
        music->type = MUS_MP3_MAD;
slouken@625
   674
        music->data.mp3_mad = mad_openFileRW(src, &used_mixer, freesrc);
slouken@625
   675
        if (music->data.mp3_mad) {
slouken@625
   676
            music->error = 0;
slouken@625
   677
        } else {
slouken@616
   678
            Mix_SetError("Could not initialize MPEG stream.");
slouken@616
   679
        }
slouken@616
   680
        break;
slouken@0
   681
#endif
slouken@0
   682
#ifdef MID_MUSIC
slouken@616
   683
    case MUS_MID:
slouken@616
   684
        music->type = MUS_MID;
slouken@98
   685
#ifdef USE_NATIVE_MIDI
slouken@625
   686
        if (native_midi_ok) {
slouken@625
   687
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@625
   688
            music->data.nativemidi = native_midi_loadsong_RW(src, freesrc);
slouken@625
   689
            if (music->data.nativemidi) {
slouken@625
   690
                music->error = 0;
slouken@625
   691
            } else {
slouken@616
   692
                Mix_SetError("%s", native_midi_error());
slouken@616
   693
            }
slouken@616
   694
            break;
slouken@616
   695
        }
chewi@506
   696
#endif
chewi@506
   697
#ifdef USE_FLUIDSYNTH_MIDI
slouken@625
   698
        if (fluidsynth_ok) {
slouken@625
   699
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@625
   700
            music->data.fluidsynthmidi = fluidsynth_loadsong_RW(src, freesrc);
slouken@625
   701
            if (music->data.fluidsynthmidi) {
slouken@625
   702
                music->error = 0;
slouken@616
   703
            }
slouken@616
   704
            break;
slouken@616
   705
        }
slouken@98
   706
#endif
slouken@106
   707
#ifdef USE_TIMIDITY_MIDI
slouken@625
   708
        if (timidity_ok) {
slouken@625
   709
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@625
   710
            music->data.midi = Timidity_LoadSong_RW(src, freesrc);
slouken@625
   711
            if (music->data.midi) {
slouken@625
   712
                music->error = 0;
slouken@625
   713
            } else {
slouken@616
   714
                Mix_SetError("%s", Timidity_Error());
slouken@616
   715
            }
slouken@616
   716
        } else {
slouken@616
   717
            Mix_SetError("%s", Timidity_Error());
slouken@616
   718
        }
slouken@106
   719
#endif
slouken@616
   720
        break;
slouken@0
   721
#endif
slouken@542
   722
#if defined(MODPLUG_MUSIC) || defined(MOD_MUSIC)
slouken@616
   723
    case MUS_MOD:
slouken@542
   724
#ifdef MODPLUG_MUSIC
slouken@625
   725
        if (music->error) {
slouken@625
   726
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@616
   727
            music->type = MUS_MODPLUG;
slouken@625
   728
            music->data.modplug = modplug_new_RW(src, freesrc);
slouken@625
   729
            if (music->data.modplug) {
slouken@616
   730
                music->error = 0;
slouken@616
   731
            }
slouken@616
   732
        }
slouken@481
   733
#endif
slouken@411
   734
#ifdef MOD_MUSIC
slouken@625
   735
        if (music->error) {
slouken@625
   736
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@616
   737
            music->type = MUS_MOD;
slouken@625
   738
            music->data.module = MOD_new_RW(src, freesrc);
slouken@625
   739
            if (music->data.module) {
slouken@616
   740
                music->error = 0;
slouken@616
   741
            }
slouken@616
   742
        }
slouken@0
   743
#endif
slouken@616
   744
        break;
slouken@542
   745
#endif
slouken@542
   746
slouken@616
   747
    default:
slouken@616
   748
        Mix_SetError("Unrecognized music format");
slouken@625
   749
        break;
slouken@616
   750
    } /* switch (want) */
chewi@506
   751
slouken@616
   752
    if (music->error) {
slouken@616
   753
        SDL_free(music);
slouken@625
   754
        if (freesrc) {
slouken@625
   755
            SDL_RWclose(src);
slouken@625
   756
        } else {
slouken@625
   757
            SDL_RWseek(src, start, RW_SEEK_SET);
slouken@625
   758
        }
slouken@625
   759
        music = NULL;
slouken@616
   760
    }
slouken@625
   761
    return music;
slouken@0
   762
}
slouken@0
   763
slouken@0
   764
/* Free a music chunk previously loaded */
slouken@0
   765
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   766
{
slouken@616
   767
    if ( music ) {
slouken@616
   768
        /* Stop the music if it's currently playing */
slouken@718
   769
        Mix_LockAudio();
slouken@616
   770
        if ( music == music_playing ) {
slouken@616
   771
            /* Wait for any fade out to finish */
slouken@616
   772
            while ( music->fading == MIX_FADING_OUT ) {
slouken@718
   773
                Mix_UnlockAudio();
slouken@616
   774
                SDL_Delay(100);
slouken@718
   775
                Mix_LockAudio();
slouken@616
   776
            }
slouken@616
   777
            if ( music == music_playing ) {
slouken@616
   778
                music_internal_halt();
slouken@616
   779
            }
slouken@616
   780
        }
slouken@718
   781
        Mix_UnlockAudio();
slouken@616
   782
        switch (music->type) {
slouken@0
   783
#ifdef CMD_MUSIC
slouken@616
   784
            case MUS_CMD:
slouken@616
   785
                MusicCMD_FreeSong(music->data.cmd);
slouken@616
   786
                break;
slouken@0
   787
#endif
slouken@0
   788
#ifdef WAV_MUSIC
slouken@616
   789
            case MUS_WAV:
slouken@616
   790
                WAVStream_FreeSong(music->data.wave);
slouken@616
   791
                break;
slouken@0
   792
#endif
slouken@481
   793
#ifdef MODPLUG_MUSIC
slouken@616
   794
            case MUS_MODPLUG:
slouken@616
   795
                modplug_delete(music->data.modplug);
slouken@616
   796
                break;
slouken@481
   797
#endif
slouken@411
   798
#ifdef MOD_MUSIC
slouken@616
   799
            case MUS_MOD:
slouken@616
   800
                MOD_delete(music->data.module);
slouken@616
   801
                break;
slouken@0
   802
#endif
slouken@0
   803
#ifdef MID_MUSIC
slouken@616
   804
            case MUS_MID:
slouken@98
   805
#ifdef USE_NATIVE_MIDI
slouken@616
   806
                if ( native_midi_ok ) {
slouken@616
   807
                    native_midi_freesong(music->data.nativemidi);
slouken@616
   808
                    goto skip;
slouken@616
   809
                }
chewi@506
   810
#endif
chewi@506
   811
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
   812
                if ( fluidsynth_ok ) {
slouken@616
   813
                    fluidsynth_freesong(music->data.fluidsynthmidi);
slouken@616
   814
                    goto skip;
slouken@616
   815
                }
slouken@98
   816
#endif
slouken@106
   817
#ifdef USE_TIMIDITY_MIDI
slouken@616
   818
                if ( timidity_ok ) {
slouken@616
   819
                    Timidity_FreeSong(music->data.midi);
slouken@616
   820
                    goto skip;
slouken@616
   821
                }
slouken@106
   822
#endif
slouken@616
   823
                break;
slouken@0
   824
#endif
slouken@63
   825
#ifdef OGG_MUSIC
slouken@616
   826
            case MUS_OGG:
slouken@616
   827
                OGG_delete(music->data.ogg);
slouken@616
   828
                break;
slouken@63
   829
#endif
slouken@382
   830
#ifdef FLAC_MUSIC
slouken@616
   831
            case MUS_FLAC:
slouken@616
   832
                FLAC_delete(music->data.flac);
slouken@616
   833
                break;
slouken@382
   834
#endif
slouken@0
   835
#ifdef MP3_MUSIC
slouken@616
   836
            case MUS_MP3:
slouken@616
   837
                smpeg.SMPEG_delete(music->data.mp3);
slouken@616
   838
                break;
slouken@0
   839
#endif
slouken@357
   840
#ifdef MP3_MAD_MUSIC
slouken@616
   841
            case MUS_MP3_MAD:
slouken@616
   842
                mad_closeFile(music->data.mp3_mad);
slouken@616
   843
                break;
slouken@357
   844
#endif
slouken@616
   845
            default:
slouken@616
   846
                /* Unknown music type?? */
slouken@616
   847
                break;
slouken@616
   848
        }
chewi@506
   849
chewi@506
   850
    skip:
slouken@616
   851
        SDL_free(music);
slouken@616
   852
    }
slouken@0
   853
}
slouken@0
   854
slouken@177
   855
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   856
   music, if 'music' is NULL.
slouken@177
   857
*/
slouken@177
   858
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   859
{
slouken@616
   860
    Mix_MusicType type = MUS_NONE;
slouken@177
   861
slouken@616
   862
    if ( music ) {
slouken@616
   863
        type = music->type;
slouken@616
   864
    } else {
slouken@718
   865
        Mix_LockAudio();
slouken@616
   866
        if ( music_playing ) {
slouken@616
   867
            type = music_playing->type;
slouken@616
   868
        }
slouken@718
   869
        Mix_UnlockAudio();
slouken@616
   870
    }
slouken@616
   871
    return(type);
slouken@177
   872
}
slouken@177
   873
slouken@173
   874
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   875
 */
slouken@173
   876
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   877
{
slouken@616
   878
    int retval = 0;
megastep@10
   879
slouken@547
   880
#if defined(__MACOSX__) && defined(USE_NATIVE_MIDI)
slouken@616
   881
    /* This fixes a bug with native MIDI on Mac OS X, where you
slouken@616
   882
       can't really stop and restart MIDI from the audio callback.
slouken@616
   883
    */
slouken@616
   884
    if ( music == music_playing && music->type == MUS_MID && native_midi_ok ) {
slouken@616
   885
        /* Just a seek suffices to restart playing */
slouken@616
   886
        music_internal_position(position);
slouken@616
   887
        return 0;
slouken@616
   888
    }
slouken@531
   889
#endif
slouken@529
   890
slouken@616
   891
    /* Note the music we're playing */
slouken@616
   892
    if ( music_playing ) {
slouken@616
   893
        music_internal_halt();
slouken@616
   894
    }
slouken@616
   895
    music_playing = music;
slouken@173
   896
slouken@616
   897
    /* Set the initial volume */
slouken@616
   898
    if ( music->type != MUS_MOD ) {
slouken@616
   899
        music_internal_initialize_volume();
slouken@616
   900
    }
slouken@173
   901
slouken@616
   902
    /* Set up for playback */
slouken@616
   903
    switch (music->type) {
slouken@0
   904
#ifdef CMD_MUSIC
slouken@616
   905
        case MUS_CMD:
slouken@616
   906
        MusicCMD_Start(music->data.cmd);
slouken@616
   907
        break;
slouken@0
   908
#endif
slouken@0
   909
#ifdef WAV_MUSIC
slouken@616
   910
        case MUS_WAV:
slouken@616
   911
        WAVStream_Start(music->data.wave);
slouken@616
   912
        break;
slouken@0
   913
#endif
slouken@481
   914
#ifdef MODPLUG_MUSIC
slouken@616
   915
        case MUS_MODPLUG:
slouken@616
   916
        /* can't set volume until file is loaded, so finally set it now */
slouken@616
   917
        music_internal_initialize_volume();
slouken@616
   918
        modplug_play(music->data.modplug);
slouken@616
   919
        break;
slouken@481
   920
#endif
slouken@411
   921
#ifdef MOD_MUSIC
slouken@616
   922
        case MUS_MOD:
slouken@616
   923
        MOD_play(music->data.module);
slouken@616
   924
        /* Player_SetVolume() does nothing before Player_Start() */
slouken@616
   925
        music_internal_initialize_volume();
slouken@616
   926
        break;
slouken@0
   927
#endif
slouken@0
   928
#ifdef MID_MUSIC
slouken@616
   929
        case MUS_MID:
slouken@98
   930
#ifdef USE_NATIVE_MIDI
slouken@616
   931
        if ( native_midi_ok ) {
slouken@616
   932
            native_midi_start(music->data.nativemidi, music_loops);
slouken@616
   933
            goto skip;
slouken@616
   934
        }
chewi@506
   935
#endif
chewi@506
   936
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
   937
        if (fluidsynth_ok ) {
slouken@616
   938
            fluidsynth_start(music->data.fluidsynthmidi);
slouken@616
   939
            goto skip;
slouken@616
   940
        }
slouken@98
   941
#endif
slouken@106
   942
#ifdef USE_TIMIDITY_MIDI
slouken@616
   943
        if ( timidity_ok ) {
slouken@616
   944
            Timidity_Start(music->data.midi);
slouken@616
   945
            goto skip;
slouken@616
   946
        }
slouken@106
   947
#endif
slouken@616
   948
        break;
slouken@0
   949
#endif
slouken@63
   950
#ifdef OGG_MUSIC
slouken@616
   951
        case MUS_OGG:
slouken@616
   952
        OGG_play(music->data.ogg);
slouken@616
   953
        break;
slouken@63
   954
#endif
slouken@382
   955
#ifdef FLAC_MUSIC
slouken@616
   956
        case MUS_FLAC:
slouken@616
   957
        FLAC_play(music->data.flac);
slouken@616
   958
        break;
slouken@382
   959
#endif
slouken@0
   960
#ifdef MP3_MUSIC
slouken@616
   961
        case MUS_MP3:
slouken@616
   962
        smpeg.SMPEG_enableaudio(music->data.mp3,1);
slouken@616
   963
        smpeg.SMPEG_enablevideo(music->data.mp3,0);
slouken@616
   964
        smpeg.SMPEG_play(music_playing->data.mp3);
slouken@616
   965
        break;
slouken@0
   966
#endif
slouken@357
   967
#ifdef MP3_MAD_MUSIC
slouken@616
   968
        case MUS_MP3_MAD:
slouken@616
   969
        mad_start(music->data.mp3_mad);
slouken@616
   970
        break;
slouken@357
   971
#endif
slouken@616
   972
        default:
slouken@616
   973
        Mix_SetError("Can't play unknown music type");
slouken@616
   974
        retval = -1;
slouken@616
   975
        break;
slouken@616
   976
    }
slouken@173
   977
chewi@506
   978
skip:
slouken@616
   979
    /* Set the playback position, note any errors if an offset is used */
slouken@616
   980
    if ( retval == 0 ) {
slouken@616
   981
        if ( position > 0.0 ) {
slouken@616
   982
            if ( music_internal_position(position) < 0 ) {
slouken@616
   983
                Mix_SetError("Position not implemented for music type");
slouken@616
   984
                retval = -1;
slouken@616
   985
            }
slouken@616
   986
        } else {
slouken@616
   987
            music_internal_position(0.0);
slouken@616
   988
        }
slouken@616
   989
    }
slouken@173
   990
slouken@616
   991
    /* If the setup failed, we're not playing any music anymore */
slouken@616
   992
    if ( retval < 0 ) {
slouken@616
   993
        music_playing = NULL;
slouken@616
   994
    }
slouken@616
   995
    return(retval);
slouken@173
   996
}
slouken@173
   997
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   998
{
slouken@616
   999
    int retval;
slouken@173
  1000
slouken@616
  1001
    if ( ms_per_step == 0 ) {
slouken@616
  1002
        SDL_SetError("Audio device hasn't been opened");
slouken@616
  1003
        return(-1);
slouken@616
  1004
    }
slouken@499
  1005
slouken@616
  1006
    /* Don't play null pointers :-) */
slouken@616
  1007
    if ( music == NULL ) {
slouken@616
  1008
        Mix_SetError("music parameter was NULL");
slouken@616
  1009
        return(-1);
slouken@616
  1010
    }
slouken@173
  1011
slouken@616
  1012
    /* Setup the data */
slouken@616
  1013
    if ( ms ) {
slouken@616
  1014
        music->fading = MIX_FADING_IN;
slouken@616
  1015
    } else {
slouken@616
  1016
        music->fading = MIX_NO_FADING;
slouken@616
  1017
    }
slouken@616
  1018
    music->fade_step = 0;
slouken@616
  1019
    music->fade_steps = ms/ms_per_step;
slouken@173
  1020
slouken@616
  1021
    /* Play the puppy */
slouken@718
  1022
    Mix_LockAudio();
slouken@616
  1023
    /* If the current music is fading out, wait for the fade to complete */
slouken@616
  1024
    while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@718
  1025
        Mix_UnlockAudio();
slouken@616
  1026
        SDL_Delay(100);
slouken@718
  1027
        Mix_LockAudio();
slouken@616
  1028
    }
slouken@616
  1029
    music_active = 1;
slouken@616
  1030
    if (loops == 1) {
slouken@616
  1031
        /* Loop is the number of times to play the audio */
slouken@616
  1032
        loops = 0;
slouken@616
  1033
    }
slouken@616
  1034
    music_loops = loops;
slouken@616
  1035
    retval = music_internal_play(music, position);
slouken@718
  1036
    Mix_UnlockAudio();
slouken@173
  1037
slouken@616
  1038
    return(retval);
slouken@173
  1039
}
slouken@173
  1040
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
  1041
{
slouken@616
  1042
    return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
  1043
}
slouken@173
  1044
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
  1045
{
slouken@616
  1046
    return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
  1047
}
megastep@10
  1048
slouken@173
  1049
/* Set the playing music position */
slouken@173
  1050
int music_internal_position(double position)
megastep@10
  1051
{
slouken@616
  1052
    int retval = 0;
slouken@173
  1053
slouken@616
  1054
    switch (music_playing->type) {
slouken@481
  1055
#ifdef MODPLUG_MUSIC
slouken@616
  1056
        case MUS_MODPLUG:
slouken@616
  1057
        modplug_jump_to_time(music_playing->data.modplug, position);
slouken@616
  1058
        break;
slouken@481
  1059
#endif
slouken@411
  1060
#ifdef MOD_MUSIC
slouken@616
  1061
        case MUS_MOD:
slouken@616
  1062
        MOD_jump_to_time(music_playing->data.module, position);
slouken@616
  1063
        break;
slouken@173
  1064
#endif
slouken@173
  1065
#ifdef OGG_MUSIC
slouken@616
  1066
        case MUS_OGG:
slouken@616
  1067
        OGG_jump_to_time(music_playing->data.ogg, position);
slouken@616
  1068
        break;
slouken@173
  1069
#endif
slouken@382
  1070
#ifdef FLAC_MUSIC
slouken@616
  1071
        case MUS_FLAC:
slouken@616
  1072
        FLAC_jump_to_time(music_playing->data.flac, position);
slouken@616
  1073
        break;
slouken@382
  1074
#endif
slouken@173
  1075
#ifdef MP3_MUSIC
slouken@616
  1076
        case MUS_MP3:
slouken@616
  1077
        smpeg.SMPEG_rewind(music_playing->data.mp3);
slouken@616
  1078
        smpeg.SMPEG_play(music_playing->data.mp3);
slouken@616
  1079
        if ( position > 0.0 ) {
slouken@616
  1080
            smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
slouken@616
  1081
        }
slouken@616
  1082
        break;
slouken@173
  1083
#endif
slouken@357
  1084
#ifdef MP3_MAD_MUSIC
slouken@616
  1085
        case MUS_MP3_MAD:
slouken@616
  1086
        mad_seek(music_playing->data.mp3_mad, position);
slouken@616
  1087
        break;
slouken@357
  1088
#endif
slouken@616
  1089
        default:
slouken@616
  1090
        /* TODO: Implement this for other music backends */
slouken@616
  1091
        retval = -1;
slouken@616
  1092
        break;
slouken@616
  1093
    }
slouken@616
  1094
    return(retval);
megastep@4
  1095
}
slouken@155
  1096
int Mix_SetMusicPosition(double position)
slouken@154
  1097
{
slouken@616
  1098
    int retval;
slouken@173
  1099
slouken@718
  1100
    Mix_LockAudio();
slouken@616
  1101
    if ( music_playing ) {
slouken@616
  1102
        retval = music_internal_position(position);
slouken@616
  1103
        if ( retval < 0 ) {
slouken@616
  1104
            Mix_SetError("Position not implemented for music type");
slouken@616
  1105
        }
slouken@616
  1106
    } else {
slouken@616
  1107
        Mix_SetError("Music isn't playing");
slouken@616
  1108
        retval = -1;
slouken@616
  1109
    }
slouken@718
  1110
    Mix_UnlockAudio();
slouken@154
  1111
slouken@616
  1112
    return(retval);
slouken@154
  1113
}
slouken@154
  1114
icculus@237
  1115
/* Set the music's initial volume */
icculus@237
  1116
static void music_internal_initialize_volume(void)
icculus@237
  1117
{
slouken@616
  1118
    if ( music_playing->fading == MIX_FADING_IN ) {
slouken@616
  1119
        music_internal_volume(0);
slouken@616
  1120
    } else {
slouken@616
  1121
        music_internal_volume(music_volume);
slouken@616
  1122
    }
icculus@237
  1123
}
icculus@237
  1124
slouken@0
  1125
/* Set the music volume */
slouken@173
  1126
static void music_internal_volume(int volume)
slouken@173
  1127
{
slouken@616
  1128
    switch (music_playing->type) {
slouken@173
  1129
#ifdef CMD_MUSIC
slouken@616
  1130
        case MUS_CMD:
slouken@616
  1131
        MusicCMD_SetVolume(volume);
slouken@616
  1132
        break;
slouken@173
  1133
#endif
slouken@173
  1134
#ifdef WAV_MUSIC
slouken@616
  1135
        case MUS_WAV:
slouken@616
  1136
        WAVStream_SetVolume(volume);
slouken@616
  1137
        break;
slouken@173
  1138
#endif
slouken@481
  1139
#ifdef MODPLUG_MUSIC
slouken@616
  1140
        case MUS_MODPLUG:
slouken@616
  1141
        modplug_setvolume(music_playing->data.modplug, volume);
slouken@616
  1142
        break;
slouken@481
  1143
#endif
slouken@411
  1144
#ifdef MOD_MUSIC
slouken@616
  1145
        case MUS_MOD:
slouken@616
  1146
        MOD_setvolume(music_playing->data.module, volume);
slouken@616
  1147
        break;
slouken@173
  1148
#endif
slouken@173
  1149
#ifdef MID_MUSIC
slouken@616
  1150
        case MUS_MID:
slouken@173
  1151
#ifdef USE_NATIVE_MIDI
slouken@616
  1152
        if ( native_midi_ok ) {
slouken@616
  1153
            native_midi_setvolume(volume);
slouken@616
  1154
            return;
slouken@616
  1155
        }
chewi@506
  1156
#endif
chewi@506
  1157
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
  1158
        if ( fluidsynth_ok ) {
slouken@616
  1159
            fluidsynth_setvolume(music_playing->data.fluidsynthmidi, volume);
slouken@616
  1160
            return;
slouken@616
  1161
        }
slouken@173
  1162
#endif
slouken@173
  1163
#ifdef USE_TIMIDITY_MIDI
slouken@616
  1164
        if ( timidity_ok ) {
slouken@616
  1165
            Timidity_SetVolume(volume);
slouken@616
  1166
            return;
slouken@616
  1167
        }
slouken@173
  1168
#endif
slouken@616
  1169
        break;
slouken@173
  1170
#endif
slouken@173
  1171
#ifdef OGG_MUSIC
slouken@616
  1172
        case MUS_OGG:
slouken@616
  1173
        OGG_setvolume(music_playing->data.ogg, volume);
slouken@616
  1174
        break;
slouken@173
  1175
#endif
slouken@382
  1176
#ifdef FLAC_MUSIC
slouken@616
  1177
        case MUS_FLAC:
slouken@616
  1178
        FLAC_setvolume(music_playing->data.flac, volume);
slouken@616
  1179
        break;
slouken@382
  1180
#endif
slouken@173
  1181
#ifdef MP3_MUSIC
slouken@616
  1182
        case MUS_MP3:
slouken@616
  1183
        smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@616
  1184
        break;
slouken@173
  1185
#endif
slouken@357
  1186
#ifdef MP3_MAD_MUSIC
slouken@616
  1187
        case MUS_MP3_MAD:
slouken@616
  1188
        mad_setVolume(music_playing->data.mp3_mad, volume);
slouken@616
  1189
        break;
slouken@357
  1190
#endif
slouken@616
  1191
        default:
slouken@616
  1192
        /* Unknown music type?? */
slouken@616
  1193
        break;
slouken@616
  1194
    }
slouken@173
  1195
}
slouken@0
  1196
int Mix_VolumeMusic(int volume)
slouken@0
  1197
{
slouken@616
  1198
    int prev_volume;
slouken@0
  1199
slouken@616
  1200
    prev_volume = music_volume;
slouken@616
  1201
    if ( volume < 0 ) {
slouken@616
  1202
        return prev_volume;
slouken@616
  1203
    }
slouken@616
  1204
    if ( volume > SDL_MIX_MAXVOLUME ) {
slouken@616
  1205
        volume = SDL_MIX_MAXVOLUME;
slouken@616
  1206
    }
slouken@616
  1207
    music_volume = volume;
slouken@718
  1208
    Mix_LockAudio();
slouken@616
  1209
    if ( music_playing ) {
slouken@616
  1210
        music_internal_volume(music_volume);
slouken@616
  1211
    }
slouken@718
  1212
    Mix_UnlockAudio();
slouken@616
  1213
    return(prev_volume);
slouken@0
  1214
}
slouken@0
  1215
slouken@173
  1216
/* Halt playing of music */
slouken@173
  1217
static void music_internal_halt(void)
megastep@7
  1218
{
slouken@616
  1219
    switch (music_playing->type) {
megastep@7
  1220
#ifdef CMD_MUSIC
slouken@616
  1221
        case MUS_CMD:
slouken@616
  1222
        MusicCMD_Stop(music_playing->data.cmd);
slouken@616
  1223
        break;
megastep@7
  1224
#endif
megastep@7
  1225
#ifdef WAV_MUSIC
slouken@616
  1226
        case MUS_WAV:
slouken@616
  1227
        WAVStream_Stop();
slouken@616
  1228
        break;
megastep@7
  1229
#endif
slouken@481
  1230
#ifdef MODPLUG_MUSIC
slouken@616
  1231
        case MUS_MODPLUG:
slouken@616
  1232
        modplug_stop(music_playing->data.modplug);
slouken@616
  1233
        break;
slouken@481
  1234
#endif
slouken@411
  1235
#ifdef MOD_MUSIC
slouken@616
  1236
        case MUS_MOD:
slouken@616
  1237
        MOD_stop(music_playing->data.module);
slouken@616
  1238
        break;
megastep@7
  1239
#endif
megastep@7
  1240
#ifdef MID_MUSIC
slouken@616
  1241
        case MUS_MID:
slouken@98
  1242
#ifdef USE_NATIVE_MIDI
slouken@616
  1243
        if ( native_midi_ok ) {
slouken@616
  1244
            native_midi_stop();
slouken@616
  1245
            goto skip;
slouken@616
  1246
        }
chewi@506
  1247
#endif
chewi@506
  1248
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
  1249
        if ( fluidsynth_ok ) {
slouken@616
  1250
            fluidsynth_stop(music_playing->data.fluidsynthmidi);
slouken@616
  1251
            goto skip;
slouken@616
  1252
        }
slouken@98
  1253
#endif
slouken@106
  1254
#ifdef USE_TIMIDITY_MIDI
slouken@616
  1255
        if ( timidity_ok ) {
slouken@616
  1256
            Timidity_Stop();
slouken@616
  1257
            goto skip;
slouken@616
  1258
        }
slouken@106
  1259
#endif
slouken@616
  1260
        break;
megastep@7
  1261
#endif
slouken@63
  1262
#ifdef OGG_MUSIC
slouken@616
  1263
        case MUS_OGG:
slouken@616
  1264
        OGG_stop(music_playing->data.ogg);
slouken@616
  1265
        break;
slouken@63
  1266
#endif
slouken@382
  1267
#ifdef FLAC_MUSIC
slouken@616
  1268
        case MUS_FLAC:
slouken@616
  1269
        FLAC_stop(music_playing->data.flac);
slouken@616
  1270
        break;
slouken@382
  1271
#endif
megastep@7
  1272
#ifdef MP3_MUSIC
slouken@616
  1273
        case MUS_MP3:
slouken@616
  1274
        smpeg.SMPEG_stop(music_playing->data.mp3);
slouken@616
  1275
        break;
megastep@7
  1276
#endif
slouken@357
  1277
#ifdef MP3_MAD_MUSIC
slouken@616
  1278
        case MUS_MP3_MAD:
slouken@616
  1279
        mad_stop(music_playing->data.mp3_mad);
slouken@616
  1280
        break;
slouken@357
  1281
#endif
slouken@616
  1282
        default:
slouken@616
  1283
        /* Unknown music type?? */
slouken@616
  1284
        return;
slouken@616
  1285
    }
chewi@506
  1286
chewi@506
  1287
skip:
slouken@616
  1288
    music_playing->fading = MIX_NO_FADING;
slouken@616
  1289
    music_playing = NULL;
megastep@7
  1290
}
slouken@0
  1291
int Mix_HaltMusic(void)
slouken@0
  1292
{
slouken@718
  1293
    Mix_LockAudio();
slouken@616
  1294
    if ( music_playing ) {
slouken@616
  1295
        music_internal_halt();
slouken@616
  1296
        if ( music_finished_hook ) {
slouken@616
  1297
            music_finished_hook();
slouken@616
  1298
        }
slouken@616
  1299
    }
slouken@718
  1300
    Mix_UnlockAudio();
slouken@173
  1301
slouken@616
  1302
    return(0);
slouken@0
  1303
}
slouken@0
  1304
megastep@4
  1305
/* Progressively stop the music */
megastep@4
  1306
int Mix_FadeOutMusic(int ms)
megastep@4
  1307
{
slouken@616
  1308
    int retval = 0;
slouken@173
  1309
slouken@616
  1310
    if ( ms_per_step == 0 ) {
slouken@616
  1311
        SDL_SetError("Audio device hasn't been opened");
slouken@616
  1312
        return 0;
slouken@616
  1313
    }
slouken@499
  1314
slouken@616
  1315
    if (ms <= 0) {  /* just halt immediately. */
slouken@616
  1316
        Mix_HaltMusic();
slouken@616
  1317
        return 1;
slouken@616
  1318
    }
icculus@280
  1319
slouken@718
  1320
    Mix_LockAudio();
slouken@616
  1321
    if ( music_playing) {
icculus@280
  1322
                int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
icculus@280
  1323
                if ( music_playing->fading == MIX_NO_FADING ) {
slouken@616
  1324
                music_playing->fade_step = 0;
icculus@280
  1325
                } else {
icculus@280
  1326
                        int step;
icculus@280
  1327
                        int old_fade_steps = music_playing->fade_steps;
icculus@280
  1328
                        if ( music_playing->fading == MIX_FADING_OUT ) {
icculus@280
  1329
                                step = music_playing->fade_step;
icculus@280
  1330
                        } else {
icculus@280
  1331
                                step = old_fade_steps
icculus@280
  1332
                                        - music_playing->fade_step + 1;
icculus@280
  1333
                        }
icculus@280
  1334
                        music_playing->fade_step = (step * fade_steps)
icculus@280
  1335
                                / old_fade_steps;
icculus@280
  1336
                }
slouken@616
  1337
        music_playing->fading = MIX_FADING_OUT;
slouken@616
  1338
        music_playing->fade_steps = fade_steps;
slouken@616
  1339
        retval = 1;
slouken@616
  1340
    }
slouken@718
  1341
    Mix_UnlockAudio();
slouken@173
  1342
slouken@616
  1343
    return(retval);
megastep@4
  1344
}
megastep@4
  1345
megastep@4
  1346
Mix_Fading Mix_FadingMusic(void)
megastep@4
  1347
{
slouken@616
  1348
    Mix_Fading fading = MIX_NO_FADING;
slouken@173
  1349
slouken@718
  1350
    Mix_LockAudio();
slouken@616
  1351
    if ( music_playing ) {
slouken@616
  1352
        fading = music_playing->fading;
slouken@616
  1353
    }
slouken@718
  1354
    Mix_UnlockAudio();
slouken@173
  1355
slouken@616
  1356
    return(fading);
megastep@4
  1357
}
megastep@4
  1358
slouken@0
  1359
/* Pause/Resume the music stream */
slouken@0
  1360
void Mix_PauseMusic(void)
slouken@0
  1361
{
slouken@616
  1362
    music_active = 0;
slouken@0
  1363
}
megastep@4
  1364
slouken@0
  1365
void Mix_ResumeMusic(void)
slouken@0
  1366
{
slouken@616
  1367
    music_active = 1;
slouken@0
  1368
}
slouken@0
  1369
slouken@0
  1370
void Mix_RewindMusic(void)
slouken@0
  1371
{
slouken@616
  1372
    Mix_SetMusicPosition(0.0);
slouken@0
  1373
}
slouken@0
  1374
megastep@13
  1375
int Mix_PausedMusic(void)
megastep@13
  1376
{
slouken@616
  1377
    return (music_active == 0);
megastep@13
  1378
}
megastep@13
  1379
slouken@0
  1380
/* Check the status of the music */
slouken@173
  1381
static int music_internal_playing()
slouken@173
  1382
{
slouken@616
  1383
    int playing = 1;
icculus@510
  1384
slouken@616
  1385
    if (music_playing == NULL) {
slouken@616
  1386
        return 0;
slouken@616
  1387
    }
icculus@510
  1388
slouken@616
  1389
    switch (music_playing->type) {
slouken@173
  1390
#ifdef CMD_MUSIC
slouken@616
  1391
        case MUS_CMD:
slouken@616
  1392
        if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@616
  1393
            playing = 0;
slouken@616
  1394
        }
slouken@616
  1395
        break;
slouken@173
  1396
#endif
slouken@173
  1397
#ifdef WAV_MUSIC
slouken@616
  1398
        case MUS_WAV:
slouken@616
  1399
        if ( ! WAVStream_Active() ) {
slouken@616
  1400
            playing = 0;
slouken@616
  1401
        }
slouken@616
  1402
        break;
slouken@173
  1403
#endif
slouken@481
  1404
#ifdef MODPLUG_MUSIC
slouken@616
  1405
        case MUS_MODPLUG:
slouken@616
  1406
        if ( ! modplug_playing(music_playing->data.modplug) ) {
slouken@616
  1407
            playing = 0;
slouken@616
  1408
        }
slouken@616
  1409
        break;
slouken@481
  1410
#endif
slouken@411
  1411
#ifdef MOD_MUSIC
slouken@616
  1412
        case MUS_MOD:
slouken@616
  1413
        if ( ! MOD_playing(music_playing->data.module) ) {
slouken@616
  1414
            playing = 0;
slouken@616
  1415
        }
slouken@616
  1416
        break;
slouken@173
  1417
#endif
slouken@173
  1418
#ifdef MID_MUSIC
slouken@616
  1419
        case MUS_MID:
slouken@173
  1420
#ifdef USE_NATIVE_MIDI
slouken@616
  1421
        if ( native_midi_ok ) {
slouken@616
  1422
            if ( ! native_midi_active() )
slouken@616
  1423
                playing = 0;
slouken@616
  1424
            goto skip;
slouken@616
  1425
        }
chewi@506
  1426
#endif
chewi@506
  1427
#ifdef USE_FLUIDSYNTH_MIDI
slouken@616
  1428
        if ( fluidsynth_ok ) {
slouken@616
  1429
            if ( ! fluidsynth_active(music_playing->data.fluidsynthmidi) )
slouken@616
  1430
                playing = 0;
slouken@616
  1431
            goto skip;
slouken@616
  1432
        }
slouken@173
  1433
#endif
slouken@173
  1434
#ifdef USE_TIMIDITY_MIDI
slouken@616
  1435
        if ( timidity_ok ) {
slouken@616
  1436
            if ( ! Timidity_Active() )
slouken@616
  1437
                playing = 0;
slouken@616
  1438
            goto skip;
slouken@616
  1439
        }
slouken@173
  1440
#endif
slouken@616
  1441
        break;
slouken@173
  1442
#endif
slouken@173
  1443
#ifdef OGG_MUSIC
slouken@616
  1444
        case MUS_OGG:
slouken@616
  1445
        if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@616
  1446
            playing = 0;
slouken@616
  1447
        }
slouken@616
  1448
        break;
slouken@173
  1449
#endif
slouken@382
  1450
#ifdef FLAC_MUSIC
slouken@616
  1451
        case MUS_FLAC:
slouken@616
  1452
        if ( ! FLAC_playing(music_playing->data.flac) ) {
slouken@616
  1453
            playing = 0;
slouken@616
  1454
        }
slouken@616
  1455
        break;
slouken@382
  1456
#endif
slouken@173
  1457
#ifdef MP3_MUSIC
slouken@616
  1458
        case MUS_MP3:
slouken@616
  1459
        if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@616
  1460
            playing = 0;
slouken@616
  1461
        break;
slouken@173
  1462
#endif
slouken@357
  1463
#ifdef MP3_MAD_MUSIC
slouken@616
  1464
        case MUS_MP3_MAD:
slouken@616
  1465
        if (!mad_isPlaying(music_playing->data.mp3_mad)) {
slouken@616
  1466
            playing = 0;
slouken@616
  1467
        }
slouken@616
  1468
        break;
slouken@357
  1469
#endif
slouken@616
  1470
        default:
slouken@616
  1471
        playing = 0;
slouken@616
  1472
        break;
slouken@616
  1473
    }
chewi@506
  1474
chewi@506
  1475
skip:
slouken@616
  1476
    return(playing);
slouken@173
  1477
}
megastep@13
  1478
int Mix_PlayingMusic(void)
slouken@0
  1479
{
slouken@616
  1480
    int playing = 0;
slouken@173
  1481
slouken@718
  1482
    Mix_LockAudio();
slouken@616
  1483
    if ( music_playing ) {
slouken@616
  1484
        playing = music_loops || music_internal_playing();
slouken@616
  1485
    }
slouken@718
  1486
    Mix_UnlockAudio();
slouken@173
  1487
slouken@616
  1488
    return(playing);
slouken@0
  1489
}
slouken@0
  1490
slouken@0
  1491
/* Set the external music playback command */
slouken@0
  1492
int Mix_SetMusicCMD(const char *command)
slouken@0
  1493
{
slouken@616
  1494
    Mix_HaltMusic();
slouken@616
  1495
    if ( music_cmd ) {
slouken@616
  1496
        SDL_free(music_cmd);
slouken@616
  1497
        music_cmd = NULL;
slouken@616
  1498
    }
slouken@616
  1499
    if ( command ) {
slouken@616
  1500
        music_cmd = (char *)SDL_malloc(strlen(command)+1);
slouken@616
  1501
        if ( music_cmd == NULL ) {
slouken@616
  1502
            return(-1);
slouken@616
  1503
        }
slouken@616
  1504
        strcpy(music_cmd, command);
slouken@616
  1505
    }
slouken@616
  1506
    return(0);
slouken@0
  1507
}
slouken@0
  1508
slouken@154
  1509
int Mix_SetSynchroValue(int i)
slouken@154
  1510
{
slouken@616
  1511
    /* Not supported by any players at this time */
slouken@616
  1512
    return(-1);
slouken@154
  1513
}
slouken@154
  1514
slouken@154
  1515
int Mix_GetSynchroValue(void)
slouken@154
  1516
{
slouken@616
  1517
    /* Not supported by any players at this time */
slouken@616
  1518
    return(-1);
slouken@154
  1519
}
slouken@154
  1520
slouken@154
  1521
slouken@0
  1522
/* Uninitialize the music players */
slouken@0
  1523
void close_music(void)
slouken@0
  1524
{
slouken@616
  1525
    Mix_HaltMusic();
slouken@0
  1526
#ifdef CMD_MUSIC
slouken@616
  1527
    Mix_SetMusicCMD(NULL);
slouken@0
  1528
#endif
slouken@481
  1529
#ifdef MODPLUG_MUSIC
slouken@616
  1530
    modplug_exit();
slouken@481
  1531
#endif
slouken@411
  1532
#ifdef MOD_MUSIC
slouken@616
  1533
    MOD_exit();
slouken@0
  1534
#endif
patmandin@264
  1535
#ifdef MID_MUSIC
patmandin@264
  1536
# ifdef USE_TIMIDITY_MIDI
slouken@616
  1537
    Timidity_Close();
patmandin@264
  1538
# endif
patmandin@264
  1539
#endif
icculus@390
  1540
slouken@616
  1541
    /* rcg06042009 report available decoders at runtime. */
slouken@621
  1542
    SDL_free((void *)music_decoders);
slouken@616
  1543
    music_decoders = NULL;
slouken@616
  1544
    num_decoders = 0;
slouken@499
  1545
slouken@616
  1546
    ms_per_step = 0;
slouken@0
  1547
}
slouken@0
  1548
chewi@506
  1549
int Mix_SetSoundFonts(const char *paths)
chewi@506
  1550
{
chewi@506
  1551
#ifdef MID_MUSIC
slouken@616
  1552
    if (soundfont_paths) {
slouken@616
  1553
        SDL_free(soundfont_paths);
slouken@616
  1554
        soundfont_paths = NULL;
slouken@616
  1555
    }
chewi@506
  1556
slouken@616
  1557
    if (paths) {
slouken@616
  1558
        if (!(soundfont_paths = SDL_strdup(paths))) {
slouken@616
  1559
            Mix_SetError("Insufficient memory to set SoundFonts");
slouken@616
  1560
            return 0;
slouken@616
  1561
        }
slouken@616
  1562
    }
chewi@506
  1563
#endif
slouken@616
  1564
    return 1;
chewi@506
  1565
}
chewi@506
  1566
chewi@506
  1567
#ifdef MID_MUSIC
dimitris@508
  1568
const char* Mix_GetSoundFonts(void)
chewi@506
  1569
{
slouken@616
  1570
    const char* force = getenv("SDL_FORCE_SOUNDFONTS");
chewi@506
  1571
slouken@616
  1572
    if (!soundfont_paths || (force && force[0] == '1')) {
slouken@616
  1573
        return getenv("SDL_SOUNDFONTS");
slouken@616
  1574
    } else {
slouken@616
  1575
        return soundfont_paths;
slouken@616
  1576
    }
chewi@506
  1577
}
chewi@506
  1578
chewi@506
  1579
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
  1580
{
slouken@616
  1581
    char *context, *path, *paths;
slouken@616
  1582
    const char* cpaths = Mix_GetSoundFonts();
chewi@506
  1583
slouken@616
  1584
    if (!cpaths) {
slouken@616
  1585
        Mix_SetError("No SoundFonts have been requested");
slouken@616
  1586
        return 0;
slouken@616
  1587
    }
chewi@506
  1588
slouken@616
  1589
    if (!(paths = SDL_strdup(cpaths))) {
slouken@616
  1590
        Mix_SetError("Insufficient memory to iterate over SoundFonts");
slouken@616
  1591
        return 0;
slouken@616
  1592
    }
chewi@506
  1593
icculus@513
  1594
#if defined(__MINGW32__) || defined(__MINGW64__)
slouken@616
  1595
    for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1596
#elif defined(_WIN32)
slouken@616
  1597
    for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1598
#else
slouken@616
  1599
    for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1600
#endif
slouken@616
  1601
        if (!function(path, data)) {
slouken@616
  1602
            SDL_free(paths);
slouken@616
  1603
            return 0;
slouken@616
  1604
        }
slouken@616
  1605
    }
chewi@506
  1606
slouken@616
  1607
    SDL_free(paths);
slouken@616
  1608
    return 1;
chewi@506
  1609
}
chewi@506
  1610
#endif