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