music.c
author Sam Lantinga <slouken@lokigames.com>
Mon, 03 Jul 2000 00:25:20 +0000
changeset 63 92b13db7d0a5
parent 58 7b2c2ed3399e
child 64 00b28153c609
permissions -rw-r--r--
Sam Lantinga - Sun Jul 2 14:16:44 PDT 2000
* Added support for the Ogg Vorbis music format: http://www.vorbis.org/
* Cleaned up the streaming wave code a bit
slouken@0
     1
/*
slouken@26
     2
	MIXERLIB:  An audio mixer library based on the SDL library
slouken@26
     3
	Copyright (C) 1997-1999  Sam Lantinga
slouken@0
     4
slouken@26
     5
	This library is free software; you can redistribute it and/or
slouken@26
     6
	modify it under the terms of the GNU Library General Public
slouken@26
     7
	License as published by the Free Software Foundation; either
slouken@26
     8
	version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@26
    10
	This library is distributed in the hope that it will be useful,
slouken@26
    11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@26
    12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@26
    13
	Library General Public License for more details.
slouken@0
    14
slouken@26
    15
	You should have received a copy of the GNU Library General Public
slouken@26
    16
	License along with this library; if not, write to the Free
slouken@26
    17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@26
    19
	Sam Lantinga
slouken@26
    20
	5635-34 Springhouse Dr.
slouken@26
    21
	Pleasanton, CA 94588 (USA)
slouken@26
    22
	slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@3
    25
#include <stdlib.h>
slouken@3
    26
#include <string.h>
slouken@24
    27
#include "SDL_endian.h"
slouken@24
    28
#include "SDL_audio.h"
slouken@29
    29
#include "SDL_timer.h"
slouken@0
    30
slouken@34
    31
#include "SDL_mixer.h"
slouken@0
    32
slouken@0
    33
/* The music command hack is UNIX specific */
slouken@0
    34
#ifndef unix
slouken@0
    35
#undef CMD_MUSIC
slouken@0
    36
#endif
slouken@0
    37
slouken@0
    38
#ifdef CMD_MUSIC
slouken@0
    39
#include "music_cmd.h"
slouken@0
    40
#endif
slouken@0
    41
#ifdef WAV_MUSIC
slouken@0
    42
#include "wavestream.h"
slouken@0
    43
#endif
slouken@0
    44
#ifdef MOD_MUSIC
slouken@29
    45
#  include "mikmod.h"
slouken@29
    46
#  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
slouken@29
    47
#    define UNIMOD			MODULE
slouken@29
    48
#    define MikMod_Init()		MikMod_Init(NULL)
slouken@29
    49
#    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
slouken@29
    50
#    define MikMod_FreeSong		Player_Free
slouken@29
    51
     extern int MikMod_errno;
slouken@29
    52
#  else                                        /* old MikMod 3.0.3 */
slouken@29
    53
#    define MikMod_strerror(x)		_mm_errmsg[x])
slouken@29
    54
#    define MikMod_errno		_mm_errno
slouken@29
    55
#  endif
slouken@0
    56
#endif
slouken@0
    57
#ifdef MID_MUSIC
slouken@0
    58
#include "timidity.h"
slouken@0
    59
#endif
slouken@63
    60
#ifdef OGG_MUSIC
slouken@63
    61
#include "music_ogg.h"
slouken@63
    62
#endif
slouken@0
    63
#ifdef MP3_MUSIC
slouken@0
    64
#include <smpeg/smpeg.h>
slouken@0
    65
slouken@0
    66
static SDL_AudioSpec used_mixer;
slouken@0
    67
#endif
slouken@0
    68
slouken@42
    69
int volatile music_active = 1;
slouken@42
    70
static int volatile music_stopped = 0;
slouken@0
    71
static int music_loops = 0;
slouken@0
    72
static char *music_cmd = NULL;
slouken@0
    73
static int samplesize;
slouken@42
    74
static Mix_Music * volatile music_playing = NULL;
slouken@17
    75
static int music_volume = MIX_MAX_VOLUME;
slouken@0
    76
static int music_swap8;
slouken@0
    77
static int music_swap16;
megastep@4
    78
slouken@0
    79
struct _Mix_Music {
slouken@0
    80
	enum {
slouken@0
    81
		MUS_CMD,
slouken@0
    82
		MUS_WAV,
slouken@0
    83
		MUS_MOD,
slouken@0
    84
		MUS_MID,
slouken@63
    85
		MUS_OGG,
slouken@0
    86
		MUS_MP3
slouken@0
    87
	} type;
slouken@0
    88
	union {
slouken@0
    89
#ifdef CMD_MUSIC
slouken@0
    90
		MusicCMD *cmd;
slouken@0
    91
#endif
slouken@0
    92
#ifdef WAV_MUSIC
slouken@0
    93
		WAVStream *wave;
slouken@0
    94
#endif
slouken@0
    95
#ifdef MOD_MUSIC
slouken@0
    96
		UNIMOD *module;
slouken@0
    97
#endif
slouken@0
    98
#ifdef MID_MUSIC
slouken@0
    99
		MidiSong *midi;
slouken@0
   100
#endif
slouken@63
   101
#ifdef OGG_MUSIC
slouken@63
   102
		OGG_music *ogg;
slouken@63
   103
#endif
slouken@0
   104
#ifdef MP3_MUSIC
slouken@0
   105
		SMPEG *mp3;
slouken@0
   106
#endif
slouken@0
   107
	} data;
megastep@4
   108
	Mix_Fading fading;
megastep@4
   109
	int fade_volume;
slouken@17
   110
	int fade_step;
slouken@17
   111
	int fade_steps;
slouken@0
   112
	int error;
slouken@0
   113
};
slouken@29
   114
#ifdef MID_MUSIC
slouken@0
   115
static int timidity_ok;
slouken@29
   116
#endif
slouken@0
   117
slouken@17
   118
/* Used to calculate fading steps */
slouken@17
   119
static int ms_per_step;
slouken@17
   120
megastep@10
   121
/* Local low-level functions prototypes */
megastep@7
   122
static void lowlevel_halt(void);
megastep@10
   123
static int  lowlevel_play(Mix_Music *music);
megastep@7
   124
slouken@29
   125
slouken@29
   126
/* Support for hooking when the music has finished */
slouken@29
   127
static void (*music_finished_hook)(void) = NULL;
slouken@29
   128
slouken@29
   129
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   130
{
slouken@29
   131
	SDL_LockAudio();
slouken@29
   132
	music_finished_hook = music_finished;
slouken@29
   133
	SDL_UnlockAudio();
slouken@29
   134
}
slouken@29
   135
slouken@29
   136
slouken@0
   137
/* Mixing function */
slouken@0
   138
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   139
{
slouken@0
   140
	if ( music_playing ) {
megastep@7
   141
		if ( music_stopped ) {
megastep@7
   142
			/* To avoid concurrency problems and the use of mutexes,
megastep@7
   143
			   the music is always stopped from the sound thread */
megastep@7
   144
			lowlevel_halt(); /* This function sets music_playing to NULL */
megastep@7
   145
			return;
slouken@29
   146
		}
megastep@13
   147
		/* Handle fading */
slouken@17
   148
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   149
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@26
   150
				int fade_volume = music_playing->fade_volume;
slouken@26
   151
				int fade_step = music_playing->fade_step;
slouken@26
   152
				int fade_steps = music_playing->fade_steps;
slouken@17
   153
megastep@4
   154
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@29
   155
					Mix_VolumeMusic((fade_volume * (fade_steps-fade_step))
slouken@17
   156
									/ fade_steps);
slouken@17
   157
				} else { /* Fading in */
slouken@17
   158
					Mix_VolumeMusic((fade_volume * fade_step) / fade_steps);
slouken@17
   159
				}
slouken@17
   160
			} else {
slouken@17
   161
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@17
   162
					lowlevel_halt();
megastep@4
   163
					return;
megastep@4
   164
				}
megastep@4
   165
				music_playing->fading = MIX_NO_FADING;
megastep@4
   166
			}
megastep@4
   167
		}
megastep@13
   168
		/* Restart music if it has to loop */
slouken@29
   169
		if ( !Mix_PlayingMusic() ) {
slouken@29
   170
			/* Restart music if it has to loop */
slouken@29
   171
			if ( music_loops && --music_loops ) {
megastep@11
   172
				Mix_RewindMusic();
megastep@11
   173
				if ( lowlevel_play(music_playing) < 0 ) {
megastep@11
   174
					fprintf(stderr,"Warning: Music restart failed.\n");
megastep@11
   175
					music_stopped = 1; /* Something went wrong */
megastep@11
   176
					music_playing->fading = MIX_NO_FADING;
megastep@11
   177
				}
megastep@11
   178
			}
slouken@29
   179
			else if (music_finished_hook) {
slouken@29
   180
			    lowlevel_halt();
slouken@29
   181
			    music_finished_hook();
slouken@29
   182
			    return;
slouken@29
   183
			}
megastep@11
   184
		}
megastep@16
   185
		if ( music_volume <= 0 ) { /* Don't mix if volume is null */
megastep@16
   186
			return;
megastep@16
   187
		}
slouken@0
   188
		switch (music_playing->type) {
slouken@0
   189
#ifdef CMD_MUSIC
slouken@0
   190
			case MUS_CMD:
slouken@0
   191
				/* The playing is done externally */
slouken@0
   192
				break;
slouken@0
   193
#endif
slouken@0
   194
#ifdef WAV_MUSIC
slouken@0
   195
			case MUS_WAV:
slouken@63
   196
				WAVStream_PlaySome(music_playing->data.wave, stream, len);
slouken@0
   197
				break;
slouken@0
   198
#endif
slouken@0
   199
#ifdef MOD_MUSIC
slouken@0
   200
			case MUS_MOD:
slouken@0
   201
				VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   202
				if ( music_swap8 ) {
slouken@0
   203
					Uint8 *dst;
slouken@42
   204
					int i;
slouken@0
   205
slouken@0
   206
					dst = stream;
slouken@0
   207
					for ( i=len; i; --i ) {
slouken@0
   208
						*dst++ ^= 0x80;
slouken@0
   209
					}
slouken@0
   210
				} else
slouken@0
   211
				if ( music_swap16 ) {
slouken@0
   212
					Uint8 *dst, tmp;
slouken@42
   213
					int i;
slouken@0
   214
slouken@0
   215
					dst = stream;
slouken@0
   216
					for ( i=(len/2); i; --i ) {
slouken@0
   217
						tmp = dst[0];
slouken@0
   218
						dst[0] = dst[1];
slouken@0
   219
						dst[1] = tmp;
slouken@0
   220
						dst += 2;
slouken@0
   221
					}
slouken@0
   222
				}
slouken@0
   223
				break;
slouken@0
   224
#endif
slouken@0
   225
#ifdef MID_MUSIC
slouken@0
   226
			case MUS_MID:
slouken@0
   227
				Timidity_PlaySome(stream, len/samplesize);
slouken@0
   228
				break;
slouken@0
   229
#endif
slouken@63
   230
#ifdef OGG_MUSIC
slouken@63
   231
			case MUS_OGG:
slouken@63
   232
				OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   233
				break;
slouken@63
   234
#endif
slouken@0
   235
#ifdef MP3_MUSIC
slouken@26
   236
			case MUS_MP3:
slouken@26
   237
				SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   238
				break;
slouken@0
   239
#endif
slouken@0
   240
			default:
slouken@0
   241
				/* Unknown music type?? */
slouken@0
   242
				break;
slouken@0
   243
		}
slouken@0
   244
	}
slouken@0
   245
}
slouken@0
   246
slouken@0
   247
/* Initialize the music players with a certain desired audio format */
slouken@0
   248
int open_music(SDL_AudioSpec *mixer)
slouken@0
   249
{
slouken@0
   250
	int music_error;
slouken@0
   251
slouken@0
   252
	music_error = 0;
slouken@0
   253
#ifdef WAV_MUSIC
slouken@0
   254
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   255
		++music_error;
slouken@0
   256
	}
slouken@0
   257
#endif
slouken@0
   258
#ifdef MOD_MUSIC
slouken@0
   259
	/* Set the MikMod music format */
slouken@0
   260
	music_swap8 = 0;
slouken@0
   261
	music_swap16 = 0;
slouken@0
   262
	switch (mixer->format) {
slouken@0
   263
slouken@0
   264
		case AUDIO_U8:
slouken@0
   265
		case AUDIO_S8: {
slouken@0
   266
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   267
				music_swap8 = 1;
slouken@0
   268
			}
slouken@0
   269
			md_mode = 0;
slouken@0
   270
		}
slouken@0
   271
		break;
slouken@0
   272
slouken@0
   273
		case AUDIO_S16LSB:
slouken@0
   274
		case AUDIO_S16MSB: {
slouken@0
   275
			/* See if we need to correct MikMod mixing */
slouken@0
   276
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   277
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   278
#else
slouken@0
   279
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   280
#endif
slouken@0
   281
				music_swap16 = 1;
slouken@0
   282
			}
slouken@0
   283
			md_mode = DMODE_16BITS;
slouken@0
   284
		}
slouken@0
   285
		break;
slouken@0
   286
slouken@0
   287
		default: {
slouken@26
   288
			Mix_SetError("Unknown hardware audio format");
slouken@0
   289
			++music_error;
slouken@0
   290
		}
slouken@0
   291
	}
slouken@0
   292
	if ( mixer->channels > 1 ) {
slouken@0
   293
		if ( mixer->channels > 2 ) {
slouken@26
   294
			Mix_SetError("Hardware uses more channels than mixer");
slouken@0
   295
			++music_error;
slouken@0
   296
		}
slouken@0
   297
		md_mode |= DMODE_STEREO;
slouken@0
   298
	}
slouken@26
   299
	samplesize	 = mixer->size/mixer->samples;
slouken@26
   300
	md_mixfreq	 = mixer->freq;
slouken@26
   301
	md_device	  = 0;
slouken@26
   302
	md_volume	  = 96;
slouken@0
   303
	md_musicvolume = 128;
slouken@0
   304
	md_sndfxvolume = 128;
slouken@26
   305
	md_pansep	  = 128;
slouken@26
   306
	md_reverb	  = 0;
slouken@0
   307
	MikMod_RegisterAllLoaders();
slouken@0
   308
	MikMod_RegisterAllDrivers();
slouken@0
   309
	if ( MikMod_Init() ) {
slouken@29
   310
		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   311
		++music_error;
slouken@0
   312
	}
slouken@0
   313
#endif
slouken@0
   314
#ifdef MID_MUSIC
slouken@58
   315
	samplesize	 = mixer->size/mixer->samples;
slouken@0
   316
	if ( Timidity_Init(mixer->freq,
slouken@0
   317
			mixer->format, mixer->channels, mixer->samples) == 0 ) {
slouken@0
   318
		timidity_ok = 1;
slouken@0
   319
	} else {
slouken@0
   320
		timidity_ok = 0;
slouken@0
   321
	}
slouken@0
   322
#endif
slouken@63
   323
#ifdef OGG_MUSIC
slouken@63
   324
	if ( OGG_init(mixer) < 0 ) {
slouken@63
   325
		++music_error;
slouken@63
   326
	}
slouken@63
   327
#endif
slouken@0
   328
#ifdef MP3_MUSIC
slouken@0
   329
	/* Keep a copy of the mixer */
slouken@0
   330
	used_mixer = *mixer;
slouken@0
   331
#endif
slouken@42
   332
	music_playing = NULL;
megastep@7
   333
	music_stopped = 0;
slouken@0
   334
	if ( music_error ) {
slouken@0
   335
		return(-1);
slouken@0
   336
	}
slouken@0
   337
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   338
slouken@26
   339
	/* Calculate the number of ms for each callback */
slouken@44
   340
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   341
slouken@0
   342
	return(0);
slouken@0
   343
}
slouken@0
   344
slouken@0
   345
/* Load a music file */
slouken@0
   346
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   347
{
slouken@0
   348
	FILE *fp;
slouken@52
   349
	Uint8 magic[5];
slouken@0
   350
	Mix_Music *music;
slouken@0
   351
slouken@0
   352
	/* Figure out what kind of file this is */
slouken@0
   353
	fp = fopen(file, "rb");
slouken@0
   354
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   355
		if ( fp != NULL ) {
slouken@0
   356
			fclose(fp);
slouken@0
   357
		}
slouken@26
   358
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   359
		return(NULL);
slouken@0
   360
	}
slouken@0
   361
	magic[4] = '\0';
slouken@0
   362
	fclose(fp);
slouken@0
   363
slouken@0
   364
	/* Allocate memory for the music structure */
slouken@0
   365
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   366
	if ( music == NULL ) {
slouken@26
   367
		Mix_SetError("Out of memory");
slouken@0
   368
		return(NULL);
slouken@0
   369
	}
slouken@0
   370
	music->error = 0;
slouken@0
   371
slouken@0
   372
#ifdef CMD_MUSIC
slouken@0
   373
	if ( music_cmd ) {
slouken@0
   374
		music->type = MUS_CMD;
slouken@0
   375
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   376
		if ( music->data.cmd == NULL ) {
slouken@0
   377
			music->error = 1;
slouken@0
   378
		}
slouken@0
   379
	} else
slouken@0
   380
#endif
slouken@0
   381
#ifdef WAV_MUSIC
slouken@0
   382
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   383
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   384
	 */
slouken@57
   385
	if ( (strcmp((char *)magic, "RIFF") == 0) ||
slouken@57
   386
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   387
		music->type = MUS_WAV;
slouken@57
   388
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   389
		if ( music->data.wave == NULL ) {
slouken@0
   390
			music->error = 1;
slouken@0
   391
		}
slouken@0
   392
	} else
slouken@0
   393
#endif
slouken@0
   394
#ifdef MID_MUSIC
slouken@0
   395
	/* MIDI files have the magic four bytes "MThd" */
slouken@0
   396
	if ( strcmp(magic, "MThd") == 0 ) {
slouken@0
   397
		music->type = MUS_MID;
slouken@0
   398
		if ( timidity_ok ) {
slouken@0
   399
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   400
			if ( music->data.midi == NULL ) {
slouken@26
   401
				Mix_SetError("%s", Timidity_Error());
slouken@0
   402
				music->error = 1;
slouken@0
   403
			}
slouken@0
   404
		}
slouken@0
   405
		else {
slouken@26
   406
			Mix_SetError("%s", Timidity_Error());
slouken@0
   407
			music->error = 1;
slouken@0
   408
		}
slouken@0
   409
	} else
slouken@0
   410
#endif
slouken@63
   411
#ifdef OGG_MUSIC
slouken@63
   412
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@63
   413
	if ( strcmp(magic, "OggS") == 0 ) {
slouken@63
   414
		music->type = MUS_OGG;
slouken@63
   415
		music->data.ogg = OGG_new(file);
slouken@63
   416
		if ( music->data.ogg == NULL ) {
slouken@63
   417
			music->error = 1;
slouken@63
   418
		}
slouken@63
   419
	} else
slouken@63
   420
#endif
slouken@0
   421
#ifdef MP3_MUSIC
slouken@0
   422
	if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
slouken@0
   423
		SMPEG_Info info;
slouken@0
   424
		music->type = MUS_MP3;
slouken@0
   425
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@0
   426
		if(!info.has_audio){
slouken@26
   427
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@0
   428
			music->error = 1;
slouken@0
   429
		}else{
slouken@0
   430
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   431
		}
slouken@0
   432
	} else
slouken@0
   433
#endif
slouken@0
   434
#ifdef MOD_MUSIC
slouken@0
   435
	if ( 1 ) {
slouken@0
   436
		music->type = MUS_MOD;
slouken@0
   437
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   438
		if ( music->data.module == NULL ) {
slouken@29
   439
			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   440
			music->error = 1;
slouken@0
   441
		}
slouken@0
   442
	} else
slouken@0
   443
#endif
slouken@0
   444
	{
slouken@26
   445
		Mix_SetError("Unrecognized music format");
slouken@0
   446
		music->error = 1;
slouken@0
   447
	}
slouken@0
   448
	if ( music->error ) {
slouken@0
   449
		free(music);
slouken@0
   450
		music = NULL;
slouken@0
   451
	}
slouken@0
   452
	return(music);
slouken@0
   453
}
slouken@0
   454
slouken@0
   455
/* Free a music chunk previously loaded */
slouken@0
   456
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   457
{
slouken@0
   458
	if ( music ) {
slouken@0
   459
		/* Caution: If music is playing, mixer will crash */
megastep@7
   460
		if ( music == music_playing && !music_stopped ) {
megastep@4
   461
			if ( music->fading == MIX_FADING_OUT ) {
megastep@4
   462
				/* Wait for the fade out to finish */
slouken@17
   463
				while ( music_playing && !music_stopped && (music_playing->fading == MIX_FADING_OUT) )
megastep@4
   464
					SDL_Delay(100);
megastep@4
   465
			} else {
megastep@4
   466
				Mix_HaltMusic(); /* Stop it immediately */
megastep@4
   467
			}
slouken@0
   468
		}
slouken@0
   469
		switch (music->type) {
slouken@0
   470
#ifdef CMD_MUSIC
slouken@0
   471
			case MUS_CMD:
slouken@0
   472
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   473
				break;
slouken@0
   474
#endif
slouken@0
   475
#ifdef WAV_MUSIC
slouken@0
   476
			case MUS_WAV:
slouken@0
   477
				WAVStream_FreeSong(music->data.wave);
slouken@0
   478
				break;
slouken@0
   479
#endif
slouken@0
   480
#ifdef MOD_MUSIC
slouken@0
   481
			case MUS_MOD:
slouken@0
   482
				MikMod_FreeSong(music->data.module);
slouken@0
   483
				break;
slouken@0
   484
#endif
slouken@0
   485
#ifdef MID_MUSIC
slouken@0
   486
			case MUS_MID:
slouken@0
   487
				Timidity_FreeSong(music->data.midi);
slouken@0
   488
				break;
slouken@0
   489
#endif
slouken@63
   490
#ifdef OGG_MUSIC
slouken@63
   491
			case MUS_OGG:
slouken@63
   492
				OGG_delete(music->data.ogg);
slouken@63
   493
				break;
slouken@63
   494
#endif
slouken@0
   495
#ifdef MP3_MUSIC
slouken@26
   496
			case MUS_MP3:
slouken@0
   497
				SMPEG_delete(music->data.mp3);
slouken@0
   498
				break;
slouken@0
   499
#endif
slouken@0
   500
			default:
slouken@0
   501
				/* Unknown music type?? */
slouken@0
   502
				break;
slouken@0
   503
		}
slouken@0
   504
		free(music);
slouken@0
   505
	}
slouken@0
   506
}
slouken@0
   507
megastep@10
   508
static int lowlevel_play(Mix_Music *music)
slouken@0
   509
{
megastep@10
   510
	if(!music)
slouken@0
   511
		return(-1);
megastep@10
   512
slouken@0
   513
	switch (music->type) {
slouken@0
   514
#ifdef CMD_MUSIC
slouken@0
   515
		case MUS_CMD:
slouken@0
   516
			MusicCMD_SetVolume(music_volume);
slouken@0
   517
			MusicCMD_Start(music->data.cmd);
slouken@0
   518
			break;
slouken@0
   519
#endif
slouken@0
   520
#ifdef WAV_MUSIC
slouken@0
   521
		case MUS_WAV:
slouken@0
   522
			WAVStream_SetVolume(music_volume);
slouken@0
   523
			WAVStream_Start(music->data.wave);
slouken@0
   524
			break;
slouken@0
   525
#endif
slouken@0
   526
#ifdef MOD_MUSIC
slouken@0
   527
		case MUS_MOD:
slouken@42
   528
			Player_SetVolume((SWORD)music_volume);
slouken@0
   529
			Player_Start(music->data.module);
slouken@0
   530
			Player_SetPosition(0);
slouken@0
   531
			break;
slouken@0
   532
#endif
slouken@0
   533
#ifdef MID_MUSIC
slouken@0
   534
		case MUS_MID:
slouken@0
   535
			Timidity_SetVolume(music_volume);
slouken@0
   536
			Timidity_Start(music->data.midi);
slouken@0
   537
			break;
slouken@0
   538
#endif
slouken@63
   539
#ifdef OGG_MUSIC
slouken@63
   540
		case MUS_OGG:
slouken@63
   541
			OGG_setvolume(music->data.ogg, music_volume);
slouken@63
   542
			OGG_play(music->data.ogg);
slouken@63
   543
			break;
slouken@63
   544
#endif
slouken@0
   545
#ifdef MP3_MUSIC
slouken@26
   546
		case MUS_MP3:
slouken@0
   547
			SMPEG_enableaudio(music->data.mp3,1);
slouken@0
   548
			SMPEG_enablevideo(music->data.mp3,0);
slouken@0
   549
			SMPEG_setvolume(music->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
slouken@0
   550
			SMPEG_play(music->data.mp3);
slouken@0
   551
			break;
slouken@0
   552
#endif
slouken@0
   553
		default:
slouken@0
   554
			/* Unknown music type?? */
slouken@0
   555
			return(-1);
slouken@0
   556
	}
megastep@10
   557
	return(0);
megastep@10
   558
}
megastep@10
   559
megastep@10
   560
/* Play a music chunk.  Returns 0, or -1 if there was an error.
megastep@10
   561
*/
megastep@10
   562
int Mix_PlayMusic(Mix_Music *music, int loops)
megastep@10
   563
{
megastep@10
   564
	/* Don't play null pointers :-) */
megastep@10
   565
	if ( music == NULL ) {
megastep@10
   566
		return(-1);
megastep@10
   567
	}
megastep@10
   568
	/* If the current music is fading out, wait for the fade to complete */
megastep@10
   569
	while ( music_playing && !music_stopped && music_playing->fading==MIX_FADING_OUT ) {
megastep@10
   570
		SDL_Delay(100);
megastep@10
   571
	}
megastep@10
   572
slouken@17
   573
	if ( lowlevel_play(music) < 0 ) {
megastep@10
   574
		return(-1);
slouken@26
   575
	}
slouken@0
   576
	music_active = 1;
megastep@7
   577
	music_stopped = 0;
megastep@10
   578
	music_loops = loops;
slouken@0
   579
	music_playing = music;
megastep@4
   580
	music_playing->fading = MIX_NO_FADING;
megastep@4
   581
	return(0);
megastep@4
   582
}
megastep@4
   583
megastep@4
   584
/* Fade in a music over "ms" milliseconds */
megastep@4
   585
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
megastep@4
   586
{
megastep@4
   587
	if ( music && music_volume > 0 ) { /* No need to fade if we can't hear it */
megastep@4
   588
		music->fade_volume = music_volume;
megastep@4
   589
		music_volume = 0;
slouken@17
   590
		if ( Mix_PlayMusic(music, loops) < 0 ) {
megastep@4
   591
			return(-1);
slouken@26
   592
		}
slouken@17
   593
		music_playing->fade_step = 0;
slouken@17
   594
		music_playing->fade_steps = ms/ms_per_step;
megastep@4
   595
		music_playing->fading = MIX_FADING_IN;
megastep@4
   596
	}
slouken@0
   597
	return(0);
slouken@0
   598
}
slouken@0
   599
slouken@0
   600
/* Set the music volume */
slouken@0
   601
int Mix_VolumeMusic(int volume)
slouken@0
   602
{
slouken@0
   603
	int prev_volume;
slouken@0
   604
slouken@0
   605
	prev_volume = music_volume;
megastep@4
   606
	if ( volume < 0 ) {
megastep@4
   607
		volume = 0;
megastep@4
   608
	}
megastep@4
   609
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   610
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   611
	}
megastep@4
   612
	music_volume = volume;
megastep@7
   613
	if ( music_playing && !music_stopped ) {
megastep@4
   614
		switch (music_playing->type) {
slouken@0
   615
#ifdef CMD_MUSIC
megastep@4
   616
		case MUS_CMD:
megastep@4
   617
			MusicCMD_SetVolume(music_volume);
megastep@4
   618
			break;
slouken@0
   619
#endif
slouken@0
   620
#ifdef WAV_MUSIC
megastep@4
   621
		case MUS_WAV:
megastep@4
   622
			WAVStream_SetVolume(music_volume);
megastep@4
   623
			break;
slouken@0
   624
#endif
slouken@0
   625
#ifdef MOD_MUSIC
megastep@4
   626
		case MUS_MOD:
slouken@42
   627
			Player_SetVolume((SWORD)music_volume);
megastep@4
   628
			break;
slouken@0
   629
#endif
slouken@0
   630
#ifdef MID_MUSIC
megastep@4
   631
		case MUS_MID:
megastep@4
   632
			Timidity_SetVolume(music_volume);
megastep@4
   633
			break;
slouken@0
   634
#endif
slouken@63
   635
#ifdef OGG_MUSIC
slouken@63
   636
		case MUS_OGG:
slouken@63
   637
			OGG_setvolume(music_playing->data.ogg, music_volume);
slouken@63
   638
			break;
slouken@63
   639
#endif
slouken@0
   640
#ifdef MP3_MUSIC
megastep@4
   641
		case MUS_MP3:
megastep@4
   642
			SMPEG_setvolume(music_playing->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
megastep@4
   643
			break;
slouken@0
   644
#endif
megastep@4
   645
		default:
megastep@4
   646
			/* Unknown music type?? */
megastep@4
   647
			break;
slouken@0
   648
		}
slouken@0
   649
	}
slouken@0
   650
	return(prev_volume);
slouken@0
   651
}
slouken@0
   652
megastep@7
   653
static void lowlevel_halt(void)
megastep@7
   654
{
megastep@7
   655
	switch (music_playing->type) {
megastep@7
   656
#ifdef CMD_MUSIC
megastep@7
   657
	case MUS_CMD:
megastep@7
   658
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   659
		break;
megastep@7
   660
#endif
megastep@7
   661
#ifdef WAV_MUSIC
megastep@7
   662
	case MUS_WAV:
megastep@7
   663
		WAVStream_Stop();
megastep@7
   664
		break;
megastep@7
   665
#endif
megastep@7
   666
#ifdef MOD_MUSIC
megastep@7
   667
	case MUS_MOD:
megastep@7
   668
		Player_Stop();
megastep@7
   669
		break;
megastep@7
   670
#endif
megastep@7
   671
#ifdef MID_MUSIC
megastep@7
   672
	case MUS_MID:
megastep@7
   673
		Timidity_Stop();
megastep@7
   674
		break;
megastep@7
   675
#endif
slouken@63
   676
#ifdef OGG_MUSIC
slouken@63
   677
	case MUS_OGG:
slouken@63
   678
		OGG_stop(music_playing->data.ogg);
slouken@63
   679
		break;
slouken@63
   680
#endif
megastep@7
   681
#ifdef MP3_MUSIC
megastep@7
   682
	case MUS_MP3:
megastep@7
   683
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   684
		break;
megastep@7
   685
#endif
megastep@7
   686
	default:
megastep@7
   687
		/* Unknown music type?? */
megastep@7
   688
		return;
megastep@7
   689
	}
slouken@17
   690
	if ( music_playing->fading != MIX_NO_FADING ) /* Restore volume */
megastep@7
   691
		music_volume = music_playing->fade_volume;
megastep@7
   692
	music_playing->fading = MIX_NO_FADING;
slouken@17
   693
	music_playing = NULL;
megastep@7
   694
	music_active = 0;
megastep@11
   695
	music_loops = 0;
megastep@7
   696
	music_stopped = 0;
megastep@7
   697
}
megastep@7
   698
slouken@0
   699
/* Halt playing of music */
slouken@0
   700
int Mix_HaltMusic(void)
slouken@0
   701
{
megastep@7
   702
	if ( music_playing && !music_stopped ) {
megastep@7
   703
		/* Mark the music to be stopped from the sound thread */
megastep@7
   704
		music_stopped = 1;
megastep@7
   705
		/* Wait for it to be actually stopped */
slouken@42
   706
		while ( music_playing && music_active )
megastep@7
   707
			SDL_Delay(10);
slouken@0
   708
	}
slouken@0
   709
	return(0);
slouken@0
   710
}
slouken@0
   711
megastep@4
   712
/* Progressively stop the music */
megastep@4
   713
int Mix_FadeOutMusic(int ms)
megastep@4
   714
{
slouken@26
   715
	if ( music_playing && !music_stopped &&
slouken@26
   716
	     (music_playing->fading == MIX_NO_FADING) ) {
slouken@17
   717
		if ( music_volume > 0 ) {
megastep@4
   718
			music_playing->fading = MIX_FADING_OUT;
megastep@4
   719
			music_playing->fade_volume = music_volume;
slouken@26
   720
			music_playing->fade_step = 0;
slouken@26
   721
			music_playing->fade_steps = ms/ms_per_step;
megastep@4
   722
			return(1);
megastep@4
   723
		}
megastep@4
   724
	}
megastep@4
   725
	return(0);
megastep@4
   726
}
megastep@4
   727
megastep@4
   728
Mix_Fading Mix_FadingMusic(void)
megastep@4
   729
{
megastep@7
   730
	if( music_playing && !music_stopped )
megastep@4
   731
		return music_playing->fading;
megastep@4
   732
	return MIX_NO_FADING;
megastep@4
   733
}
megastep@4
   734
slouken@0
   735
/* Pause/Resume the music stream */
slouken@0
   736
void Mix_PauseMusic(void)
slouken@0
   737
{
slouken@17
   738
	if ( music_playing && !music_stopped ) {
megastep@13
   739
		music_active = 0;
slouken@0
   740
	}
slouken@0
   741
}
megastep@4
   742
slouken@0
   743
void Mix_ResumeMusic(void)
slouken@0
   744
{
slouken@17
   745
	if ( music_playing && !music_stopped ) {
megastep@13
   746
		music_active = 1;
slouken@0
   747
	}
slouken@0
   748
}
slouken@0
   749
slouken@0
   750
void Mix_RewindMusic(void)
slouken@0
   751
{
megastep@7
   752
	if ( music_playing && !music_stopped ) {
slouken@0
   753
		switch ( music_playing->type ) {
slouken@29
   754
#ifdef MOD_MUSIC
slouken@29
   755
		case MUS_MOD:
slouken@29
   756
			Player_Start(music_playing->data.module);
slouken@29
   757
			Player_SetPosition(0);
slouken@29
   758
			break;
slouken@29
   759
#endif
slouken@0
   760
#ifdef MP3_MUSIC
slouken@0
   761
		case MUS_MP3:
slouken@0
   762
			SMPEG_rewind(music_playing->data.mp3);
slouken@0
   763
			break;
slouken@0
   764
#endif
slouken@29
   765
		default:
megastep@7
   766
			/* TODO: Implement this for other music backends */
slouken@29
   767
			break;
slouken@0
   768
		}
slouken@0
   769
	}
slouken@0
   770
}
slouken@0
   771
megastep@13
   772
int Mix_PausedMusic(void)
megastep@13
   773
{
slouken@17
   774
	return (music_active == 0);
megastep@13
   775
}
megastep@13
   776
slouken@0
   777
/* Check the status of the music */
megastep@13
   778
int Mix_PlayingMusic(void)
slouken@0
   779
{
megastep@13
   780
	if ( music_playing && ! music_stopped ) {
slouken@0
   781
		switch (music_playing->type) {
slouken@0
   782
#ifdef CMD_MUSIC
slouken@0
   783
			case MUS_CMD:
slouken@0
   784
				if (!MusicCMD_Active(music_playing->data.cmd)) {
megastep@10
   785
					return(0);
slouken@0
   786
				}
slouken@0
   787
				break;
slouken@0
   788
#endif
slouken@0
   789
#ifdef WAV_MUSIC
slouken@0
   790
			case MUS_WAV:
slouken@63
   791
				if ( ! WAVStream_Active(music_playing->data.wave) ) {
megastep@10
   792
					return(0);
slouken@0
   793
				}
slouken@0
   794
				break;
slouken@0
   795
#endif
slouken@0
   796
#ifdef MOD_MUSIC
slouken@0
   797
			case MUS_MOD:
slouken@0
   798
				if ( ! Player_Active() ) {
megastep@10
   799
					return(0);
slouken@0
   800
				}
slouken@0
   801
				break;
slouken@0
   802
#endif
slouken@0
   803
#ifdef MID_MUSIC
slouken@0
   804
			case MUS_MID:
slouken@0
   805
				if ( ! Timidity_Active() ) {
megastep@10
   806
					return(0);
slouken@0
   807
				}
slouken@0
   808
				break;
slouken@0
   809
#endif
slouken@63
   810
#ifdef OGG_MUSIC
slouken@63
   811
			case MUS_OGG:
slouken@63
   812
				if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@63
   813
					return(0);
slouken@63
   814
				}
slouken@63
   815
				break;
slouken@63
   816
#endif
slouken@0
   817
#ifdef MP3_MUSIC
slouken@0
   818
			case MUS_MP3:
slouken@0
   819
				if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
megastep@10
   820
					return(0);
slouken@0
   821
				break;
slouken@0
   822
#endif
slouken@29
   823
			default:
slouken@29
   824
				break;
slouken@0
   825
		}
megastep@13
   826
		return(1);
slouken@0
   827
	}
megastep@13
   828
	return(0);
slouken@0
   829
}
slouken@0
   830
slouken@0
   831
/* Set the external music playback command */
slouken@0
   832
int Mix_SetMusicCMD(const char *command)
slouken@0
   833
{
slouken@0
   834
	Mix_HaltMusic();
slouken@0
   835
	if ( music_cmd ) {
slouken@0
   836
		free(music_cmd);
slouken@0
   837
		music_cmd = NULL;
slouken@0
   838
	}
slouken@0
   839
	if ( command ) {
slouken@0
   840
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
   841
		if ( music_cmd == NULL ) {
slouken@0
   842
			return(-1);
slouken@0
   843
		}
slouken@0
   844
		strcpy(music_cmd, command);
slouken@0
   845
	}
slouken@0
   846
	return(0);
slouken@0
   847
}
slouken@0
   848
slouken@0
   849
/* Uninitialize the music players */
slouken@0
   850
void close_music(void)
slouken@0
   851
{
slouken@0
   852
	Mix_HaltMusic();
slouken@0
   853
#ifdef CMD_MUSIC
slouken@0
   854
	Mix_SetMusicCMD(NULL);
slouken@0
   855
#endif
slouken@0
   856
#ifdef MOD_MUSIC
slouken@0
   857
	MikMod_Exit();
slouken@0
   858
#endif
slouken@0
   859
}
slouken@0
   860