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