music.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 23:00:03 -0500
changeset 527 1eddea51f162
parent 521 565549e046b0
child 528 bc0dfe8ae026
permissions -rw-r--r--
Partial fix for bug 934 - Native MIDI on OS X does not loop

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