music.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 19 Nov 2005 18:37:45 +0000
changeset 280 1e6fd744d20c
parent 279 33311d0a5d0d
child 281 33730d0864d8
permissions -rw-r--r--
To: sdl@libsdl.org
From: Rainer Deyke <rainerd@eldwood.com>
Date: Mon, 12 Sep 2005 19:29:01 -0600
Subject: SDL_mixer bug

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