music.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 21 Oct 1999 18:02:08 +0000
changeset 0 4ce2db4db959
child 3 f6dabb779e45
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    MIXERLIB:  An audio mixer library based on the SDL library
slouken@0
     3
    Copyright (C) 1997-1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@0
    25
#include <SDL/SDL_endian.h>
slouken@0
    26
#include <SDL/SDL_audio.h>
slouken@0
    27
slouken@0
    28
#include "mixer.h"
slouken@0
    29
slouken@0
    30
/* The music command hack is UNIX specific */
slouken@0
    31
#ifndef unix
slouken@0
    32
#undef CMD_MUSIC
slouken@0
    33
#endif
slouken@0
    34
slouken@0
    35
#ifdef CMD_MUSIC
slouken@0
    36
#include "music_cmd.h"
slouken@0
    37
#endif
slouken@0
    38
#ifdef WAV_MUSIC
slouken@0
    39
#include "wavestream.h"
slouken@0
    40
#endif
slouken@0
    41
#ifdef MOD_MUSIC
slouken@0
    42
#include "mikmod.h"
slouken@0
    43
#endif
slouken@0
    44
#ifdef MID_MUSIC
slouken@0
    45
#include "timidity.h"
slouken@0
    46
#endif
slouken@0
    47
#ifdef MP3_MUSIC
slouken@0
    48
#include <smpeg/smpeg.h>
slouken@0
    49
slouken@0
    50
static SDL_AudioSpec used_mixer;
slouken@0
    51
#endif
slouken@0
    52
slouken@0
    53
int music_active = 1;
slouken@0
    54
static int music_loops = 0;
slouken@0
    55
static char *music_cmd = NULL;
slouken@0
    56
static int samplesize;
slouken@0
    57
static Mix_Music *music_playing = 0;
slouken@0
    58
static int music_volume;
slouken@0
    59
static int music_swap8;
slouken@0
    60
static int music_swap16;
slouken@0
    61
struct _Mix_Music {
slouken@0
    62
	enum {
slouken@0
    63
		MUS_CMD,
slouken@0
    64
		MUS_WAV,
slouken@0
    65
		MUS_MOD,
slouken@0
    66
		MUS_MID,
slouken@0
    67
		MUS_MP3
slouken@0
    68
	} type;
slouken@0
    69
	union {
slouken@0
    70
#ifdef CMD_MUSIC
slouken@0
    71
		MusicCMD *cmd;
slouken@0
    72
#endif
slouken@0
    73
#ifdef WAV_MUSIC
slouken@0
    74
		WAVStream *wave;
slouken@0
    75
#endif
slouken@0
    76
#ifdef MOD_MUSIC
slouken@0
    77
		UNIMOD *module;
slouken@0
    78
#endif
slouken@0
    79
#ifdef MID_MUSIC
slouken@0
    80
		MidiSong *midi;
slouken@0
    81
#endif
slouken@0
    82
#ifdef MP3_MUSIC
slouken@0
    83
		SMPEG *mp3;
slouken@0
    84
#endif
slouken@0
    85
	} data;
slouken@0
    86
	int error;
slouken@0
    87
};
slouken@0
    88
static int timidity_ok;
slouken@0
    89
slouken@0
    90
/* Mixing function */
slouken@0
    91
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
    92
{
slouken@0
    93
	int i;
slouken@0
    94
slouken@0
    95
	if ( music_playing ) {
slouken@0
    96
		switch (music_playing->type) {
slouken@0
    97
#ifdef CMD_MUSIC
slouken@0
    98
			case MUS_CMD:
slouken@0
    99
				/* The playing is done externally */
slouken@0
   100
				break;
slouken@0
   101
#endif
slouken@0
   102
#ifdef WAV_MUSIC
slouken@0
   103
			case MUS_WAV:
slouken@0
   104
				WAVStream_PlaySome(stream, len);
slouken@0
   105
				break;
slouken@0
   106
#endif
slouken@0
   107
#ifdef MOD_MUSIC
slouken@0
   108
			case MUS_MOD:
slouken@0
   109
				VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   110
				if ( music_swap8 ) {
slouken@0
   111
					Uint8 *dst;
slouken@0
   112
slouken@0
   113
					dst = stream;
slouken@0
   114
					for ( i=len; i; --i ) {
slouken@0
   115
						*dst++ ^= 0x80;
slouken@0
   116
					}
slouken@0
   117
				} else
slouken@0
   118
				if ( music_swap16 ) {
slouken@0
   119
					Uint8 *dst, tmp;
slouken@0
   120
slouken@0
   121
					dst = stream;
slouken@0
   122
					for ( i=(len/2); i; --i ) {
slouken@0
   123
						tmp = dst[0];
slouken@0
   124
						dst[0] = dst[1];
slouken@0
   125
						dst[1] = tmp;
slouken@0
   126
						dst += 2;
slouken@0
   127
					}
slouken@0
   128
				}
slouken@0
   129
				break;
slouken@0
   130
#endif
slouken@0
   131
#ifdef MID_MUSIC
slouken@0
   132
			case MUS_MID:
slouken@0
   133
				Timidity_PlaySome(stream, len/samplesize);
slouken@0
   134
				break;
slouken@0
   135
#endif
slouken@0
   136
#ifdef MP3_MUSIC
slouken@0
   137
		    case MUS_MP3:
slouken@0
   138
			    SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   139
				break;
slouken@0
   140
#endif
slouken@0
   141
			default:
slouken@0
   142
				/* Unknown music type?? */
slouken@0
   143
				break;
slouken@0
   144
		}
slouken@0
   145
	}
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
/* Initialize the music players with a certain desired audio format */
slouken@0
   149
int open_music(SDL_AudioSpec *mixer)
slouken@0
   150
{
slouken@0
   151
	int music_error;
slouken@0
   152
slouken@0
   153
	music_error = 0;
slouken@0
   154
#ifdef WAV_MUSIC
slouken@0
   155
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   156
		++music_error;
slouken@0
   157
	}
slouken@0
   158
#endif
slouken@0
   159
#ifdef MOD_MUSIC
slouken@0
   160
	/* Set the MikMod music format */
slouken@0
   161
	music_swap8 = 0;
slouken@0
   162
	music_swap16 = 0;
slouken@0
   163
	switch (mixer->format) {
slouken@0
   164
slouken@0
   165
		case AUDIO_U8:
slouken@0
   166
		case AUDIO_S8: {
slouken@0
   167
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   168
				music_swap8 = 1;
slouken@0
   169
			}
slouken@0
   170
			md_mode = 0;
slouken@0
   171
		}
slouken@0
   172
		break;
slouken@0
   173
slouken@0
   174
		case AUDIO_S16LSB:
slouken@0
   175
		case AUDIO_S16MSB: {
slouken@0
   176
			/* See if we need to correct MikMod mixing */
slouken@0
   177
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   178
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   179
#else
slouken@0
   180
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   181
#endif
slouken@0
   182
				music_swap16 = 1;
slouken@0
   183
			}
slouken@0
   184
			md_mode = DMODE_16BITS;
slouken@0
   185
		}
slouken@0
   186
		break;
slouken@0
   187
slouken@0
   188
		default: {
slouken@0
   189
			SDL_SetError("Unknown hardware audio format");
slouken@0
   190
			++music_error;
slouken@0
   191
		}
slouken@0
   192
	}
slouken@0
   193
	if ( mixer->channels > 1 ) {
slouken@0
   194
		if ( mixer->channels > 2 ) {
slouken@0
   195
			SDL_SetError("Hardware uses more channels than mixer");
slouken@0
   196
			++music_error;
slouken@0
   197
		}
slouken@0
   198
		md_mode |= DMODE_STEREO;
slouken@0
   199
	}
slouken@0
   200
	samplesize     = mixer->size/mixer->samples;
slouken@0
   201
	md_mixfreq     = mixer->freq;
slouken@0
   202
	md_device      = 0;
slouken@0
   203
	md_volume      = 96;
slouken@0
   204
	md_musicvolume = 128;
slouken@0
   205
	md_sndfxvolume = 128;
slouken@0
   206
	md_pansep      = 128;
slouken@0
   207
	md_reverb      = 0;
slouken@0
   208
	MikMod_RegisterAllLoaders();
slouken@0
   209
	MikMod_RegisterAllDrivers();
slouken@0
   210
	if ( MikMod_Init() ) {
slouken@0
   211
		SDL_SetError("%s", _mm_errmsg[_mm_errno]);
slouken@0
   212
		++music_error;
slouken@0
   213
	}
slouken@0
   214
#endif
slouken@0
   215
#ifdef MID_MUSIC
slouken@0
   216
	if ( Timidity_Init(mixer->freq,
slouken@0
   217
			mixer->format, mixer->channels, mixer->samples) == 0 ) {
slouken@0
   218
		timidity_ok = 1;
slouken@0
   219
	} else {
slouken@0
   220
		timidity_ok = 0;
slouken@0
   221
	}
slouken@0
   222
#endif
slouken@0
   223
#ifdef MP3_MUSIC
slouken@0
   224
	/* Keep a copy of the mixer */
slouken@0
   225
	used_mixer = *mixer;
slouken@0
   226
#endif
slouken@0
   227
	music_playing = 0;
slouken@0
   228
	if ( music_error ) {
slouken@0
   229
		return(-1);
slouken@0
   230
	}
slouken@0
   231
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   232
slouken@0
   233
	return(0);
slouken@0
   234
}
slouken@0
   235
slouken@0
   236
/* Load a music file */
slouken@0
   237
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   238
{
slouken@0
   239
	FILE *fp;
slouken@0
   240
	unsigned char magic[5];
slouken@0
   241
	Mix_Music *music;
slouken@0
   242
slouken@0
   243
	/* Figure out what kind of file this is */
slouken@0
   244
	fp = fopen(file, "rb");
slouken@0
   245
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   246
		if ( fp != NULL ) {
slouken@0
   247
			fclose(fp);
slouken@0
   248
		}
slouken@0
   249
		SDL_SetError("Couldn't read from '%s'", file);
slouken@0
   250
		return(NULL);
slouken@0
   251
	}
slouken@0
   252
	magic[4] = '\0';
slouken@0
   253
	fclose(fp);
slouken@0
   254
slouken@0
   255
	/* Allocate memory for the music structure */
slouken@0
   256
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   257
	if ( music == NULL ) {
slouken@0
   258
		SDL_SetError("Out of memory");
slouken@0
   259
		return(NULL);
slouken@0
   260
	}
slouken@0
   261
	music->error = 0;
slouken@0
   262
slouken@0
   263
#ifdef CMD_MUSIC
slouken@0
   264
	if ( music_cmd ) {
slouken@0
   265
		music->type = MUS_CMD;
slouken@0
   266
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   267
		if ( music->data.cmd == NULL ) {
slouken@0
   268
			music->error = 1;
slouken@0
   269
		}
slouken@0
   270
	} else
slouken@0
   271
#endif
slouken@0
   272
#ifdef WAV_MUSIC
slouken@0
   273
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   274
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   275
	 */
slouken@0
   276
	if ( (strcmp(magic, "RIFF") == 0) || (strcmp(magic, "FORM") == 0) ) {
slouken@0
   277
		music->type = MUS_WAV;
slouken@0
   278
		music->data.wave = WAVStream_LoadSong(file, magic);
slouken@0
   279
		if ( music->data.wave == NULL ) {
slouken@0
   280
			music->error = 1;
slouken@0
   281
		}
slouken@0
   282
	} else
slouken@0
   283
#endif
slouken@0
   284
#ifdef MID_MUSIC
slouken@0
   285
	/* MIDI files have the magic four bytes "MThd" */
slouken@0
   286
	if ( strcmp(magic, "MThd") == 0 ) {
slouken@0
   287
		music->type = MUS_MID;
slouken@0
   288
		if ( timidity_ok ) {
slouken@0
   289
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   290
			if ( music->data.midi == NULL ) {
slouken@0
   291
				SDL_SetError("%s", Timidity_Error());
slouken@0
   292
				music->error = 1;
slouken@0
   293
			}
slouken@0
   294
		}
slouken@0
   295
		else {
slouken@0
   296
			SDL_SetError("%s", Timidity_Error());
slouken@0
   297
			music->error = 1;
slouken@0
   298
		}
slouken@0
   299
	} else
slouken@0
   300
#endif
slouken@0
   301
#ifdef MP3_MUSIC
slouken@0
   302
	if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   303
		SMPEG_Info info;
slouken@0
   304
		music->type = MUS_MP3;
slouken@0
   305
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   306
		if(!info.has_audio){
slouken@0
   307
			SDL_SetError("MPEG file does not have any audio stream.");
slouken@0
   308
			music->error = 1;
slouken@0
   309
		}else{
slouken@0
   310
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   311
		}
slouken@0
   312
	} else
slouken@0
   313
#endif
slouken@0
   314
#ifdef MOD_MUSIC
slouken@0
   315
	if ( 1 ) {
slouken@0
   316
		music->type = MUS_MOD;
slouken@0
   317
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   318
		if ( music->data.module == NULL ) {
slouken@0
   319
			SDL_SetError("%s", _mm_errmsg[_mm_errno]);
slouken@0
   320
			music->error = 1;
slouken@0
   321
		}
slouken@0
   322
	} else
slouken@0
   323
#endif
slouken@0
   324
	{
slouken@0
   325
		SDL_SetError("Unrecognized music format");
slouken@0
   326
		music->error = 1;
slouken@0
   327
	}
slouken@0
   328
	if ( music->error ) {
slouken@0
   329
		free(music);
slouken@0
   330
		music = NULL;
slouken@0
   331
	}
slouken@0
   332
	return(music);
slouken@0
   333
}
slouken@0
   334
slouken@0
   335
/* Free a music chunk previously loaded */
slouken@0
   336
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   337
{
slouken@0
   338
	if ( music ) {
slouken@0
   339
		/* Caution: If music is playing, mixer will crash */
slouken@0
   340
		if ( music == music_playing ) {
slouken@0
   341
			Mix_HaltMusic();
slouken@0
   342
		}
slouken@0
   343
		switch (music->type) {
slouken@0
   344
#ifdef CMD_MUSIC
slouken@0
   345
			case MUS_CMD:
slouken@0
   346
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   347
				break;
slouken@0
   348
#endif
slouken@0
   349
#ifdef WAV_MUSIC
slouken@0
   350
			case MUS_WAV:
slouken@0
   351
				WAVStream_FreeSong(music->data.wave);
slouken@0
   352
				break;
slouken@0
   353
#endif
slouken@0
   354
#ifdef MOD_MUSIC
slouken@0
   355
			case MUS_MOD:
slouken@0
   356
				MikMod_FreeSong(music->data.module);
slouken@0
   357
				break;
slouken@0
   358
#endif
slouken@0
   359
#ifdef MID_MUSIC
slouken@0
   360
			case MUS_MID:
slouken@0
   361
				Timidity_FreeSong(music->data.midi);
slouken@0
   362
				break;
slouken@0
   363
#endif
slouken@0
   364
#ifdef MP3_MUSIC
slouken@0
   365
		    case MUS_MP3:
slouken@0
   366
				SMPEG_delete(music->data.mp3);
slouken@0
   367
				break;
slouken@0
   368
#endif
slouken@0
   369
			default:
slouken@0
   370
				/* Unknown music type?? */
slouken@0
   371
				break;
slouken@0
   372
		}
slouken@0
   373
		free(music);
slouken@0
   374
	}
slouken@0
   375
}
slouken@0
   376
slouken@0
   377
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@0
   378
*/
slouken@0
   379
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@0
   380
{
slouken@0
   381
	/* Don't play null pointers :-) */
slouken@0
   382
	if ( music == NULL ) {
slouken@0
   383
		return(-1);
slouken@0
   384
	}
slouken@0
   385
	switch (music->type) {
slouken@0
   386
#ifdef CMD_MUSIC
slouken@0
   387
		case MUS_CMD:
slouken@0
   388
			MusicCMD_SetVolume(music_volume);
slouken@0
   389
			MusicCMD_Start(music->data.cmd);
slouken@0
   390
			break;
slouken@0
   391
#endif
slouken@0
   392
#ifdef WAV_MUSIC
slouken@0
   393
		case MUS_WAV:
slouken@0
   394
			WAVStream_SetVolume(music_volume);
slouken@0
   395
			WAVStream_Start(music->data.wave);
slouken@0
   396
			break;
slouken@0
   397
#endif
slouken@0
   398
#ifdef MOD_MUSIC
slouken@0
   399
		case MUS_MOD:
slouken@0
   400
			Player_SetVolume(music_volume);
slouken@0
   401
			Player_Start(music->data.module);
slouken@0
   402
			Player_SetPosition(0);
slouken@0
   403
			break;
slouken@0
   404
#endif
slouken@0
   405
#ifdef MID_MUSIC
slouken@0
   406
		case MUS_MID:
slouken@0
   407
			Timidity_SetVolume(music_volume);
slouken@0
   408
			Timidity_Start(music->data.midi);
slouken@0
   409
			break;
slouken@0
   410
#endif
slouken@0
   411
#ifdef MP3_MUSIC
slouken@0
   412
	    case MUS_MP3:
slouken@0
   413
			SMPEG_enableaudio(music->data.mp3,1);
slouken@0
   414
			SMPEG_enablevideo(music->data.mp3,0);
slouken@0
   415
			SMPEG_setvolume(music->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
slouken@0
   416
			SMPEG_loop(music->data.mp3, loops);
slouken@0
   417
			SMPEG_play(music->data.mp3);
slouken@0
   418
			break;
slouken@0
   419
#endif
slouken@0
   420
		default:
slouken@0
   421
			/* Unknown music type?? */
slouken@0
   422
			return(-1);
slouken@0
   423
	}
slouken@0
   424
	music_active = 1;
slouken@0
   425
	music_playing = music;
slouken@0
   426
	return(0);
slouken@0
   427
}
slouken@0
   428
slouken@0
   429
/* Set the music volume */
slouken@0
   430
int Mix_VolumeMusic(int volume)
slouken@0
   431
{
slouken@0
   432
	int prev_volume;
slouken@0
   433
slouken@0
   434
	prev_volume = music_volume;
slouken@0
   435
	if ( volume >= 0 ) {
slouken@0
   436
		if ( volume > SDL_MIX_MAXVOLUME ) {
slouken@0
   437
			volume = SDL_MIX_MAXVOLUME;
slouken@0
   438
		}
slouken@0
   439
		music_volume = volume;
slouken@0
   440
		if ( music_playing ) {
slouken@0
   441
			switch (music_playing->type) {
slouken@0
   442
#ifdef CMD_MUSIC
slouken@0
   443
				case MUS_CMD:
slouken@0
   444
					MusicCMD_SetVolume(music_volume);
slouken@0
   445
					break;
slouken@0
   446
#endif
slouken@0
   447
#ifdef WAV_MUSIC
slouken@0
   448
				case MUS_WAV:
slouken@0
   449
					WAVStream_SetVolume(music_volume);
slouken@0
   450
					break;
slouken@0
   451
#endif
slouken@0
   452
#ifdef MOD_MUSIC
slouken@0
   453
				case MUS_MOD:
slouken@0
   454
					Player_SetVolume(music_volume);
slouken@0
   455
					break;
slouken@0
   456
#endif
slouken@0
   457
#ifdef MID_MUSIC
slouken@0
   458
				case MUS_MID:
slouken@0
   459
					Timidity_SetVolume(music_volume);
slouken@0
   460
					break;
slouken@0
   461
#endif
slouken@0
   462
#ifdef MP3_MUSIC
slouken@0
   463
			    case MUS_MP3:
slouken@0
   464
					SMPEG_setvolume(music_playing->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
slouken@0
   465
					break;
slouken@0
   466
#endif
slouken@0
   467
				default:
slouken@0
   468
					/* Unknown music type?? */
slouken@0
   469
					break;
slouken@0
   470
			}
slouken@0
   471
		}
slouken@0
   472
	}
slouken@0
   473
	return(prev_volume);
slouken@0
   474
}
slouken@0
   475
slouken@0
   476
/* Halt playing of music */
slouken@0
   477
int Mix_HaltMusic(void)
slouken@0
   478
{
slouken@0
   479
	if ( music_playing ) {
slouken@0
   480
		switch (music_playing->type) {
slouken@0
   481
#ifdef CMD_MUSIC
slouken@0
   482
			case MUS_CMD:
slouken@0
   483
				MusicCMD_Stop(music_playing->data.cmd);
slouken@0
   484
				break;
slouken@0
   485
#endif
slouken@0
   486
#ifdef WAV_MUSIC
slouken@0
   487
			case MUS_WAV:
slouken@0
   488
				WAVStream_Stop();
slouken@0
   489
				break;
slouken@0
   490
#endif
slouken@0
   491
#ifdef MOD_MUSIC
slouken@0
   492
			case MUS_MOD:
slouken@0
   493
				Player_Stop();
slouken@0
   494
				break;
slouken@0
   495
#endif
slouken@0
   496
#ifdef MID_MUSIC
slouken@0
   497
			case MUS_MID:
slouken@0
   498
				Timidity_Stop();
slouken@0
   499
				break;
slouken@0
   500
#endif
slouken@0
   501
#ifdef MP3_MUSIC
slouken@0
   502
		    case MUS_MP3:
slouken@0
   503
				SMPEG_stop(music_playing->data.mp3);
slouken@0
   504
				break;
slouken@0
   505
#endif
slouken@0
   506
			default:
slouken@0
   507
				/* Unknown music type?? */
slouken@0
   508
				return(-1);
slouken@0
   509
		}
slouken@0
   510
		music_playing = 0;
slouken@0
   511
	}
slouken@0
   512
	return(0);
slouken@0
   513
}
slouken@0
   514
slouken@0
   515
/* Pause/Resume the music stream */
slouken@0
   516
void Mix_PauseMusic(void)
slouken@0
   517
{
slouken@0
   518
	if ( music_playing ) {
slouken@0
   519
		switch ( music_playing->type ) {
slouken@0
   520
#ifdef CMD_MUSIC
slouken@0
   521
		case MUS_CMD:
slouken@0
   522
			MusicCMD_Pause(music_playing->data.cmd);
slouken@0
   523
			break;
slouken@0
   524
#endif
slouken@0
   525
#ifdef MP3_MUSIC
slouken@0
   526
		case MUS_MP3:
slouken@0
   527
			SMPEG_pause(music_playing->data.mp3);
slouken@0
   528
			break;
slouken@0
   529
#endif
slouken@0
   530
		}
slouken@0
   531
	}
slouken@0
   532
	music_active = 0;
slouken@0
   533
}
slouken@0
   534
void Mix_ResumeMusic(void)
slouken@0
   535
{
slouken@0
   536
	if ( music_playing ) {
slouken@0
   537
		switch ( music_playing->type ) {
slouken@0
   538
#ifdef CMD_MUSIC
slouken@0
   539
		case MUS_CMD:
slouken@0
   540
			MusicCMD_Resume(music_playing->data.cmd);
slouken@0
   541
			break;
slouken@0
   542
#endif
slouken@0
   543
#ifdef MP3_MUSIC
slouken@0
   544
		case MUS_MP3:
slouken@0
   545
			SMPEG_pause(music_playing->data.mp3);
slouken@0
   546
			break;
slouken@0
   547
#endif
slouken@0
   548
		}
slouken@0
   549
	}
slouken@0
   550
	music_active = 1;
slouken@0
   551
}
slouken@0
   552
slouken@0
   553
void Mix_RewindMusic(void)
slouken@0
   554
{
slouken@0
   555
	if ( music_playing ) {
slouken@0
   556
		switch ( music_playing->type ) {
slouken@0
   557
#ifdef MP3_MUSIC
slouken@0
   558
		case MUS_MP3:
slouken@0
   559
			SMPEG_rewind(music_playing->data.mp3);
slouken@0
   560
			break;
slouken@0
   561
#endif
slouken@0
   562
		}
slouken@0
   563
	}
slouken@0
   564
}
slouken@0
   565
slouken@0
   566
/* Check the status of the music */
slouken@0
   567
int Mix_PlayingMusic(void)
slouken@0
   568
{
slouken@0
   569
	if ( music_playing ) {
slouken@0
   570
		switch (music_playing->type) {
slouken@0
   571
#ifdef CMD_MUSIC
slouken@0
   572
			case MUS_CMD:
slouken@0
   573
				if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@0
   574
					music_playing = 0;
slouken@0
   575
				}
slouken@0
   576
				break;
slouken@0
   577
#endif
slouken@0
   578
#ifdef WAV_MUSIC
slouken@0
   579
			case MUS_WAV:
slouken@0
   580
				if ( ! WAVStream_Active() ) {
slouken@0
   581
					music_playing = 0;
slouken@0
   582
				}
slouken@0
   583
				break;
slouken@0
   584
#endif
slouken@0
   585
#ifdef MOD_MUSIC
slouken@0
   586
			case MUS_MOD:
slouken@0
   587
				if ( ! Player_Active() ) {
slouken@0
   588
					music_playing = 0;
slouken@0
   589
				}
slouken@0
   590
				break;
slouken@0
   591
#endif
slouken@0
   592
#ifdef MID_MUSIC
slouken@0
   593
			case MUS_MID:
slouken@0
   594
				if ( ! Timidity_Active() ) {
slouken@0
   595
					music_playing = 0;
slouken@0
   596
				}
slouken@0
   597
				break;
slouken@0
   598
#endif
slouken@0
   599
#ifdef MP3_MUSIC
slouken@0
   600
			case MUS_MP3:
slouken@0
   601
				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
slouken@0
   602
					music_playing = 0;
slouken@0
   603
				break;
slouken@0
   604
#endif
slouken@0
   605
		}
slouken@0
   606
	}
slouken@0
   607
	return(music_playing ? 1 : 0);
slouken@0
   608
}
slouken@0
   609
slouken@0
   610
/* Set the external music playback command */
slouken@0
   611
int Mix_SetMusicCMD(const char *command)
slouken@0
   612
{
slouken@0
   613
	Mix_HaltMusic();
slouken@0
   614
	if ( music_cmd ) {
slouken@0
   615
		free(music_cmd);
slouken@0
   616
		music_cmd = NULL;
slouken@0
   617
	}
slouken@0
   618
	if ( command ) {
slouken@0
   619
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
   620
		if ( music_cmd == NULL ) {
slouken@0
   621
			return(-1);
slouken@0
   622
		}
slouken@0
   623
		strcpy(music_cmd, command);
slouken@0
   624
	}
slouken@0
   625
	return(0);
slouken@0
   626
}
slouken@0
   627
slouken@0
   628
/* Uninitialize the music players */
slouken@0
   629
void close_music(void)
slouken@0
   630
{
slouken@0
   631
	Mix_HaltMusic();
slouken@0
   632
#ifdef CMD_MUSIC
slouken@0
   633
	Mix_SetMusicCMD(NULL);
slouken@0
   634
#endif
slouken@0
   635
#ifdef MOD_MUSIC
slouken@0
   636
	MikMod_Exit();
slouken@0
   637
#endif
slouken@0
   638
}
slouken@0
   639