music.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 05 Jun 2009 16:07:08 +0000
changeset 390 246b88b9b1f1
parent 386 695494546b3c
child 398 15acba767487
permissions -rw-r--r--
SDL_mixer: Added decoder enumeration API.
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@245
    35
#define SDL_SURROUND
slouken@245
    36
slouken@245
    37
#ifdef SDL_SURROUND
slouken@245
    38
#define MAX_OUTPUT_CHANNELS 6
slouken@245
    39
#else
slouken@245
    40
#define MAX_OUTPUT_CHANNELS 2
slouken@245
    41
#endif
slouken@245
    42
slouken@0
    43
/* The music command hack is UNIX specific */
slouken@0
    44
#ifndef unix
slouken@0
    45
#undef CMD_MUSIC
slouken@0
    46
#endif
slouken@0
    47
slouken@0
    48
#ifdef CMD_MUSIC
slouken@0
    49
#include "music_cmd.h"
slouken@0
    50
#endif
slouken@0
    51
#ifdef WAV_MUSIC
slouken@0
    52
#include "wavestream.h"
slouken@0
    53
#endif
slouken@249
    54
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@29
    55
#  include "mikmod.h"
slouken@29
    56
#  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
slouken@29
    57
#    define UNIMOD			MODULE
slouken@29
    58
#    define MikMod_Init()		MikMod_Init(NULL)
slouken@29
    59
#    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
slouken@254
    60
#    ifndef LIBMIKMOD_MUSIC
slouken@254
    61
#    define MikMod_LoadSongRW(a,b)	Player_LoadRW(a,b,0)
slouken@249
    62
#    endif
slouken@29
    63
#    define MikMod_FreeSong		Player_Free
slouken@29
    64
     extern int MikMod_errno;
slouken@29
    65
#  else                                        /* old MikMod 3.0.3 */
slouken@29
    66
#    define MikMod_strerror(x)		_mm_errmsg[x])
slouken@29
    67
#    define MikMod_errno		_mm_errno
slouken@29
    68
#  endif
slouken@0
    69
#endif
slouken@0
    70
#ifdef MID_MUSIC
slouken@106
    71
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    72
#    include "timidity.h"
slouken@106
    73
#  endif
slouken@106
    74
#  ifdef USE_NATIVE_MIDI
slouken@106
    75
#    include "native_midi.h"
slouken@106
    76
#  endif
slouken@106
    77
#  if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
slouken@106
    78
#    define MIDI_ELSE	else
slouken@106
    79
#  else
slouken@106
    80
#    define MIDI_ELSE
slouken@106
    81
#  endif
slouken@0
    82
#endif
slouken@63
    83
#ifdef OGG_MUSIC
slouken@63
    84
#include "music_ogg.h"
slouken@63
    85
#endif
slouken@0
    86
#ifdef MP3_MUSIC
slouken@314
    87
#include "dynamic_mp3.h"
slouken@357
    88
#endif
slouken@357
    89
#ifdef MP3_MAD_MUSIC
slouken@357
    90
#include "music_mad.h"
slouken@357
    91
#endif
slouken@382
    92
#ifdef FLAC_MUSIC
slouken@382
    93
#include "music_flac.h"
slouken@382
    94
#endif
slouken@0
    95
slouken@357
    96
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
    97
static SDL_AudioSpec used_mixer;
slouken@0
    98
#endif
slouken@0
    99
slouken@357
   100
slouken@42
   101
int volatile music_active = 1;
slouken@42
   102
static int volatile music_stopped = 0;
slouken@0
   103
static int music_loops = 0;
slouken@0
   104
static char *music_cmd = NULL;
slouken@42
   105
static Mix_Music * volatile music_playing = NULL;
slouken@17
   106
static int music_volume = MIX_MAX_VOLUME;
slouken@0
   107
static int music_swap8;
slouken@0
   108
static int music_swap16;
megastep@4
   109
slouken@0
   110
struct _Mix_Music {
slouken@177
   111
	Mix_MusicType type;
slouken@0
   112
	union {
slouken@0
   113
#ifdef CMD_MUSIC
slouken@0
   114
		MusicCMD *cmd;
slouken@0
   115
#endif
slouken@0
   116
#ifdef WAV_MUSIC
slouken@0
   117
		WAVStream *wave;
slouken@0
   118
#endif
slouken@249
   119
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   120
		UNIMOD *module;
slouken@0
   121
#endif
slouken@0
   122
#ifdef MID_MUSIC
slouken@106
   123
#ifdef USE_TIMIDITY_MIDI
slouken@0
   124
		MidiSong *midi;
slouken@106
   125
#endif
slouken@98
   126
#ifdef USE_NATIVE_MIDI
slouken@98
   127
		NativeMidiSong *nativemidi;
slouken@98
   128
#endif
slouken@0
   129
#endif
slouken@63
   130
#ifdef OGG_MUSIC
slouken@63
   131
		OGG_music *ogg;
slouken@63
   132
#endif
slouken@0
   133
#ifdef MP3_MUSIC
slouken@0
   134
		SMPEG *mp3;
slouken@0
   135
#endif
slouken@357
   136
#ifdef MP3_MAD_MUSIC
slouken@357
   137
		mad_data *mp3_mad;
slouken@357
   138
#endif
slouken@382
   139
#ifdef FLAC_MUSIC
slouken@382
   140
		FLAC_music *flac;
slouken@382
   141
#endif
slouken@0
   142
	} data;
megastep@4
   143
	Mix_Fading fading;
slouken@17
   144
	int fade_step;
slouken@17
   145
	int fade_steps;
slouken@0
   146
	int error;
slouken@0
   147
};
slouken@29
   148
#ifdef MID_MUSIC
slouken@106
   149
#ifdef USE_TIMIDITY_MIDI
slouken@0
   150
static int timidity_ok;
slouken@142
   151
static int samplesize;
slouken@106
   152
#endif
slouken@98
   153
#ifdef USE_NATIVE_MIDI
slouken@98
   154
static int native_midi_ok;
slouken@98
   155
#endif
slouken@29
   156
#endif
slouken@0
   157
slouken@245
   158
/* Reference for converting mikmod output to 4/6 channels */
slouken@245
   159
static int current_output_channels;
slouken@245
   160
static Uint16 current_output_format;
slouken@245
   161
slouken@17
   162
/* Used to calculate fading steps */
slouken@17
   163
static int ms_per_step;
slouken@17
   164
icculus@390
   165
/* rcg06042009 report available decoders at runtime. */
icculus@390
   166
static const char **music_decoders = NULL;
icculus@390
   167
static int num_decoders = 0;
icculus@390
   168
icculus@390
   169
int Mix_NumMusicDecoders(void)
icculus@390
   170
{
icculus@390
   171
	return(num_decoders);
icculus@390
   172
}
icculus@390
   173
icculus@390
   174
const char *Mix_GetMusicDecoder(int index)
icculus@390
   175
{
icculus@390
   176
	if ((index < 0) || (index >= num_decoders)) {
icculus@390
   177
		return NULL;
icculus@390
   178
	}
icculus@390
   179
	return(music_decoders[index]);
icculus@390
   180
}
icculus@390
   181
icculus@390
   182
static void add_music_decoder(const char *decoder)
icculus@390
   183
{
icculus@390
   184
	void *ptr = realloc(music_decoders, num_decoders * sizeof (const char **));
icculus@390
   185
	if (ptr == NULL) {
icculus@390
   186
		return;  /* oh well, go on without it. */
icculus@390
   187
	}
icculus@390
   188
    music_decoders = (const char **) ptr;
icculus@390
   189
	music_decoders[num_decoders++] = decoder;
icculus@390
   190
}
icculus@390
   191
megastep@10
   192
/* Local low-level functions prototypes */
icculus@237
   193
static void music_internal_initialize_volume(void);
slouken@173
   194
static void music_internal_volume(int volume);
slouken@173
   195
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   196
static int  music_internal_position(double position);
slouken@173
   197
static int  music_internal_playing();
slouken@173
   198
static void music_internal_halt(void);
megastep@7
   199
slouken@29
   200
slouken@29
   201
/* Support for hooking when the music has finished */
slouken@29
   202
static void (*music_finished_hook)(void) = NULL;
slouken@29
   203
slouken@29
   204
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   205
{
slouken@29
   206
	SDL_LockAudio();
slouken@29
   207
	music_finished_hook = music_finished;
slouken@29
   208
	SDL_UnlockAudio();
slouken@29
   209
}
slouken@29
   210
slouken@29
   211
icculus@281
   212
/* If music isn't playing, halt it if no looping is required, restart it */
icculus@281
   213
/* otherwhise. NOP if the music is playing */
icculus@281
   214
static int music_halt_or_loop (void)
icculus@281
   215
{
icculus@281
   216
	/* Restart music if it has to loop */
icculus@281
   217
	
icculus@281
   218
	if (!music_internal_playing()) 
icculus@281
   219
	{
icculus@281
   220
		/* Restart music if it has to loop at a high level */
icculus@281
   221
		if (music_loops && --music_loops)
icculus@281
   222
		{
icculus@281
   223
			Mix_Fading current_fade = music_playing->fading;
icculus@281
   224
			music_internal_play(music_playing, 0.0);
icculus@281
   225
			music_playing->fading = current_fade;
icculus@281
   226
		} 
icculus@281
   227
		else 
icculus@281
   228
		{
icculus@281
   229
			music_internal_halt();
icculus@281
   230
			if (music_finished_hook)
icculus@281
   231
				music_finished_hook();
icculus@281
   232
			
icculus@281
   233
			return 0;
icculus@281
   234
		}
icculus@281
   235
	}
icculus@281
   236
	
icculus@281
   237
	return 1;
icculus@281
   238
}
icculus@281
   239
icculus@281
   240
icculus@281
   241
slouken@0
   242
/* Mixing function */
slouken@0
   243
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   244
{
slouken@173
   245
	if ( music_playing && music_active ) {
megastep@13
   246
		/* Handle fading */
slouken@17
   247
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   248
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@173
   249
				int volume;
slouken@26
   250
				int fade_step = music_playing->fade_step;
slouken@26
   251
				int fade_steps = music_playing->fade_steps;
slouken@17
   252
megastep@4
   253
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   254
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@17
   255
				} else { /* Fading in */
slouken@173
   256
					volume = (music_volume * fade_step) / fade_steps;
slouken@17
   257
				}
slouken@173
   258
				music_internal_volume(volume);
slouken@17
   259
			} else {
slouken@17
   260
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   261
					music_internal_halt();
slouken@173
   262
					if ( music_finished_hook ) {
slouken@173
   263
						music_finished_hook();
slouken@173
   264
					}
megastep@4
   265
					return;
megastep@4
   266
				}
megastep@4
   267
				music_playing->fading = MIX_NO_FADING;
megastep@4
   268
			}
megastep@4
   269
		}
icculus@281
   270
		
icculus@281
   271
		if (music_halt_or_loop() == 0)
icculus@281
   272
			return;
icculus@281
   273
		
icculus@281
   274
		
slouken@0
   275
		switch (music_playing->type) {
slouken@0
   276
#ifdef CMD_MUSIC
slouken@0
   277
			case MUS_CMD:
slouken@0
   278
				/* The playing is done externally */
slouken@0
   279
				break;
slouken@0
   280
#endif
slouken@0
   281
#ifdef WAV_MUSIC
slouken@0
   282
			case MUS_WAV:
slouken@64
   283
				WAVStream_PlaySome(stream, len);
slouken@0
   284
				break;
slouken@0
   285
#endif
slouken@249
   286
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   287
			case MUS_MOD:
slouken@245
   288
				if (current_output_channels > 2) {
slouken@245
   289
					int small_len = 2 * len / current_output_channels;
slouken@245
   290
					int i;
slouken@245
   291
					Uint8 *src, *dst;
slouken@245
   292
slouken@245
   293
					VC_WriteBytes((SBYTE *)stream, small_len);
slouken@245
   294
					/* and extend to len by copying channels */
slouken@245
   295
					src = stream + small_len;
slouken@245
   296
					dst = stream + len;
slouken@245
   297
slouken@245
   298
					switch (current_output_format & 0xFF) {
slouken@245
   299
						case 8:
slouken@245
   300
							for ( i=small_len/2; i; --i ) {
slouken@245
   301
								src -= 2;
slouken@245
   302
								dst -= current_output_channels;
slouken@245
   303
								dst[0] = src[0];
slouken@245
   304
								dst[1] = src[1];
slouken@245
   305
								dst[2] = src[0];
slouken@245
   306
								dst[3] = src[1];
slouken@245
   307
								if (current_output_channels == 6) {
slouken@245
   308
									dst[4] = src[0];
slouken@245
   309
									dst[5] = src[1];
slouken@245
   310
								}
slouken@245
   311
							}
slouken@245
   312
							break;
slouken@245
   313
						case 16:
slouken@245
   314
							for ( i=small_len/4; i; --i ) {
slouken@245
   315
								src -= 4;
slouken@245
   316
								dst -= 2 * current_output_channels;
slouken@245
   317
								dst[0] = src[0];
slouken@245
   318
								dst[1] = src[1];
slouken@245
   319
								dst[2] = src[2];
slouken@245
   320
								dst[3] = src[3];
slouken@245
   321
								dst[4] = src[0];
slouken@245
   322
								dst[5] = src[1];
slouken@245
   323
								dst[6] = src[2];
slouken@245
   324
								dst[7] = src[3];
slouken@245
   325
								if (current_output_channels == 6) {
slouken@245
   326
									dst[8] = src[0];
slouken@245
   327
									dst[9] = src[1];
slouken@245
   328
									dst[10] = src[2];
slouken@245
   329
									dst[11] = src[3];
slouken@245
   330
								}
slouken@245
   331
							}
slouken@245
   332
							break;
slouken@245
   333
					}
slouken@245
   334
slouken@245
   335
slouken@245
   336
slouken@245
   337
				}
slouken@245
   338
				else VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   339
				if ( music_swap8 ) {
slouken@0
   340
					Uint8 *dst;
slouken@42
   341
					int i;
slouken@0
   342
slouken@0
   343
					dst = stream;
slouken@0
   344
					for ( i=len; i; --i ) {
slouken@0
   345
						*dst++ ^= 0x80;
slouken@0
   346
					}
slouken@0
   347
				} else
slouken@0
   348
				if ( music_swap16 ) {
slouken@0
   349
					Uint8 *dst, tmp;
slouken@42
   350
					int i;
slouken@0
   351
slouken@0
   352
					dst = stream;
slouken@0
   353
					for ( i=(len/2); i; --i ) {
slouken@0
   354
						tmp = dst[0];
slouken@0
   355
						dst[0] = dst[1];
slouken@0
   356
						dst[1] = tmp;
slouken@0
   357
						dst += 2;
slouken@0
   358
					}
slouken@0
   359
				}
slouken@0
   360
				break;
slouken@0
   361
#endif
slouken@0
   362
#ifdef MID_MUSIC
slouken@106
   363
#ifdef USE_TIMIDITY_MIDI
slouken@0
   364
			case MUS_MID:
slouken@98
   365
				if ( timidity_ok ) {
slouken@98
   366
					int samples = len / samplesize;
slouken@98
   367
  					Timidity_PlaySome(stream, samples);
slouken@98
   368
				}
slouken@0
   369
				break;
slouken@0
   370
#endif
slouken@106
   371
#endif
slouken@63
   372
#ifdef OGG_MUSIC
slouken@63
   373
			case MUS_OGG:
icculus@281
   374
				
icculus@281
   375
				len = OGG_playAudio(music_playing->data.ogg, stream, len);
icculus@281
   376
				if (len > 0 && music_halt_or_loop())
icculus@281
   377
					OGG_playAudio(music_playing->data.ogg, stream, len);
icculus@281
   378
			
slouken@63
   379
				break;
slouken@63
   380
#endif
slouken@382
   381
#ifdef FLAC_MUSIC
slouken@382
   382
			case MUS_FLAC:
slouken@382
   383
				len = FLAC_playAudio(music_playing->data.flac, stream, len);
slouken@382
   384
				if (len > 0 && music_halt_or_loop())
slouken@382
   385
					FLAC_playAudio(music_playing->data.flac, stream, len);
slouken@382
   386
				break;
slouken@382
   387
#endif
slouken@0
   388
#ifdef MP3_MUSIC
slouken@26
   389
			case MUS_MP3:
slouken@314
   390
				smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   391
				break;
slouken@0
   392
#endif
slouken@357
   393
#ifdef MP3_MAD_MUSIC
slouken@357
   394
			case MUS_MP3_MAD:
slouken@357
   395
				mad_getSamples(music_playing->data.mp3_mad, stream, len);
slouken@357
   396
				break;
slouken@357
   397
#endif
slouken@0
   398
			default:
slouken@0
   399
				/* Unknown music type?? */
slouken@0
   400
				break;
slouken@0
   401
		}
slouken@0
   402
	}
slouken@0
   403
}
slouken@0
   404
slouken@0
   405
/* Initialize the music players with a certain desired audio format */
slouken@0
   406
int open_music(SDL_AudioSpec *mixer)
slouken@0
   407
{
slouken@0
   408
	int music_error;
slouken@276
   409
#ifdef LIBMIKMOD_MUSIC
slouken@276
   410
	CHAR *list;
slouken@276
   411
#endif
slouken@0
   412
slouken@0
   413
	music_error = 0;
slouken@0
   414
#ifdef WAV_MUSIC
slouken@0
   415
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   416
		++music_error;
icculus@390
   417
	} else {
icculus@390
   418
		add_music_decoder("WAVE");
slouken@0
   419
	}
slouken@0
   420
#endif
slouken@249
   421
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   422
	/* Set the MikMod music format */
slouken@0
   423
	music_swap8 = 0;
slouken@0
   424
	music_swap16 = 0;
slouken@0
   425
	switch (mixer->format) {
slouken@0
   426
slouken@0
   427
		case AUDIO_U8:
slouken@0
   428
		case AUDIO_S8: {
slouken@0
   429
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   430
				music_swap8 = 1;
slouken@0
   431
			}
slouken@0
   432
			md_mode = 0;
slouken@0
   433
		}
slouken@0
   434
		break;
slouken@0
   435
slouken@0
   436
		case AUDIO_S16LSB:
slouken@0
   437
		case AUDIO_S16MSB: {
slouken@0
   438
			/* See if we need to correct MikMod mixing */
slouken@0
   439
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   440
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   441
#else
slouken@0
   442
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   443
#endif
slouken@0
   444
				music_swap16 = 1;
slouken@0
   445
			}
slouken@0
   446
			md_mode = DMODE_16BITS;
slouken@0
   447
		}
slouken@0
   448
		break;
slouken@0
   449
slouken@0
   450
		default: {
slouken@26
   451
			Mix_SetError("Unknown hardware audio format");
slouken@0
   452
			++music_error;
slouken@0
   453
		}
slouken@0
   454
	}
slouken@245
   455
	current_output_channels = mixer->channels;
slouken@245
   456
	current_output_format = mixer->format;
slouken@0
   457
	if ( mixer->channels > 1 ) {
slouken@245
   458
		if ( mixer->channels > MAX_OUTPUT_CHANNELS ) {
slouken@26
   459
			Mix_SetError("Hardware uses more channels than mixer");
slouken@0
   460
			++music_error;
slouken@0
   461
		}
slouken@0
   462
		md_mode |= DMODE_STEREO;
slouken@0
   463
	}
slouken@277
   464
	md_mixfreq = mixer->freq;
slouken@277
   465
	md_device  = 0;
slouken@277
   466
	md_volume  = 96;
slouken@0
   467
	md_musicvolume = 128;
slouken@0
   468
	md_sndfxvolume = 128;
slouken@277
   469
	md_pansep  = 128;
slouken@277
   470
	md_reverb  = 0;
slouken@277
   471
	md_mode    |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
slouken@249
   472
#ifdef LIBMIKMOD_MUSIC
slouken@276
   473
	list = MikMod_InfoDriver();
slouken@276
   474
	if ( list )
slouken@276
   475
	  free(list);
slouken@276
   476
	else
slouken@278
   477
#endif
slouken@277
   478
	MikMod_RegisterDriver(&drv_nos);
slouken@249
   479
#ifdef LIBMIKMOD_MUSIC
slouken@276
   480
	list = MikMod_InfoLoader();
slouken@276
   481
	if ( list )
slouken@276
   482
	  free(list);
slouken@276
   483
	else
slouken@249
   484
#endif
slouken@0
   485
	MikMod_RegisterAllLoaders();
slouken@0
   486
	if ( MikMod_Init() ) {
slouken@29
   487
		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   488
		++music_error;
slouken@0
   489
	}
icculus@390
   490
	add_music_decoder("MIKMOD");
slouken@0
   491
#endif
slouken@0
   492
#ifdef MID_MUSIC
slouken@106
   493
#ifdef USE_TIMIDITY_MIDI
slouken@142
   494
	samplesize = mixer->size / mixer->samples;
slouken@100
   495
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@102
   496
	                    mixer->channels, mixer->samples) == 0 ) {
slouken@100
   497
		timidity_ok = 1;
icculus@390
   498
		add_music_decoder("TIMIDITY");
slouken@0
   499
	} else {
slouken@0
   500
		timidity_ok = 0;
slouken@0
   501
	}
slouken@106
   502
#endif
slouken@100
   503
#ifdef USE_NATIVE_MIDI
slouken@106
   504
#ifdef USE_TIMIDITY_MIDI
slouken@100
   505
	native_midi_ok = !timidity_ok;
slouken@106
   506
	if ( native_midi_ok )
slouken@106
   507
#endif
slouken@101
   508
		native_midi_ok = native_midi_detect();
icculus@390
   509
	if ( native_midi_ok )
icculus@390
   510
		add_music_decoder("NATIVEMIDI");
slouken@100
   511
#endif
slouken@0
   512
#endif
slouken@63
   513
#ifdef OGG_MUSIC
slouken@63
   514
	if ( OGG_init(mixer) < 0 ) {
slouken@63
   515
		++music_error;
icculus@390
   516
	} else {
icculus@390
   517
		add_music_decoder("OGG");
slouken@63
   518
	}
slouken@63
   519
#endif
slouken@382
   520
#ifdef FLAC_MUSIC
slouken@382
   521
	if ( FLAC_init(mixer) < 0 ) {
slouken@382
   522
		++music_error;
icculus@390
   523
	} else {
icculus@390
   524
		add_music_decoder("FLAC");
slouken@382
   525
	}
slouken@382
   526
#endif
slouken@357
   527
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
   528
	/* Keep a copy of the mixer */
slouken@0
   529
	used_mixer = *mixer;
icculus@390
   530
	add_music_decoder("MP3");
slouken@0
   531
#endif
icculus@390
   532
slouken@42
   533
	music_playing = NULL;
megastep@7
   534
	music_stopped = 0;
slouken@0
   535
	if ( music_error ) {
slouken@0
   536
		return(-1);
slouken@0
   537
	}
slouken@0
   538
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   539
slouken@26
   540
	/* Calculate the number of ms for each callback */
slouken@44
   541
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   542
slouken@0
   543
	return(0);
slouken@0
   544
}
slouken@0
   545
slouken@125
   546
/* Portable case-insensitive string compare function */
slouken@125
   547
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   548
{
slouken@125
   549
	while ( *str1 && *str2 ) {
slouken@125
   550
		if ( toupper((unsigned char)*str1) !=
slouken@125
   551
		     toupper((unsigned char)*str2) )
slouken@125
   552
			break;
slouken@125
   553
		++str1;
slouken@125
   554
		++str2;
slouken@125
   555
	}
slouken@125
   556
	return (!*str1 && !*str2);
slouken@125
   557
}
slouken@125
   558
slouken@0
   559
/* Load a music file */
slouken@0
   560
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   561
{
slouken@0
   562
	FILE *fp;
slouken@125
   563
	char *ext;
slouken@245
   564
	Uint8 magic[5], moremagic[9];
slouken@0
   565
	Mix_Music *music;
slouken@0
   566
slouken@0
   567
	/* Figure out what kind of file this is */
slouken@0
   568
	fp = fopen(file, "rb");
slouken@0
   569
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   570
		if ( fp != NULL ) {
slouken@0
   571
			fclose(fp);
slouken@0
   572
		}
slouken@26
   573
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   574
		return(NULL);
slouken@0
   575
	}
slouken@245
   576
	if (!fread(moremagic, 8, 1, fp)) {
slouken@245
   577
		Mix_SetError("Couldn't read from '%s'", file);
slouken@245
   578
		return(NULL);
slouken@245
   579
	}
slouken@0
   580
	magic[4] = '\0';
slouken@245
   581
	moremagic[8] = '\0';
slouken@0
   582
	fclose(fp);
slouken@0
   583
slouken@125
   584
	/* Figure out the file extension, so we can determine the type */
slouken@125
   585
	ext = strrchr(file, '.');
slouken@125
   586
	if ( ext ) ++ext; /* skip the dot in the extension */
slouken@125
   587
slouken@0
   588
	/* Allocate memory for the music structure */
slouken@0
   589
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   590
	if ( music == NULL ) {
slouken@26
   591
		Mix_SetError("Out of memory");
slouken@0
   592
		return(NULL);
slouken@0
   593
	}
slouken@0
   594
	music->error = 0;
slouken@0
   595
slouken@0
   596
#ifdef CMD_MUSIC
slouken@0
   597
	if ( music_cmd ) {
slouken@0
   598
		music->type = MUS_CMD;
slouken@0
   599
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   600
		if ( music->data.cmd == NULL ) {
slouken@0
   601
			music->error = 1;
slouken@0
   602
		}
slouken@0
   603
	} else
slouken@0
   604
#endif
slouken@0
   605
#ifdef WAV_MUSIC
slouken@0
   606
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   607
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   608
	 */
slouken@135
   609
	if ( (ext && MIX_string_equals(ext, "WAV")) ||
slouken@245
   610
	     ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@57
   611
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   612
		music->type = MUS_WAV;
slouken@57
   613
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   614
		if ( music->data.wave == NULL ) {
slouken@135
   615
		  	Mix_SetError("Unable to load WAV file");
slouken@0
   616
			music->error = 1;
slouken@0
   617
		}
slouken@0
   618
	} else
slouken@0
   619
#endif
slouken@0
   620
#ifdef MID_MUSIC
slouken@0
   621
	/* MIDI files have the magic four bytes "MThd" */
slouken@135
   622
	if ( (ext && MIX_string_equals(ext, "MID")) ||
slouken@135
   623
	     (ext && MIX_string_equals(ext, "MIDI")) ||
slouken@245
   624
	     strcmp((char *)magic, "MThd") == 0  ||
slouken@245
   625
	     ( strcmp((char *)magic, "RIFF") == 0  &&
slouken@245
   626
	  	strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
slouken@0
   627
		music->type = MUS_MID;
slouken@98
   628
#ifdef USE_NATIVE_MIDI
slouken@98
   629
  		if ( native_midi_ok ) {
slouken@98
   630
  			music->data.nativemidi = native_midi_loadsong((char *)file);
slouken@98
   631
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   632
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   633
			  	music->error = 1;
slouken@98
   634
			}
slouken@106
   635
	  	} MIDI_ELSE
slouken@98
   636
#endif
slouken@106
   637
#ifdef USE_TIMIDITY_MIDI
slouken@0
   638
		if ( timidity_ok ) {
slouken@0
   639
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   640
			if ( music->data.midi == NULL ) {
slouken@26
   641
				Mix_SetError("%s", Timidity_Error());
slouken@0
   642
				music->error = 1;
slouken@0
   643
			}
slouken@98
   644
		} else {
slouken@26
   645
			Mix_SetError("%s", Timidity_Error());
slouken@0
   646
			music->error = 1;
slouken@0
   647
		}
slouken@106
   648
#endif
slouken@0
   649
	} else
slouken@0
   650
#endif
slouken@63
   651
#ifdef OGG_MUSIC
slouken@63
   652
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@135
   653
	if ( (ext && MIX_string_equals(ext, "OGG")) ||
slouken@133
   654
	     strcmp((char *)magic, "OggS") == 0 ) {
slouken@63
   655
		music->type = MUS_OGG;
slouken@63
   656
		music->data.ogg = OGG_new(file);
slouken@63
   657
		if ( music->data.ogg == NULL ) {
slouken@63
   658
			music->error = 1;
slouken@63
   659
		}
slouken@63
   660
	} else
slouken@63
   661
#endif
slouken@382
   662
#ifdef FLAC_MUSIC
slouken@382
   663
	/* FLAC files have the magic four bytes "fLaC" */
slouken@382
   664
	if ( (ext && MIX_string_equals(ext, "FLAC")) ||
slouken@382
   665
		 strcmp((char *)magic, "fLaC") == 0 ) {
slouken@382
   666
		music->type = MUS_FLAC;
slouken@382
   667
		music->data.flac = FLAC_new(file);
slouken@382
   668
		if ( music->data.flac == NULL ) {
slouken@382
   669
			music->error = 1;
slouken@382
   670
		}
slouken@382
   671
	} else
slouken@382
   672
#endif
slouken@0
   673
#ifdef MP3_MUSIC
slouken@135
   674
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
icculus@186
   675
	     (ext && MIX_string_equals(ext, "MP3")) ||
slouken@135
   676
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@256
   677
	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ) {
slouken@314
   678
		if ( Mix_InitMP3() == 0 ) {
slouken@314
   679
			SMPEG_Info info;
slouken@314
   680
			music->type = MUS_MP3;
slouken@314
   681
			music->data.mp3 = smpeg.SMPEG_new(file, &info, 0);
slouken@314
   682
			if ( !info.has_audio ) {
slouken@314
   683
				Mix_SetError("MPEG file does not have any audio stream.");
slouken@314
   684
				music->error = 1;
slouken@314
   685
			} else {
slouken@314
   686
				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@314
   687
			}
slouken@314
   688
		} else {
slouken@0
   689
			music->error = 1;
slouken@0
   690
		}
slouken@0
   691
	} else
slouken@0
   692
#endif
slouken@357
   693
#ifdef MP3_MAD_MUSIC
slouken@357
   694
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
slouken@357
   695
	     (ext && MIX_string_equals(ext, "MP3")) ||
slouken@357
   696
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@357
   697
	     (ext && MIX_string_equals(ext, "MAD")) ||
slouken@357
   698
	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ) {
slouken@357
   699
		music->type = MUS_MP3_MAD;
slouken@357
   700
		music->data.mp3_mad = mad_openFile(file, &used_mixer);
slouken@357
   701
		if (music->data.mp3_mad == 0) {
slouken@357
   702
		    Mix_SetError("Could not initialize MPEG stream.");
slouken@357
   703
			music->error = 1;
slouken@357
   704
		}
slouken@357
   705
	} else
slouken@357
   706
#endif
slouken@249
   707
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   708
	if ( 1 ) {
slouken@0
   709
		music->type = MUS_MOD;
slouken@0
   710
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   711
		if ( music->data.module == NULL ) {
slouken@29
   712
			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   713
			music->error = 1;
slouken@126
   714
		} else {
slouken@126
   715
			/* Stop implicit looping, fade out and other flags. */
slouken@126
   716
			music->data.module->extspd  = 1;
slouken@126
   717
			music->data.module->panflag = 1;
slouken@126
   718
			music->data.module->wrap    = 0;
slouken@285
   719
			music->data.module->loop    = 1;
slouken@129
   720
#if 0 /* Don't set fade out by default - unfortunately there's no real way
slouken@129
   721
         to query the status of the song or set trigger actions.  Hum. */
slouken@126
   722
			music->data.module->fadeout = 1;
slouken@129
   723
#endif
slouken@0
   724
		}
slouken@0
   725
	} else
slouken@0
   726
#endif
slouken@0
   727
	{
slouken@26
   728
		Mix_SetError("Unrecognized music format");
slouken@0
   729
		music->error = 1;
slouken@0
   730
	}
slouken@0
   731
	if ( music->error ) {
slouken@0
   732
		free(music);
slouken@0
   733
		music = NULL;
slouken@0
   734
	}
slouken@0
   735
	return(music);
slouken@0
   736
}
slouken@0
   737
slouken@0
   738
/* Free a music chunk previously loaded */
slouken@0
   739
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   740
{
slouken@0
   741
	if ( music ) {
slouken@173
   742
		/* Stop the music if it's currently playing */
slouken@173
   743
		SDL_LockAudio();
slouken@173
   744
		if ( music == music_playing ) {
slouken@173
   745
			/* Wait for any fade out to finish */
slouken@173
   746
			while ( music->fading == MIX_FADING_OUT ) {
slouken@173
   747
				SDL_UnlockAudio();
slouken@173
   748
				SDL_Delay(100);
slouken@173
   749
				SDL_LockAudio();
slouken@173
   750
			}
slouken@173
   751
			if ( music == music_playing ) {
slouken@173
   752
				music_internal_halt();
megastep@4
   753
			}
slouken@0
   754
		}
slouken@173
   755
		SDL_UnlockAudio();
slouken@0
   756
		switch (music->type) {
slouken@0
   757
#ifdef CMD_MUSIC
slouken@0
   758
			case MUS_CMD:
slouken@0
   759
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   760
				break;
slouken@0
   761
#endif
slouken@0
   762
#ifdef WAV_MUSIC
slouken@0
   763
			case MUS_WAV:
slouken@0
   764
				WAVStream_FreeSong(music->data.wave);
slouken@0
   765
				break;
slouken@0
   766
#endif
slouken@249
   767
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   768
			case MUS_MOD:
slouken@0
   769
				MikMod_FreeSong(music->data.module);
slouken@0
   770
				break;
slouken@0
   771
#endif
slouken@0
   772
#ifdef MID_MUSIC
slouken@0
   773
			case MUS_MID:
slouken@98
   774
#ifdef USE_NATIVE_MIDI
slouken@98
   775
  				if ( native_midi_ok ) {
slouken@98
   776
					native_midi_freesong(music->data.nativemidi);
slouken@106
   777
				} MIDI_ELSE
slouken@98
   778
#endif
slouken@106
   779
#ifdef USE_TIMIDITY_MIDI
slouken@98
   780
				if ( timidity_ok ) {
slouken@98
   781
					Timidity_FreeSong(music->data.midi);
slouken@98
   782
				}
slouken@106
   783
#endif
slouken@0
   784
				break;
slouken@0
   785
#endif
slouken@63
   786
#ifdef OGG_MUSIC
slouken@63
   787
			case MUS_OGG:
slouken@63
   788
				OGG_delete(music->data.ogg);
slouken@63
   789
				break;
slouken@63
   790
#endif
slouken@382
   791
#ifdef FLAC_MUSIC
slouken@382
   792
			case MUS_FLAC:
slouken@382
   793
				FLAC_delete(music->data.flac);
slouken@382
   794
				break;
slouken@382
   795
#endif
slouken@0
   796
#ifdef MP3_MUSIC
slouken@26
   797
			case MUS_MP3:
slouken@314
   798
				smpeg.SMPEG_delete(music->data.mp3);
slouken@314
   799
				Mix_QuitMP3();
slouken@0
   800
				break;
slouken@0
   801
#endif
slouken@357
   802
#ifdef MP3_MAD_MUSIC
slouken@357
   803
			case MUS_MP3_MAD:
slouken@357
   804
				mad_closeFile(music->data.mp3_mad);
slouken@357
   805
				break;
slouken@357
   806
#endif
slouken@0
   807
			default:
slouken@0
   808
				/* Unknown music type?? */
slouken@0
   809
				break;
slouken@0
   810
		}
slouken@0
   811
		free(music);
slouken@0
   812
	}
slouken@0
   813
}
slouken@0
   814
slouken@177
   815
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   816
   music, if 'music' is NULL.
slouken@177
   817
*/
slouken@177
   818
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   819
{
slouken@177
   820
	Mix_MusicType type = MUS_NONE;
slouken@177
   821
slouken@177
   822
	if ( music ) {
slouken@177
   823
		type = music->type;
slouken@177
   824
	} else {
slouken@177
   825
		SDL_LockAudio();
slouken@177
   826
		if ( music_playing ) {
slouken@177
   827
			type = music_playing->type;
slouken@177
   828
		}
slouken@177
   829
		SDL_UnlockAudio();
slouken@177
   830
	}
slouken@177
   831
	return(type);
slouken@177
   832
}
slouken@177
   833
slouken@173
   834
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   835
 */
slouken@173
   836
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   837
{
slouken@173
   838
	int retval = 0;
megastep@10
   839
slouken@173
   840
	/* Note the music we're playing */
slouken@173
   841
	if ( music_playing ) {
slouken@173
   842
		music_internal_halt();
slouken@173
   843
	}
slouken@173
   844
	music_playing = music;
slouken@173
   845
slouken@173
   846
	/* Set the initial volume */
icculus@237
   847
	if ( music->type != MUS_MOD ) {
icculus@237
   848
		music_internal_initialize_volume();
slouken@173
   849
	}
slouken@173
   850
slouken@173
   851
	/* Set up for playback */
slouken@0
   852
	switch (music->type) {
slouken@0
   853
#ifdef CMD_MUSIC
slouken@173
   854
	    case MUS_CMD:
slouken@173
   855
		MusicCMD_Start(music->data.cmd);
slouken@173
   856
		break;
slouken@0
   857
#endif
slouken@0
   858
#ifdef WAV_MUSIC
slouken@173
   859
	    case MUS_WAV:
slouken@173
   860
		WAVStream_Start(music->data.wave);
slouken@173
   861
		break;
slouken@0
   862
#endif
slouken@249
   863
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
   864
	    case MUS_MOD:
slouken@173
   865
		Player_Start(music->data.module);
icculus@237
   866
		/* Player_SetVolume() does nothing before Player_Start() */
icculus@237
   867
		music_internal_initialize_volume();
slouken@173
   868
		break;
slouken@0
   869
#endif
slouken@0
   870
#ifdef MID_MUSIC
slouken@173
   871
	    case MUS_MID:
slouken@98
   872
#ifdef USE_NATIVE_MIDI
slouken@173
   873
		if ( native_midi_ok ) {
slouken@173
   874
			native_midi_start(music->data.nativemidi);
slouken@173
   875
		} MIDI_ELSE
slouken@98
   876
#endif
slouken@106
   877
#ifdef USE_TIMIDITY_MIDI
slouken@173
   878
		if ( timidity_ok ) {
slouken@173
   879
			Timidity_Start(music->data.midi);
slouken@173
   880
		}
slouken@106
   881
#endif
slouken@173
   882
		break;
slouken@0
   883
#endif
slouken@63
   884
#ifdef OGG_MUSIC
slouken@173
   885
	    case MUS_OGG:
slouken@173
   886
		OGG_play(music->data.ogg);
slouken@173
   887
		break;
slouken@63
   888
#endif
slouken@382
   889
#ifdef FLAC_MUSIC
slouken@382
   890
	    case MUS_FLAC:
slouken@382
   891
		FLAC_play(music->data.flac);
slouken@382
   892
		break;
slouken@382
   893
#endif
slouken@0
   894
#ifdef MP3_MUSIC
slouken@173
   895
	    case MUS_MP3:
slouken@314
   896
		smpeg.SMPEG_enableaudio(music->data.mp3,1);
slouken@314
   897
		smpeg.SMPEG_enablevideo(music->data.mp3,0);
slouken@314
   898
		smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
   899
		break;
slouken@0
   900
#endif
slouken@357
   901
#ifdef MP3_MAD_MUSIC
slouken@357
   902
	    case MUS_MP3_MAD:
slouken@357
   903
		mad_start(music->data.mp3_mad);
slouken@357
   904
		break;
slouken@357
   905
#endif
slouken@173
   906
	    default:
slouken@173
   907
		Mix_SetError("Can't play unknown music type");
slouken@173
   908
		retval = -1;
slouken@173
   909
		break;
slouken@0
   910
	}
slouken@173
   911
slouken@173
   912
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   913
	if ( retval == 0 ) {
slouken@173
   914
		if ( position > 0.0 ) {
slouken@173
   915
			if ( music_internal_position(position) < 0 ) {
slouken@173
   916
				Mix_SetError("Position not implemented for music type");
slouken@173
   917
				retval = -1;
slouken@173
   918
			}
slouken@173
   919
		} else {
slouken@173
   920
			music_internal_position(0.0);
slouken@173
   921
		}
slouken@173
   922
	}
slouken@173
   923
slouken@173
   924
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   925
	if ( retval < 0 ) {
slouken@173
   926
		music_playing = NULL;
slouken@173
   927
	}
slouken@173
   928
	return(retval);
slouken@173
   929
}
slouken@173
   930
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   931
{
slouken@173
   932
	int retval;
slouken@173
   933
slouken@173
   934
	/* Don't play null pointers :-) */
slouken@173
   935
	if ( music == NULL ) {
slouken@173
   936
		Mix_SetError("music parameter was NULL");
slouken@173
   937
		return(-1);
slouken@173
   938
	}
slouken@173
   939
slouken@173
   940
	/* Setup the data */
slouken@173
   941
	if ( ms ) {
slouken@173
   942
		music->fading = MIX_FADING_IN;
slouken@173
   943
	} else {
slouken@173
   944
		music->fading = MIX_NO_FADING;
slouken@173
   945
	}
slouken@173
   946
	music->fade_step = 0;
slouken@173
   947
	music->fade_steps = ms/ms_per_step;
slouken@173
   948
slouken@173
   949
	/* Play the puppy */
slouken@173
   950
	SDL_LockAudio();
slouken@173
   951
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   952
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   953
		SDL_UnlockAudio();
slouken@173
   954
		SDL_Delay(100);
slouken@173
   955
		SDL_LockAudio();
slouken@173
   956
	}
slouken@173
   957
	music_active = 1;
slouken@173
   958
	music_loops = loops;
slouken@173
   959
	retval = music_internal_play(music, position);
slouken@173
   960
	SDL_UnlockAudio();
slouken@173
   961
slouken@173
   962
	return(retval);
slouken@173
   963
}
slouken@173
   964
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   965
{
slouken@173
   966
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   967
}
slouken@173
   968
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   969
{
slouken@173
   970
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   971
}
megastep@10
   972
slouken@173
   973
/* Set the playing music position */
slouken@173
   974
int music_internal_position(double position)
megastep@10
   975
{
slouken@173
   976
	int retval = 0;
slouken@173
   977
slouken@173
   978
	switch (music_playing->type) {
slouken@249
   979
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
   980
	    case MUS_MOD:
slouken@173
   981
		Player_SetPosition((UWORD)position);
slouken@173
   982
		break;
slouken@173
   983
#endif
slouken@173
   984
#ifdef OGG_MUSIC
slouken@173
   985
	    case MUS_OGG:
slouken@173
   986
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
   987
		break;
slouken@173
   988
#endif
slouken@382
   989
#ifdef FLAC_MUSIC
slouken@382
   990
	    case MUS_FLAC:
slouken@382
   991
		FLAC_jump_to_time(music_playing->data.flac, position);
slouken@382
   992
		break;
slouken@382
   993
#endif
slouken@173
   994
#ifdef MP3_MUSIC
slouken@173
   995
	    case MUS_MP3:
slouken@176
   996
		if ( position > 0.0 ) {
slouken@314
   997
			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
slouken@179
   998
		} else {
slouken@314
   999
			smpeg.SMPEG_rewind(music_playing->data.mp3);
slouken@314
  1000
			smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
  1001
		}
slouken@173
  1002
		break;
slouken@173
  1003
#endif
slouken@357
  1004
#ifdef MP3_MAD_MUSIC
slouken@357
  1005
	    case MUS_MP3_MAD:
slouken@357
  1006
		mad_seek(music_playing->data.mp3_mad, position);
slouken@357
  1007
		break;
slouken@357
  1008
#endif
slouken@173
  1009
	    default:
slouken@173
  1010
		/* TODO: Implement this for other music backends */
slouken@173
  1011
		retval = -1;
slouken@173
  1012
		break;
megastep@10
  1013
	}
slouken@173
  1014
	return(retval);
megastep@4
  1015
}
slouken@155
  1016
int Mix_SetMusicPosition(double position)
slouken@154
  1017
{
slouken@173
  1018
	int retval;
slouken@173
  1019
slouken@173
  1020
	SDL_LockAudio();
slouken@173
  1021
	if ( music_playing ) {
slouken@173
  1022
		retval = music_internal_position(position);
slouken@173
  1023
		if ( retval < 0 ) {
slouken@173
  1024
			Mix_SetError("Position not implemented for music type");
slouken@154
  1025
		}
slouken@173
  1026
	} else {
slouken@173
  1027
		Mix_SetError("Music isn't playing");
slouken@173
  1028
		retval = -1;
slouken@154
  1029
	}
slouken@173
  1030
	SDL_UnlockAudio();
slouken@154
  1031
slouken@173
  1032
	return(retval);
slouken@154
  1033
}
slouken@154
  1034
icculus@237
  1035
/* Set the music's initial volume */
icculus@237
  1036
static void music_internal_initialize_volume(void)
icculus@237
  1037
{
icculus@237
  1038
	if ( music_playing->fading == MIX_FADING_IN ) {
icculus@237
  1039
		music_internal_volume(0);
icculus@237
  1040
	} else {
icculus@237
  1041
		music_internal_volume(music_volume);
icculus@237
  1042
	}
icculus@237
  1043
}
icculus@237
  1044
slouken@0
  1045
/* Set the music volume */
slouken@173
  1046
static void music_internal_volume(int volume)
slouken@173
  1047
{
slouken@173
  1048
	switch (music_playing->type) {
slouken@173
  1049
#ifdef CMD_MUSIC
slouken@173
  1050
	    case MUS_CMD:
slouken@173
  1051
		MusicCMD_SetVolume(volume);
slouken@173
  1052
		break;
slouken@173
  1053
#endif
slouken@173
  1054
#ifdef WAV_MUSIC
slouken@173
  1055
	    case MUS_WAV:
slouken@173
  1056
		WAVStream_SetVolume(volume);
slouken@173
  1057
		break;
slouken@173
  1058
#endif
slouken@249
  1059
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1060
	    case MUS_MOD:
slouken@173
  1061
		Player_SetVolume((SWORD)volume);
slouken@173
  1062
		break;
slouken@173
  1063
#endif
slouken@173
  1064
#ifdef MID_MUSIC
slouken@173
  1065
	    case MUS_MID:
slouken@173
  1066
#ifdef USE_NATIVE_MIDI
slouken@173
  1067
		if ( native_midi_ok ) {
slouken@173
  1068
			native_midi_setvolume(volume);
slouken@173
  1069
		} MIDI_ELSE
slouken@173
  1070
#endif
slouken@173
  1071
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1072
		if ( timidity_ok ) {
slouken@173
  1073
			Timidity_SetVolume(volume);
slouken@173
  1074
		}
slouken@173
  1075
#endif
slouken@173
  1076
		break;
slouken@173
  1077
#endif
slouken@173
  1078
#ifdef OGG_MUSIC
slouken@173
  1079
	    case MUS_OGG:
slouken@173
  1080
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
  1081
		break;
slouken@173
  1082
#endif
slouken@382
  1083
#ifdef FLAC_MUSIC
slouken@382
  1084
	    case MUS_FLAC:
slouken@382
  1085
		FLAC_setvolume(music_playing->data.flac, volume);
slouken@382
  1086
		break;
slouken@382
  1087
#endif
slouken@173
  1088
#ifdef MP3_MUSIC
slouken@173
  1089
	    case MUS_MP3:
slouken@314
  1090
		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
  1091
		break;
slouken@173
  1092
#endif
slouken@357
  1093
#ifdef MP3_MAD_MUSIC
slouken@357
  1094
	    case MUS_MP3_MAD:
slouken@357
  1095
		mad_setVolume(music_playing->data.mp3_mad, volume);
slouken@357
  1096
		break;
slouken@357
  1097
#endif
slouken@173
  1098
	    default:
slouken@173
  1099
		/* Unknown music type?? */
slouken@173
  1100
		break;
slouken@173
  1101
	}
slouken@173
  1102
}
slouken@0
  1103
int Mix_VolumeMusic(int volume)
slouken@0
  1104
{
slouken@0
  1105
	int prev_volume;
slouken@0
  1106
slouken@0
  1107
	prev_volume = music_volume;
megastep@4
  1108
	if ( volume < 0 ) {
slouken@163
  1109
		return prev_volume;
megastep@4
  1110
	}
megastep@4
  1111
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
  1112
		volume = SDL_MIX_MAXVOLUME;
megastep@4
  1113
	}
megastep@4
  1114
	music_volume = volume;
slouken@173
  1115
	SDL_LockAudio();
slouken@173
  1116
	if ( music_playing ) {
slouken@173
  1117
		music_internal_volume(music_volume);
slouken@0
  1118
	}
slouken@173
  1119
	SDL_UnlockAudio();
slouken@0
  1120
	return(prev_volume);
slouken@0
  1121
}
slouken@0
  1122
slouken@173
  1123
/* Halt playing of music */
slouken@173
  1124
static void music_internal_halt(void)
megastep@7
  1125
{
megastep@7
  1126
	switch (music_playing->type) {
megastep@7
  1127
#ifdef CMD_MUSIC
slouken@173
  1128
	    case MUS_CMD:
megastep@7
  1129
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
  1130
		break;
megastep@7
  1131
#endif
megastep@7
  1132
#ifdef WAV_MUSIC
slouken@173
  1133
	    case MUS_WAV:
megastep@7
  1134
		WAVStream_Stop();
megastep@7
  1135
		break;
megastep@7
  1136
#endif
slouken@249
  1137
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1138
	    case MUS_MOD:
megastep@7
  1139
		Player_Stop();
megastep@7
  1140
		break;
megastep@7
  1141
#endif
megastep@7
  1142
#ifdef MID_MUSIC
slouken@173
  1143
	    case MUS_MID:
slouken@98
  1144
#ifdef USE_NATIVE_MIDI
slouken@98
  1145
		if ( native_midi_ok ) {
slouken@98
  1146
			native_midi_stop();
slouken@106
  1147
		} MIDI_ELSE
slouken@98
  1148
#endif
slouken@106
  1149
#ifdef USE_TIMIDITY_MIDI
slouken@98
  1150
		if ( timidity_ok ) {
slouken@98
  1151
			Timidity_Stop();
slouken@98
  1152
		}
slouken@106
  1153
#endif
megastep@7
  1154
		break;
megastep@7
  1155
#endif
slouken@63
  1156
#ifdef OGG_MUSIC
slouken@173
  1157
	    case MUS_OGG:
slouken@63
  1158
		OGG_stop(music_playing->data.ogg);
slouken@63
  1159
		break;
slouken@63
  1160
#endif
slouken@382
  1161
#ifdef FLAC_MUSIC
slouken@382
  1162
	    case MUS_FLAC:
slouken@382
  1163
		FLAC_stop(music_playing->data.flac);
slouken@382
  1164
		break;
slouken@382
  1165
#endif
megastep@7
  1166
#ifdef MP3_MUSIC
slouken@173
  1167
	    case MUS_MP3:
slouken@314
  1168
		smpeg.SMPEG_stop(music_playing->data.mp3);
megastep@7
  1169
		break;
megastep@7
  1170
#endif
slouken@357
  1171
#ifdef MP3_MAD_MUSIC
slouken@357
  1172
	    case MUS_MP3_MAD:
slouken@357
  1173
		mad_stop(music_playing->data.mp3_mad);
slouken@357
  1174
		break;
slouken@357
  1175
#endif
slouken@173
  1176
	    default:
megastep@7
  1177
		/* Unknown music type?? */
megastep@7
  1178
		return;
megastep@7
  1179
	}
megastep@7
  1180
	music_playing->fading = MIX_NO_FADING;
slouken@17
  1181
	music_playing = NULL;
megastep@7
  1182
}
slouken@0
  1183
int Mix_HaltMusic(void)
slouken@0
  1184
{
slouken@173
  1185
	SDL_LockAudio();
slouken@173
  1186
	if ( music_playing ) {
slouken@173
  1187
		music_internal_halt();
slouken@0
  1188
	}
slouken@173
  1189
	SDL_UnlockAudio();
slouken@173
  1190
slouken@0
  1191
	return(0);
slouken@0
  1192
}
slouken@0
  1193
megastep@4
  1194
/* Progressively stop the music */
megastep@4
  1195
int Mix_FadeOutMusic(int ms)
megastep@4
  1196
{
slouken@173
  1197
	int retval = 0;
slouken@173
  1198
icculus@280
  1199
	if (ms <= 0) {  /* just halt immediately. */
icculus@280
  1200
		Mix_HaltMusic();
icculus@280
  1201
		return 1;
icculus@280
  1202
	}
icculus@280
  1203
slouken@173
  1204
	SDL_LockAudio();
icculus@280
  1205
	if ( music_playing) {
icculus@280
  1206
                int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
icculus@280
  1207
                if ( music_playing->fading == MIX_NO_FADING ) {
icculus@280
  1208
	        	music_playing->fade_step = 0;
icculus@280
  1209
                } else {
icculus@280
  1210
                        int step;
icculus@280
  1211
                        int old_fade_steps = music_playing->fade_steps;
icculus@280
  1212
                        if ( music_playing->fading == MIX_FADING_OUT ) {
icculus@280
  1213
                                step = music_playing->fade_step;
icculus@280
  1214
                        } else {
icculus@280
  1215
                                step = old_fade_steps
icculus@280
  1216
                                        - music_playing->fade_step + 1;
icculus@280
  1217
                        }
icculus@280
  1218
                        music_playing->fade_step = (step * fade_steps)
icculus@280
  1219
                                / old_fade_steps;
icculus@280
  1220
                }
slouken@173
  1221
		music_playing->fading = MIX_FADING_OUT;
icculus@280
  1222
		music_playing->fade_steps = fade_steps;
slouken@173
  1223
		retval = 1;
megastep@4
  1224
	}
slouken@173
  1225
	SDL_UnlockAudio();
slouken@173
  1226
slouken@173
  1227
	return(retval);
megastep@4
  1228
}
megastep@4
  1229
megastep@4
  1230
Mix_Fading Mix_FadingMusic(void)
megastep@4
  1231
{
slouken@173
  1232
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
  1233
slouken@173
  1234
	SDL_LockAudio();
slouken@173
  1235
	if ( music_playing ) {
slouken@173
  1236
		fading = music_playing->fading;
slouken@173
  1237
	}
slouken@173
  1238
	SDL_UnlockAudio();
slouken@173
  1239
slouken@173
  1240
	return(fading);
megastep@4
  1241
}
megastep@4
  1242
slouken@0
  1243
/* Pause/Resume the music stream */
slouken@0
  1244
void Mix_PauseMusic(void)
slouken@0
  1245
{
slouken@173
  1246
	music_active = 0;
slouken@0
  1247
}
megastep@4
  1248
slouken@0
  1249
void Mix_ResumeMusic(void)
slouken@0
  1250
{
slouken@173
  1251
	music_active = 1;
slouken@0
  1252
}
slouken@0
  1253
slouken@0
  1254
void Mix_RewindMusic(void)
slouken@0
  1255
{
slouken@173
  1256
	Mix_SetMusicPosition(0.0);
slouken@0
  1257
}
slouken@0
  1258
megastep@13
  1259
int Mix_PausedMusic(void)
megastep@13
  1260
{
slouken@17
  1261
	return (music_active == 0);
megastep@13
  1262
}
megastep@13
  1263
slouken@0
  1264
/* Check the status of the music */
slouken@173
  1265
static int music_internal_playing()
slouken@173
  1266
{
slouken@173
  1267
	int playing = 1;
slouken@173
  1268
	switch (music_playing->type) {
slouken@173
  1269
#ifdef CMD_MUSIC
slouken@173
  1270
	    case MUS_CMD:
slouken@173
  1271
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
  1272
			playing = 0;
slouken@173
  1273
		}
slouken@173
  1274
		break;
slouken@173
  1275
#endif
slouken@173
  1276
#ifdef WAV_MUSIC
slouken@173
  1277
	    case MUS_WAV:
slouken@173
  1278
		if ( ! WAVStream_Active() ) {
slouken@173
  1279
			playing = 0;
slouken@173
  1280
		}
slouken@173
  1281
		break;
slouken@173
  1282
#endif
slouken@249
  1283
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1284
	    case MUS_MOD:
slouken@173
  1285
		if ( ! Player_Active() ) {
slouken@173
  1286
			playing = 0;
slouken@173
  1287
		}
slouken@173
  1288
		break;
slouken@173
  1289
#endif
slouken@173
  1290
#ifdef MID_MUSIC
slouken@173
  1291
	    case MUS_MID:
slouken@173
  1292
#ifdef USE_NATIVE_MIDI
slouken@173
  1293
		if ( native_midi_ok ) {
slouken@173
  1294
			if ( ! native_midi_active() )
slouken@173
  1295
				playing = 0;
slouken@173
  1296
		} MIDI_ELSE
slouken@173
  1297
#endif
slouken@173
  1298
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1299
		if ( timidity_ok ) {
slouken@173
  1300
			if ( ! Timidity_Active() )
slouken@173
  1301
				playing = 0;
slouken@173
  1302
		}
slouken@173
  1303
#endif
slouken@173
  1304
		break;
slouken@173
  1305
#endif
slouken@173
  1306
#ifdef OGG_MUSIC
slouken@173
  1307
	    case MUS_OGG:
slouken@173
  1308
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1309
			playing = 0;
slouken@173
  1310
		}
slouken@173
  1311
		break;
slouken@173
  1312
#endif
slouken@382
  1313
#ifdef FLAC_MUSIC
slouken@382
  1314
	    case MUS_FLAC:
slouken@382
  1315
		if ( ! FLAC_playing(music_playing->data.flac) ) {
slouken@382
  1316
			playing = 0;
slouken@382
  1317
		}
slouken@382
  1318
		break;
slouken@382
  1319
#endif
slouken@173
  1320
#ifdef MP3_MUSIC
slouken@173
  1321
	    case MUS_MP3:
slouken@314
  1322
		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1323
			playing = 0;
slouken@173
  1324
		break;
slouken@173
  1325
#endif
slouken@357
  1326
#ifdef MP3_MAD_MUSIC
slouken@357
  1327
	    case MUS_MP3_MAD:
slouken@357
  1328
		if (!mad_isPlaying(music_playing->data.mp3_mad)) {
slouken@357
  1329
			playing = 0;
slouken@357
  1330
		}
slouken@357
  1331
		break;
slouken@357
  1332
#endif
slouken@173
  1333
	    default:
slouken@173
  1334
		playing = 0;
slouken@173
  1335
		break;
slouken@173
  1336
	}
slouken@173
  1337
	return(playing);
slouken@173
  1338
}
megastep@13
  1339
int Mix_PlayingMusic(void)
slouken@0
  1340
{
slouken@173
  1341
	int playing = 0;
slouken@173
  1342
slouken@173
  1343
	SDL_LockAudio();
slouken@173
  1344
	if ( music_playing ) {
slouken@173
  1345
		playing = music_internal_playing();
slouken@0
  1346
	}
slouken@173
  1347
	SDL_UnlockAudio();
slouken@173
  1348
slouken@173
  1349
	return(playing);
slouken@0
  1350
}
slouken@0
  1351
slouken@0
  1352
/* Set the external music playback command */
slouken@0
  1353
int Mix_SetMusicCMD(const char *command)
slouken@0
  1354
{
slouken@0
  1355
	Mix_HaltMusic();
slouken@0
  1356
	if ( music_cmd ) {
slouken@0
  1357
		free(music_cmd);
slouken@0
  1358
		music_cmd = NULL;
slouken@0
  1359
	}
slouken@0
  1360
	if ( command ) {
slouken@0
  1361
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
  1362
		if ( music_cmd == NULL ) {
slouken@0
  1363
			return(-1);
slouken@0
  1364
		}
slouken@0
  1365
		strcpy(music_cmd, command);
slouken@0
  1366
	}
slouken@0
  1367
	return(0);
slouken@0
  1368
}
slouken@0
  1369
slouken@249
  1370
#ifdef LIBMIKMOD_MUSIC
slouken@249
  1371
static int _pl_synchro_value;
slouken@249
  1372
void Player_SetSynchroValue(int i)
slouken@249
  1373
{
slouken@249
  1374
	fprintf(stderr,"SDL_mixer: Player_SetSynchroValue is not supported.\n");
slouken@249
  1375
	_pl_synchro_value = i;
slouken@249
  1376
}
slouken@249
  1377
slouken@249
  1378
int Player_GetSynchroValue(void)
slouken@249
  1379
{
slouken@249
  1380
	fprintf(stderr,"SDL_mixer: Player_GetSynchroValue is not supported.\n");
slouken@249
  1381
	return _pl_synchro_value;
slouken@249
  1382
}
slouken@249
  1383
#endif
slouken@249
  1384
slouken@154
  1385
int Mix_SetSynchroValue(int i)
slouken@154
  1386
{
slouken@154
  1387
	if ( music_playing && ! music_stopped ) {
slouken@154
  1388
		switch (music_playing->type) {
slouken@249
  1389
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1390
		    case MUS_MOD:
slouken@173
  1391
			if ( ! Player_Active() ) {
slouken@173
  1392
				return(-1);
slouken@173
  1393
			}
slouken@173
  1394
			Player_SetSynchroValue(i);
slouken@173
  1395
			return 0;
slouken@173
  1396
			break;
slouken@154
  1397
#endif
slouken@173
  1398
		    default:
slouken@173
  1399
			return(-1);
slouken@173
  1400
			break;
slouken@154
  1401
		}
slouken@154
  1402
		return(-1);
slouken@154
  1403
	}
slouken@154
  1404
	return(-1);
slouken@154
  1405
}
slouken@154
  1406
slouken@154
  1407
int Mix_GetSynchroValue(void)
slouken@154
  1408
{
slouken@154
  1409
	if ( music_playing && ! music_stopped ) {
slouken@154
  1410
		switch (music_playing->type) {
slouken@249
  1411
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1412
		    case MUS_MOD:
slouken@173
  1413
			if ( ! Player_Active() ) {
slouken@173
  1414
				return(-1);
slouken@173
  1415
			}
slouken@173
  1416
			return Player_GetSynchroValue();
slouken@173
  1417
			break;
slouken@154
  1418
#endif
slouken@173
  1419
		    default:
slouken@173
  1420
			return(-1);
slouken@173
  1421
			break;
slouken@154
  1422
		}
slouken@154
  1423
		return(-1);
slouken@154
  1424
	}
slouken@154
  1425
	return(-1);
slouken@154
  1426
}
slouken@154
  1427
slouken@154
  1428
slouken@0
  1429
/* Uninitialize the music players */
slouken@0
  1430
void close_music(void)
slouken@0
  1431
{
slouken@0
  1432
	Mix_HaltMusic();
slouken@0
  1433
#ifdef CMD_MUSIC
slouken@0
  1434
	Mix_SetMusicCMD(NULL);
slouken@0
  1435
#endif
slouken@249
  1436
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
  1437
	MikMod_Exit();
slouken@249
  1438
# ifndef LIBMIKMOD_MUSIC
slouken@101
  1439
	MikMod_UnregisterAllLoaders();
slouken@101
  1440
	MikMod_UnregisterAllDrivers();
slouken@249
  1441
# endif
slouken@0
  1442
#endif
patmandin@264
  1443
#ifdef MID_MUSIC
patmandin@264
  1444
# ifdef USE_TIMIDITY_MIDI
patmandin@264
  1445
	Timidity_Close();
patmandin@264
  1446
# endif
patmandin@264
  1447
#endif
icculus@390
  1448
icculus@390
  1449
	/* rcg06042009 report available decoders at runtime. */
icculus@390
  1450
	free(music_decoders);
icculus@390
  1451
	music_decoders = NULL;
icculus@390
  1452
	num_decoders = 0;
slouken@0
  1453
}
slouken@0
  1454
slouken@249
  1455
# ifdef LIBMIKMOD_MUSIC
slouken@249
  1456
typedef struct
slouken@249
  1457
{
slouken@249
  1458
	MREADER mr;
slouken@249
  1459
	int offset;
slouken@249
  1460
	int eof;
slouken@249
  1461
	SDL_RWops *rw;
slouken@249
  1462
} LMM_MREADER;
slouken@249
  1463
BOOL LMM_Seek(struct MREADER *mr,long to,int dir)
slouken@249
  1464
{
slouken@249
  1465
	int at;
slouken@249
  1466
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1467
	if(dir==SEEK_SET)
slouken@249
  1468
		to+=lmmmr->offset;
slouken@249
  1469
	at=SDL_RWseek(lmmmr->rw, to, dir);
slouken@249
  1470
	return at<lmmmr->offset;
slouken@249
  1471
}
slouken@249
  1472
long LMM_Tell(struct MREADER *mr)
slouken@249
  1473
{
slouken@249
  1474
	int at;
slouken@249
  1475
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1476
	at=SDL_RWtell(lmmmr->rw)-lmmmr->offset;
slouken@249
  1477
	return at;
slouken@249
  1478
}
slouken@249
  1479
BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
slouken@249
  1480
{
slouken@249
  1481
	int got;
slouken@249
  1482
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1483
	got=SDL_RWread(lmmmr->rw, buf, sz, 1);
slouken@249
  1484
	return got;
slouken@249
  1485
}
slouken@249
  1486
int LMM_Get(struct MREADER *mr)
slouken@249
  1487
{
slouken@249
  1488
	unsigned char c;
slouken@249
  1489
	int i=EOF;
slouken@249
  1490
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1491
	if(SDL_RWread(lmmmr->rw,&c,1,1))
slouken@249
  1492
		i=c;
slouken@249
  1493
	return i;
slouken@249
  1494
}
slouken@249
  1495
BOOL LMM_Eof(struct MREADER *mr)
slouken@249
  1496
{
slouken@249
  1497
	int offset;
slouken@249
  1498
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1499
	offset=LMM_Tell(mr);
slouken@249
  1500
	return offset>=lmmmr->eof;
slouken@249
  1501
}
slouken@249
  1502
MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan)
slouken@249
  1503
{
slouken@249
  1504
	LMM_MREADER lmmmr={
slouken@249
  1505
		LMM_Seek,
slouken@249
  1506
		LMM_Tell,
slouken@249
  1507
		LMM_Read,
slouken@249
  1508
		LMM_Get,
slouken@249
  1509
		LMM_Eof,
slouken@249
  1510
		0,
slouken@249
  1511
		0,
slouken@249
  1512
		rw
slouken@249
  1513
	};
slouken@249
  1514
	MODULE *m;
slouken@249
  1515
	lmmmr.offset=SDL_RWtell(rw);
slouken@249
  1516
	SDL_RWseek(rw,0,SEEK_END);
slouken@249
  1517
	lmmmr.eof=SDL_RWtell(rw);
slouken@249
  1518
	SDL_RWseek(rw,lmmmr.offset,SEEK_SET);
slouken@249
  1519
	m=Player_LoadGeneric((MREADER*)&lmmmr,maxchan,0);
slouken@249
  1520
	return m;
slouken@249
  1521
}
slouken@249
  1522
# endif
slouken@249
  1523
slouken@381
  1524
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
slouken@381
  1525
{
slouken@246
  1526
	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
slouken@246
  1527
	/*		    or there are too many formats supported by MikMod or MikMod does */
slouken@246
  1528
	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
slouken@246
  1529
	/*		    the check can be uncommented */
slouken@381
  1530
	Uint8 moremagic[9];
slouken@226
  1531
	Mix_Music *music;
slouken@246
  1532
	int start;
slouken@226
  1533
patmandin@336
  1534
	if (!rw) {
patmandin@336
  1535
		Mix_SetError("RWops pointer is NULL");
patmandin@336
  1536
		return NULL;
patmandin@336
  1537
	}
patmandin@336
  1538
slouken@226
  1539
	/* Figure out what kind of file this is */
slouken@246
  1540
	start = SDL_RWtell(rw);
slouken@381
  1541
	if ( SDL_RWread(rw,magic,1,4) != 4 ||
slouken@381
  1542
	     SDL_RWread(rw,moremagic,1,8) != 8 ) {
slouken@226
  1543
		Mix_SetError("Couldn't read from RWops");
slouken@226
  1544
		return NULL;
slouken@226
  1545
	}
slouken@246
  1546
	SDL_RWseek(rw, start, SEEK_SET);
slouken@246
  1547
	magic[4]='\0';
slouken@381
  1548
	moremagic[8] = '\0';
slouken@226
  1549
slouken@226
  1550
	/* Allocate memory for the music structure */
slouken@226
  1551
	music=(Mix_Music *)malloc(sizeof(Mix_Music));
slouken@226
  1552
	if (music==NULL ) {
slouken@226
  1553
		Mix_SetError("Out of memory");
slouken@226
  1554
		return(NULL);
slouken@226
  1555
	}
slouken@226
  1556
	music->error = 0;
slouken@226
  1557
slouken@381
  1558
#ifdef WAV_MUSIC
slouken@381
  1559
	/* WAVE files have the magic four bytes "RIFF"
slouken@381
  1560
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@381
  1561
	 */
slouken@381
  1562
	if ( ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@381
  1563
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@381
  1564
		music->type = MUS_WAV;
slouken@381
  1565
		music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic);
slouken@381
  1566
		if ( music->data.wave == NULL ) {
slouken@381
  1567
			music->error = 1;
slouken@381
  1568
		}
slouken@381
  1569
slouken@381
  1570
	} else
slouken@381
  1571
#endif
slouken@246
  1572
#ifdef OGG_MUSIC
slouken@246
  1573
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@246
  1574
	if ( strcmp((char *)magic, "OggS") == 0 ) {
slouken@246
  1575
		music->type = MUS_OGG;
slouken@246
  1576
		music->data.ogg = OGG_new_RW(rw);
slouken@246
  1577
		if ( music->data.ogg == NULL ) {
slouken@246
  1578
			music->error = 1;
slouken@246
  1579
		}
slouken@246
  1580
	} else
slouken@246
  1581
#endif
slouken@382
  1582
#ifdef FLAC_MUSIC
slouken@382
  1583
	/* FLAC files have the magic four bytes "fLaC" */
slouken@382
  1584
	if ( strcmp((char *)magic, "fLaC") == 0 ) {
slouken@382
  1585
		music->type = MUS_FLAC;
slouken@382
  1586
		music->data.flac = FLAC_new_RW(rw);
slouken@382
  1587
		if ( music->data.flac == NULL ) {
slouken@382
  1588
			music->error = 1;
slouken@382
  1589
		}
slouken@382
  1590
	} else
slouken@382
  1591
#endif
slouken@256
  1592
#ifdef MP3_MUSIC
slouken@256
  1593
	if ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0 ) {
slouken@314
  1594
		if ( Mix_InitMP3() == 0 ) {
slouken@314
  1595
			SMPEG_Info info;
slouken@314
  1596
			music->type = MUS_MP3;
slouken@314
  1597
			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
slouken@314
  1598
			if ( !info.has_audio ) {
slouken@314
  1599
				Mix_SetError("MPEG file does not have any audio stream.");
slouken@314
  1600
				music->error = 1;
slouken@314
  1601
			} else {
slouken@314
  1602
				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@314
  1603
			}
slouken@314
  1604
		} else {
slouken@256
  1605
			music->error = 1;
slouken@256
  1606
		}
slouken@256
  1607
	} else
slouken@256
  1608
#endif
slouken@357
  1609
#ifdef MP3_MAD_MUSIC
slouken@357
  1610
	if ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0 ) {
slouken@357
  1611
		music->type = MUS_MP3_MAD;
slouken@357
  1612
		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer);
slouken@357
  1613
		if (music->data.mp3_mad == 0) {
slouken@357
  1614
			Mix_SetError("Could not initialize MPEG stream.");
slouken@357
  1615
			music->error = 1;
slouken@357
  1616
		}
slouken@357
  1617
	} else
slouken@357
  1618
#endif
patmandin@265
  1619
#ifdef MID_MUSIC
patmandin@265
  1620
	/* MIDI files have the magic four bytes "MThd" */
patmandin@265
  1621
	if ( strcmp((char *)magic, "MThd") == 0 ) {
patmandin@265
  1622
		music->type = MUS_MID;
patmandin@269
  1623
#ifdef USE_NATIVE_MIDI
patmandin@269
  1624
		if ( native_midi_ok ) {
patmandin@269
  1625
			music->data.nativemidi = native_midi_loadsong_RW(rw);
patmandin@335
  1626
	  		if ( music->data.nativemidi == NULL ) {
patmandin@335
  1627
		  		Mix_SetError("%s", native_midi_error());
patmandin@335
  1628
			  	music->error = 1;
patmandin@269
  1629
			}
patmandin@269
  1630
		} MIDI_ELSE
patmandin@269
  1631
#endif
patmandin@265
  1632
#ifdef USE_TIMIDITY_MIDI
patmandin@269
  1633
		if ( timidity_ok ) {
patmandin@269
  1634
			music->data.midi = Timidity_LoadSong_RW(rw);
patmandin@335
  1635
			if ( music->data.midi == NULL ) {
patmandin@335
  1636
				Mix_SetError("%s", Timidity_Error());
patmandin@335
  1637
				music->error = 1;
patmandin@269
  1638
			}
patmandin@335
  1639
		} else {
patmandin@335
  1640
			Mix_SetError("%s", Timidity_Error());
patmandin@335
  1641
			music->error = 1;
patmandin@265
  1642
		}
patmandin@265
  1643
#endif
patmandin@265
  1644
	} else
patmandin@265
  1645
#endif
slouken@249
  1646
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@226
  1647
	if (1) {
slouken@226
  1648
		music->type=MUS_MOD;
slouken@226
  1649
		music->data.module=MikMod_LoadSongRW(rw,64);
slouken@226
  1650
		if (music->data.module==NULL) {
slouken@226
  1651
			Mix_SetError("%s",MikMod_strerror(MikMod_errno));
slouken@226
  1652
			music->error=1;
icculus@279
  1653
		} else {
icculus@279
  1654
			/* Stop implicit looping, fade out and other flags. */
icculus@279
  1655
			music->data.module->extspd  = 1;
icculus@279
  1656
			music->data.module->panflag = 1;
icculus@279
  1657
			music->data.module->wrap    = 0;
icculus@279
  1658
			music->data.module->loop    = 0;
icculus@279
  1659
#if 0 /* Don't set fade out by default - unfortunately there's no real way
icculus@279
  1660
         to query the status of the song or set trigger actions.  Hum. */
icculus@279
  1661
			music->data.module->fadeout = 1;
icculus@279
  1662
#endif
slouken@226
  1663
		}
slouken@226
  1664
	} else
slouken@226
  1665
#endif
slouken@226
  1666
	{
slouken@226
  1667
		Mix_SetError("Unrecognized music format");
slouken@226
  1668
		music->error=1;
slouken@226
  1669
	}
slouken@226
  1670
	if (music->error) {
slouken@226
  1671
		free(music);
slouken@226
  1672
		music=NULL;
slouken@226
  1673
	}
slouken@226
  1674
	return(music);
slouken@226
  1675
}