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