music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2012 14:23:22 -0500
changeset 531 f9c790738ec3
parent 529 9ca74f21d466
child 533 f19368c22ebd
permissions -rwxr-xr-x
This fix really is specific to native MIDI on Mac OS X
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@518
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@140
    22
/* $Id$ */
slouken@138
    23
slouken@3
    24
#include <stdlib.h>
slouken@3
    25
#include <string.h>
slouken@125
    26
#include <ctype.h>
icculus@280
    27
#include <assert.h>
slouken@24
    28
#include "SDL_endian.h"
slouken@24
    29
#include "SDL_audio.h"
slouken@29
    30
#include "SDL_timer.h"
slouken@0
    31
slouken@34
    32
#include "SDL_mixer.h"
slouken@0
    33
slouken@0
    34
#ifdef CMD_MUSIC
slouken@0
    35
#include "music_cmd.h"
slouken@0
    36
#endif
slouken@0
    37
#ifdef WAV_MUSIC
slouken@0
    38
#include "wavestream.h"
slouken@0
    39
#endif
slouken@481
    40
#ifdef MODPLUG_MUSIC
slouken@481
    41
#include "music_modplug.h"
slouken@481
    42
#endif
slouken@411
    43
#ifdef MOD_MUSIC
slouken@411
    44
#include "music_mod.h"
slouken@0
    45
#endif
slouken@0
    46
#ifdef MID_MUSIC
slouken@106
    47
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    48
#    include "timidity.h"
slouken@106
    49
#  endif
chewi@506
    50
#  ifdef USE_FLUIDSYNTH_MIDI
chewi@506
    51
#    include "fluidsynth.h"
chewi@506
    52
#  endif
slouken@106
    53
#  ifdef USE_NATIVE_MIDI
slouken@106
    54
#    include "native_midi.h"
slouken@106
    55
#  endif
slouken@0
    56
#endif
slouken@63
    57
#ifdef OGG_MUSIC
slouken@63
    58
#include "music_ogg.h"
slouken@63
    59
#endif
slouken@0
    60
#ifdef MP3_MUSIC
slouken@314
    61
#include "dynamic_mp3.h"
slouken@357
    62
#endif
slouken@357
    63
#ifdef MP3_MAD_MUSIC
slouken@357
    64
#include "music_mad.h"
slouken@357
    65
#endif
slouken@382
    66
#ifdef FLAC_MUSIC
slouken@382
    67
#include "music_flac.h"
slouken@382
    68
#endif
slouken@0
    69
slouken@357
    70
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
    71
static SDL_AudioSpec used_mixer;
slouken@0
    72
#endif
slouken@0
    73
slouken@357
    74
slouken@42
    75
int volatile music_active = 1;
slouken@42
    76
static int volatile music_stopped = 0;
slouken@0
    77
static int music_loops = 0;
slouken@0
    78
static char *music_cmd = NULL;
slouken@42
    79
static Mix_Music * volatile music_playing = NULL;
slouken@17
    80
static int music_volume = MIX_MAX_VOLUME;
megastep@4
    81
slouken@0
    82
struct _Mix_Music {
slouken@177
    83
	Mix_MusicType type;
slouken@0
    84
	union {
slouken@0
    85
#ifdef CMD_MUSIC
slouken@0
    86
		MusicCMD *cmd;
slouken@0
    87
#endif
slouken@0
    88
#ifdef WAV_MUSIC
slouken@0
    89
		WAVStream *wave;
slouken@0
    90
#endif
slouken@481
    91
#ifdef MODPLUG_MUSIC
slouken@481
    92
		modplug_data *modplug;
slouken@481
    93
#endif
slouken@411
    94
#ifdef MOD_MUSIC
slouken@411
    95
		struct MODULE *module;
slouken@0
    96
#endif
slouken@0
    97
#ifdef MID_MUSIC
slouken@106
    98
#ifdef USE_TIMIDITY_MIDI
slouken@0
    99
		MidiSong *midi;
slouken@106
   100
#endif
chewi@506
   101
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   102
		FluidSynthMidiSong *fluidsynthmidi;
chewi@506
   103
#endif
slouken@98
   104
#ifdef USE_NATIVE_MIDI
slouken@98
   105
		NativeMidiSong *nativemidi;
slouken@98
   106
#endif
slouken@0
   107
#endif
slouken@63
   108
#ifdef OGG_MUSIC
slouken@63
   109
		OGG_music *ogg;
slouken@63
   110
#endif
slouken@0
   111
#ifdef MP3_MUSIC
slouken@0
   112
		SMPEG *mp3;
slouken@0
   113
#endif
slouken@357
   114
#ifdef MP3_MAD_MUSIC
slouken@357
   115
		mad_data *mp3_mad;
slouken@357
   116
#endif
slouken@382
   117
#ifdef FLAC_MUSIC
slouken@382
   118
		FLAC_music *flac;
slouken@382
   119
#endif
slouken@0
   120
	} data;
megastep@4
   121
	Mix_Fading fading;
slouken@17
   122
	int fade_step;
slouken@17
   123
	int fade_steps;
slouken@0
   124
	int error;
slouken@0
   125
};
slouken@29
   126
#ifdef MID_MUSIC
slouken@106
   127
#ifdef USE_TIMIDITY_MIDI
slouken@0
   128
static int timidity_ok;
slouken@142
   129
static int samplesize;
slouken@106
   130
#endif
chewi@506
   131
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   132
static int fluidsynth_ok;
chewi@506
   133
#endif
slouken@98
   134
#ifdef USE_NATIVE_MIDI
slouken@98
   135
static int native_midi_ok;
slouken@98
   136
#endif
slouken@29
   137
#endif
slouken@0
   138
slouken@17
   139
/* Used to calculate fading steps */
slouken@17
   140
static int ms_per_step;
slouken@17
   141
icculus@390
   142
/* rcg06042009 report available decoders at runtime. */
icculus@390
   143
static const char **music_decoders = NULL;
icculus@390
   144
static int num_decoders = 0;
icculus@390
   145
chewi@506
   146
/* Semicolon-separated SoundFont paths */
chewi@506
   147
#ifdef MID_MUSIC
chewi@506
   148
char* soundfont_paths = NULL;
chewi@506
   149
#endif
chewi@506
   150
slouken@466
   151
int Mix_GetNumMusicDecoders(void)
icculus@390
   152
{
icculus@390
   153
	return(num_decoders);
icculus@390
   154
}
icculus@390
   155
icculus@390
   156
const char *Mix_GetMusicDecoder(int index)
icculus@390
   157
{
icculus@390
   158
	if ((index < 0) || (index >= num_decoders)) {
icculus@390
   159
		return NULL;
icculus@390
   160
	}
icculus@390
   161
	return(music_decoders[index]);
icculus@390
   162
}
icculus@390
   163
icculus@390
   164
static void add_music_decoder(const char *decoder)
icculus@390
   165
{
slouken@412
   166
	void *ptr = realloc(music_decoders, (num_decoders + 1) * sizeof (const char **));
icculus@390
   167
	if (ptr == NULL) {
icculus@390
   168
		return;  /* oh well, go on without it. */
icculus@390
   169
	}
slouken@411
   170
	music_decoders = (const char **) ptr;
icculus@390
   171
	music_decoders[num_decoders++] = decoder;
icculus@390
   172
}
icculus@390
   173
megastep@10
   174
/* Local low-level functions prototypes */
icculus@237
   175
static void music_internal_initialize_volume(void);
slouken@173
   176
static void music_internal_volume(int volume);
slouken@173
   177
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   178
static int  music_internal_position(double position);
slouken@173
   179
static int  music_internal_playing();
slouken@173
   180
static void music_internal_halt(void);
megastep@7
   181
slouken@29
   182
slouken@29
   183
/* Support for hooking when the music has finished */
slouken@29
   184
static void (*music_finished_hook)(void) = NULL;
slouken@29
   185
slouken@29
   186
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   187
{
slouken@29
   188
	SDL_LockAudio();
slouken@29
   189
	music_finished_hook = music_finished;
slouken@29
   190
	SDL_UnlockAudio();
slouken@29
   191
}
slouken@29
   192
slouken@29
   193
icculus@281
   194
/* If music isn't playing, halt it if no looping is required, restart it */
icculus@281
   195
/* otherwhise. NOP if the music is playing */
icculus@281
   196
static int music_halt_or_loop (void)
icculus@281
   197
{
icculus@281
   198
	/* Restart music if it has to loop */
slouken@528
   199
	
icculus@281
   200
	if (!music_internal_playing()) 
icculus@281
   201
	{
icculus@281
   202
		/* Restart music if it has to loop at a high level */
icculus@281
   203
		if (music_loops && --music_loops)
icculus@281
   204
		{
icculus@281
   205
			Mix_Fading current_fade = music_playing->fading;
icculus@281
   206
			music_internal_play(music_playing, 0.0);
icculus@281
   207
			music_playing->fading = current_fade;
icculus@281
   208
		} 
icculus@281
   209
		else 
icculus@281
   210
		{
icculus@281
   211
			music_internal_halt();
icculus@281
   212
			if (music_finished_hook)
icculus@281
   213
				music_finished_hook();
icculus@281
   214
			
icculus@281
   215
			return 0;
icculus@281
   216
		}
icculus@281
   217
	}
icculus@281
   218
	
icculus@281
   219
	return 1;
icculus@281
   220
}
icculus@281
   221
icculus@281
   222
icculus@281
   223
slouken@0
   224
/* Mixing function */
slouken@0
   225
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   226
{
slouken@407
   227
	int left = 0;
slouken@407
   228
slouken@173
   229
	if ( music_playing && music_active ) {
megastep@13
   230
		/* Handle fading */
slouken@17
   231
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   232
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@173
   233
				int volume;
slouken@26
   234
				int fade_step = music_playing->fade_step;
slouken@26
   235
				int fade_steps = music_playing->fade_steps;
slouken@17
   236
megastep@4
   237
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   238
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@17
   239
				} else { /* Fading in */
slouken@173
   240
					volume = (music_volume * fade_step) / fade_steps;
slouken@17
   241
				}
slouken@173
   242
				music_internal_volume(volume);
slouken@17
   243
			} else {
slouken@17
   244
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   245
					music_internal_halt();
slouken@173
   246
					if ( music_finished_hook ) {
slouken@173
   247
						music_finished_hook();
slouken@173
   248
					}
megastep@4
   249
					return;
megastep@4
   250
				}
megastep@4
   251
				music_playing->fading = MIX_NO_FADING;
megastep@4
   252
			}
megastep@4
   253
		}
icculus@281
   254
		
slouken@528
   255
		music_halt_or_loop();
slouken@528
   256
		if (!music_internal_playing())
slouken@528
   257
			return;
slouken@528
   258
slouken@0
   259
		switch (music_playing->type) {
slouken@0
   260
#ifdef CMD_MUSIC
slouken@0
   261
			case MUS_CMD:
slouken@0
   262
				/* The playing is done externally */
slouken@0
   263
				break;
slouken@0
   264
#endif
slouken@0
   265
#ifdef WAV_MUSIC
slouken@0
   266
			case MUS_WAV:
slouken@407
   267
				left = WAVStream_PlaySome(stream, len);
slouken@0
   268
				break;
slouken@0
   269
#endif
slouken@481
   270
#ifdef MODPLUG_MUSIC
slouken@481
   271
			case MUS_MODPLUG:
slouken@481
   272
				left = modplug_playAudio(music_playing->data.modplug, stream, len);
slouken@481
   273
				break;
slouken@481
   274
#endif
slouken@411
   275
#ifdef MOD_MUSIC
slouken@0
   276
			case MUS_MOD:
slouken@411
   277
				left = MOD_playAudio(music_playing->data.module, stream, len);
slouken@0
   278
				break;
slouken@0
   279
#endif
slouken@0
   280
#ifdef MID_MUSIC
chewi@506
   281
			case MUS_MID:
slouken@529
   282
#ifdef USE_NATIVE_MIDI
slouken@529
   283
  				if ( native_midi_ok ) {
slouken@529
   284
					/* Native midi is handled asynchronously */
slouken@529
   285
					goto skip;
slouken@529
   286
	  			}
slouken@529
   287
#endif
chewi@506
   288
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   289
				if ( fluidsynth_ok ) {
chewi@506
   290
					fluidsynth_playsome(music_playing->data.fluidsynthmidi, stream, len);
slouken@528
   291
					goto skip;
chewi@506
   292
				}
chewi@506
   293
#endif
slouken@106
   294
#ifdef USE_TIMIDITY_MIDI
slouken@98
   295
				if ( timidity_ok ) {
slouken@98
   296
					int samples = len / samplesize;
slouken@98
   297
  					Timidity_PlaySome(stream, samples);
slouken@528
   298
					goto skip;
slouken@98
   299
				}
chewi@506
   300
#endif
slouken@0
   301
				break;
slouken@0
   302
#endif
slouken@63
   303
#ifdef OGG_MUSIC
slouken@63
   304
			case MUS_OGG:
icculus@281
   305
				
slouken@407
   306
				left = OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   307
				break;
slouken@63
   308
#endif
slouken@382
   309
#ifdef FLAC_MUSIC
slouken@382
   310
			case MUS_FLAC:
slouken@407
   311
				left = FLAC_playAudio(music_playing->data.flac, stream, len);
slouken@382
   312
				break;
slouken@382
   313
#endif
slouken@0
   314
#ifdef MP3_MUSIC
slouken@26
   315
			case MUS_MP3:
slouken@407
   316
				left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
slouken@0
   317
				break;
slouken@0
   318
#endif
slouken@357
   319
#ifdef MP3_MAD_MUSIC
slouken@357
   320
			case MUS_MP3_MAD:
slouken@407
   321
				left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
slouken@357
   322
				break;
slouken@357
   323
#endif
slouken@0
   324
			default:
slouken@0
   325
				/* Unknown music type?? */
slouken@0
   326
				break;
slouken@0
   327
		}
slouken@528
   328
	}
slouken@407
   329
slouken@528
   330
skip:
slouken@528
   331
	/* Handle seamless music looping */
slouken@528
   332
	if (left > 0 && left < len) {
icculus@509
   333
		music_halt_or_loop();
slouken@528
   334
		if (music_internal_playing())
icculus@509
   335
			music_mixer(udata, stream+(len-left), left);
slouken@407
   336
	}
slouken@0
   337
}
slouken@0
   338
slouken@0
   339
/* Initialize the music players with a certain desired audio format */
slouken@0
   340
int open_music(SDL_AudioSpec *mixer)
slouken@0
   341
{
slouken@0
   342
#ifdef WAV_MUSIC
slouken@479
   343
	if ( WAVStream_Init(mixer) == 0 ) {
icculus@390
   344
		add_music_decoder("WAVE");
slouken@0
   345
	}
slouken@0
   346
#endif
slouken@481
   347
#ifdef MODPLUG_MUSIC
icculus@484
   348
	if ( modplug_init(mixer) == 0 ) {
slouken@481
   349
		add_music_decoder("MODPLUG");
slouken@481
   350
	}
slouken@481
   351
#endif
slouken@411
   352
#ifdef MOD_MUSIC
slouken@479
   353
	if ( MOD_init(mixer) == 0 ) {
slouken@411
   354
		add_music_decoder("MIKMOD");
slouken@0
   355
	}
slouken@0
   356
#endif
slouken@0
   357
#ifdef MID_MUSIC
slouken@106
   358
#ifdef USE_TIMIDITY_MIDI
slouken@142
   359
	samplesize = mixer->size / mixer->samples;
slouken@100
   360
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@102
   361
	                    mixer->channels, mixer->samples) == 0 ) {
slouken@100
   362
		timidity_ok = 1;
icculus@390
   363
		add_music_decoder("TIMIDITY");
slouken@0
   364
	} else {
slouken@0
   365
		timidity_ok = 0;
slouken@0
   366
	}
slouken@106
   367
#endif
chewi@506
   368
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   369
	if ( fluidsynth_init(mixer) == 0 ) {
chewi@506
   370
		fluidsynth_ok = 1;
chewi@506
   371
		add_music_decoder("FLUIDSYNTH");
chewi@506
   372
	} else {
chewi@506
   373
		fluidsynth_ok = 0;
chewi@506
   374
	}
chewi@506
   375
#endif
slouken@100
   376
#ifdef USE_NATIVE_MIDI
chewi@506
   377
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   378
	native_midi_ok = !fluidsynth_ok;
chewi@506
   379
	if ( native_midi_ok )
chewi@506
   380
#endif
slouken@106
   381
#ifdef USE_TIMIDITY_MIDI
chewi@506
   382
		native_midi_ok = !timidity_ok;
slouken@415
   383
	if ( !native_midi_ok ) {
slouken@415
   384
		native_midi_ok = (getenv("SDL_NATIVE_MUSIC") != NULL);
slouken@415
   385
	}
slouken@106
   386
	if ( native_midi_ok )
slouken@106
   387
#endif
slouken@101
   388
		native_midi_ok = native_midi_detect();
icculus@390
   389
	if ( native_midi_ok )
icculus@390
   390
		add_music_decoder("NATIVEMIDI");
slouken@100
   391
#endif
slouken@0
   392
#endif
slouken@63
   393
#ifdef OGG_MUSIC
slouken@479
   394
	if ( OGG_init(mixer) == 0 ) {
icculus@390
   395
		add_music_decoder("OGG");
slouken@63
   396
	}
slouken@63
   397
#endif
slouken@382
   398
#ifdef FLAC_MUSIC
slouken@479
   399
	if ( FLAC_init(mixer) == 0 ) {
icculus@390
   400
		add_music_decoder("FLAC");
slouken@382
   401
	}
slouken@382
   402
#endif
slouken@357
   403
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
   404
	/* Keep a copy of the mixer */
slouken@0
   405
	used_mixer = *mixer;
icculus@390
   406
	add_music_decoder("MP3");
slouken@0
   407
#endif
icculus@390
   408
slouken@42
   409
	music_playing = NULL;
megastep@7
   410
	music_stopped = 0;
slouken@0
   411
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   412
slouken@26
   413
	/* Calculate the number of ms for each callback */
slouken@44
   414
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   415
slouken@0
   416
	return(0);
slouken@0
   417
}
slouken@0
   418
slouken@125
   419
/* Portable case-insensitive string compare function */
slouken@125
   420
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   421
{
slouken@125
   422
	while ( *str1 && *str2 ) {
slouken@125
   423
		if ( toupper((unsigned char)*str1) !=
slouken@125
   424
		     toupper((unsigned char)*str2) )
slouken@125
   425
			break;
slouken@125
   426
		++str1;
slouken@125
   427
		++str2;
slouken@125
   428
	}
slouken@125
   429
	return (!*str1 && !*str2);
slouken@125
   430
}
slouken@125
   431
slouken@0
   432
/* Load a music file */
slouken@0
   433
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   434
{
slouken@0
   435
	FILE *fp;
slouken@125
   436
	char *ext;
slouken@245
   437
	Uint8 magic[5], moremagic[9];
slouken@0
   438
	Mix_Music *music;
slouken@0
   439
slouken@0
   440
	/* Figure out what kind of file this is */
slouken@0
   441
	fp = fopen(file, "rb");
slouken@0
   442
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   443
		if ( fp != NULL ) {
slouken@0
   444
			fclose(fp);
slouken@0
   445
		}
slouken@26
   446
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   447
		return(NULL);
slouken@0
   448
	}
slouken@245
   449
	if (!fread(moremagic, 8, 1, fp)) {
slouken@245
   450
		Mix_SetError("Couldn't read from '%s'", file);
slouken@245
   451
		return(NULL);
slouken@245
   452
	}
slouken@0
   453
	magic[4] = '\0';
slouken@245
   454
	moremagic[8] = '\0';
slouken@0
   455
	fclose(fp);
slouken@0
   456
slouken@125
   457
	/* Figure out the file extension, so we can determine the type */
slouken@125
   458
	ext = strrchr(file, '.');
slouken@125
   459
	if ( ext ) ++ext; /* skip the dot in the extension */
slouken@125
   460
slouken@0
   461
	/* Allocate memory for the music structure */
slouken@0
   462
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   463
	if ( music == NULL ) {
slouken@26
   464
		Mix_SetError("Out of memory");
slouken@0
   465
		return(NULL);
slouken@0
   466
	}
slouken@0
   467
	music->error = 0;
slouken@0
   468
slouken@0
   469
#ifdef CMD_MUSIC
slouken@0
   470
	if ( music_cmd ) {
slouken@0
   471
		music->type = MUS_CMD;
slouken@0
   472
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   473
		if ( music->data.cmd == NULL ) {
slouken@0
   474
			music->error = 1;
slouken@0
   475
		}
slouken@0
   476
	} else
slouken@0
   477
#endif
slouken@0
   478
#ifdef WAV_MUSIC
slouken@0
   479
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   480
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   481
	 */
slouken@135
   482
	if ( (ext && MIX_string_equals(ext, "WAV")) ||
slouken@245
   483
	     ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@57
   484
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   485
		music->type = MUS_WAV;
slouken@57
   486
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   487
		if ( music->data.wave == NULL ) {
slouken@135
   488
		  	Mix_SetError("Unable to load WAV file");
slouken@0
   489
			music->error = 1;
slouken@0
   490
		}
slouken@0
   491
	} else
slouken@0
   492
#endif
slouken@0
   493
#ifdef MID_MUSIC
slouken@0
   494
	/* MIDI files have the magic four bytes "MThd" */
slouken@135
   495
	if ( (ext && MIX_string_equals(ext, "MID")) ||
slouken@135
   496
	     (ext && MIX_string_equals(ext, "MIDI")) ||
slouken@245
   497
	     strcmp((char *)magic, "MThd") == 0  ||
slouken@245
   498
	     ( strcmp((char *)magic, "RIFF") == 0  &&
slouken@245
   499
	  	strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
slouken@0
   500
		music->type = MUS_MID;
slouken@98
   501
#ifdef USE_NATIVE_MIDI
slouken@98
   502
  		if ( native_midi_ok ) {
slouken@529
   503
/*printf("Native MIDI\n");*/
slouken@401
   504
  			music->data.nativemidi = native_midi_loadsong(file);
slouken@98
   505
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   506
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   507
			  	music->error = 1;
slouken@98
   508
			}
chewi@506
   509
			goto skip;
chewi@506
   510
	  	}
chewi@506
   511
#endif
chewi@506
   512
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   513
		if ( fluidsynth_ok ) {
slouken@529
   514
/*printf("FluidSynth MIDI\n");*/
chewi@506
   515
			music->data.fluidsynthmidi = fluidsynth_loadsong(file);
chewi@506
   516
			if ( music->data.fluidsynthmidi == NULL ) {
chewi@506
   517
				music->error = 1;
chewi@506
   518
			}
chewi@506
   519
			goto skip;
chewi@506
   520
		}
slouken@98
   521
#endif
slouken@106
   522
#ifdef USE_TIMIDITY_MIDI
slouken@0
   523
		if ( timidity_ok ) {
slouken@529
   524
/*printf("Timidity MIDI\n");*/
slouken@433
   525
			music->data.midi = Timidity_LoadSong(file);
slouken@0
   526
			if ( music->data.midi == NULL ) {
slouken@26
   527
				Mix_SetError("%s", Timidity_Error());
slouken@0
   528
				music->error = 1;
slouken@0
   529
			}
slouken@98
   530
		} else {
slouken@26
   531
			Mix_SetError("%s", Timidity_Error());
slouken@0
   532
			music->error = 1;
slouken@0
   533
		}
slouken@106
   534
#endif
slouken@0
   535
	} else
slouken@0
   536
#endif
slouken@63
   537
#ifdef OGG_MUSIC
slouken@63
   538
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@135
   539
	if ( (ext && MIX_string_equals(ext, "OGG")) ||
slouken@133
   540
	     strcmp((char *)magic, "OggS") == 0 ) {
slouken@63
   541
		music->type = MUS_OGG;
slouken@63
   542
		music->data.ogg = OGG_new(file);
slouken@63
   543
		if ( music->data.ogg == NULL ) {
slouken@63
   544
			music->error = 1;
slouken@63
   545
		}
slouken@63
   546
	} else
slouken@63
   547
#endif
slouken@382
   548
#ifdef FLAC_MUSIC
slouken@382
   549
	/* FLAC files have the magic four bytes "fLaC" */
slouken@382
   550
	if ( (ext && MIX_string_equals(ext, "FLAC")) ||
slouken@382
   551
		 strcmp((char *)magic, "fLaC") == 0 ) {
slouken@382
   552
		music->type = MUS_FLAC;
slouken@382
   553
		music->data.flac = FLAC_new(file);
slouken@382
   554
		if ( music->data.flac == NULL ) {
slouken@382
   555
			music->error = 1;
slouken@382
   556
		}
slouken@382
   557
	} else
slouken@382
   558
#endif
slouken@0
   559
#ifdef MP3_MUSIC
slouken@135
   560
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
icculus@186
   561
	     (ext && MIX_string_equals(ext, "MP3")) ||
slouken@135
   562
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@398
   563
	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
slouken@398
   564
	     (strncmp((char *)magic, "ID3", 3) == 0) ) {
slouken@470
   565
		if ( Mix_Init(MIX_INIT_MP3) ) {
slouken@314
   566
			SMPEG_Info info;
slouken@314
   567
			music->type = MUS_MP3;
slouken@314
   568
			music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
slouken@314
   569
			if ( !info.has_audio ) {
slouken@314
   570
				Mix_SetError("MPEG file does not have any audio stream.");
slouken@314
   571
				music->error = 1;
slouken@314
   572
			} else {
slouken@314
   573
				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@314
   574
			}
slouken@314
   575
		} else {
slouken@0
   576
			music->error = 1;
slouken@0
   577
		}
slouken@0
   578
	} else
slouken@0
   579
#endif
slouken@357
   580
#ifdef MP3_MAD_MUSIC
slouken@357
   581
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
slouken@357
   582
	     (ext && MIX_string_equals(ext, "MP3")) ||
slouken@357
   583
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@357
   584
	     (ext && MIX_string_equals(ext, "MAD")) ||
slouken@398
   585
	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ||
slouken@398
   586
	     (strncmp((char *)magic, "ID3", 3) == 0) ) {
slouken@357
   587
		music->type = MUS_MP3_MAD;
slouken@357
   588
		music->data.mp3_mad = mad_openFile(file, &used_mixer);
slouken@357
   589
		if (music->data.mp3_mad == 0) {
slouken@357
   590
		    Mix_SetError("Could not initialize MPEG stream.");
slouken@357
   591
			music->error = 1;
slouken@357
   592
		}
slouken@357
   593
	} else
slouken@357
   594
#endif
slouken@481
   595
#ifdef MODPLUG_MUSIC
slouken@481
   596
	if ( 1 ) {
slouken@481
   597
		music->type = MUS_MODPLUG;
slouken@481
   598
		music->data.modplug = modplug_new(file);
slouken@481
   599
		if ( music->data.modplug == NULL ) {
slouken@481
   600
			music->error = 1;
slouken@481
   601
		}
slouken@481
   602
	} else
slouken@481
   603
#endif
slouken@411
   604
#ifdef MOD_MUSIC
slouken@0
   605
	if ( 1 ) {
slouken@0
   606
		music->type = MUS_MOD;
slouken@411
   607
		music->data.module = MOD_new(file);
slouken@0
   608
		if ( music->data.module == NULL ) {
slouken@0
   609
			music->error = 1;
slouken@0
   610
		}
slouken@0
   611
	} else
slouken@0
   612
#endif
slouken@0
   613
	{
slouken@26
   614
		Mix_SetError("Unrecognized music format");
slouken@0
   615
		music->error = 1;
slouken@0
   616
	}
chewi@506
   617
chewi@506
   618
skip:
slouken@0
   619
	if ( music->error ) {
slouken@0
   620
		free(music);
slouken@0
   621
		music = NULL;
slouken@0
   622
	}
slouken@0
   623
	return(music);
slouken@0
   624
}
slouken@0
   625
slouken@0
   626
/* Free a music chunk previously loaded */
slouken@0
   627
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   628
{
slouken@0
   629
	if ( music ) {
slouken@173
   630
		/* Stop the music if it's currently playing */
slouken@173
   631
		SDL_LockAudio();
slouken@173
   632
		if ( music == music_playing ) {
slouken@173
   633
			/* Wait for any fade out to finish */
slouken@173
   634
			while ( music->fading == MIX_FADING_OUT ) {
slouken@173
   635
				SDL_UnlockAudio();
slouken@173
   636
				SDL_Delay(100);
slouken@173
   637
				SDL_LockAudio();
slouken@173
   638
			}
slouken@173
   639
			if ( music == music_playing ) {
slouken@173
   640
				music_internal_halt();
megastep@4
   641
			}
slouken@0
   642
		}
slouken@173
   643
		SDL_UnlockAudio();
slouken@0
   644
		switch (music->type) {
slouken@0
   645
#ifdef CMD_MUSIC
slouken@0
   646
			case MUS_CMD:
slouken@0
   647
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   648
				break;
slouken@0
   649
#endif
slouken@0
   650
#ifdef WAV_MUSIC
slouken@0
   651
			case MUS_WAV:
slouken@0
   652
				WAVStream_FreeSong(music->data.wave);
slouken@0
   653
				break;
slouken@0
   654
#endif
slouken@481
   655
#ifdef MODPLUG_MUSIC
slouken@481
   656
			case MUS_MODPLUG:
slouken@481
   657
				modplug_delete(music->data.modplug);
slouken@481
   658
				break;
slouken@481
   659
#endif
slouken@411
   660
#ifdef MOD_MUSIC
slouken@0
   661
			case MUS_MOD:
slouken@411
   662
				MOD_delete(music->data.module);
slouken@0
   663
				break;
slouken@0
   664
#endif
slouken@0
   665
#ifdef MID_MUSIC
slouken@0
   666
			case MUS_MID:
slouken@98
   667
#ifdef USE_NATIVE_MIDI
slouken@98
   668
  				if ( native_midi_ok ) {
slouken@98
   669
					native_midi_freesong(music->data.nativemidi);
chewi@506
   670
					goto skip;
chewi@506
   671
				}
chewi@506
   672
#endif
chewi@506
   673
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   674
				if ( fluidsynth_ok ) {
chewi@506
   675
					fluidsynth_freesong(music->data.fluidsynthmidi);
chewi@506
   676
					goto skip;
chewi@506
   677
				}
slouken@98
   678
#endif
slouken@106
   679
#ifdef USE_TIMIDITY_MIDI
slouken@98
   680
				if ( timidity_ok ) {
slouken@98
   681
					Timidity_FreeSong(music->data.midi);
chewi@506
   682
					goto skip;
slouken@98
   683
				}
slouken@106
   684
#endif
slouken@0
   685
				break;
slouken@0
   686
#endif
slouken@63
   687
#ifdef OGG_MUSIC
slouken@63
   688
			case MUS_OGG:
slouken@63
   689
				OGG_delete(music->data.ogg);
slouken@63
   690
				break;
slouken@63
   691
#endif
slouken@382
   692
#ifdef FLAC_MUSIC
slouken@382
   693
			case MUS_FLAC:
slouken@382
   694
				FLAC_delete(music->data.flac);
slouken@382
   695
				break;
slouken@382
   696
#endif
slouken@0
   697
#ifdef MP3_MUSIC
slouken@26
   698
			case MUS_MP3:
slouken@314
   699
				smpeg.SMPEG_delete(music->data.mp3);
slouken@0
   700
				break;
slouken@0
   701
#endif
slouken@357
   702
#ifdef MP3_MAD_MUSIC
slouken@357
   703
			case MUS_MP3_MAD:
slouken@357
   704
				mad_closeFile(music->data.mp3_mad);
slouken@357
   705
				break;
slouken@357
   706
#endif
slouken@0
   707
			default:
slouken@0
   708
				/* Unknown music type?? */
slouken@0
   709
				break;
slouken@0
   710
		}
chewi@506
   711
chewi@506
   712
    skip:
slouken@0
   713
		free(music);
slouken@0
   714
	}
slouken@0
   715
}
slouken@0
   716
slouken@177
   717
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   718
   music, if 'music' is NULL.
slouken@177
   719
*/
slouken@177
   720
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   721
{
slouken@177
   722
	Mix_MusicType type = MUS_NONE;
slouken@177
   723
slouken@177
   724
	if ( music ) {
slouken@177
   725
		type = music->type;
slouken@177
   726
	} else {
slouken@177
   727
		SDL_LockAudio();
slouken@177
   728
		if ( music_playing ) {
slouken@177
   729
			type = music_playing->type;
slouken@177
   730
		}
slouken@177
   731
		SDL_UnlockAudio();
slouken@177
   732
	}
slouken@177
   733
	return(type);
slouken@177
   734
}
slouken@177
   735
slouken@173
   736
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   737
 */
slouken@173
   738
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   739
{
slouken@173
   740
	int retval = 0;
megastep@10
   741
slouken@531
   742
#ifdef __MACOSX__
slouken@531
   743
	/* This fixes a bug with native MIDI on Mac OS X, where you
slouken@531
   744
	   can't really stop and restart MIDI from the audio callback.
slouken@529
   745
	*/
slouken@531
   746
	if ( music == music_playing && music->type == MUS_MID && native_midi_ok ) {
slouken@531
   747
		/* Just a seek suffices to restart playing */
slouken@531
   748
		music_internal_position(position);
slouken@531
   749
		return 0;
slouken@529
   750
	}
slouken@531
   751
#endif
slouken@529
   752
slouken@173
   753
	/* Note the music we're playing */
slouken@173
   754
	if ( music_playing ) {
slouken@173
   755
		music_internal_halt();
slouken@173
   756
	}
slouken@173
   757
	music_playing = music;
slouken@173
   758
slouken@173
   759
	/* Set the initial volume */
icculus@237
   760
	if ( music->type != MUS_MOD ) {
icculus@237
   761
		music_internal_initialize_volume();
slouken@173
   762
	}
slouken@173
   763
slouken@173
   764
	/* Set up for playback */
slouken@0
   765
	switch (music->type) {
slouken@0
   766
#ifdef CMD_MUSIC
slouken@173
   767
	    case MUS_CMD:
slouken@173
   768
		MusicCMD_Start(music->data.cmd);
slouken@173
   769
		break;
slouken@0
   770
#endif
slouken@0
   771
#ifdef WAV_MUSIC
slouken@173
   772
	    case MUS_WAV:
slouken@173
   773
		WAVStream_Start(music->data.wave);
slouken@173
   774
		break;
slouken@0
   775
#endif
slouken@481
   776
#ifdef MODPLUG_MUSIC
slouken@481
   777
	    case MUS_MODPLUG:
slouken@481
   778
		/* can't set volume until file is loaded, so finally set it now */
slouken@481
   779
		music_internal_initialize_volume();
slouken@481
   780
		modplug_play(music->data.modplug);
slouken@481
   781
		break;
slouken@481
   782
#endif
slouken@411
   783
#ifdef MOD_MUSIC
slouken@173
   784
	    case MUS_MOD:
slouken@411
   785
		MOD_play(music->data.module);
icculus@237
   786
		/* Player_SetVolume() does nothing before Player_Start() */
icculus@237
   787
		music_internal_initialize_volume();
slouken@173
   788
		break;
slouken@0
   789
#endif
slouken@0
   790
#ifdef MID_MUSIC
slouken@173
   791
	    case MUS_MID:
slouken@98
   792
#ifdef USE_NATIVE_MIDI
slouken@173
   793
		if ( native_midi_ok ) {
slouken@173
   794
			native_midi_start(music->data.nativemidi);
chewi@506
   795
			goto skip;
chewi@506
   796
		}
chewi@506
   797
#endif
chewi@506
   798
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   799
		if (fluidsynth_ok ) {
chewi@506
   800
			fluidsynth_start(music->data.fluidsynthmidi);
chewi@506
   801
			goto skip;
chewi@506
   802
		}
slouken@98
   803
#endif
slouken@106
   804
#ifdef USE_TIMIDITY_MIDI
slouken@173
   805
		if ( timidity_ok ) {
slouken@173
   806
			Timidity_Start(music->data.midi);
chewi@506
   807
			goto skip;
slouken@173
   808
		}
slouken@106
   809
#endif
slouken@173
   810
		break;
slouken@0
   811
#endif
slouken@63
   812
#ifdef OGG_MUSIC
slouken@173
   813
	    case MUS_OGG:
slouken@173
   814
		OGG_play(music->data.ogg);
slouken@173
   815
		break;
slouken@63
   816
#endif
slouken@382
   817
#ifdef FLAC_MUSIC
slouken@382
   818
	    case MUS_FLAC:
slouken@382
   819
		FLAC_play(music->data.flac);
slouken@382
   820
		break;
slouken@382
   821
#endif
slouken@0
   822
#ifdef MP3_MUSIC
slouken@173
   823
	    case MUS_MP3:
slouken@314
   824
		smpeg.SMPEG_enableaudio(music->data.mp3,1);
slouken@314
   825
		smpeg.SMPEG_enablevideo(music->data.mp3,0);
slouken@314
   826
		smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
   827
		break;
slouken@0
   828
#endif
slouken@357
   829
#ifdef MP3_MAD_MUSIC
slouken@357
   830
	    case MUS_MP3_MAD:
slouken@357
   831
		mad_start(music->data.mp3_mad);
slouken@357
   832
		break;
slouken@357
   833
#endif
slouken@173
   834
	    default:
slouken@173
   835
		Mix_SetError("Can't play unknown music type");
slouken@173
   836
		retval = -1;
slouken@173
   837
		break;
slouken@0
   838
	}
slouken@173
   839
chewi@506
   840
skip:
slouken@173
   841
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   842
	if ( retval == 0 ) {
slouken@173
   843
		if ( position > 0.0 ) {
slouken@173
   844
			if ( music_internal_position(position) < 0 ) {
slouken@173
   845
				Mix_SetError("Position not implemented for music type");
slouken@173
   846
				retval = -1;
slouken@173
   847
			}
slouken@173
   848
		} else {
slouken@173
   849
			music_internal_position(0.0);
slouken@173
   850
		}
slouken@173
   851
	}
slouken@173
   852
slouken@173
   853
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   854
	if ( retval < 0 ) {
slouken@173
   855
		music_playing = NULL;
slouken@173
   856
	}
slouken@173
   857
	return(retval);
slouken@173
   858
}
slouken@173
   859
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   860
{
slouken@173
   861
	int retval;
slouken@173
   862
slouken@499
   863
	if ( ms_per_step == 0 ) {
slouken@499
   864
		SDL_SetError("Audio device hasn't been opened");
slouken@499
   865
		return(-1);
slouken@499
   866
	}
slouken@499
   867
slouken@173
   868
	/* Don't play null pointers :-) */
slouken@173
   869
	if ( music == NULL ) {
slouken@173
   870
		Mix_SetError("music parameter was NULL");
slouken@173
   871
		return(-1);
slouken@173
   872
	}
slouken@173
   873
slouken@173
   874
	/* Setup the data */
slouken@173
   875
	if ( ms ) {
slouken@173
   876
		music->fading = MIX_FADING_IN;
slouken@173
   877
	} else {
slouken@173
   878
		music->fading = MIX_NO_FADING;
slouken@173
   879
	}
slouken@173
   880
	music->fade_step = 0;
slouken@173
   881
	music->fade_steps = ms/ms_per_step;
slouken@173
   882
slouken@173
   883
	/* Play the puppy */
slouken@173
   884
	SDL_LockAudio();
slouken@173
   885
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   886
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   887
		SDL_UnlockAudio();
slouken@173
   888
		SDL_Delay(100);
slouken@173
   889
		SDL_LockAudio();
slouken@173
   890
	}
slouken@173
   891
	music_active = 1;
slouken@173
   892
	music_loops = loops;
slouken@173
   893
	retval = music_internal_play(music, position);
slouken@173
   894
	SDL_UnlockAudio();
slouken@173
   895
slouken@173
   896
	return(retval);
slouken@173
   897
}
slouken@173
   898
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   899
{
slouken@173
   900
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   901
}
slouken@173
   902
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   903
{
slouken@173
   904
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   905
}
megastep@10
   906
slouken@173
   907
/* Set the playing music position */
slouken@173
   908
int music_internal_position(double position)
megastep@10
   909
{
slouken@173
   910
	int retval = 0;
slouken@173
   911
slouken@173
   912
	switch (music_playing->type) {
slouken@481
   913
#ifdef MODPLUG_MUSIC
slouken@481
   914
	    case MUS_MODPLUG:
slouken@481
   915
		modplug_jump_to_time(music_playing->data.modplug, position);
slouken@481
   916
		break;
slouken@481
   917
#endif
slouken@411
   918
#ifdef MOD_MUSIC
slouken@173
   919
	    case MUS_MOD:
slouken@411
   920
		MOD_jump_to_time(music_playing->data.module, position);
slouken@173
   921
		break;
slouken@173
   922
#endif
slouken@529
   923
#ifdef MID_MUSIC
slouken@529
   924
	    case MUS_MID:
slouken@529
   925
#ifdef USE_NATIVE_MIDI
slouken@529
   926
		if ( native_midi_ok ) {
slouken@529
   927
			retval = native_midi_jump_to_time(music_playing->data.nativemidi, position);
slouken@529
   928
			break;
slouken@529
   929
		}
slouken@529
   930
#endif
slouken@529
   931
		/* TODO: Implement this for other music backends */
slouken@529
   932
		retval = -1;
slouken@529
   933
		break;
slouken@529
   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