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