music.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 08:46:55 +0000
changeset 412 4c1f81481f87
parent 411 cade1e3a4cb8
child 415 494bc6858d1f
permissions -rw-r--r--
Fixed bug #819

O.Sezer 2009-10-02 08:41:50 PDT

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