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