music.c
author Ozkan Sezer
Sat, 14 Dec 2019 18:14:10 +0300
branchSDL-1.2
changeset 1081 119300487b73
parent 945 c606c85c8863
permissions -rw-r--r--
backport default/2.0 commits 6c6adcc and 1e215d1 for fluidsynth support:

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