music.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 04 Feb 2010 20:32:01 +0000
changeset 484 a77df8583d18
parent 483 685cb4a41b92
child 499 6ab2b1f10df9
permissions -rw-r--r--
Correctly return a value from modplug_init(), and correctly check it, too.

Patch by Ozkan Sezer.

Fixes Bugzilla #941.
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@173
   782
	/* Don't play null pointers :-) */
slouken@173
   783
	if ( music == NULL ) {
slouken@173
   784
		Mix_SetError("music parameter was NULL");
slouken@173
   785
		return(-1);
slouken@173
   786
	}
slouken@173
   787
slouken@173
   788
	/* Setup the data */
slouken@173
   789
	if ( ms ) {
slouken@173
   790
		music->fading = MIX_FADING_IN;
slouken@173
   791
	} else {
slouken@173
   792
		music->fading = MIX_NO_FADING;
slouken@173
   793
	}
slouken@173
   794
	music->fade_step = 0;
slouken@173
   795
	music->fade_steps = ms/ms_per_step;
slouken@173
   796
slouken@173
   797
	/* Play the puppy */
slouken@173
   798
	SDL_LockAudio();
slouken@173
   799
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   800
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   801
		SDL_UnlockAudio();
slouken@173
   802
		SDL_Delay(100);
slouken@173
   803
		SDL_LockAudio();
slouken@173
   804
	}
slouken@173
   805
	music_active = 1;
slouken@173
   806
	music_loops = loops;
slouken@173
   807
	retval = music_internal_play(music, position);
slouken@173
   808
	SDL_UnlockAudio();
slouken@173
   809
slouken@173
   810
	return(retval);
slouken@173
   811
}
slouken@173
   812
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   813
{
slouken@173
   814
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   815
}
slouken@173
   816
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   817
{
slouken@173
   818
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   819
}
megastep@10
   820
slouken@173
   821
/* Set the playing music position */
slouken@173
   822
int music_internal_position(double position)
megastep@10
   823
{
slouken@173
   824
	int retval = 0;
slouken@173
   825
slouken@173
   826
	switch (music_playing->type) {
slouken@481
   827
#ifdef MODPLUG_MUSIC
slouken@481
   828
	    case MUS_MODPLUG:
slouken@481
   829
		modplug_jump_to_time(music_playing->data.modplug, position);
slouken@481
   830
		break;
slouken@481
   831
#endif
slouken@411
   832
#ifdef MOD_MUSIC
slouken@173
   833
	    case MUS_MOD:
slouken@411
   834
		MOD_jump_to_time(music_playing->data.module, position);
slouken@173
   835
		break;
slouken@173
   836
#endif
slouken@173
   837
#ifdef OGG_MUSIC
slouken@173
   838
	    case MUS_OGG:
slouken@173
   839
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
   840
		break;
slouken@173
   841
#endif
slouken@382
   842
#ifdef FLAC_MUSIC
slouken@382
   843
	    case MUS_FLAC:
slouken@382
   844
		FLAC_jump_to_time(music_playing->data.flac, position);
slouken@382
   845
		break;
slouken@382
   846
#endif
slouken@173
   847
#ifdef MP3_MUSIC
slouken@173
   848
	    case MUS_MP3:
slouken@176
   849
		if ( position > 0.0 ) {
slouken@314
   850
			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
slouken@179
   851
		} else {
slouken@314
   852
			smpeg.SMPEG_rewind(music_playing->data.mp3);
slouken@314
   853
			smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
   854
		}
slouken@173
   855
		break;
slouken@173
   856
#endif
slouken@357
   857
#ifdef MP3_MAD_MUSIC
slouken@357
   858
	    case MUS_MP3_MAD:
slouken@357
   859
		mad_seek(music_playing->data.mp3_mad, position);
slouken@357
   860
		break;
slouken@357
   861
#endif
slouken@173
   862
	    default:
slouken@173
   863
		/* TODO: Implement this for other music backends */
slouken@173
   864
		retval = -1;
slouken@173
   865
		break;
megastep@10
   866
	}
slouken@173
   867
	return(retval);
megastep@4
   868
}
slouken@155
   869
int Mix_SetMusicPosition(double position)
slouken@154
   870
{
slouken@173
   871
	int retval;
slouken@173
   872
slouken@173
   873
	SDL_LockAudio();
slouken@173
   874
	if ( music_playing ) {
slouken@173
   875
		retval = music_internal_position(position);
slouken@173
   876
		if ( retval < 0 ) {
slouken@173
   877
			Mix_SetError("Position not implemented for music type");
slouken@154
   878
		}
slouken@173
   879
	} else {
slouken@173
   880
		Mix_SetError("Music isn't playing");
slouken@173
   881
		retval = -1;
slouken@154
   882
	}
slouken@173
   883
	SDL_UnlockAudio();
slouken@154
   884
slouken@173
   885
	return(retval);
slouken@154
   886
}
slouken@154
   887
icculus@237
   888
/* Set the music's initial volume */
icculus@237
   889
static void music_internal_initialize_volume(void)
icculus@237
   890
{
icculus@237
   891
	if ( music_playing->fading == MIX_FADING_IN ) {
icculus@237
   892
		music_internal_volume(0);
icculus@237
   893
	} else {
icculus@237
   894
		music_internal_volume(music_volume);
icculus@237
   895
	}
icculus@237
   896
}
icculus@237
   897
slouken@0
   898
/* Set the music volume */
slouken@173
   899
static void music_internal_volume(int volume)
slouken@173
   900
{
slouken@173
   901
	switch (music_playing->type) {
slouken@173
   902
#ifdef CMD_MUSIC
slouken@173
   903
	    case MUS_CMD:
slouken@173
   904
		MusicCMD_SetVolume(volume);
slouken@173
   905
		break;
slouken@173
   906
#endif
slouken@173
   907
#ifdef WAV_MUSIC
slouken@173
   908
	    case MUS_WAV:
slouken@173
   909
		WAVStream_SetVolume(volume);
slouken@173
   910
		break;
slouken@173
   911
#endif
slouken@481
   912
#ifdef MODPLUG_MUSIC
slouken@481
   913
	    case MUS_MODPLUG:
slouken@481
   914
		modplug_setvolume(music_playing->data.modplug, volume);
slouken@481
   915
		break;
slouken@481
   916
#endif
slouken@411
   917
#ifdef MOD_MUSIC
slouken@173
   918
	    case MUS_MOD:
slouken@411
   919
		MOD_setvolume(music_playing->data.module, volume);
slouken@173
   920
		break;
slouken@173
   921
#endif
slouken@173
   922
#ifdef MID_MUSIC
slouken@173
   923
	    case MUS_MID:
slouken@173
   924
#ifdef USE_NATIVE_MIDI
slouken@173
   925
		if ( native_midi_ok ) {
slouken@173
   926
			native_midi_setvolume(volume);
slouken@173
   927
		} MIDI_ELSE
slouken@173
   928
#endif
slouken@173
   929
#ifdef USE_TIMIDITY_MIDI
slouken@173
   930
		if ( timidity_ok ) {
slouken@173
   931
			Timidity_SetVolume(volume);
slouken@173
   932
		}
slouken@173
   933
#endif
slouken@173
   934
		break;
slouken@173
   935
#endif
slouken@173
   936
#ifdef OGG_MUSIC
slouken@173
   937
	    case MUS_OGG:
slouken@173
   938
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
   939
		break;
slouken@173
   940
#endif
slouken@382
   941
#ifdef FLAC_MUSIC
slouken@382
   942
	    case MUS_FLAC:
slouken@382
   943
		FLAC_setvolume(music_playing->data.flac, volume);
slouken@382
   944
		break;
slouken@382
   945
#endif
slouken@173
   946
#ifdef MP3_MUSIC
slouken@173
   947
	    case MUS_MP3:
slouken@314
   948
		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
   949
		break;
slouken@173
   950
#endif
slouken@357
   951
#ifdef MP3_MAD_MUSIC
slouken@357
   952
	    case MUS_MP3_MAD:
slouken@357
   953
		mad_setVolume(music_playing->data.mp3_mad, volume);
slouken@357
   954
		break;
slouken@357
   955
#endif
slouken@173
   956
	    default:
slouken@173
   957
		/* Unknown music type?? */
slouken@173
   958
		break;
slouken@173
   959
	}
slouken@173
   960
}
slouken@0
   961
int Mix_VolumeMusic(int volume)
slouken@0
   962
{
slouken@0
   963
	int prev_volume;
slouken@0
   964
slouken@0
   965
	prev_volume = music_volume;
megastep@4
   966
	if ( volume < 0 ) {
slouken@163
   967
		return prev_volume;
megastep@4
   968
	}
megastep@4
   969
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   970
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   971
	}
megastep@4
   972
	music_volume = volume;
slouken@173
   973
	SDL_LockAudio();
slouken@173
   974
	if ( music_playing ) {
slouken@173
   975
		music_internal_volume(music_volume);
slouken@0
   976
	}
slouken@173
   977
	SDL_UnlockAudio();
slouken@0
   978
	return(prev_volume);
slouken@0
   979
}
slouken@0
   980
slouken@173
   981
/* Halt playing of music */
slouken@173
   982
static void music_internal_halt(void)
megastep@7
   983
{
megastep@7
   984
	switch (music_playing->type) {
megastep@7
   985
#ifdef CMD_MUSIC
slouken@173
   986
	    case MUS_CMD:
megastep@7
   987
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   988
		break;
megastep@7
   989
#endif
megastep@7
   990
#ifdef WAV_MUSIC
slouken@173
   991
	    case MUS_WAV:
megastep@7
   992
		WAVStream_Stop();
megastep@7
   993
		break;
megastep@7
   994
#endif
slouken@481
   995
#ifdef MODPLUG_MUSIC
slouken@481
   996
	    case MUS_MODPLUG:
slouken@481
   997
		modplug_stop(music_playing->data.modplug);
slouken@481
   998
		break;
slouken@481
   999
#endif
slouken@411
  1000
#ifdef MOD_MUSIC
slouken@173
  1001
	    case MUS_MOD:
slouken@411
  1002
		MOD_stop(music_playing->data.module);
megastep@7
  1003
		break;
megastep@7
  1004
#endif
megastep@7
  1005
#ifdef MID_MUSIC
slouken@173
  1006
	    case MUS_MID:
slouken@98
  1007
#ifdef USE_NATIVE_MIDI
slouken@98
  1008
		if ( native_midi_ok ) {
slouken@98
  1009
			native_midi_stop();
slouken@106
  1010
		} MIDI_ELSE
slouken@98
  1011
#endif
slouken@106
  1012
#ifdef USE_TIMIDITY_MIDI
slouken@98
  1013
		if ( timidity_ok ) {
slouken@98
  1014
			Timidity_Stop();
slouken@98
  1015
		}
slouken@106
  1016
#endif
megastep@7
  1017
		break;
megastep@7
  1018
#endif
slouken@63
  1019
#ifdef OGG_MUSIC
slouken@173
  1020
	    case MUS_OGG:
slouken@63
  1021
		OGG_stop(music_playing->data.ogg);
slouken@63
  1022
		break;
slouken@63
  1023
#endif
slouken@382
  1024
#ifdef FLAC_MUSIC
slouken@382
  1025
	    case MUS_FLAC:
slouken@382
  1026
		FLAC_stop(music_playing->data.flac);
slouken@382
  1027
		break;
slouken@382
  1028
#endif
megastep@7
  1029
#ifdef MP3_MUSIC
slouken@173
  1030
	    case MUS_MP3:
slouken@314
  1031
		smpeg.SMPEG_stop(music_playing->data.mp3);
megastep@7
  1032
		break;
megastep@7
  1033
#endif
slouken@357
  1034
#ifdef MP3_MAD_MUSIC
slouken@357
  1035
	    case MUS_MP3_MAD:
slouken@357
  1036
		mad_stop(music_playing->data.mp3_mad);
slouken@357
  1037
		break;
slouken@357
  1038
#endif
slouken@173
  1039
	    default:
megastep@7
  1040
		/* Unknown music type?? */
megastep@7
  1041
		return;
megastep@7
  1042
	}
megastep@7
  1043
	music_playing->fading = MIX_NO_FADING;
slouken@17
  1044
	music_playing = NULL;
megastep@7
  1045
}
slouken@0
  1046
int Mix_HaltMusic(void)
slouken@0
  1047
{
slouken@173
  1048
	SDL_LockAudio();
slouken@173
  1049
	if ( music_playing ) {
slouken@173
  1050
		music_internal_halt();
slouken@0
  1051
	}
slouken@173
  1052
	SDL_UnlockAudio();
slouken@173
  1053
slouken@0
  1054
	return(0);
slouken@0
  1055
}
slouken@0
  1056
megastep@4
  1057
/* Progressively stop the music */
megastep@4
  1058
int Mix_FadeOutMusic(int ms)
megastep@4
  1059
{
slouken@173
  1060
	int retval = 0;
slouken@173
  1061
icculus@280
  1062
	if (ms <= 0) {  /* just halt immediately. */
icculus@280
  1063
		Mix_HaltMusic();
icculus@280
  1064
		return 1;
icculus@280
  1065
	}
icculus@280
  1066
slouken@173
  1067
	SDL_LockAudio();
icculus@280
  1068
	if ( music_playing) {
icculus@280
  1069
                int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
icculus@280
  1070
                if ( music_playing->fading == MIX_NO_FADING ) {
icculus@280
  1071
	        	music_playing->fade_step = 0;
icculus@280
  1072
                } else {
icculus@280
  1073
                        int step;
icculus@280
  1074
                        int old_fade_steps = music_playing->fade_steps;
icculus@280
  1075
                        if ( music_playing->fading == MIX_FADING_OUT ) {
icculus@280
  1076
                                step = music_playing->fade_step;
icculus@280
  1077
                        } else {
icculus@280
  1078
                                step = old_fade_steps
icculus@280
  1079
                                        - music_playing->fade_step + 1;
icculus@280
  1080
                        }
icculus@280
  1081
                        music_playing->fade_step = (step * fade_steps)
icculus@280
  1082
                                / old_fade_steps;
icculus@280
  1083
                }
slouken@173
  1084
		music_playing->fading = MIX_FADING_OUT;
icculus@280
  1085
		music_playing->fade_steps = fade_steps;
slouken@173
  1086
		retval = 1;
megastep@4
  1087
	}
slouken@173
  1088
	SDL_UnlockAudio();
slouken@173
  1089
slouken@173
  1090
	return(retval);
megastep@4
  1091
}
megastep@4
  1092
megastep@4
  1093
Mix_Fading Mix_FadingMusic(void)
megastep@4
  1094
{
slouken@173
  1095
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
  1096
slouken@173
  1097
	SDL_LockAudio();
slouken@173
  1098
	if ( music_playing ) {
slouken@173
  1099
		fading = music_playing->fading;
slouken@173
  1100
	}
slouken@173
  1101
	SDL_UnlockAudio();
slouken@173
  1102
slouken@173
  1103
	return(fading);
megastep@4
  1104
}
megastep@4
  1105
slouken@0
  1106
/* Pause/Resume the music stream */
slouken@0
  1107
void Mix_PauseMusic(void)
slouken@0
  1108
{
slouken@173
  1109
	music_active = 0;
slouken@0
  1110
}
megastep@4
  1111
slouken@0
  1112
void Mix_ResumeMusic(void)
slouken@0
  1113
{
slouken@173
  1114
	music_active = 1;
slouken@0
  1115
}
slouken@0
  1116
slouken@0
  1117
void Mix_RewindMusic(void)
slouken@0
  1118
{
slouken@173
  1119
	Mix_SetMusicPosition(0.0);
slouken@0
  1120
}
slouken@0
  1121
megastep@13
  1122
int Mix_PausedMusic(void)
megastep@13
  1123
{
slouken@17
  1124
	return (music_active == 0);
megastep@13
  1125
}
megastep@13
  1126
slouken@0
  1127
/* Check the status of the music */
slouken@173
  1128
static int music_internal_playing()
slouken@173
  1129
{
slouken@173
  1130
	int playing = 1;
slouken@173
  1131
	switch (music_playing->type) {
slouken@173
  1132
#ifdef CMD_MUSIC
slouken@173
  1133
	    case MUS_CMD:
slouken@173
  1134
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
  1135
			playing = 0;
slouken@173
  1136
		}
slouken@173
  1137
		break;
slouken@173
  1138
#endif
slouken@173
  1139
#ifdef WAV_MUSIC
slouken@173
  1140
	    case MUS_WAV:
slouken@173
  1141
		if ( ! WAVStream_Active() ) {
slouken@173
  1142
			playing = 0;
slouken@173
  1143
		}
slouken@173
  1144
		break;
slouken@173
  1145
#endif
slouken@481
  1146
#ifdef MODPLUG_MUSIC
slouken@481
  1147
	    case MUS_MODPLUG:
slouken@481
  1148
		if ( ! modplug_playing(music_playing->data.modplug) ) {
slouken@481
  1149
			playing = 0;
slouken@481
  1150
		}
slouken@481
  1151
		break;
slouken@481
  1152
#endif
slouken@411
  1153
#ifdef MOD_MUSIC
slouken@173
  1154
	    case MUS_MOD:
slouken@411
  1155
		if ( ! MOD_playing(music_playing->data.module) ) {
slouken@173
  1156
			playing = 0;
slouken@173
  1157
		}
slouken@173
  1158
		break;
slouken@173
  1159
#endif
slouken@173
  1160
#ifdef MID_MUSIC
slouken@173
  1161
	    case MUS_MID:
slouken@173
  1162
#ifdef USE_NATIVE_MIDI
slouken@173
  1163
		if ( native_midi_ok ) {
slouken@173
  1164
			if ( ! native_midi_active() )
slouken@173
  1165
				playing = 0;
slouken@173
  1166
		} MIDI_ELSE
slouken@173
  1167
#endif
slouken@173
  1168
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1169
		if ( timidity_ok ) {
slouken@173
  1170
			if ( ! Timidity_Active() )
slouken@173
  1171
				playing = 0;
slouken@173
  1172
		}
slouken@173
  1173
#endif
slouken@173
  1174
		break;
slouken@173
  1175
#endif
slouken@173
  1176
#ifdef OGG_MUSIC
slouken@173
  1177
	    case MUS_OGG:
slouken@173
  1178
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1179
			playing = 0;
slouken@173
  1180
		}
slouken@173
  1181
		break;
slouken@173
  1182
#endif
slouken@382
  1183
#ifdef FLAC_MUSIC
slouken@382
  1184
	    case MUS_FLAC:
slouken@382
  1185
		if ( ! FLAC_playing(music_playing->data.flac) ) {
slouken@382
  1186
			playing = 0;
slouken@382
  1187
		}
slouken@382
  1188
		break;
slouken@382
  1189
#endif
slouken@173
  1190
#ifdef MP3_MUSIC
slouken@173
  1191
	    case MUS_MP3:
slouken@314
  1192
		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1193
			playing = 0;
slouken@173
  1194
		break;
slouken@173
  1195
#endif
slouken@357
  1196
#ifdef MP3_MAD_MUSIC
slouken@357
  1197
	    case MUS_MP3_MAD:
slouken@357
  1198
		if (!mad_isPlaying(music_playing->data.mp3_mad)) {
slouken@357
  1199
			playing = 0;
slouken@357
  1200
		}
slouken@357
  1201
		break;
slouken@357
  1202
#endif
slouken@173
  1203
	    default:
slouken@173
  1204
		playing = 0;
slouken@173
  1205
		break;
slouken@173
  1206
	}
slouken@173
  1207
	return(playing);
slouken@173
  1208
}
megastep@13
  1209
int Mix_PlayingMusic(void)
slouken@0
  1210
{
slouken@173
  1211
	int playing = 0;
slouken@173
  1212
slouken@173
  1213
	SDL_LockAudio();
slouken@173
  1214
	if ( music_playing ) {
slouken@173
  1215
		playing = music_internal_playing();
slouken@0
  1216
	}
slouken@173
  1217
	SDL_UnlockAudio();
slouken@173
  1218
slouken@173
  1219
	return(playing);
slouken@0
  1220
}
slouken@0
  1221
slouken@0
  1222
/* Set the external music playback command */
slouken@0
  1223
int Mix_SetMusicCMD(const char *command)
slouken@0
  1224
{
slouken@0
  1225
	Mix_HaltMusic();
slouken@0
  1226
	if ( music_cmd ) {
slouken@0
  1227
		free(music_cmd);
slouken@0
  1228
		music_cmd = NULL;
slouken@0
  1229
	}
slouken@0
  1230
	if ( command ) {
slouken@0
  1231
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
  1232
		if ( music_cmd == NULL ) {
slouken@0
  1233
			return(-1);
slouken@0
  1234
		}
slouken@0
  1235
		strcpy(music_cmd, command);
slouken@0
  1236
	}
slouken@0
  1237
	return(0);
slouken@0
  1238
}
slouken@0
  1239
slouken@154
  1240
int Mix_SetSynchroValue(int i)
slouken@154
  1241
{
slouken@419
  1242
	/* Not supported by any players at this time */
slouken@154
  1243
	return(-1);
slouken@154
  1244
}
slouken@154
  1245
slouken@154
  1246
int Mix_GetSynchroValue(void)
slouken@154
  1247
{
slouken@419
  1248
	/* Not supported by any players at this time */
slouken@154
  1249
	return(-1);
slouken@154
  1250
}
slouken@154
  1251
slouken@154
  1252
slouken@0
  1253
/* Uninitialize the music players */
slouken@0
  1254
void close_music(void)
slouken@0
  1255
{
slouken@0
  1256
	Mix_HaltMusic();
slouken@0
  1257
#ifdef CMD_MUSIC
slouken@0
  1258
	Mix_SetMusicCMD(NULL);
slouken@0
  1259
#endif
slouken@481
  1260
#ifdef MODPLUG_MUSIC
slouken@481
  1261
	modplug_exit();
slouken@481
  1262
#endif
slouken@411
  1263
#ifdef MOD_MUSIC
slouken@411
  1264
	MOD_exit();
slouken@0
  1265
#endif
patmandin@264
  1266
#ifdef MID_MUSIC
patmandin@264
  1267
# ifdef USE_TIMIDITY_MIDI
patmandin@264
  1268
	Timidity_Close();
patmandin@264
  1269
# endif
patmandin@264
  1270
#endif
icculus@390
  1271
icculus@390
  1272
	/* rcg06042009 report available decoders at runtime. */
icculus@390
  1273
	free(music_decoders);
icculus@390
  1274
	music_decoders = NULL;
icculus@390
  1275
	num_decoders = 0;
slouken@0
  1276
}
slouken@0
  1277
slouken@381
  1278
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
slouken@381
  1279
{
slouken@246
  1280
	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
slouken@246
  1281
	/*		    or there are too many formats supported by MikMod or MikMod does */
slouken@246
  1282
	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
slouken@246
  1283
	/*		    the check can be uncommented */
slouken@381
  1284
	Uint8 moremagic[9];
slouken@226
  1285
	Mix_Music *music;
slouken@246
  1286
	int start;
slouken@226
  1287
patmandin@336
  1288
	if (!rw) {
patmandin@336
  1289
		Mix_SetError("RWops pointer is NULL");
patmandin@336
  1290
		return NULL;
patmandin@336
  1291
	}
patmandin@336
  1292
slouken@226
  1293
	/* Figure out what kind of file this is */
slouken@246
  1294
	start = SDL_RWtell(rw);
slouken@381
  1295
	if ( SDL_RWread(rw,magic,1,4) != 4 ||
slouken@381
  1296
	     SDL_RWread(rw,moremagic,1,8) != 8 ) {
slouken@226
  1297
		Mix_SetError("Couldn't read from RWops");
slouken@226
  1298
		return NULL;
slouken@226
  1299
	}
slouken@473
  1300
	SDL_RWseek(rw, start, RW_SEEK_SET);
slouken@246
  1301
	magic[4]='\0';
slouken@381
  1302
	moremagic[8] = '\0';
slouken@226
  1303
slouken@226
  1304
	/* Allocate memory for the music structure */
slouken@226
  1305
	music=(Mix_Music *)malloc(sizeof(Mix_Music));
slouken@226
  1306
	if (music==NULL ) {
slouken@226
  1307
		Mix_SetError("Out of memory");
slouken@226
  1308
		return(NULL);
slouken@226
  1309
	}
slouken@226
  1310
	music->error = 0;
slouken@226
  1311
slouken@381
  1312
#ifdef WAV_MUSIC
slouken@381
  1313
	/* WAVE files have the magic four bytes "RIFF"
slouken@381
  1314
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@381
  1315
	 */
slouken@381
  1316
	if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@381
  1317
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@381
  1318
		music->type = MUS_WAV;
slouken@381
  1319
		music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic);
slouken@381
  1320
		if ( music->data.wave == NULL ) {
slouken@381
  1321
			music->error = 1;
slouken@381
  1322
		}
slouken@381
  1323
slouken@381
  1324
	} else
slouken@381
  1325
#endif
slouken@246
  1326
#ifdef OGG_MUSIC
slouken@246
  1327
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@246
  1328
	if ( strcmp((char *)magic, "OggS") == 0 ) {
slouken@246
  1329
		music->type = MUS_OGG;
slouken@246
  1330
		music->data.ogg = OGG_new_RW(rw);
slouken@246
  1331
		if ( music->data.ogg == NULL ) {
slouken@246
  1332
			music->error = 1;
slouken@246
  1333
		}
slouken@246
  1334
	} else
slouken@246
  1335
#endif
slouken@382
  1336
#ifdef FLAC_MUSIC
slouken@382
  1337
	/* FLAC files have the magic four bytes "fLaC" */
slouken@382
  1338
	if ( strcmp((char *)magic, "fLaC") == 0 ) {
slouken@382
  1339
		music->type = MUS_FLAC;
slouken@382
  1340
		music->data.flac = FLAC_new_RW(rw);
slouken@382
  1341
		if ( music->data.flac == NULL ) {
slouken@382
  1342
			music->error = 1;
slouken@382
  1343
		}
slouken@382
  1344
	} else
slouken@382
  1345
#endif
slouken@256
  1346
#ifdef MP3_MUSIC
slouken@398
  1347
	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
slouken@470
  1348
		if ( Mix_Init(MIX_INIT_MP3) ) {
slouken@314
  1349
			SMPEG_Info info;
slouken@314
  1350
			music->type = MUS_MP3;
slouken@314
  1351
			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
slouken@314
  1352
			if ( !info.has_audio ) {
slouken@314
  1353
				Mix_SetError("MPEG file does not have any audio stream.");
slouken@314
  1354
				music->error = 1;
slouken@314
  1355
			} else {
slouken@314
  1356
				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@314
  1357
			}
slouken@314
  1358
		} else {
slouken@256
  1359
			music->error = 1;
slouken@256
  1360
		}
slouken@256
  1361
	} else
slouken@256
  1362
#endif
slouken@357
  1363
#ifdef MP3_MAD_MUSIC
slouken@398
  1364
	if ( ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) || ( strncmp((char *)magic, "ID3", 3) == 0 ) ) {
slouken@357
  1365
		music->type = MUS_MP3_MAD;
slouken@357
  1366
		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer);
slouken@357
  1367
		if (music->data.mp3_mad == 0) {
slouken@357
  1368
			Mix_SetError("Could not initialize MPEG stream.");
slouken@357
  1369
			music->error = 1;
slouken@357
  1370
		}
slouken@357
  1371
	} else
slouken@357
  1372
#endif
patmandin@265
  1373
#ifdef MID_MUSIC
patmandin@265
  1374
	/* MIDI files have the magic four bytes "MThd" */
patmandin@265
  1375
	if ( strcmp((char *)magic, "MThd") == 0 ) {
patmandin@265
  1376
		music->type = MUS_MID;
patmandin@269
  1377
#ifdef USE_NATIVE_MIDI
patmandin@269
  1378
		if ( native_midi_ok ) {
patmandin@269
  1379
			music->data.nativemidi = native_midi_loadsong_RW(rw);
patmandin@335
  1380
	  		if ( music->data.nativemidi == NULL ) {
patmandin@335
  1381
		  		Mix_SetError("%s", native_midi_error());
patmandin@335
  1382
			  	music->error = 1;
patmandin@269
  1383
			}
patmandin@269
  1384
		} MIDI_ELSE
patmandin@269
  1385
#endif
patmandin@265
  1386
#ifdef USE_TIMIDITY_MIDI
patmandin@269
  1387
		if ( timidity_ok ) {
patmandin@269
  1388
			music->data.midi = Timidity_LoadSong_RW(rw);
patmandin@335
  1389
			if ( music->data.midi == NULL ) {
patmandin@335
  1390
				Mix_SetError("%s", Timidity_Error());
patmandin@335
  1391
				music->error = 1;
patmandin@269
  1392
			}
patmandin@335
  1393
		} else {
patmandin@335
  1394
			Mix_SetError("%s", Timidity_Error());
patmandin@335
  1395
			music->error = 1;
patmandin@265
  1396
		}
patmandin@265
  1397
#endif
patmandin@265
  1398
	} else
patmandin@265
  1399
#endif
slouken@481
  1400
#if defined(MODPLUG_MUSIC) || defined(MODPLUG_MUSIC)
slouken@481
  1401
	if (1) {
slouken@481
  1402
		music->error = 1;
slouken@481
  1403
#ifdef MODPLUG_MUSIC
slouken@481
  1404
		if ( music->error ) {
slouken@481
  1405
			music->type = MUS_MODPLUG;
slouken@481
  1406
			music->data.modplug = modplug_new_RW(rw);
slouken@481
  1407
			if ( music->data.modplug ) {
slouken@481
  1408
				music->error = 0;
slouken@481
  1409
			}
slouken@481
  1410
		}
slouken@481
  1411
#endif
slouken@411
  1412
#ifdef MOD_MUSIC
slouken@481
  1413
		if ( music->error ) {
slouken@481
  1414
			music->type = MUS_MOD;
slouken@481
  1415
			music->data.module = MOD_new_RW(rw);
slouken@481
  1416
			if ( music->data.module ) {
slouken@481
  1417
				music->error = 0;
slouken@481
  1418
			}
slouken@226
  1419
		}
slouken@481
  1420
#endif
slouken@226
  1421
	} else
slouken@481
  1422
#endif /* MODPLUG_MUSIC || MOD_MUSIC */
slouken@226
  1423
	{
slouken@226
  1424
		Mix_SetError("Unrecognized music format");
slouken@226
  1425
		music->error=1;
slouken@226
  1426
	}
slouken@226
  1427
	if (music->error) {
slouken@226
  1428
		free(music);
slouken@226
  1429
		music=NULL;
slouken@226
  1430
	}
slouken@226
  1431
	return(music);
slouken@226
  1432
}