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