music.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 18 Jul 2011 23:35:08 -0700
changeset 538 b5e3fe1b5a09
parent 533 f19368c22ebd
child 541 323b314739ef
permissions -rwxr-xr-x
Mix_LoadMUS(): Don't check MP3 magic when we've got filenames.

This will allow corrupt, but otherwise-playable MP3s to work.

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