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