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