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