music.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 Feb 2008 11:46:22 +0000
changeset 381 2064088ea781
parent 357 d6d0cfdbea65
child 382 50501e45c57b
permissions -rw-r--r--
Date: Sat, 16 Feb 2008 20:59:24 +0100
From: Tilman Sauerbeck
Subject: [PATCH] Allow SDL_mixer to open wave streams via SDL_rwops

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