music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2012 00:47:32 -0500
changeset 529 9ca74f21d466
parent 528 bc0dfe8ae026
child 531 f9c790738ec3
permissions -rw-r--r--
Fixed bug 934 - Native MIDI on OS X does not loop
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@529
   742
	/* Try to seek instead of do a full halt and restart.
slouken@529
   743
	   This fixes a bug with native MIDI on Mac OS X, where you
slouken@529
   744
	   can't really stop and restart from the audio callback.
slouken@529
   745
	*/
slouken@529
   746
	if ( music == music_playing
slouken@529
   747
	  /* MUS_MOD position isn't time, it's pattern order number (??) */
slouken@529
   748
	     && music->type != MUS_MOD ) {
slouken@529
   749
		if ( music_internal_position(position) == 0 ) {
slouken@529
   750
			return 0;
slouken@529
   751
		}
slouken@529
   752
	}
slouken@529
   753
slouken@173
   754
	/* Note the music we're playing */
slouken@173
   755
	if ( music_playing ) {
slouken@173
   756
		music_internal_halt();
slouken@173
   757
	}
slouken@173
   758
	music_playing = music;
slouken@173
   759
slouken@173
   760
	/* Set the initial volume */
icculus@237
   761
	if ( music->type != MUS_MOD ) {
icculus@237
   762
		music_internal_initialize_volume();
slouken@173
   763
	}
slouken@173
   764
slouken@173
   765
	/* Set up for playback */
slouken@0
   766
	switch (music->type) {
slouken@0
   767
#ifdef CMD_MUSIC
slouken@173
   768
	    case MUS_CMD:
slouken@173
   769
		MusicCMD_Start(music->data.cmd);
slouken@173
   770
		break;
slouken@0
   771
#endif
slouken@0
   772
#ifdef WAV_MUSIC
slouken@173
   773
	    case MUS_WAV:
slouken@173
   774
		WAVStream_Start(music->data.wave);
slouken@173
   775
		break;
slouken@0
   776
#endif
slouken@481
   777
#ifdef MODPLUG_MUSIC
slouken@481
   778
	    case MUS_MODPLUG:
slouken@481
   779
		/* can't set volume until file is loaded, so finally set it now */
slouken@481
   780
		music_internal_initialize_volume();
slouken@481
   781
		modplug_play(music->data.modplug);
slouken@481
   782
		break;
slouken@481
   783
#endif
slouken@411
   784
#ifdef MOD_MUSIC
slouken@173
   785
	    case MUS_MOD:
slouken@411
   786
		MOD_play(music->data.module);
icculus@237
   787
		/* Player_SetVolume() does nothing before Player_Start() */
icculus@237
   788
		music_internal_initialize_volume();
slouken@173
   789
		break;
slouken@0
   790
#endif
slouken@0
   791
#ifdef MID_MUSIC
slouken@173
   792
	    case MUS_MID:
slouken@98
   793
#ifdef USE_NATIVE_MIDI
slouken@173
   794
		if ( native_midi_ok ) {
slouken@173
   795
			native_midi_start(music->data.nativemidi);
chewi@506
   796
			goto skip;
chewi@506
   797
		}
chewi@506
   798
#endif
chewi@506
   799
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   800
		if (fluidsynth_ok ) {
chewi@506
   801
			fluidsynth_start(music->data.fluidsynthmidi);
chewi@506
   802
			goto skip;
chewi@506
   803
		}
slouken@98
   804
#endif
slouken@106
   805
#ifdef USE_TIMIDITY_MIDI
slouken@173
   806
		if ( timidity_ok ) {
slouken@173
   807
			Timidity_Start(music->data.midi);
chewi@506
   808
			goto skip;
slouken@173
   809
		}
slouken@106
   810
#endif
slouken@173
   811
		break;
slouken@0
   812
#endif
slouken@63
   813
#ifdef OGG_MUSIC
slouken@173
   814
	    case MUS_OGG:
slouken@173
   815
		OGG_play(music->data.ogg);
slouken@173
   816
		break;
slouken@63
   817
#endif
slouken@382
   818
#ifdef FLAC_MUSIC
slouken@382
   819
	    case MUS_FLAC:
slouken@382
   820
		FLAC_play(music->data.flac);
slouken@382
   821
		break;
slouken@382
   822
#endif
slouken@0
   823
#ifdef MP3_MUSIC
slouken@173
   824
	    case MUS_MP3:
slouken@314
   825
		smpeg.SMPEG_enableaudio(music->data.mp3,1);
slouken@314
   826
		smpeg.SMPEG_enablevideo(music->data.mp3,0);
slouken@314
   827
		smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
   828
		break;
slouken@0
   829
#endif
slouken@357
   830
#ifdef MP3_MAD_MUSIC
slouken@357
   831
	    case MUS_MP3_MAD:
slouken@357
   832
		mad_start(music->data.mp3_mad);
slouken@357
   833
		break;
slouken@357
   834
#endif
slouken@173
   835
	    default:
slouken@173
   836
		Mix_SetError("Can't play unknown music type");
slouken@173
   837
		retval = -1;
slouken@173
   838
		break;
slouken@0
   839
	}
slouken@173
   840
chewi@506
   841
skip:
slouken@173
   842
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   843
	if ( retval == 0 ) {
slouken@173
   844
		if ( position > 0.0 ) {
slouken@173
   845
			if ( music_internal_position(position) < 0 ) {
slouken@173
   846
				Mix_SetError("Position not implemented for music type");
slouken@173
   847
				retval = -1;
slouken@173
   848
			}
slouken@173
   849
		} else {
slouken@173
   850
			music_internal_position(0.0);
slouken@173
   851
		}
slouken@173
   852
	}
slouken@173
   853
slouken@173
   854
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   855
	if ( retval < 0 ) {
slouken@173
   856
		music_playing = NULL;
slouken@173
   857
	}
slouken@173
   858
	return(retval);
slouken@173
   859
}
slouken@173
   860
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   861
{
slouken@173
   862
	int retval;
slouken@173
   863
slouken@499
   864
	if ( ms_per_step == 0 ) {
slouken@499
   865
		SDL_SetError("Audio device hasn't been opened");
slouken@499
   866
		return(-1);
slouken@499
   867
	}
slouken@499
   868
slouken@173
   869
	/* Don't play null pointers :-) */
slouken@173
   870
	if ( music == NULL ) {
slouken@173
   871
		Mix_SetError("music parameter was NULL");
slouken@173
   872
		return(-1);
slouken@173
   873
	}
slouken@173
   874
slouken@173
   875
	/* Setup the data */
slouken@173
   876
	if ( ms ) {
slouken@173
   877
		music->fading = MIX_FADING_IN;
slouken@173
   878
	} else {
slouken@173
   879
		music->fading = MIX_NO_FADING;
slouken@173
   880
	}
slouken@173
   881
	music->fade_step = 0;
slouken@173
   882
	music->fade_steps = ms/ms_per_step;
slouken@173
   883
slouken@173
   884
	/* Play the puppy */
slouken@173
   885
	SDL_LockAudio();
slouken@173
   886
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   887
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   888
		SDL_UnlockAudio();
slouken@173
   889
		SDL_Delay(100);
slouken@173
   890
		SDL_LockAudio();
slouken@173
   891
	}
slouken@173
   892
	music_active = 1;
slouken@173
   893
	music_loops = loops;
slouken@173
   894
	retval = music_internal_play(music, position);
slouken@173
   895
	SDL_UnlockAudio();
slouken@173
   896
slouken@173
   897
	return(retval);
slouken@173
   898
}
slouken@173
   899
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   900
{
slouken@173
   901
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   902
}
slouken@173
   903
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   904
{
slouken@173
   905
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   906
}
megastep@10
   907
slouken@173
   908
/* Set the playing music position */
slouken@173
   909
int music_internal_position(double position)
megastep@10
   910
{
slouken@173
   911
	int retval = 0;
slouken@173
   912
slouken@173
   913
	switch (music_playing->type) {
slouken@481
   914
#ifdef MODPLUG_MUSIC
slouken@481
   915
	    case MUS_MODPLUG:
slouken@481
   916
		modplug_jump_to_time(music_playing->data.modplug, position);
slouken@481
   917
		break;
slouken@481
   918
#endif
slouken@411
   919
#ifdef MOD_MUSIC
slouken@173
   920
	    case MUS_MOD:
slouken@411
   921
		MOD_jump_to_time(music_playing->data.module, position);
slouken@173
   922
		break;
slouken@173
   923
#endif
slouken@529
   924
#ifdef MID_MUSIC
slouken@529
   925
	    case MUS_MID:
slouken@529
   926
#ifdef USE_NATIVE_MIDI
slouken@529
   927
		if ( native_midi_ok ) {
slouken@529
   928
			retval = native_midi_jump_to_time(music_playing->data.nativemidi, position);
slouken@529
   929
			break;
slouken@529
   930
		}
slouken@529
   931
#endif
slouken@529
   932
		/* TODO: Implement this for other music backends */
slouken@529
   933
		retval = -1;
slouken@529
   934
		break;
slouken@529
   935
#endif
slouken@173
   936
#ifdef OGG_MUSIC
slouken@173
   937
	    case MUS_OGG:
slouken@173
   938
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
   939
		break;
slouken@173
   940
#endif
slouken@382
   941
#ifdef FLAC_MUSIC
slouken@382
   942
	    case MUS_FLAC:
slouken@382
   943
		FLAC_jump_to_time(music_playing->data.flac, position);
slouken@382
   944
		break;
slouken@382
   945
#endif
slouken@173
   946
#ifdef MP3_MUSIC
slouken@173
   947
	    case MUS_MP3:
slouken@176
   948
		if ( position > 0.0 ) {
slouken@314
   949
			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
slouken@179
   950
		} else {
slouken@314
   951
			smpeg.SMPEG_rewind(music_playing->data.mp3);
slouken@314
   952
			smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
   953
		}
slouken@173
   954
		break;
slouken@173
   955
#endif
slouken@357
   956
#ifdef MP3_MAD_MUSIC
slouken@357
   957
	    case MUS_MP3_MAD:
slouken@357
   958
		mad_seek(music_playing->data.mp3_mad, position);
slouken@357
   959
		break;
slouken@357
   960
#endif
slouken@173
   961
	    default:
slouken@173
   962
		/* TODO: Implement this for other music backends */
slouken@173
   963
		retval = -1;
slouken@173
   964
		break;
megastep@10
   965
	}
slouken@173
   966
	return(retval);
megastep@4
   967
}
slouken@155
   968
int Mix_SetMusicPosition(double position)
slouken@154
   969
{
slouken@173
   970
	int retval;
slouken@173
   971
slouken@173
   972
	SDL_LockAudio();
slouken@173
   973
	if ( music_playing ) {
slouken@173
   974
		retval = music_internal_position(position);
slouken@173
   975
		if ( retval < 0 ) {
slouken@173
   976
			Mix_SetError("Position not implemented for music type");
slouken@154
   977
		}
slouken@173
   978
	} else {
slouken@173
   979
		Mix_SetError("Music isn't playing");
slouken@173
   980
		retval = -1;
slouken@154
   981
	}
slouken@173
   982
	SDL_UnlockAudio();
slouken@154
   983
slouken@173
   984
	return(retval);
slouken@154
   985
}
slouken@154
   986
icculus@237
   987
/* Set the music's initial volume */
icculus@237
   988
static void music_internal_initialize_volume(void)
icculus@237
   989
{
icculus@237
   990
	if ( music_playing->fading == MIX_FADING_IN ) {
icculus@237
   991
		music_internal_volume(0);
icculus@237
   992
	} else {
icculus@237
   993
		music_internal_volume(music_volume);
icculus@237
   994
	}
icculus@237
   995
}
icculus@237
   996
slouken@0
   997
/* Set the music volume */
slouken@173
   998
static void music_internal_volume(int volume)
slouken@173
   999
{
slouken@173
  1000
	switch (music_playing->type) {
slouken@173
  1001
#ifdef CMD_MUSIC
slouken@173
  1002
	    case MUS_CMD:
slouken@173
  1003
		MusicCMD_SetVolume(volume);
slouken@173
  1004
		break;
slouken@173
  1005
#endif
slouken@173
  1006
#ifdef WAV_MUSIC
slouken@173
  1007
	    case MUS_WAV:
slouken@173
  1008
		WAVStream_SetVolume(volume);
slouken@173
  1009
		break;
slouken@173
  1010
#endif
slouken@481
  1011
#ifdef MODPLUG_MUSIC
slouken@481
  1012
	    case MUS_MODPLUG:
slouken@481
  1013
		modplug_setvolume(music_playing->data.modplug, volume);
slouken@481
  1014
		break;
slouken@481
  1015
#endif
slouken@411
  1016
#ifdef MOD_MUSIC
slouken@173
  1017
	    case MUS_MOD:
slouken@411
  1018
		MOD_setvolume(music_playing->data.module, volume);
slouken@173
  1019
		break;
slouken@173
  1020
#endif
slouken@173
  1021
#ifdef MID_MUSIC
slouken@173
  1022
	    case MUS_MID:
slouken@173
  1023
#ifdef USE_NATIVE_MIDI
slouken@173
  1024
		if ( native_midi_ok ) {
slouken@173
  1025
			native_midi_setvolume(volume);
chewi@506
  1026
			return;
chewi@506
  1027
		}
chewi@506
  1028
#endif
chewi@506
  1029
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1030
		if ( fluidsynth_ok ) {
chewi@506
  1031
			fluidsynth_setvolume(music_playing->data.fluidsynthmidi, volume);
chewi@506
  1032
			return;
chewi@506
  1033
		}
slouken@173
  1034
#endif
slouken@173
  1035
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1036
		if ( timidity_ok ) {
slouken@173
  1037
			Timidity_SetVolume(volume);
chewi@506
  1038
			return;
slouken@173
  1039
		}
slouken@173
  1040
#endif
slouken@173
  1041
		break;
slouken@173
  1042
#endif
slouken@173
  1043
#ifdef OGG_MUSIC
slouken@173
  1044
	    case MUS_OGG:
slouken@173
  1045
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
  1046
		break;
slouken@173
  1047
#endif
slouken@382
  1048
#ifdef FLAC_MUSIC
slouken@382
  1049
	    case MUS_FLAC:
slouken@382
  1050
		FLAC_setvolume(music_playing->data.flac, volume);
slouken@382
  1051
		break;
slouken@382
  1052
#endif
slouken@173
  1053
#ifdef MP3_MUSIC
slouken@173
  1054
	    case MUS_MP3:
slouken@314
  1055
		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
  1056
		break;
slouken@173
  1057
#endif
slouken@357
  1058
#ifdef MP3_MAD_MUSIC
slouken@357
  1059
	    case MUS_MP3_MAD:
slouken@357
  1060
		mad_setVolume(music_playing->data.mp3_mad, volume);
slouken@357
  1061
		break;
slouken@357
  1062
#endif
slouken@173
  1063
	    default:
slouken@173
  1064
		/* Unknown music type?? */
slouken@173
  1065
		break;
slouken@173
  1066
	}
slouken@173
  1067
}
slouken@0
  1068
int Mix_VolumeMusic(int volume)
slouken@0
  1069
{
slouken@0
  1070
	int prev_volume;
slouken@0
  1071
slouken@0
  1072
	prev_volume = music_volume;
megastep@4
  1073
	if ( volume < 0 ) {
slouken@163
  1074
		return prev_volume;
megastep@4
  1075
	}
megastep@4
  1076
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
  1077
		volume = SDL_MIX_MAXVOLUME;
megastep@4
  1078
	}
megastep@4
  1079
	music_volume = volume;
slouken@173
  1080
	SDL_LockAudio();
slouken@173
  1081
	if ( music_playing ) {
slouken@173
  1082
		music_internal_volume(music_volume);
slouken@0
  1083
	}
slouken@173
  1084
	SDL_UnlockAudio();
slouken@0
  1085
	return(prev_volume);
slouken@0
  1086
}
slouken@0
  1087
slouken@173
  1088
/* Halt playing of music */
slouken@173
  1089
static void music_internal_halt(void)
megastep@7
  1090
{
megastep@7
  1091
	switch (music_playing->type) {
megastep@7
  1092
#ifdef CMD_MUSIC
slouken@173
  1093
	    case MUS_CMD:
megastep@7
  1094
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
  1095
		break;
megastep@7
  1096
#endif
megastep@7
  1097
#ifdef WAV_MUSIC
slouken@173
  1098
	    case MUS_WAV:
megastep@7
  1099
		WAVStream_Stop();
megastep@7
  1100
		break;
megastep@7
  1101
#endif
slouken@481
  1102
#ifdef MODPLUG_MUSIC
slouken@481
  1103
	    case MUS_MODPLUG:
slouken@481
  1104
		modplug_stop(music_playing->data.modplug);
slouken@481
  1105
		break;
slouken@481
  1106
#endif
slouken@411
  1107
#ifdef MOD_MUSIC
slouken@173
  1108
	    case MUS_MOD:
slouken@411
  1109
		MOD_stop(music_playing->data.module);
megastep@7
  1110
		break;
megastep@7
  1111
#endif
megastep@7
  1112
#ifdef MID_MUSIC
slouken@173
  1113
	    case MUS_MID:
slouken@98
  1114
#ifdef USE_NATIVE_MIDI
slouken@98
  1115
		if ( native_midi_ok ) {
slouken@98
  1116
			native_midi_stop();
chewi@506
  1117
			goto skip;
chewi@506
  1118
		}
chewi@506
  1119
#endif
chewi@506
  1120
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1121
		if ( fluidsynth_ok ) {
chewi@506
  1122
			fluidsynth_stop(music_playing->data.fluidsynthmidi);
chewi@506
  1123
			goto skip;
chewi@506
  1124
		}
slouken@98
  1125
#endif
slouken@106
  1126
#ifdef USE_TIMIDITY_MIDI
slouken@98
  1127
		if ( timidity_ok ) {
slouken@98
  1128
			Timidity_Stop();
chewi@506
  1129
			goto skip;
slouken@98
  1130
		}
slouken@106
  1131
#endif
megastep@7
  1132
		break;
megastep@7
  1133
#endif
slouken@63
  1134
#ifdef OGG_MUSIC
slouken@173
  1135
	    case MUS_OGG:
slouken@63
  1136
		OGG_stop(music_playing->data.ogg);
slouken@63
  1137
		break;
slouken@63
  1138
#endif
slouken@382
  1139
#ifdef FLAC_MUSIC
slouken@382
  1140
	    case MUS_FLAC:
slouken@382
  1141
		FLAC_stop(music_playing->data.flac);
slouken@382
  1142
		break;
slouken@382
  1143
#endif
megastep@7
  1144
#ifdef MP3_MUSIC
slouken@173
  1145
	    case MUS_MP3:
slouken@314
  1146
		smpeg.SMPEG_stop(music_playing->data.mp3);
megastep@7
  1147
		break;
megastep@7
  1148
#endif
slouken@357
  1149
#ifdef MP3_MAD_MUSIC
slouken@357
  1150
	    case MUS_MP3_MAD:
slouken@357
  1151
		mad_stop(music_playing->data.mp3_mad);
slouken@357
  1152
		break;
slouken@357
  1153
#endif
slouken@173
  1154
	    default:
megastep@7
  1155
		/* Unknown music type?? */
megastep@7
  1156
		return;
megastep@7
  1157
	}
chewi@506
  1158
chewi@506
  1159
skip:
megastep@7
  1160
	music_playing->fading = MIX_NO_FADING;
slouken@17
  1161
	music_playing = NULL;
megastep@7
  1162
}
slouken@0
  1163
int Mix_HaltMusic(void)
slouken@0
  1164
{
slouken@173
  1165
	SDL_LockAudio();
slouken@173
  1166
	if ( music_playing ) {
slouken@173
  1167
		music_internal_halt();
slouken@0
  1168
	}
slouken@173
  1169
	SDL_UnlockAudio();
slouken@173
  1170
slouken@0
  1171
	return(0);
slouken@0
  1172
}
slouken@0
  1173
megastep@4
  1174
/* Progressively stop the music */
megastep@4
  1175
int Mix_FadeOutMusic(int ms)
megastep@4
  1176
{
slouken@173
  1177
	int retval = 0;
slouken@173
  1178
slouken@499
  1179
	if ( ms_per_step == 0 ) {
slouken@499
  1180
		SDL_SetError("Audio device hasn't been opened");
slouken@499
  1181
		return 0;
slouken@499
  1182
	}
slouken@499
  1183
icculus@280
  1184
	if (ms <= 0) {  /* just halt immediately. */
icculus@280
  1185
		Mix_HaltMusic();
icculus@280
  1186
		return 1;
icculus@280
  1187
	}
icculus@280
  1188
slouken@173
  1189
	SDL_LockAudio();
icculus@280
  1190
	if ( music_playing) {
icculus@280
  1191
                int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
icculus@280
  1192
                if ( music_playing->fading == MIX_NO_FADING ) {
icculus@280
  1193
	        	music_playing->fade_step = 0;
icculus@280
  1194
                } else {
icculus@280
  1195
                        int step;
icculus@280
  1196
                        int old_fade_steps = music_playing->fade_steps;
icculus@280
  1197
                        if ( music_playing->fading == MIX_FADING_OUT ) {
icculus@280
  1198
                                step = music_playing->fade_step;
icculus@280
  1199
                        } else {
icculus@280
  1200
                                step = old_fade_steps
icculus@280
  1201
                                        - music_playing->fade_step + 1;
icculus@280
  1202
                        }
icculus@280
  1203
                        music_playing->fade_step = (step * fade_steps)
icculus@280
  1204
                                / old_fade_steps;
icculus@280
  1205
                }
slouken@173
  1206
		music_playing->fading = MIX_FADING_OUT;
icculus@280
  1207
		music_playing->fade_steps = fade_steps;
slouken@173
  1208
		retval = 1;
megastep@4
  1209
	}
slouken@173
  1210
	SDL_UnlockAudio();
slouken@173
  1211
slouken@173
  1212
	return(retval);
megastep@4
  1213
}
megastep@4
  1214
megastep@4
  1215
Mix_Fading Mix_FadingMusic(void)
megastep@4
  1216
{
slouken@173
  1217
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
  1218
slouken@173
  1219
	SDL_LockAudio();
slouken@173
  1220
	if ( music_playing ) {
slouken@173
  1221
		fading = music_playing->fading;
slouken@173
  1222
	}
slouken@173
  1223
	SDL_UnlockAudio();
slouken@173
  1224
slouken@173
  1225
	return(fading);
megastep@4
  1226
}
megastep@4
  1227
slouken@0
  1228
/* Pause/Resume the music stream */
slouken@0
  1229
void Mix_PauseMusic(void)
slouken@0
  1230
{
slouken@173
  1231
	music_active = 0;
slouken@0
  1232
}
megastep@4
  1233
slouken@0
  1234
void Mix_ResumeMusic(void)
slouken@0
  1235
{
slouken@173
  1236
	music_active = 1;
slouken@0
  1237
}
slouken@0
  1238
slouken@0
  1239
void Mix_RewindMusic(void)
slouken@0
  1240
{
slouken@173
  1241
	Mix_SetMusicPosition(0.0);
slouken@0
  1242
}
slouken@0
  1243
megastep@13
  1244
int Mix_PausedMusic(void)
megastep@13
  1245
{
slouken@17
  1246
	return (music_active == 0);
megastep@13
  1247
}
megastep@13
  1248
slouken@0
  1249
/* Check the status of the music */
slouken@173
  1250
static int music_internal_playing()
slouken@173
  1251
{
slouken@173
  1252
	int playing = 1;
icculus@510
  1253
icculus@510
  1254
	if (music_playing == NULL) {
icculus@510
  1255
		return 0;
icculus@510
  1256
	}
icculus@510
  1257
slouken@173
  1258
	switch (music_playing->type) {
slouken@173
  1259
#ifdef CMD_MUSIC
slouken@173
  1260
	    case MUS_CMD:
slouken@173
  1261
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
  1262
			playing = 0;
slouken@173
  1263
		}
slouken@173
  1264
		break;
slouken@173
  1265
#endif
slouken@173
  1266
#ifdef WAV_MUSIC
slouken@173
  1267
	    case MUS_WAV:
slouken@173
  1268
		if ( ! WAVStream_Active() ) {
slouken@173
  1269
			playing = 0;
slouken@173
  1270
		}
slouken@173
  1271
		break;
slouken@173
  1272
#endif
slouken@481
  1273
#ifdef MODPLUG_MUSIC
slouken@481
  1274
	    case MUS_MODPLUG:
slouken@481
  1275
		if ( ! modplug_playing(music_playing->data.modplug) ) {
slouken@481
  1276
			playing = 0;
slouken@481
  1277
		}
slouken@481
  1278
		break;
slouken@481
  1279
#endif
slouken@411
  1280
#ifdef MOD_MUSIC
slouken@173
  1281
	    case MUS_MOD:
slouken@411
  1282
		if ( ! MOD_playing(music_playing->data.module) ) {
slouken@173
  1283
			playing = 0;
slouken@173
  1284
		}
slouken@173
  1285
		break;
slouken@173
  1286
#endif
slouken@173
  1287
#ifdef MID_MUSIC
slouken@173
  1288
	    case MUS_MID:
slouken@173
  1289
#ifdef USE_NATIVE_MIDI
slouken@173
  1290
		if ( native_midi_ok ) {
slouken@173
  1291
			if ( ! native_midi_active() )
slouken@173
  1292
				playing = 0;
chewi@506
  1293
			goto skip;
chewi@506
  1294
		}
chewi@506
  1295
#endif
chewi@506
  1296
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1297
		if ( fluidsynth_ok ) {
chewi@506
  1298
			if ( ! fluidsynth_active(music_playing->data.fluidsynthmidi) )
chewi@506
  1299
				playing = 0;
chewi@506
  1300
			goto skip;
chewi@506
  1301
		}
slouken@173
  1302
#endif
slouken@173
  1303
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1304
		if ( timidity_ok ) {
slouken@173
  1305
			if ( ! Timidity_Active() )
slouken@173
  1306
				playing = 0;
chewi@506
  1307
			goto skip;
slouken@173
  1308
		}
slouken@173
  1309
#endif
slouken@173
  1310
		break;
slouken@173
  1311
#endif
slouken@173
  1312
#ifdef OGG_MUSIC
slouken@173
  1313
	    case MUS_OGG:
slouken@173
  1314
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1315
			playing = 0;
slouken@173
  1316
		}
slouken@173
  1317
		break;
slouken@173
  1318
#endif
slouken@382
  1319
#ifdef FLAC_MUSIC
slouken@382
  1320
	    case MUS_FLAC:
slouken@382
  1321
		if ( ! FLAC_playing(music_playing->data.flac) ) {
slouken@382
  1322
			playing = 0;
slouken@382
  1323
		}
slouken@382
  1324
		break;
slouken@382
  1325
#endif
slouken@173
  1326
#ifdef MP3_MUSIC
slouken@173
  1327
	    case MUS_MP3:
slouken@314
  1328
		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1329
			playing = 0;
slouken@173
  1330
		break;
slouken@173
  1331
#endif
slouken@357
  1332
#ifdef MP3_MAD_MUSIC
slouken@357
  1333
	    case MUS_MP3_MAD:
slouken@357
  1334
		if (!mad_isPlaying(music_playing->data.mp3_mad)) {
slouken@357
  1335
			playing = 0;
slouken@357
  1336
		}
slouken@357
  1337
		break;
slouken@357
  1338
#endif
slouken@173
  1339
	    default:
slouken@173
  1340
		playing = 0;
slouken@173
  1341
		break;
slouken@173
  1342
	}
chewi@506
  1343
chewi@506
  1344
skip:
slouken@173
  1345
	return(playing);
slouken@173
  1346
}
megastep@13
  1347
int Mix_PlayingMusic(void)
slouken@0
  1348
{
slouken@173
  1349
	int playing = 0;
slouken@173
  1350
slouken@173
  1351
	SDL_LockAudio();
slouken@173
  1352
	if ( music_playing ) {
slouken@529
  1353
		playing = music_loops || music_internal_playing();
slouken@0
  1354
	}
slouken@173
  1355
	SDL_UnlockAudio();
slouken@173
  1356
slouken@173
  1357
	return(playing);
slouken@0
  1358
}
slouken@0
  1359
slouken@0
  1360
/* Set the external music playback command */
slouken@0
  1361
int Mix_SetMusicCMD(const char *command)
slouken@0
  1362
{
slouken@0
  1363
	Mix_HaltMusic();
slouken@0
  1364
	if ( music_cmd ) {
slouken@0
  1365
		free(music_cmd);
slouken@0
  1366
		music_cmd = NULL;
slouken@0
  1367
	}
slouken@0
  1368
	if ( command ) {
slouken@0
  1369
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
  1370
		if ( music_cmd == NULL ) {
slouken@0
  1371
			return(-1);
slouken@0
  1372
		}
slouken@0
  1373
		strcpy(music_cmd, command);
slouken@0
  1374
	}
slouken@0
  1375
	return(0);
slouken@0
  1376
}
slouken@0
  1377
slouken@154
  1378
int Mix_SetSynchroValue(int i)
slouken@154
  1379
{
slouken@419
  1380
	/* Not supported by any players at this time */
slouken@154
  1381
	return(-1);
slouken@154
  1382
}
slouken@154
  1383
slouken@154
  1384
int Mix_GetSynchroValue(void)
slouken@154
  1385
{
slouken@419
  1386
	/* Not supported by any players at this time */
slouken@154
  1387
	return(-1);
slouken@154
  1388
}
slouken@154
  1389
slouken@154
  1390
slouken@0
  1391
/* Uninitialize the music players */
slouken@0
  1392
void close_music(void)
slouken@0
  1393
{
slouken@0
  1394
	Mix_HaltMusic();
slouken@0
  1395
#ifdef CMD_MUSIC
slouken@0
  1396
	Mix_SetMusicCMD(NULL);
slouken@0
  1397
#endif
slouken@481
  1398
#ifdef MODPLUG_MUSIC
slouken@481
  1399
	modplug_exit();
slouken@481
  1400
#endif
slouken@411
  1401
#ifdef MOD_MUSIC
slouken@411
  1402
	MOD_exit();
slouken@0
  1403
#endif
patmandin@264
  1404
#ifdef MID_MUSIC
patmandin@264
  1405
# ifdef USE_TIMIDITY_MIDI
patmandin@264
  1406
	Timidity_Close();
patmandin@264
  1407
# endif
patmandin@264
  1408
#endif
icculus@390
  1409
icculus@390
  1410
	/* rcg06042009 report available decoders at runtime. */
icculus@390
  1411
	free(music_decoders);
icculus@390
  1412
	music_decoders = NULL;
icculus@390
  1413
	num_decoders = 0;
slouken@499
  1414
slouken@499
  1415
	ms_per_step = 0;
slouken@0
  1416
}
slouken@0
  1417
slouken@381
  1418
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
slouken@381
  1419
{
slouken@246
  1420
	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
slouken@246
  1421
	/*		    or there are too many formats supported by MikMod or MikMod does */
slouken@246
  1422
	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
slouken@246
  1423
	/*		    the check can be uncommented */
slouken@381
  1424
	Uint8 moremagic[9];
slouken@226
  1425
	Mix_Music *music;
slouken@246
  1426
	int start;
slouken@521
  1427
	int freerw = 0;
slouken@226
  1428
patmandin@336
  1429
	if (!rw) {
patmandin@336
  1430
		Mix_SetError("RWops pointer is NULL");
patmandin@336
  1431
		return NULL;
patmandin@336
  1432
	}
patmandin@336
  1433
slouken@226
  1434
	/* Figure out what kind of file this is */
slouken@246
  1435
	start = SDL_RWtell(rw);
slouken@381
  1436
	if ( SDL_RWread(rw,magic,1,4) != 4 ||
slouken@381
  1437
	     SDL_RWread(rw,moremagic,1,8) != 8 ) {
slouken@226
  1438
		Mix_SetError("Couldn't read from RWops");
slouken@226
  1439
		return NULL;
slouken@226
  1440
	}
slouken@473
  1441
	SDL_RWseek(rw, start, RW_SEEK_SET);
slouken@246
  1442
	magic[4]='\0';
slouken@381
  1443
	moremagic[8] = '\0';
slouken@226
  1444
slouken@226
  1445
	/* Allocate memory for the music structure */
slouken@226
  1446
	music=(Mix_Music *)malloc(sizeof(Mix_Music));
slouken@226
  1447
	if (music==NULL ) {
slouken@226
  1448
		Mix_SetError("Out of memory");
slouken@226
  1449
		return(NULL);
slouken@226
  1450
	}
slouken@226
  1451
	music->error = 0;
slouken@226
  1452
slouken@381
  1453
#ifdef WAV_MUSIC
slouken@381
  1454
	/* WAVE files have the magic four bytes "RIFF"
slouken@381
  1455
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@381
  1456
	 */
slouken@381
  1457
	if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@381
  1458
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@381
  1459
		music->type = MUS_WAV;
slouken@521
  1460
		music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic, freerw);
slouken@381
  1461
		if ( music->data.wave == NULL ) {
slouken@381
  1462
			music->error = 1;
slouken@381
  1463
		}
slouken@381
  1464
slouken@381
  1465
	} else
slouken@381
  1466
#endif
slouken@246
  1467
#ifdef OGG_MUSIC
slouken@246
  1468
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@246
  1469
	if ( strcmp((char *)magic, "OggS") == 0 ) {
slouken@246
  1470
		music->type = MUS_OGG;
slouken@521
  1471
		music->data.ogg = OGG_new_RW(rw, freerw);
slouken@246
  1472
		if ( music->data.ogg == NULL ) {
slouken@246
  1473
			music->error = 1;
slouken@246
  1474
		}
slouken@246
  1475
	} else
slouken@246
  1476
#endif
slouken@382
  1477
#ifdef FLAC_MUSIC
slouken@382
  1478
	/* FLAC files have the magic four bytes "fLaC" */
slouken@382
  1479
	if ( strcmp((char *)magic, "fLaC") == 0 ) {
slouken@382
  1480
		music->type = MUS_FLAC;
slouken@521
  1481
		music->data.flac = FLAC_new_RW(rw, freerw);
slouken@382
  1482
		if ( music->data.flac == NULL ) {
slouken@382
  1483
			music->error = 1;
slouken@382
  1484
		}
slouken@382
  1485
	} else
slouken@382
  1486
#endif
slouken@256
  1487
#ifdef MP3_MUSIC
slouken@398
  1488
	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
slouken@470
  1489
		if ( Mix_Init(MIX_INIT_MP3) ) {
slouken@314
  1490
			SMPEG_Info info;
slouken@314
  1491
			music->type = MUS_MP3;
slouken@314
  1492
			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
slouken@314
  1493
			if ( !info.has_audio ) {
slouken@314
  1494
				Mix_SetError("MPEG file does not have any audio stream.");
slouken@314
  1495
				music->error = 1;
slouken@314
  1496
			} else {
slouken@314
  1497
				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@314
  1498
			}
slouken@314
  1499
		} else {
slouken@256
  1500
			music->error = 1;
slouken@256
  1501
		}
slouken@256
  1502
	} else
slouken@256
  1503
#endif
slouken@357
  1504
#ifdef MP3_MAD_MUSIC
slouken@398
  1505
	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
slouken@357
  1506
		music->type = MUS_MP3_MAD;
slouken@521
  1507
		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer, freerw);
slouken@357
  1508
		if (music->data.mp3_mad == 0) {
slouken@357
  1509
			Mix_SetError("Could not initialize MPEG stream.");
slouken@357
  1510
			music->error = 1;
slouken@357
  1511
		}
slouken@357
  1512
	} else
slouken@357
  1513
#endif
patmandin@265
  1514
#ifdef MID_MUSIC
patmandin@265
  1515
	/* MIDI files have the magic four bytes "MThd" */
patmandin@265
  1516
	if ( strcmp((char *)magic, "MThd") == 0 ) {
patmandin@265
  1517
		music->type = MUS_MID;
patmandin@269
  1518
#ifdef USE_NATIVE_MIDI
patmandin@269
  1519
		if ( native_midi_ok ) {
slouken@521
  1520
			music->data.nativemidi = native_midi_loadsong_RW(rw, freerw);
patmandin@335
  1521
	  		if ( music->data.nativemidi == NULL ) {
patmandin@335
  1522
		  		Mix_SetError("%s", native_midi_error());
patmandin@335
  1523
			  	music->error = 1;
patmandin@269
  1524
			}
chewi@506
  1525
			goto skip;
chewi@506
  1526
		}
chewi@506
  1527
#endif
chewi@506
  1528
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1529
		if ( fluidsynth_ok ) {
slouken@521
  1530
			music->data.fluidsynthmidi = fluidsynth_loadsong_RW(rw, freerw);
chewi@506
  1531
			if ( music->data.fluidsynthmidi == NULL ) {
chewi@506
  1532
				music->error = 1;
chewi@506
  1533
			}
chewi@506
  1534
			goto skip;
chewi@506
  1535
		}
patmandin@269
  1536
#endif
patmandin@265
  1537
#ifdef USE_TIMIDITY_MIDI
patmandin@269
  1538
		if ( timidity_ok ) {
slouken@521
  1539
			music->data.midi = Timidity_LoadSong_RW(rw, freerw);
patmandin@335
  1540
			if ( music->data.midi == NULL ) {
patmandin@335
  1541
				Mix_SetError("%s", Timidity_Error());
patmandin@335
  1542
				music->error = 1;
patmandin@269
  1543
			}
patmandin@335
  1544
		} else {
patmandin@335
  1545
			Mix_SetError("%s", Timidity_Error());
patmandin@335
  1546
			music->error = 1;
patmandin@265
  1547
		}
patmandin@265
  1548
#endif
patmandin@265
  1549
	} else
patmandin@265
  1550
#endif
slouken@504
  1551
#if defined(MODPLUG_MUSIC) || defined(MOD_MUSIC)
slouken@481
  1552
	if (1) {
slouken@481
  1553
		music->error = 1;
slouken@481
  1554
#ifdef MODPLUG_MUSIC
slouken@481
  1555
		if ( music->error ) {
slouken@481
  1556
			music->type = MUS_MODPLUG;
slouken@521
  1557
			music->data.modplug = modplug_new_RW(rw, freerw);
slouken@481
  1558
			if ( music->data.modplug ) {
slouken@481
  1559
				music->error = 0;
slouken@481
  1560
			}
slouken@481
  1561
		}
slouken@481
  1562
#endif
slouken@411
  1563
#ifdef MOD_MUSIC
slouken@481
  1564
		if ( music->error ) {
slouken@481
  1565
			music->type = MUS_MOD;
slouken@521
  1566
			music->data.module = MOD_new_RW(rw, freerw);
slouken@481
  1567
			if ( music->data.module ) {
slouken@481
  1568
				music->error = 0;
slouken@481
  1569
			}
slouken@226
  1570
		}
slouken@481
  1571
#endif
slouken@226
  1572
	} else
slouken@481
  1573
#endif /* MODPLUG_MUSIC || MOD_MUSIC */
slouken@226
  1574
	{
slouken@226
  1575
		Mix_SetError("Unrecognized music format");
slouken@226
  1576
		music->error=1;
slouken@226
  1577
	}
chewi@506
  1578
chewi@506
  1579
skip:
slouken@226
  1580
	if (music->error) {
slouken@226
  1581
		free(music);
slouken@226
  1582
		music=NULL;
slouken@226
  1583
	}
slouken@226
  1584
	return(music);
slouken@226
  1585
}
chewi@506
  1586
chewi@506
  1587
int Mix_SetSoundFonts(const char *paths)
chewi@506
  1588
{
chewi@506
  1589
#ifdef MID_MUSIC
chewi@506
  1590
	if (soundfont_paths) {
chewi@506
  1591
		free(soundfont_paths);
chewi@506
  1592
		soundfont_paths = NULL;
chewi@506
  1593
	}
chewi@506
  1594
chewi@506
  1595
	if (paths) {
chewi@506
  1596
		if (!(soundfont_paths = strdup(paths))) {
chewi@506
  1597
			Mix_SetError("Insufficient memory to set SoundFonts");
chewi@506
  1598
			return 0;
chewi@506
  1599
		}
chewi@506
  1600
	}
chewi@506
  1601
#endif
chewi@506
  1602
	return 1;
chewi@506
  1603
}
chewi@506
  1604
chewi@506
  1605
#ifdef MID_MUSIC
dimitris@508
  1606
const char* Mix_GetSoundFonts(void)
chewi@506
  1607
{
chewi@506
  1608
	const char* force = getenv("SDL_FORCE_SOUNDFONTS");
chewi@506
  1609
chewi@506
  1610
	if (!soundfont_paths || (force && force[0] == '1')) {
chewi@506
  1611
		return getenv("SDL_SOUNDFONTS");
chewi@506
  1612
	} else {
chewi@506
  1613
		return soundfont_paths;
chewi@506
  1614
	}
chewi@506
  1615
}
chewi@506
  1616
chewi@506
  1617
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
  1618
{
chewi@506
  1619
	char *context, *path, *paths;
chewi@506
  1620
	const char* cpaths = Mix_GetSoundFonts();
chewi@506
  1621
chewi@506
  1622
	if (!cpaths) {
chewi@506
  1623
		Mix_SetError("No SoundFonts have been requested");
chewi@506
  1624
		return 0;
chewi@506
  1625
	}
chewi@506
  1626
chewi@506
  1627
	if (!(paths = strdup(cpaths))) {
chewi@506
  1628
		Mix_SetError("Insufficient memory to iterate over SoundFonts");
chewi@506
  1629
		return 0;
chewi@506
  1630
	}
chewi@506
  1631
icculus@513
  1632
#if defined(__MINGW32__) || defined(__MINGW64__)
icculus@513
  1633
	for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1634
#elif defined(_WIN32)
chewi@506
  1635
	for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1636
#else
chewi@506
  1637
	for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1638
#endif
chewi@506
  1639
		if (!function(path, data)) {
chewi@506
  1640
			free(paths);
chewi@506
  1641
			return 0;
chewi@506
  1642
		}
chewi@506
  1643
	}
chewi@506
  1644
chewi@506
  1645
	free(paths);
chewi@506
  1646
	return 1;
chewi@506
  1647
}
chewi@506
  1648
#endif