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