music.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 May 2002 20:47:22 +0000
changeset 177 0108822670c8
parent 176 fa3cf3b26af4
child 179 33596bb9f061
permissions -rw-r--r--
Sam Lantinga - Sun May 19 13:46:29 PDT 2002
* Added a function to query the music format: Mix_GetMusicType()
slouken@0
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@138
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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>
slouken@24
    28
#include "SDL_endian.h"
slouken@24
    29
#include "SDL_audio.h"
slouken@29
    30
#include "SDL_timer.h"
slouken@0
    31
slouken@34
    32
#include "SDL_mixer.h"
slouken@0
    33
slouken@0
    34
/* The music command hack is UNIX specific */
slouken@0
    35
#ifndef unix
slouken@0
    36
#undef CMD_MUSIC
slouken@0
    37
#endif
slouken@0
    38
slouken@0
    39
#ifdef CMD_MUSIC
slouken@0
    40
#include "music_cmd.h"
slouken@0
    41
#endif
slouken@0
    42
#ifdef WAV_MUSIC
slouken@0
    43
#include "wavestream.h"
slouken@0
    44
#endif
slouken@0
    45
#ifdef MOD_MUSIC
slouken@29
    46
#  include "mikmod.h"
slouken@29
    47
#  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
slouken@29
    48
#    define UNIMOD			MODULE
slouken@29
    49
#    define MikMod_Init()		MikMod_Init(NULL)
slouken@29
    50
#    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
slouken@29
    51
#    define MikMod_FreeSong		Player_Free
slouken@29
    52
     extern int MikMod_errno;
slouken@29
    53
#  else                                        /* old MikMod 3.0.3 */
slouken@29
    54
#    define MikMod_strerror(x)		_mm_errmsg[x])
slouken@29
    55
#    define MikMod_errno		_mm_errno
slouken@29
    56
#  endif
slouken@0
    57
#endif
slouken@0
    58
#ifdef MID_MUSIC
slouken@106
    59
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    60
#    include "timidity.h"
slouken@106
    61
#  endif
slouken@106
    62
#  ifdef USE_NATIVE_MIDI
slouken@106
    63
#    include "native_midi.h"
slouken@106
    64
#  endif
slouken@106
    65
#  if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
slouken@106
    66
#    define MIDI_ELSE	else
slouken@106
    67
#  else
slouken@106
    68
#    define MIDI_ELSE
slouken@106
    69
#  endif
slouken@0
    70
#endif
slouken@63
    71
#ifdef OGG_MUSIC
slouken@63
    72
#include "music_ogg.h"
slouken@63
    73
#endif
slouken@0
    74
#ifdef MP3_MUSIC
slouken@83
    75
#include "smpeg.h"
slouken@0
    76
slouken@0
    77
static SDL_AudioSpec used_mixer;
slouken@0
    78
#endif
slouken@0
    79
slouken@42
    80
int volatile music_active = 1;
slouken@42
    81
static int volatile music_stopped = 0;
slouken@0
    82
static int music_loops = 0;
slouken@0
    83
static char *music_cmd = NULL;
slouken@42
    84
static Mix_Music * volatile music_playing = NULL;
slouken@17
    85
static int music_volume = MIX_MAX_VOLUME;
slouken@0
    86
static int music_swap8;
slouken@0
    87
static int music_swap16;
megastep@4
    88
slouken@0
    89
struct _Mix_Music {
slouken@177
    90
	Mix_MusicType type;
slouken@0
    91
	union {
slouken@0
    92
#ifdef CMD_MUSIC
slouken@0
    93
		MusicCMD *cmd;
slouken@0
    94
#endif
slouken@0
    95
#ifdef WAV_MUSIC
slouken@0
    96
		WAVStream *wave;
slouken@0
    97
#endif
slouken@0
    98
#ifdef MOD_MUSIC
slouken@0
    99
		UNIMOD *module;
slouken@0
   100
#endif
slouken@0
   101
#ifdef MID_MUSIC
slouken@106
   102
#ifdef USE_TIMIDITY_MIDI
slouken@0
   103
		MidiSong *midi;
slouken@106
   104
#endif
slouken@98
   105
#ifdef USE_NATIVE_MIDI
slouken@98
   106
		NativeMidiSong *nativemidi;
slouken@98
   107
#endif
slouken@0
   108
#endif
slouken@63
   109
#ifdef OGG_MUSIC
slouken@63
   110
		OGG_music *ogg;
slouken@63
   111
#endif
slouken@0
   112
#ifdef MP3_MUSIC
slouken@0
   113
		SMPEG *mp3;
slouken@0
   114
#endif
slouken@0
   115
	} data;
megastep@4
   116
	Mix_Fading fading;
slouken@17
   117
	int fade_step;
slouken@17
   118
	int fade_steps;
slouken@0
   119
	int error;
slouken@0
   120
};
slouken@29
   121
#ifdef MID_MUSIC
slouken@106
   122
#ifdef USE_TIMIDITY_MIDI
slouken@0
   123
static int timidity_ok;
slouken@142
   124
static int samplesize;
slouken@106
   125
#endif
slouken@98
   126
#ifdef USE_NATIVE_MIDI
slouken@98
   127
static int native_midi_ok;
slouken@98
   128
#endif
slouken@29
   129
#endif
slouken@0
   130
slouken@17
   131
/* Used to calculate fading steps */
slouken@17
   132
static int ms_per_step;
slouken@17
   133
megastep@10
   134
/* Local low-level functions prototypes */
slouken@173
   135
static void music_internal_volume(int volume);
slouken@173
   136
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   137
static int  music_internal_position(double position);
slouken@173
   138
static int  music_internal_playing();
slouken@173
   139
static void music_internal_halt(void);
megastep@7
   140
slouken@29
   141
slouken@29
   142
/* Support for hooking when the music has finished */
slouken@29
   143
static void (*music_finished_hook)(void) = NULL;
slouken@29
   144
slouken@29
   145
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   146
{
slouken@29
   147
	SDL_LockAudio();
slouken@29
   148
	music_finished_hook = music_finished;
slouken@29
   149
	SDL_UnlockAudio();
slouken@29
   150
}
slouken@29
   151
slouken@29
   152
slouken@0
   153
/* Mixing function */
slouken@0
   154
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   155
{
slouken@173
   156
	if ( music_playing && music_active ) {
megastep@13
   157
		/* Handle fading */
slouken@17
   158
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   159
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@173
   160
				int volume;
slouken@26
   161
				int fade_step = music_playing->fade_step;
slouken@26
   162
				int fade_steps = music_playing->fade_steps;
slouken@17
   163
megastep@4
   164
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   165
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@17
   166
				} else { /* Fading in */
slouken@173
   167
					volume = (music_volume * fade_step) / fade_steps;
slouken@17
   168
				}
slouken@173
   169
				music_internal_volume(volume);
slouken@17
   170
			} else {
slouken@17
   171
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   172
					music_internal_halt();
slouken@173
   173
					if ( music_finished_hook ) {
slouken@173
   174
						music_finished_hook();
slouken@173
   175
					}
megastep@4
   176
					return;
megastep@4
   177
				}
megastep@4
   178
				music_playing->fading = MIX_NO_FADING;
megastep@4
   179
			}
megastep@4
   180
		}
megastep@13
   181
		/* Restart music if it has to loop */
slouken@173
   182
		if ( !music_internal_playing() ) {
slouken@173
   183
			/* Restart music if it has to loop at a high level */
slouken@29
   184
			if ( music_loops && --music_loops ) {
slouken@173
   185
				music_internal_play(music_playing, 0.0);
slouken@173
   186
			} else {
slouken@173
   187
				music_internal_halt();
slouken@173
   188
				if ( music_finished_hook ) {
slouken@173
   189
					music_finished_hook();
megastep@11
   190
				}
slouken@173
   191
				return;
megastep@11
   192
			}
megastep@16
   193
		}
slouken@0
   194
		switch (music_playing->type) {
slouken@0
   195
#ifdef CMD_MUSIC
slouken@0
   196
			case MUS_CMD:
slouken@0
   197
				/* The playing is done externally */
slouken@0
   198
				break;
slouken@0
   199
#endif
slouken@0
   200
#ifdef WAV_MUSIC
slouken@0
   201
			case MUS_WAV:
slouken@64
   202
				WAVStream_PlaySome(stream, len);
slouken@0
   203
				break;
slouken@0
   204
#endif
slouken@0
   205
#ifdef MOD_MUSIC
slouken@0
   206
			case MUS_MOD:
slouken@0
   207
				VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   208
				if ( music_swap8 ) {
slouken@0
   209
					Uint8 *dst;
slouken@42
   210
					int i;
slouken@0
   211
slouken@0
   212
					dst = stream;
slouken@0
   213
					for ( i=len; i; --i ) {
slouken@0
   214
						*dst++ ^= 0x80;
slouken@0
   215
					}
slouken@0
   216
				} else
slouken@0
   217
				if ( music_swap16 ) {
slouken@0
   218
					Uint8 *dst, tmp;
slouken@42
   219
					int i;
slouken@0
   220
slouken@0
   221
					dst = stream;
slouken@0
   222
					for ( i=(len/2); i; --i ) {
slouken@0
   223
						tmp = dst[0];
slouken@0
   224
						dst[0] = dst[1];
slouken@0
   225
						dst[1] = tmp;
slouken@0
   226
						dst += 2;
slouken@0
   227
					}
slouken@0
   228
				}
slouken@0
   229
				break;
slouken@0
   230
#endif
slouken@0
   231
#ifdef MID_MUSIC
slouken@106
   232
#ifdef USE_TIMIDITY_MIDI
slouken@0
   233
			case MUS_MID:
slouken@98
   234
				if ( timidity_ok ) {
slouken@98
   235
					int samples = len / samplesize;
slouken@98
   236
  					Timidity_PlaySome(stream, samples);
slouken@98
   237
				}
slouken@0
   238
				break;
slouken@0
   239
#endif
slouken@106
   240
#endif
slouken@63
   241
#ifdef OGG_MUSIC
slouken@63
   242
			case MUS_OGG:
slouken@63
   243
				OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   244
				break;
slouken@63
   245
#endif
slouken@0
   246
#ifdef MP3_MUSIC
slouken@26
   247
			case MUS_MP3:
slouken@26
   248
				SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   249
				break;
slouken@0
   250
#endif
slouken@0
   251
			default:
slouken@0
   252
				/* Unknown music type?? */
slouken@0
   253
				break;
slouken@0
   254
		}
slouken@0
   255
	}
slouken@0
   256
}
slouken@0
   257
slouken@0
   258
/* Initialize the music players with a certain desired audio format */
slouken@0
   259
int open_music(SDL_AudioSpec *mixer)
slouken@0
   260
{
slouken@0
   261
	int music_error;
slouken@0
   262
slouken@0
   263
	music_error = 0;
slouken@0
   264
#ifdef WAV_MUSIC
slouken@0
   265
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   266
		++music_error;
slouken@0
   267
	}
slouken@0
   268
#endif
slouken@0
   269
#ifdef MOD_MUSIC
slouken@0
   270
	/* Set the MikMod music format */
slouken@0
   271
	music_swap8 = 0;
slouken@0
   272
	music_swap16 = 0;
slouken@0
   273
	switch (mixer->format) {
slouken@0
   274
slouken@0
   275
		case AUDIO_U8:
slouken@0
   276
		case AUDIO_S8: {
slouken@0
   277
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   278
				music_swap8 = 1;
slouken@0
   279
			}
slouken@0
   280
			md_mode = 0;
slouken@0
   281
		}
slouken@0
   282
		break;
slouken@0
   283
slouken@0
   284
		case AUDIO_S16LSB:
slouken@0
   285
		case AUDIO_S16MSB: {
slouken@0
   286
			/* See if we need to correct MikMod mixing */
slouken@0
   287
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   288
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   289
#else
slouken@0
   290
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   291
#endif
slouken@0
   292
				music_swap16 = 1;
slouken@0
   293
			}
slouken@0
   294
			md_mode = DMODE_16BITS;
slouken@0
   295
		}
slouken@0
   296
		break;
slouken@0
   297
slouken@0
   298
		default: {
slouken@26
   299
			Mix_SetError("Unknown hardware audio format");
slouken@0
   300
			++music_error;
slouken@0
   301
		}
slouken@0
   302
	}
slouken@0
   303
	if ( mixer->channels > 1 ) {
slouken@0
   304
		if ( mixer->channels > 2 ) {
slouken@26
   305
			Mix_SetError("Hardware uses more channels than mixer");
slouken@0
   306
			++music_error;
slouken@0
   307
		}
slouken@0
   308
		md_mode |= DMODE_STEREO;
slouken@0
   309
	}
slouken@26
   310
	md_mixfreq	 = mixer->freq;
slouken@26
   311
	md_device	  = 0;
slouken@26
   312
	md_volume	  = 96;
slouken@0
   313
	md_musicvolume = 128;
slouken@0
   314
	md_sndfxvolume = 128;
slouken@26
   315
	md_pansep	  = 128;
slouken@26
   316
	md_reverb	  = 0;
slouken@0
   317
	MikMod_RegisterAllLoaders();
slouken@0
   318
	MikMod_RegisterAllDrivers();
slouken@0
   319
	if ( MikMod_Init() ) {
slouken@29
   320
		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   321
		++music_error;
slouken@0
   322
	}
slouken@0
   323
#endif
slouken@0
   324
#ifdef MID_MUSIC
slouken@106
   325
#ifdef USE_TIMIDITY_MIDI
slouken@142
   326
	samplesize = mixer->size / mixer->samples;
slouken@100
   327
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@102
   328
	                    mixer->channels, mixer->samples) == 0 ) {
slouken@100
   329
		timidity_ok = 1;
slouken@0
   330
	} else {
slouken@0
   331
		timidity_ok = 0;
slouken@0
   332
	}
slouken@106
   333
#endif
slouken@100
   334
#ifdef USE_NATIVE_MIDI
slouken@106
   335
#ifdef USE_TIMIDITY_MIDI
slouken@100
   336
	native_midi_ok = !timidity_ok;
slouken@106
   337
	if ( native_midi_ok )
slouken@106
   338
#endif
slouken@101
   339
		native_midi_ok = native_midi_detect();
slouken@100
   340
#endif
slouken@0
   341
#endif
slouken@63
   342
#ifdef OGG_MUSIC
slouken@63
   343
	if ( OGG_init(mixer) < 0 ) {
slouken@63
   344
		++music_error;
slouken@63
   345
	}
slouken@63
   346
#endif
slouken@0
   347
#ifdef MP3_MUSIC
slouken@0
   348
	/* Keep a copy of the mixer */
slouken@0
   349
	used_mixer = *mixer;
slouken@0
   350
#endif
slouken@42
   351
	music_playing = NULL;
megastep@7
   352
	music_stopped = 0;
slouken@0
   353
	if ( music_error ) {
slouken@0
   354
		return(-1);
slouken@0
   355
	}
slouken@0
   356
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   357
slouken@26
   358
	/* Calculate the number of ms for each callback */
slouken@44
   359
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   360
slouken@0
   361
	return(0);
slouken@0
   362
}
slouken@0
   363
slouken@125
   364
/* Portable case-insensitive string compare function */
slouken@125
   365
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   366
{
slouken@125
   367
	while ( *str1 && *str2 ) {
slouken@125
   368
		if ( toupper((unsigned char)*str1) !=
slouken@125
   369
		     toupper((unsigned char)*str2) )
slouken@125
   370
			break;
slouken@125
   371
		++str1;
slouken@125
   372
		++str2;
slouken@125
   373
	}
slouken@125
   374
	return (!*str1 && !*str2);
slouken@125
   375
}
slouken@125
   376
slouken@0
   377
/* Load a music file */
slouken@0
   378
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   379
{
slouken@0
   380
	FILE *fp;
slouken@125
   381
	char *ext;
slouken@52
   382
	Uint8 magic[5];
slouken@0
   383
	Mix_Music *music;
slouken@0
   384
slouken@0
   385
	/* Figure out what kind of file this is */
slouken@0
   386
	fp = fopen(file, "rb");
slouken@0
   387
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   388
		if ( fp != NULL ) {
slouken@0
   389
			fclose(fp);
slouken@0
   390
		}
slouken@26
   391
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   392
		return(NULL);
slouken@0
   393
	}
slouken@0
   394
	magic[4] = '\0';
slouken@0
   395
	fclose(fp);
slouken@0
   396
slouken@125
   397
	/* Figure out the file extension, so we can determine the type */
slouken@125
   398
	ext = strrchr(file, '.');
slouken@125
   399
	if ( ext ) ++ext; /* skip the dot in the extension */
slouken@125
   400
slouken@0
   401
	/* Allocate memory for the music structure */
slouken@0
   402
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   403
	if ( music == NULL ) {
slouken@26
   404
		Mix_SetError("Out of memory");
slouken@0
   405
		return(NULL);
slouken@0
   406
	}
slouken@0
   407
	music->error = 0;
slouken@0
   408
slouken@0
   409
#ifdef CMD_MUSIC
slouken@0
   410
	if ( music_cmd ) {
slouken@0
   411
		music->type = MUS_CMD;
slouken@0
   412
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   413
		if ( music->data.cmd == NULL ) {
slouken@0
   414
			music->error = 1;
slouken@0
   415
		}
slouken@0
   416
	} else
slouken@0
   417
#endif
slouken@0
   418
#ifdef WAV_MUSIC
slouken@0
   419
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   420
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   421
	 */
slouken@135
   422
	if ( (ext && MIX_string_equals(ext, "WAV")) ||
slouken@125
   423
	     (strcmp((char *)magic, "RIFF") == 0) ||
slouken@57
   424
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   425
		music->type = MUS_WAV;
slouken@57
   426
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   427
		if ( music->data.wave == NULL ) {
slouken@135
   428
		  	Mix_SetError("Unable to load WAV file");
slouken@0
   429
			music->error = 1;
slouken@0
   430
		}
slouken@0
   431
	} else
slouken@0
   432
#endif
slouken@0
   433
#ifdef MID_MUSIC
slouken@0
   434
	/* MIDI files have the magic four bytes "MThd" */
slouken@135
   435
	if ( (ext && MIX_string_equals(ext, "MID")) ||
slouken@135
   436
	     (ext && MIX_string_equals(ext, "MIDI")) ||
slouken@133
   437
	     strcmp((char *)magic, "MThd") == 0 ) {
slouken@0
   438
		music->type = MUS_MID;
slouken@98
   439
#ifdef USE_NATIVE_MIDI
slouken@98
   440
  		if ( native_midi_ok ) {
slouken@98
   441
  			music->data.nativemidi = native_midi_loadsong((char *)file);
slouken@98
   442
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   443
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   444
			  	music->error = 1;
slouken@98
   445
			}
slouken@106
   446
	  	} MIDI_ELSE
slouken@98
   447
#endif
slouken@106
   448
#ifdef USE_TIMIDITY_MIDI
slouken@0
   449
		if ( timidity_ok ) {
slouken@0
   450
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   451
			if ( music->data.midi == NULL ) {
slouken@26
   452
				Mix_SetError("%s", Timidity_Error());
slouken@0
   453
				music->error = 1;
slouken@0
   454
			}
slouken@98
   455
		} else {
slouken@26
   456
			Mix_SetError("%s", Timidity_Error());
slouken@0
   457
			music->error = 1;
slouken@0
   458
		}
slouken@106
   459
#endif
slouken@0
   460
	} else
slouken@0
   461
#endif
slouken@63
   462
#ifdef OGG_MUSIC
slouken@63
   463
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@135
   464
	if ( (ext && MIX_string_equals(ext, "OGG")) ||
slouken@133
   465
	     strcmp((char *)magic, "OggS") == 0 ) {
slouken@63
   466
		music->type = MUS_OGG;
slouken@63
   467
		music->data.ogg = OGG_new(file);
slouken@63
   468
		if ( music->data.ogg == NULL ) {
slouken@63
   469
			music->error = 1;
slouken@63
   470
		}
slouken@63
   471
	} else
slouken@63
   472
#endif
slouken@0
   473
#ifdef MP3_MUSIC
slouken@135
   474
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
slouken@135
   475
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@125
   476
	     magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   477
		SMPEG_Info info;
slouken@0
   478
		music->type = MUS_MP3;
slouken@0
   479
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   480
		if(!info.has_audio){
slouken@26
   481
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@0
   482
			music->error = 1;
slouken@0
   483
		}else{
slouken@0
   484
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   485
		}
slouken@0
   486
	} else
slouken@0
   487
#endif
slouken@0
   488
#ifdef MOD_MUSIC
slouken@0
   489
	if ( 1 ) {
slouken@0
   490
		music->type = MUS_MOD;
slouken@0
   491
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   492
		if ( music->data.module == NULL ) {
slouken@29
   493
			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   494
			music->error = 1;
slouken@126
   495
		} else {
slouken@126
   496
			/* Stop implicit looping, fade out and other flags. */
slouken@126
   497
			music->data.module->extspd  = 1;
slouken@126
   498
			music->data.module->panflag = 1;
slouken@126
   499
			music->data.module->wrap    = 0;
slouken@126
   500
			music->data.module->loop    = 0;
slouken@129
   501
#if 0 /* Don't set fade out by default - unfortunately there's no real way
slouken@129
   502
         to query the status of the song or set trigger actions.  Hum. */
slouken@126
   503
			music->data.module->fadeout = 1;
slouken@129
   504
#endif
slouken@0
   505
		}
slouken@0
   506
	} else
slouken@0
   507
#endif
slouken@0
   508
	{
slouken@26
   509
		Mix_SetError("Unrecognized music format");
slouken@0
   510
		music->error = 1;
slouken@0
   511
	}
slouken@0
   512
	if ( music->error ) {
slouken@0
   513
		free(music);
slouken@0
   514
		music = NULL;
slouken@0
   515
	}
slouken@0
   516
	return(music);
slouken@0
   517
}
slouken@0
   518
slouken@0
   519
/* Free a music chunk previously loaded */
slouken@0
   520
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   521
{
slouken@0
   522
	if ( music ) {
slouken@173
   523
		/* Stop the music if it's currently playing */
slouken@173
   524
		SDL_LockAudio();
slouken@173
   525
		if ( music == music_playing ) {
slouken@173
   526
			/* Wait for any fade out to finish */
slouken@173
   527
			while ( music->fading == MIX_FADING_OUT ) {
slouken@173
   528
				SDL_UnlockAudio();
slouken@173
   529
				SDL_Delay(100);
slouken@173
   530
				SDL_LockAudio();
slouken@173
   531
			}
slouken@173
   532
			if ( music == music_playing ) {
slouken@173
   533
				music_internal_halt();
megastep@4
   534
			}
slouken@0
   535
		}
slouken@173
   536
		SDL_UnlockAudio();
slouken@0
   537
		switch (music->type) {
slouken@0
   538
#ifdef CMD_MUSIC
slouken@0
   539
			case MUS_CMD:
slouken@0
   540
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   541
				break;
slouken@0
   542
#endif
slouken@0
   543
#ifdef WAV_MUSIC
slouken@0
   544
			case MUS_WAV:
slouken@0
   545
				WAVStream_FreeSong(music->data.wave);
slouken@0
   546
				break;
slouken@0
   547
#endif
slouken@0
   548
#ifdef MOD_MUSIC
slouken@0
   549
			case MUS_MOD:
slouken@0
   550
				MikMod_FreeSong(music->data.module);
slouken@0
   551
				break;
slouken@0
   552
#endif
slouken@0
   553
#ifdef MID_MUSIC
slouken@0
   554
			case MUS_MID:
slouken@98
   555
#ifdef USE_NATIVE_MIDI
slouken@98
   556
  				if ( native_midi_ok ) {
slouken@98
   557
					native_midi_freesong(music->data.nativemidi);
slouken@106
   558
				} MIDI_ELSE
slouken@98
   559
#endif
slouken@106
   560
#ifdef USE_TIMIDITY_MIDI
slouken@98
   561
				if ( timidity_ok ) {
slouken@98
   562
					Timidity_FreeSong(music->data.midi);
slouken@98
   563
				}
slouken@106
   564
#endif
slouken@0
   565
				break;
slouken@0
   566
#endif
slouken@63
   567
#ifdef OGG_MUSIC
slouken@63
   568
			case MUS_OGG:
slouken@63
   569
				OGG_delete(music->data.ogg);
slouken@63
   570
				break;
slouken@63
   571
#endif
slouken@0
   572
#ifdef MP3_MUSIC
slouken@26
   573
			case MUS_MP3:
slouken@0
   574
				SMPEG_delete(music->data.mp3);
slouken@0
   575
				break;
slouken@0
   576
#endif
slouken@0
   577
			default:
slouken@0
   578
				/* Unknown music type?? */
slouken@0
   579
				break;
slouken@0
   580
		}
slouken@0
   581
		free(music);
slouken@0
   582
	}
slouken@0
   583
}
slouken@0
   584
slouken@177
   585
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   586
   music, if 'music' is NULL.
slouken@177
   587
*/
slouken@177
   588
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   589
{
slouken@177
   590
	Mix_MusicType type = MUS_NONE;
slouken@177
   591
slouken@177
   592
	if ( music ) {
slouken@177
   593
		type = music->type;
slouken@177
   594
	} else {
slouken@177
   595
		SDL_LockAudio();
slouken@177
   596
		if ( music_playing ) {
slouken@177
   597
			type = music_playing->type;
slouken@177
   598
		}
slouken@177
   599
		SDL_UnlockAudio();
slouken@177
   600
	}
slouken@177
   601
	return(type);
slouken@177
   602
}
slouken@177
   603
slouken@173
   604
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   605
 */
slouken@173
   606
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   607
{
slouken@173
   608
	int retval = 0;
megastep@10
   609
slouken@173
   610
	/* Note the music we're playing */
slouken@173
   611
	if ( music_playing ) {
slouken@173
   612
		music_internal_halt();
slouken@173
   613
	}
slouken@173
   614
	music_playing = music;
slouken@173
   615
slouken@173
   616
	/* Set the initial volume */
slouken@173
   617
	if ( music->fading == MIX_FADING_IN ) {
slouken@173
   618
		music_internal_volume(0);
slouken@173
   619
	} else {
slouken@173
   620
		music_internal_volume(music_volume);
slouken@173
   621
	}
slouken@173
   622
slouken@173
   623
	/* Set up for playback */
slouken@0
   624
	switch (music->type) {
slouken@0
   625
#ifdef CMD_MUSIC
slouken@173
   626
	    case MUS_CMD:
slouken@173
   627
		MusicCMD_Start(music->data.cmd);
slouken@173
   628
		break;
slouken@0
   629
#endif
slouken@0
   630
#ifdef WAV_MUSIC
slouken@173
   631
	    case MUS_WAV:
slouken@173
   632
		WAVStream_Start(music->data.wave);
slouken@173
   633
		break;
slouken@0
   634
#endif
slouken@0
   635
#ifdef MOD_MUSIC
slouken@173
   636
	    case MUS_MOD:
slouken@173
   637
		Player_Start(music->data.module);
slouken@173
   638
		break;
slouken@0
   639
#endif
slouken@0
   640
#ifdef MID_MUSIC
slouken@173
   641
	    case MUS_MID:
slouken@98
   642
#ifdef USE_NATIVE_MIDI
slouken@173
   643
		if ( native_midi_ok ) {
slouken@173
   644
			native_midi_start(music->data.nativemidi);
slouken@173
   645
		} MIDI_ELSE
slouken@98
   646
#endif
slouken@106
   647
#ifdef USE_TIMIDITY_MIDI
slouken@173
   648
		if ( timidity_ok ) {
slouken@173
   649
			Timidity_Start(music->data.midi);
slouken@173
   650
		}
slouken@106
   651
#endif
slouken@173
   652
		break;
slouken@0
   653
#endif
slouken@63
   654
#ifdef OGG_MUSIC
slouken@173
   655
	    case MUS_OGG:
slouken@173
   656
		OGG_play(music->data.ogg);
slouken@173
   657
		break;
slouken@63
   658
#endif
slouken@0
   659
#ifdef MP3_MUSIC
slouken@173
   660
	    case MUS_MP3:
slouken@173
   661
		SMPEG_enableaudio(music->data.mp3,1);
slouken@173
   662
		SMPEG_enablevideo(music->data.mp3,0);
slouken@176
   663
		SMPEG_rewind(music_playing->data.mp3);
slouken@173
   664
		SMPEG_play(music->data.mp3);
slouken@173
   665
		break;
slouken@0
   666
#endif
slouken@173
   667
	    default:
slouken@173
   668
		Mix_SetError("Can't play unknown music type");
slouken@173
   669
		retval = -1;
slouken@173
   670
		break;
slouken@0
   671
	}
slouken@173
   672
slouken@173
   673
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   674
	if ( retval == 0 ) {
slouken@173
   675
		if ( position > 0.0 ) {
slouken@173
   676
			if ( music_internal_position(position) < 0 ) {
slouken@173
   677
				Mix_SetError("Position not implemented for music type");
slouken@173
   678
				retval = -1;
slouken@173
   679
			}
slouken@173
   680
		} else {
slouken@173
   681
			music_internal_position(0.0);
slouken@173
   682
		}
slouken@173
   683
	}
slouken@173
   684
slouken@173
   685
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   686
	if ( retval < 0 ) {
slouken@173
   687
		music_playing = NULL;
slouken@173
   688
	}
slouken@173
   689
	return(retval);
slouken@173
   690
}
slouken@173
   691
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   692
{
slouken@173
   693
	int retval;
slouken@173
   694
slouken@173
   695
	/* Don't play null pointers :-) */
slouken@173
   696
	if ( music == NULL ) {
slouken@173
   697
		Mix_SetError("music parameter was NULL");
slouken@173
   698
		return(-1);
slouken@173
   699
	}
slouken@173
   700
slouken@173
   701
	/* Setup the data */
slouken@173
   702
	if ( ms ) {
slouken@173
   703
		music->fading = MIX_FADING_IN;
slouken@173
   704
	} else {
slouken@173
   705
		music->fading = MIX_NO_FADING;
slouken@173
   706
	}
slouken@173
   707
	music->fade_step = 0;
slouken@173
   708
	music->fade_steps = ms/ms_per_step;
slouken@173
   709
slouken@173
   710
	/* Play the puppy */
slouken@173
   711
	SDL_LockAudio();
slouken@173
   712
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   713
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   714
		SDL_UnlockAudio();
slouken@173
   715
		SDL_Delay(100);
slouken@173
   716
		SDL_LockAudio();
slouken@173
   717
	}
slouken@173
   718
	music_active = 1;
slouken@173
   719
	music_loops = loops;
slouken@173
   720
	retval = music_internal_play(music, position);
slouken@173
   721
	SDL_UnlockAudio();
slouken@173
   722
slouken@173
   723
	return(retval);
slouken@173
   724
}
slouken@173
   725
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   726
{
slouken@173
   727
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   728
}
slouken@173
   729
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   730
{
slouken@173
   731
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   732
}
megastep@10
   733
slouken@173
   734
/* Set the playing music position */
slouken@173
   735
int music_internal_position(double position)
megastep@10
   736
{
slouken@173
   737
	int retval = 0;
slouken@173
   738
slouken@173
   739
	switch (music_playing->type) {
slouken@173
   740
#ifdef MOD_MUSIC
slouken@173
   741
	    case MUS_MOD:
slouken@173
   742
		Player_SetPosition((UWORD)position);
slouken@173
   743
		break;
slouken@173
   744
#endif
slouken@173
   745
#ifdef OGG_MUSIC
slouken@173
   746
	    case MUS_OGG:
slouken@173
   747
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
   748
		break;
slouken@173
   749
#endif
slouken@173
   750
#ifdef MP3_MUSIC
slouken@173
   751
	    case MUS_MP3:
slouken@176
   752
		if ( position > 0.0 ) {
slouken@173
   753
			SMPEG_skip(music_playing->data.mp3, position);
slouken@173
   754
		}
slouken@173
   755
		break;
slouken@173
   756
#endif
slouken@173
   757
	    default:
slouken@173
   758
		/* TODO: Implement this for other music backends */
slouken@173
   759
		retval = -1;
slouken@173
   760
		break;
megastep@10
   761
	}
slouken@173
   762
	return(retval);
megastep@4
   763
}
slouken@155
   764
int Mix_SetMusicPosition(double position)
slouken@154
   765
{
slouken@173
   766
	int retval;
slouken@173
   767
slouken@173
   768
	SDL_LockAudio();
slouken@173
   769
	if ( music_playing ) {
slouken@173
   770
		retval = music_internal_position(position);
slouken@173
   771
		if ( retval < 0 ) {
slouken@173
   772
			Mix_SetError("Position not implemented for music type");
slouken@154
   773
		}
slouken@173
   774
	} else {
slouken@173
   775
		Mix_SetError("Music isn't playing");
slouken@173
   776
		retval = -1;
slouken@154
   777
	}
slouken@173
   778
	SDL_UnlockAudio();
slouken@154
   779
slouken@173
   780
	return(retval);
slouken@154
   781
}
slouken@154
   782
slouken@0
   783
/* Set the music volume */
slouken@173
   784
static void music_internal_volume(int volume)
slouken@173
   785
{
slouken@173
   786
	switch (music_playing->type) {
slouken@173
   787
#ifdef CMD_MUSIC
slouken@173
   788
	    case MUS_CMD:
slouken@173
   789
		MusicCMD_SetVolume(volume);
slouken@173
   790
		break;
slouken@173
   791
#endif
slouken@173
   792
#ifdef WAV_MUSIC
slouken@173
   793
	    case MUS_WAV:
slouken@173
   794
		WAVStream_SetVolume(volume);
slouken@173
   795
		break;
slouken@173
   796
#endif
slouken@173
   797
#ifdef MOD_MUSIC
slouken@173
   798
	    case MUS_MOD:
slouken@173
   799
		Player_SetVolume((SWORD)volume);
slouken@173
   800
		break;
slouken@173
   801
#endif
slouken@173
   802
#ifdef MID_MUSIC
slouken@173
   803
	    case MUS_MID:
slouken@173
   804
#ifdef USE_NATIVE_MIDI
slouken@173
   805
		if ( native_midi_ok ) {
slouken@173
   806
			native_midi_setvolume(volume);
slouken@173
   807
		} MIDI_ELSE
slouken@173
   808
#endif
slouken@173
   809
#ifdef USE_TIMIDITY_MIDI
slouken@173
   810
		if ( timidity_ok ) {
slouken@173
   811
			Timidity_SetVolume(volume);
slouken@173
   812
		}
slouken@173
   813
#endif
slouken@173
   814
		break;
slouken@173
   815
#endif
slouken@173
   816
#ifdef OGG_MUSIC
slouken@173
   817
	    case MUS_OGG:
slouken@173
   818
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
   819
		break;
slouken@173
   820
#endif
slouken@173
   821
#ifdef MP3_MUSIC
slouken@173
   822
	    case MUS_MP3:
slouken@173
   823
		SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
   824
		break;
slouken@173
   825
#endif
slouken@173
   826
	    default:
slouken@173
   827
		/* Unknown music type?? */
slouken@173
   828
		break;
slouken@173
   829
	}
slouken@173
   830
}
slouken@0
   831
int Mix_VolumeMusic(int volume)
slouken@0
   832
{
slouken@0
   833
	int prev_volume;
slouken@0
   834
slouken@0
   835
	prev_volume = music_volume;
megastep@4
   836
	if ( volume < 0 ) {
slouken@163
   837
		return prev_volume;
megastep@4
   838
	}
megastep@4
   839
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   840
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   841
	}
megastep@4
   842
	music_volume = volume;
slouken@173
   843
	SDL_LockAudio();
slouken@173
   844
	if ( music_playing ) {
slouken@173
   845
		music_internal_volume(music_volume);
slouken@0
   846
	}
slouken@173
   847
	SDL_UnlockAudio();
slouken@0
   848
	return(prev_volume);
slouken@0
   849
}
slouken@0
   850
slouken@173
   851
/* Halt playing of music */
slouken@173
   852
static void music_internal_halt(void)
megastep@7
   853
{
megastep@7
   854
	switch (music_playing->type) {
megastep@7
   855
#ifdef CMD_MUSIC
slouken@173
   856
	    case MUS_CMD:
megastep@7
   857
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   858
		break;
megastep@7
   859
#endif
megastep@7
   860
#ifdef WAV_MUSIC
slouken@173
   861
	    case MUS_WAV:
megastep@7
   862
		WAVStream_Stop();
megastep@7
   863
		break;
megastep@7
   864
#endif
megastep@7
   865
#ifdef MOD_MUSIC
slouken@173
   866
	    case MUS_MOD:
megastep@7
   867
		Player_Stop();
megastep@7
   868
		break;
megastep@7
   869
#endif
megastep@7
   870
#ifdef MID_MUSIC
slouken@173
   871
	    case MUS_MID:
slouken@98
   872
#ifdef USE_NATIVE_MIDI
slouken@98
   873
		if ( native_midi_ok ) {
slouken@98
   874
			native_midi_stop();
slouken@106
   875
		} MIDI_ELSE
slouken@98
   876
#endif
slouken@106
   877
#ifdef USE_TIMIDITY_MIDI
slouken@98
   878
		if ( timidity_ok ) {
slouken@98
   879
			Timidity_Stop();
slouken@98
   880
		}
slouken@106
   881
#endif
megastep@7
   882
		break;
megastep@7
   883
#endif
slouken@63
   884
#ifdef OGG_MUSIC
slouken@173
   885
	    case MUS_OGG:
slouken@63
   886
		OGG_stop(music_playing->data.ogg);
slouken@63
   887
		break;
slouken@63
   888
#endif
megastep@7
   889
#ifdef MP3_MUSIC
slouken@173
   890
	    case MUS_MP3:
megastep@7
   891
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   892
		break;
megastep@7
   893
#endif
slouken@173
   894
	    default:
megastep@7
   895
		/* Unknown music type?? */
megastep@7
   896
		return;
megastep@7
   897
	}
megastep@7
   898
	music_playing->fading = MIX_NO_FADING;
slouken@17
   899
	music_playing = NULL;
megastep@7
   900
}
slouken@0
   901
int Mix_HaltMusic(void)
slouken@0
   902
{
slouken@173
   903
	SDL_LockAudio();
slouken@173
   904
	if ( music_playing ) {
slouken@173
   905
		music_internal_halt();
slouken@0
   906
	}
slouken@173
   907
	SDL_UnlockAudio();
slouken@173
   908
slouken@0
   909
	return(0);
slouken@0
   910
}
slouken@0
   911
megastep@4
   912
/* Progressively stop the music */
megastep@4
   913
int Mix_FadeOutMusic(int ms)
megastep@4
   914
{
slouken@173
   915
	int retval = 0;
slouken@173
   916
slouken@173
   917
	SDL_LockAudio();
slouken@173
   918
	if ( music_playing && (music_playing->fading == MIX_NO_FADING) ) {
slouken@173
   919
		music_playing->fading = MIX_FADING_OUT;
slouken@173
   920
		music_playing->fade_step = 0;
slouken@173
   921
		music_playing->fade_steps = ms/ms_per_step;
slouken@173
   922
		retval = 1;
megastep@4
   923
	}
slouken@173
   924
	SDL_UnlockAudio();
slouken@173
   925
slouken@173
   926
	return(retval);
megastep@4
   927
}
megastep@4
   928
megastep@4
   929
Mix_Fading Mix_FadingMusic(void)
megastep@4
   930
{
slouken@173
   931
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
   932
slouken@173
   933
	SDL_LockAudio();
slouken@173
   934
	if ( music_playing ) {
slouken@173
   935
		fading = music_playing->fading;
slouken@173
   936
	}
slouken@173
   937
	SDL_UnlockAudio();
slouken@173
   938
slouken@173
   939
	return(fading);
megastep@4
   940
}
megastep@4
   941
slouken@0
   942
/* Pause/Resume the music stream */
slouken@0
   943
void Mix_PauseMusic(void)
slouken@0
   944
{
slouken@173
   945
	music_active = 0;
slouken@0
   946
}
megastep@4
   947
slouken@0
   948
void Mix_ResumeMusic(void)
slouken@0
   949
{
slouken@173
   950
	music_active = 1;
slouken@0
   951
}
slouken@0
   952
slouken@0
   953
void Mix_RewindMusic(void)
slouken@0
   954
{
slouken@173
   955
	Mix_SetMusicPosition(0.0);
slouken@0
   956
}
slouken@0
   957
megastep@13
   958
int Mix_PausedMusic(void)
megastep@13
   959
{
slouken@17
   960
	return (music_active == 0);
megastep@13
   961
}
megastep@13
   962
slouken@0
   963
/* Check the status of the music */
slouken@173
   964
static int music_internal_playing()
slouken@173
   965
{
slouken@173
   966
	int playing = 1;
slouken@173
   967
	switch (music_playing->type) {
slouken@173
   968
#ifdef CMD_MUSIC
slouken@173
   969
	    case MUS_CMD:
slouken@173
   970
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
   971
			playing = 0;
slouken@173
   972
		}
slouken@173
   973
		break;
slouken@173
   974
#endif
slouken@173
   975
#ifdef WAV_MUSIC
slouken@173
   976
	    case MUS_WAV:
slouken@173
   977
		if ( ! WAVStream_Active() ) {
slouken@173
   978
			playing = 0;
slouken@173
   979
		}
slouken@173
   980
		break;
slouken@173
   981
#endif
slouken@173
   982
#ifdef MOD_MUSIC
slouken@173
   983
	    case MUS_MOD:
slouken@173
   984
		if ( ! Player_Active() ) {
slouken@173
   985
			playing = 0;
slouken@173
   986
		}
slouken@173
   987
		break;
slouken@173
   988
#endif
slouken@173
   989
#ifdef MID_MUSIC
slouken@173
   990
	    case MUS_MID:
slouken@173
   991
#ifdef USE_NATIVE_MIDI
slouken@173
   992
		if ( native_midi_ok ) {
slouken@173
   993
			if ( ! native_midi_active() )
slouken@173
   994
				playing = 0;
slouken@173
   995
		} MIDI_ELSE
slouken@173
   996
#endif
slouken@173
   997
#ifdef USE_TIMIDITY_MIDI
slouken@173
   998
		if ( timidity_ok ) {
slouken@173
   999
			if ( ! Timidity_Active() )
slouken@173
  1000
				playing = 0;
slouken@173
  1001
		}
slouken@173
  1002
#endif
slouken@173
  1003
		break;
slouken@173
  1004
#endif
slouken@173
  1005
#ifdef OGG_MUSIC
slouken@173
  1006
	    case MUS_OGG:
slouken@173
  1007
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1008
			playing = 0;
slouken@173
  1009
		}
slouken@173
  1010
		break;
slouken@173
  1011
#endif
slouken@173
  1012
#ifdef MP3_MUSIC
slouken@173
  1013
	    case MUS_MP3:
slouken@173
  1014
		if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1015
			playing = 0;
slouken@173
  1016
		break;
slouken@173
  1017
#endif
slouken@173
  1018
	    default:
slouken@173
  1019
		playing = 0;
slouken@173
  1020
		break;
slouken@173
  1021
	}
slouken@173
  1022
	return(playing);
slouken@173
  1023
}
megastep@13
  1024
int Mix_PlayingMusic(void)
slouken@0
  1025
{
slouken@173
  1026
	int playing = 0;
slouken@173
  1027
slouken@173
  1028
	SDL_LockAudio();
slouken@173
  1029
	if ( music_playing ) {
slouken@173
  1030
		playing = music_internal_playing();
slouken@0
  1031
	}
slouken@173
  1032
	SDL_UnlockAudio();
slouken@173
  1033
slouken@173
  1034
	return(playing);
slouken@0
  1035
}
slouken@0
  1036
slouken@0
  1037
/* Set the external music playback command */
slouken@0
  1038
int Mix_SetMusicCMD(const char *command)
slouken@0
  1039
{
slouken@0
  1040
	Mix_HaltMusic();
slouken@0
  1041
	if ( music_cmd ) {
slouken@0
  1042
		free(music_cmd);
slouken@0
  1043
		music_cmd = NULL;
slouken@0
  1044
	}
slouken@0
  1045
	if ( command ) {
slouken@0
  1046
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
  1047
		if ( music_cmd == NULL ) {
slouken@0
  1048
			return(-1);
slouken@0
  1049
		}
slouken@0
  1050
		strcpy(music_cmd, command);
slouken@0
  1051
	}
slouken@0
  1052
	return(0);
slouken@0
  1053
}
slouken@0
  1054
slouken@154
  1055
int Mix_SetSynchroValue(int i)
slouken@154
  1056
{
slouken@154
  1057
	if ( music_playing && ! music_stopped ) {
slouken@154
  1058
		switch (music_playing->type) {
slouken@154
  1059
#ifdef MOD_MUSIC
slouken@173
  1060
		    case MUS_MOD:
slouken@173
  1061
			if ( ! Player_Active() ) {
slouken@173
  1062
				return(-1);
slouken@173
  1063
			}
slouken@173
  1064
			Player_SetSynchroValue(i);
slouken@173
  1065
			return 0;
slouken@173
  1066
			break;
slouken@154
  1067
#endif
slouken@173
  1068
		    default:
slouken@173
  1069
			return(-1);
slouken@173
  1070
			break;
slouken@154
  1071
		}
slouken@154
  1072
		return(-1);
slouken@154
  1073
	}
slouken@154
  1074
	return(-1);
slouken@154
  1075
}
slouken@154
  1076
slouken@154
  1077
int Mix_GetSynchroValue(void)
slouken@154
  1078
{
slouken@154
  1079
	if ( music_playing && ! music_stopped ) {
slouken@154
  1080
		switch (music_playing->type) {
slouken@154
  1081
#ifdef MOD_MUSIC
slouken@173
  1082
		    case MUS_MOD:
slouken@173
  1083
			if ( ! Player_Active() ) {
slouken@173
  1084
				return(-1);
slouken@173
  1085
			}
slouken@173
  1086
			return Player_GetSynchroValue();
slouken@173
  1087
			break;
slouken@154
  1088
#endif
slouken@173
  1089
		    default:
slouken@173
  1090
			return(-1);
slouken@173
  1091
			break;
slouken@154
  1092
		}
slouken@154
  1093
		return(-1);
slouken@154
  1094
	}
slouken@154
  1095
	return(-1);
slouken@154
  1096
}
slouken@154
  1097
slouken@154
  1098
slouken@0
  1099
/* Uninitialize the music players */
slouken@0
  1100
void close_music(void)
slouken@0
  1101
{
slouken@0
  1102
	Mix_HaltMusic();
slouken@0
  1103
#ifdef CMD_MUSIC
slouken@0
  1104
	Mix_SetMusicCMD(NULL);
slouken@0
  1105
#endif
slouken@0
  1106
#ifdef MOD_MUSIC
slouken@0
  1107
	MikMod_Exit();
slouken@101
  1108
	MikMod_UnregisterAllLoaders();
slouken@101
  1109
	MikMod_UnregisterAllDrivers();
slouken@0
  1110
#endif
slouken@0
  1111
}
slouken@0
  1112