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