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