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