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