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