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