music.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 09:29:50 +0000
changeset 415 494bc6858d1f
parent 412 4c1f81481f87
child 419 e27fe0bfe470
permissions -rw-r--r--
Fixed bug #509

Patrice Mandin 2007-10-27 04:52:36 PDT

in music.c, line 461:
native_midi_ok = !timidity_ok;

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