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