music.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Dec 2004 20:08:57 +0000
changeset 256 6d7e102500c9
parent 254 1ad5ff083036
child 260 87f2a3d6e4e5
permissions -rw-r--r--
Added SDL_RWops support for reading MP3 files
slouken@0
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@241
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@138
     5
    This library is free software; you can redistribute it and/or
slouken@138
     6
    modify it under the terms of the GNU Library General Public
slouken@138
     7
    License as published by the Free Software Foundation; either
slouken@138
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@138
    10
    This library is distributed in the hope that it will be useful,
slouken@138
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@138
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@138
    13
    Library General Public License for more details.
slouken@0
    14
slouken@138
    15
    You should have received a copy of the GNU Library General Public
slouken@138
    16
    License along with this library; if not, write to the Free
slouken@138
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@138
    19
    Sam Lantinga
slouken@138
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@140
    23
/* $Id$ */
slouken@138
    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@245
    34
#define SDL_SURROUND
slouken@245
    35
slouken@245
    36
#ifdef SDL_SURROUND
slouken@245
    37
#define MAX_OUTPUT_CHANNELS 6
slouken@245
    38
#else
slouken@245
    39
#define MAX_OUTPUT_CHANNELS 2
slouken@245
    40
#endif
slouken@245
    41
slouken@0
    42
/* The music command hack is UNIX specific */
slouken@0
    43
#ifndef unix
slouken@0
    44
#undef CMD_MUSIC
slouken@0
    45
#endif
slouken@0
    46
slouken@0
    47
#ifdef CMD_MUSIC
slouken@0
    48
#include "music_cmd.h"
slouken@0
    49
#endif
slouken@0
    50
#ifdef WAV_MUSIC
slouken@0
    51
#include "wavestream.h"
slouken@0
    52
#endif
slouken@249
    53
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@29
    54
#  include "mikmod.h"
slouken@29
    55
#  if defined(LIBMIKMOD_VERSION)                /* libmikmod 3.1.8 */
slouken@29
    56
#    define UNIMOD			MODULE
slouken@29
    57
#    define MikMod_Init()		MikMod_Init(NULL)
slouken@29
    58
#    define MikMod_LoadSong(a,b)	Player_Load(a,b,0)
slouken@254
    59
#    ifndef LIBMIKMOD_MUSIC
slouken@254
    60
#    define MikMod_LoadSongRW(a,b)	Player_LoadRW(a,b,0)
slouken@249
    61
#    endif
slouken@29
    62
#    define MikMod_FreeSong		Player_Free
slouken@29
    63
     extern int MikMod_errno;
slouken@29
    64
#  else                                        /* old MikMod 3.0.3 */
slouken@29
    65
#    define MikMod_strerror(x)		_mm_errmsg[x])
slouken@29
    66
#    define MikMod_errno		_mm_errno
slouken@29
    67
#  endif
slouken@0
    68
#endif
slouken@0
    69
#ifdef MID_MUSIC
slouken@106
    70
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    71
#    include "timidity.h"
slouken@106
    72
#  endif
slouken@106
    73
#  ifdef USE_NATIVE_MIDI
slouken@106
    74
#    include "native_midi.h"
slouken@106
    75
#  endif
slouken@106
    76
#  if defined(USE_TIMIDITY_MIDI) && defined(USE_NATIVE_MIDI)
slouken@106
    77
#    define MIDI_ELSE	else
slouken@106
    78
#  else
slouken@106
    79
#    define MIDI_ELSE
slouken@106
    80
#  endif
slouken@0
    81
#endif
slouken@63
    82
#ifdef OGG_MUSIC
slouken@63
    83
#include "music_ogg.h"
slouken@63
    84
#endif
slouken@0
    85
#ifdef MP3_MUSIC
slouken@83
    86
#include "smpeg.h"
slouken@0
    87
slouken@0
    88
static SDL_AudioSpec used_mixer;
slouken@0
    89
#endif
slouken@0
    90
slouken@42
    91
int volatile music_active = 1;
slouken@42
    92
static int volatile music_stopped = 0;
slouken@0
    93
static int music_loops = 0;
slouken@0
    94
static char *music_cmd = NULL;
slouken@42
    95
static Mix_Music * volatile music_playing = NULL;
slouken@17
    96
static int music_volume = MIX_MAX_VOLUME;
slouken@0
    97
static int music_swap8;
slouken@0
    98
static int music_swap16;
megastep@4
    99
slouken@0
   100
struct _Mix_Music {
slouken@177
   101
	Mix_MusicType type;
slouken@0
   102
	union {
slouken@0
   103
#ifdef CMD_MUSIC
slouken@0
   104
		MusicCMD *cmd;
slouken@0
   105
#endif
slouken@0
   106
#ifdef WAV_MUSIC
slouken@0
   107
		WAVStream *wave;
slouken@0
   108
#endif
slouken@249
   109
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   110
		UNIMOD *module;
slouken@0
   111
#endif
slouken@0
   112
#ifdef MID_MUSIC
slouken@106
   113
#ifdef USE_TIMIDITY_MIDI
slouken@0
   114
		MidiSong *midi;
slouken@106
   115
#endif
slouken@98
   116
#ifdef USE_NATIVE_MIDI
slouken@98
   117
		NativeMidiSong *nativemidi;
slouken@98
   118
#endif
slouken@0
   119
#endif
slouken@63
   120
#ifdef OGG_MUSIC
slouken@63
   121
		OGG_music *ogg;
slouken@63
   122
#endif
slouken@0
   123
#ifdef MP3_MUSIC
slouken@0
   124
		SMPEG *mp3;
slouken@0
   125
#endif
slouken@0
   126
	} data;
megastep@4
   127
	Mix_Fading fading;
slouken@17
   128
	int fade_step;
slouken@17
   129
	int fade_steps;
slouken@0
   130
	int error;
slouken@0
   131
};
slouken@29
   132
#ifdef MID_MUSIC
slouken@106
   133
#ifdef USE_TIMIDITY_MIDI
slouken@0
   134
static int timidity_ok;
slouken@142
   135
static int samplesize;
slouken@106
   136
#endif
slouken@98
   137
#ifdef USE_NATIVE_MIDI
slouken@98
   138
static int native_midi_ok;
slouken@98
   139
#endif
slouken@29
   140
#endif
slouken@0
   141
slouken@245
   142
/* Reference for converting mikmod output to 4/6 channels */
slouken@245
   143
static int current_output_channels;
slouken@245
   144
static Uint16 current_output_format;
slouken@245
   145
slouken@17
   146
/* Used to calculate fading steps */
slouken@17
   147
static int ms_per_step;
slouken@17
   148
megastep@10
   149
/* Local low-level functions prototypes */
icculus@237
   150
static void music_internal_initialize_volume(void);
slouken@173
   151
static void music_internal_volume(int volume);
slouken@173
   152
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   153
static int  music_internal_position(double position);
slouken@173
   154
static int  music_internal_playing();
slouken@173
   155
static void music_internal_halt(void);
megastep@7
   156
slouken@29
   157
slouken@29
   158
/* Support for hooking when the music has finished */
slouken@29
   159
static void (*music_finished_hook)(void) = NULL;
slouken@29
   160
slouken@29
   161
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   162
{
slouken@29
   163
	SDL_LockAudio();
slouken@29
   164
	music_finished_hook = music_finished;
slouken@29
   165
	SDL_UnlockAudio();
slouken@29
   166
}
slouken@29
   167
slouken@29
   168
slouken@0
   169
/* Mixing function */
slouken@0
   170
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   171
{
slouken@173
   172
	if ( music_playing && music_active ) {
megastep@13
   173
		/* Handle fading */
slouken@17
   174
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   175
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@173
   176
				int volume;
slouken@26
   177
				int fade_step = music_playing->fade_step;
slouken@26
   178
				int fade_steps = music_playing->fade_steps;
slouken@17
   179
megastep@4
   180
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   181
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@17
   182
				} else { /* Fading in */
slouken@173
   183
					volume = (music_volume * fade_step) / fade_steps;
slouken@17
   184
				}
slouken@173
   185
				music_internal_volume(volume);
slouken@17
   186
			} else {
slouken@17
   187
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   188
					music_internal_halt();
slouken@173
   189
					if ( music_finished_hook ) {
slouken@173
   190
						music_finished_hook();
slouken@173
   191
					}
megastep@4
   192
					return;
megastep@4
   193
				}
megastep@4
   194
				music_playing->fading = MIX_NO_FADING;
megastep@4
   195
			}
megastep@4
   196
		}
megastep@13
   197
		/* Restart music if it has to loop */
slouken@173
   198
		if ( !music_internal_playing() ) {
slouken@173
   199
			/* Restart music if it has to loop at a high level */
slouken@29
   200
			if ( music_loops && --music_loops ) {
slouken@213
   201
				Mix_Fading current_fade = music_playing->fading;
slouken@173
   202
				music_internal_play(music_playing, 0.0);
slouken@213
   203
				music_playing->fading = current_fade;
slouken@173
   204
			} else {
slouken@173
   205
				music_internal_halt();
slouken@173
   206
				if ( music_finished_hook ) {
slouken@173
   207
					music_finished_hook();
megastep@11
   208
				}
slouken@173
   209
				return;
megastep@11
   210
			}
megastep@16
   211
		}
slouken@0
   212
		switch (music_playing->type) {
slouken@0
   213
#ifdef CMD_MUSIC
slouken@0
   214
			case MUS_CMD:
slouken@0
   215
				/* The playing is done externally */
slouken@0
   216
				break;
slouken@0
   217
#endif
slouken@0
   218
#ifdef WAV_MUSIC
slouken@0
   219
			case MUS_WAV:
slouken@64
   220
				WAVStream_PlaySome(stream, len);
slouken@0
   221
				break;
slouken@0
   222
#endif
slouken@249
   223
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   224
			case MUS_MOD:
slouken@245
   225
				if (current_output_channels > 2) {
slouken@245
   226
					int small_len = 2 * len / current_output_channels;
slouken@245
   227
					int i;
slouken@245
   228
					Uint8 *src, *dst;
slouken@245
   229
slouken@245
   230
					VC_WriteBytes((SBYTE *)stream, small_len);
slouken@245
   231
					/* and extend to len by copying channels */
slouken@245
   232
					src = stream + small_len;
slouken@245
   233
					dst = stream + len;
slouken@245
   234
slouken@245
   235
					switch (current_output_format & 0xFF) {
slouken@245
   236
						case 8:
slouken@245
   237
							for ( i=small_len/2; i; --i ) {
slouken@245
   238
								src -= 2;
slouken@245
   239
								dst -= current_output_channels;
slouken@245
   240
								dst[0] = src[0];
slouken@245
   241
								dst[1] = src[1];
slouken@245
   242
								dst[2] = src[0];
slouken@245
   243
								dst[3] = src[1];
slouken@245
   244
								if (current_output_channels == 6) {
slouken@245
   245
									dst[4] = src[0];
slouken@245
   246
									dst[5] = src[1];
slouken@245
   247
								}
slouken@245
   248
							}
slouken@245
   249
							break;
slouken@245
   250
						case 16:
slouken@245
   251
							for ( i=small_len/4; i; --i ) {
slouken@245
   252
								src -= 4;
slouken@245
   253
								dst -= 2 * current_output_channels;
slouken@245
   254
								dst[0] = src[0];
slouken@245
   255
								dst[1] = src[1];
slouken@245
   256
								dst[2] = src[2];
slouken@245
   257
								dst[3] = src[3];
slouken@245
   258
								dst[4] = src[0];
slouken@245
   259
								dst[5] = src[1];
slouken@245
   260
								dst[6] = src[2];
slouken@245
   261
								dst[7] = src[3];
slouken@245
   262
								if (current_output_channels == 6) {
slouken@245
   263
									dst[8] = src[0];
slouken@245
   264
									dst[9] = src[1];
slouken@245
   265
									dst[10] = src[2];
slouken@245
   266
									dst[11] = src[3];
slouken@245
   267
								}
slouken@245
   268
							}
slouken@245
   269
							break;
slouken@245
   270
					}
slouken@245
   271
slouken@245
   272
slouken@245
   273
slouken@245
   274
				}
slouken@245
   275
				else VC_WriteBytes((SBYTE *)stream, len);
slouken@0
   276
				if ( music_swap8 ) {
slouken@0
   277
					Uint8 *dst;
slouken@42
   278
					int i;
slouken@0
   279
slouken@0
   280
					dst = stream;
slouken@0
   281
					for ( i=len; i; --i ) {
slouken@0
   282
						*dst++ ^= 0x80;
slouken@0
   283
					}
slouken@0
   284
				} else
slouken@0
   285
				if ( music_swap16 ) {
slouken@0
   286
					Uint8 *dst, tmp;
slouken@42
   287
					int i;
slouken@0
   288
slouken@0
   289
					dst = stream;
slouken@0
   290
					for ( i=(len/2); i; --i ) {
slouken@0
   291
						tmp = dst[0];
slouken@0
   292
						dst[0] = dst[1];
slouken@0
   293
						dst[1] = tmp;
slouken@0
   294
						dst += 2;
slouken@0
   295
					}
slouken@0
   296
				}
slouken@0
   297
				break;
slouken@0
   298
#endif
slouken@0
   299
#ifdef MID_MUSIC
slouken@106
   300
#ifdef USE_TIMIDITY_MIDI
slouken@0
   301
			case MUS_MID:
slouken@98
   302
				if ( timidity_ok ) {
slouken@98
   303
					int samples = len / samplesize;
slouken@98
   304
  					Timidity_PlaySome(stream, samples);
slouken@98
   305
				}
slouken@0
   306
				break;
slouken@0
   307
#endif
slouken@106
   308
#endif
slouken@63
   309
#ifdef OGG_MUSIC
slouken@63
   310
			case MUS_OGG:
slouken@63
   311
				OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   312
				break;
slouken@63
   313
#endif
slouken@0
   314
#ifdef MP3_MUSIC
slouken@26
   315
			case MUS_MP3:
slouken@26
   316
				SMPEG_playAudio(music_playing->data.mp3, stream, len);
slouken@0
   317
				break;
slouken@0
   318
#endif
slouken@0
   319
			default:
slouken@0
   320
				/* Unknown music type?? */
slouken@0
   321
				break;
slouken@0
   322
		}
slouken@0
   323
	}
slouken@0
   324
}
slouken@0
   325
slouken@0
   326
/* Initialize the music players with a certain desired audio format */
slouken@0
   327
int open_music(SDL_AudioSpec *mixer)
slouken@0
   328
{
slouken@0
   329
	int music_error;
slouken@0
   330
slouken@0
   331
	music_error = 0;
slouken@0
   332
#ifdef WAV_MUSIC
slouken@0
   333
	if ( WAVStream_Init(mixer) < 0 ) {
slouken@0
   334
		++music_error;
slouken@0
   335
	}
slouken@0
   336
#endif
slouken@249
   337
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   338
	/* Set the MikMod music format */
slouken@0
   339
	music_swap8 = 0;
slouken@0
   340
	music_swap16 = 0;
slouken@0
   341
	switch (mixer->format) {
slouken@0
   342
slouken@0
   343
		case AUDIO_U8:
slouken@0
   344
		case AUDIO_S8: {
slouken@0
   345
			if ( mixer->format == AUDIO_S8 ) {
slouken@0
   346
				music_swap8 = 1;
slouken@0
   347
			}
slouken@0
   348
			md_mode = 0;
slouken@0
   349
		}
slouken@0
   350
		break;
slouken@0
   351
slouken@0
   352
		case AUDIO_S16LSB:
slouken@0
   353
		case AUDIO_S16MSB: {
slouken@0
   354
			/* See if we need to correct MikMod mixing */
slouken@0
   355
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   356
			if ( mixer->format == AUDIO_S16MSB ) {
slouken@0
   357
#else
slouken@0
   358
			if ( mixer->format == AUDIO_S16LSB ) {
slouken@0
   359
#endif
slouken@0
   360
				music_swap16 = 1;
slouken@0
   361
			}
slouken@0
   362
			md_mode = DMODE_16BITS;
slouken@0
   363
		}
slouken@0
   364
		break;
slouken@0
   365
slouken@0
   366
		default: {
slouken@26
   367
			Mix_SetError("Unknown hardware audio format");
slouken@0
   368
			++music_error;
slouken@0
   369
		}
slouken@0
   370
	}
slouken@245
   371
	current_output_channels = mixer->channels;
slouken@245
   372
	current_output_format = mixer->format;
slouken@0
   373
	if ( mixer->channels > 1 ) {
slouken@245
   374
		if ( mixer->channels > MAX_OUTPUT_CHANNELS ) {
slouken@26
   375
			Mix_SetError("Hardware uses more channels than mixer");
slouken@0
   376
			++music_error;
slouken@0
   377
		}
slouken@0
   378
		md_mode |= DMODE_STEREO;
slouken@0
   379
	}
slouken@26
   380
	md_mixfreq	 = mixer->freq;
slouken@26
   381
	md_device	  = 0;
slouken@26
   382
	md_volume	  = 96;
slouken@0
   383
	md_musicvolume = 128;
slouken@0
   384
	md_sndfxvolume = 128;
slouken@26
   385
	md_pansep	  = 128;
slouken@26
   386
	md_reverb	  = 0;
slouken@249
   387
#ifdef LIBMIKMOD_MUSIC
slouken@249
   388
	md_mode |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
slouken@249
   389
#endif
slouken@249
   390
#ifdef LIBMIKMOD_MUSIC
slouken@249
   391
	if(!MikMod_InfoDriver())
slouken@249
   392
#endif
slouken@249
   393
	MikMod_RegisterAllDrivers();
slouken@249
   394
#ifdef LIBMIKMOD_MUSIC
slouken@249
   395
	if(!MikMod_InfoLoader())
slouken@249
   396
#endif
slouken@0
   397
	MikMod_RegisterAllLoaders();
slouken@0
   398
	if ( MikMod_Init() ) {
slouken@29
   399
		Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   400
		++music_error;
slouken@0
   401
	}
slouken@0
   402
#endif
slouken@0
   403
#ifdef MID_MUSIC
slouken@106
   404
#ifdef USE_TIMIDITY_MIDI
slouken@142
   405
	samplesize = mixer->size / mixer->samples;
slouken@100
   406
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@102
   407
	                    mixer->channels, mixer->samples) == 0 ) {
slouken@100
   408
		timidity_ok = 1;
slouken@0
   409
	} else {
slouken@0
   410
		timidity_ok = 0;
slouken@0
   411
	}
slouken@106
   412
#endif
slouken@100
   413
#ifdef USE_NATIVE_MIDI
slouken@106
   414
#ifdef USE_TIMIDITY_MIDI
slouken@100
   415
	native_midi_ok = !timidity_ok;
slouken@106
   416
	if ( native_midi_ok )
slouken@106
   417
#endif
slouken@101
   418
		native_midi_ok = native_midi_detect();
slouken@100
   419
#endif
slouken@0
   420
#endif
slouken@63
   421
#ifdef OGG_MUSIC
slouken@63
   422
	if ( OGG_init(mixer) < 0 ) {
slouken@63
   423
		++music_error;
slouken@63
   424
	}
slouken@63
   425
#endif
slouken@0
   426
#ifdef MP3_MUSIC
slouken@0
   427
	/* Keep a copy of the mixer */
slouken@0
   428
	used_mixer = *mixer;
slouken@0
   429
#endif
slouken@42
   430
	music_playing = NULL;
megastep@7
   431
	music_stopped = 0;
slouken@0
   432
	if ( music_error ) {
slouken@0
   433
		return(-1);
slouken@0
   434
	}
slouken@0
   435
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   436
slouken@26
   437
	/* Calculate the number of ms for each callback */
slouken@44
   438
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   439
slouken@0
   440
	return(0);
slouken@0
   441
}
slouken@0
   442
slouken@125
   443
/* Portable case-insensitive string compare function */
slouken@125
   444
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   445
{
slouken@125
   446
	while ( *str1 && *str2 ) {
slouken@125
   447
		if ( toupper((unsigned char)*str1) !=
slouken@125
   448
		     toupper((unsigned char)*str2) )
slouken@125
   449
			break;
slouken@125
   450
		++str1;
slouken@125
   451
		++str2;
slouken@125
   452
	}
slouken@125
   453
	return (!*str1 && !*str2);
slouken@125
   454
}
slouken@125
   455
slouken@0
   456
/* Load a music file */
slouken@0
   457
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   458
{
slouken@0
   459
	FILE *fp;
slouken@125
   460
	char *ext;
slouken@245
   461
	Uint8 magic[5], moremagic[9];
slouken@0
   462
	Mix_Music *music;
slouken@0
   463
slouken@0
   464
	/* Figure out what kind of file this is */
slouken@0
   465
	fp = fopen(file, "rb");
slouken@0
   466
	if ( (fp == NULL) || !fread(magic, 4, 1, fp) ) {
slouken@0
   467
		if ( fp != NULL ) {
slouken@0
   468
			fclose(fp);
slouken@0
   469
		}
slouken@26
   470
		Mix_SetError("Couldn't read from '%s'", file);
slouken@0
   471
		return(NULL);
slouken@0
   472
	}
slouken@245
   473
	if (!fread(moremagic, 8, 1, fp)) {
slouken@245
   474
		Mix_SetError("Couldn't read from '%s'", file);
slouken@245
   475
		return(NULL);
slouken@245
   476
	}
slouken@0
   477
	magic[4] = '\0';
slouken@245
   478
	moremagic[8] = '\0';
slouken@0
   479
	fclose(fp);
slouken@0
   480
slouken@125
   481
	/* Figure out the file extension, so we can determine the type */
slouken@125
   482
	ext = strrchr(file, '.');
slouken@125
   483
	if ( ext ) ++ext; /* skip the dot in the extension */
slouken@125
   484
slouken@0
   485
	/* Allocate memory for the music structure */
slouken@0
   486
	music = (Mix_Music *)malloc(sizeof(Mix_Music));
slouken@0
   487
	if ( music == NULL ) {
slouken@26
   488
		Mix_SetError("Out of memory");
slouken@0
   489
		return(NULL);
slouken@0
   490
	}
slouken@0
   491
	music->error = 0;
slouken@0
   492
slouken@0
   493
#ifdef CMD_MUSIC
slouken@0
   494
	if ( music_cmd ) {
slouken@0
   495
		music->type = MUS_CMD;
slouken@0
   496
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@0
   497
		if ( music->data.cmd == NULL ) {
slouken@0
   498
			music->error = 1;
slouken@0
   499
		}
slouken@0
   500
	} else
slouken@0
   501
#endif
slouken@0
   502
#ifdef WAV_MUSIC
slouken@0
   503
	/* WAVE files have the magic four bytes "RIFF"
slouken@0
   504
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF"
slouken@0
   505
	 */
slouken@135
   506
	if ( (ext && MIX_string_equals(ext, "WAV")) ||
slouken@245
   507
	     ((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@57
   508
	     (strcmp((char *)magic, "FORM") == 0) ) {
slouken@0
   509
		music->type = MUS_WAV;
slouken@57
   510
		music->data.wave = WAVStream_LoadSong(file, (char *)magic);
slouken@0
   511
		if ( music->data.wave == NULL ) {
slouken@135
   512
		  	Mix_SetError("Unable to load WAV file");
slouken@0
   513
			music->error = 1;
slouken@0
   514
		}
slouken@0
   515
	} else
slouken@0
   516
#endif
slouken@0
   517
#ifdef MID_MUSIC
slouken@0
   518
	/* MIDI files have the magic four bytes "MThd" */
slouken@135
   519
	if ( (ext && MIX_string_equals(ext, "MID")) ||
slouken@135
   520
	     (ext && MIX_string_equals(ext, "MIDI")) ||
slouken@245
   521
	     strcmp((char *)magic, "MThd") == 0  ||
slouken@245
   522
	     ( strcmp((char *)magic, "RIFF") == 0  &&
slouken@245
   523
	  	strcmp((char *)(moremagic+4), "RMID") == 0 ) ) {
slouken@0
   524
		music->type = MUS_MID;
slouken@98
   525
#ifdef USE_NATIVE_MIDI
slouken@98
   526
  		if ( native_midi_ok ) {
slouken@98
   527
  			music->data.nativemidi = native_midi_loadsong((char *)file);
slouken@98
   528
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   529
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   530
			  	music->error = 1;
slouken@98
   531
			}
slouken@106
   532
	  	} MIDI_ELSE
slouken@98
   533
#endif
slouken@106
   534
#ifdef USE_TIMIDITY_MIDI
slouken@0
   535
		if ( timidity_ok ) {
slouken@0
   536
			music->data.midi = Timidity_LoadSong((char *)file);
slouken@0
   537
			if ( music->data.midi == NULL ) {
slouken@26
   538
				Mix_SetError("%s", Timidity_Error());
slouken@0
   539
				music->error = 1;
slouken@0
   540
			}
slouken@98
   541
		} else {
slouken@26
   542
			Mix_SetError("%s", Timidity_Error());
slouken@0
   543
			music->error = 1;
slouken@0
   544
		}
slouken@106
   545
#endif
slouken@0
   546
	} else
slouken@0
   547
#endif
slouken@63
   548
#ifdef OGG_MUSIC
slouken@63
   549
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@135
   550
	if ( (ext && MIX_string_equals(ext, "OGG")) ||
slouken@133
   551
	     strcmp((char *)magic, "OggS") == 0 ) {
slouken@63
   552
		music->type = MUS_OGG;
slouken@63
   553
		music->data.ogg = OGG_new(file);
slouken@63
   554
		if ( music->data.ogg == NULL ) {
slouken@63
   555
			music->error = 1;
slouken@63
   556
		}
slouken@63
   557
	} else
slouken@63
   558
#endif
slouken@0
   559
#ifdef MP3_MUSIC
slouken@135
   560
	if ( (ext && MIX_string_equals(ext, "MPG")) ||
icculus@186
   561
	     (ext && MIX_string_equals(ext, "MP3")) ||
slouken@135
   562
	     (ext && MIX_string_equals(ext, "MPEG")) ||
slouken@256
   563
	     (magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0) ) {
slouken@0
   564
		SMPEG_Info info;
slouken@0
   565
		music->type = MUS_MP3;
slouken@0
   566
		music->data.mp3 = SMPEG_new(file, &info, 0);
slouken@256
   567
		if ( !info.has_audio ) {
slouken@26
   568
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@0
   569
			music->error = 1;
slouken@256
   570
		} else {
slouken@0
   571
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@0
   572
		}
slouken@0
   573
	} else
slouken@0
   574
#endif
slouken@249
   575
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   576
	if ( 1 ) {
slouken@0
   577
		music->type = MUS_MOD;
slouken@0
   578
		music->data.module = MikMod_LoadSong((char *)file, 64);
slouken@0
   579
		if ( music->data.module == NULL ) {
slouken@29
   580
			Mix_SetError("%s", MikMod_strerror(MikMod_errno));
slouken@0
   581
			music->error = 1;
slouken@126
   582
		} else {
slouken@126
   583
			/* Stop implicit looping, fade out and other flags. */
slouken@126
   584
			music->data.module->extspd  = 1;
slouken@126
   585
			music->data.module->panflag = 1;
slouken@126
   586
			music->data.module->wrap    = 0;
slouken@126
   587
			music->data.module->loop    = 0;
slouken@129
   588
#if 0 /* Don't set fade out by default - unfortunately there's no real way
slouken@129
   589
         to query the status of the song or set trigger actions.  Hum. */
slouken@126
   590
			music->data.module->fadeout = 1;
slouken@129
   591
#endif
slouken@0
   592
		}
slouken@0
   593
	} else
slouken@0
   594
#endif
slouken@0
   595
	{
slouken@26
   596
		Mix_SetError("Unrecognized music format");
slouken@0
   597
		music->error = 1;
slouken@0
   598
	}
slouken@0
   599
	if ( music->error ) {
slouken@0
   600
		free(music);
slouken@0
   601
		music = NULL;
slouken@0
   602
	}
slouken@0
   603
	return(music);
slouken@0
   604
}
slouken@0
   605
slouken@0
   606
/* Free a music chunk previously loaded */
slouken@0
   607
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   608
{
slouken@0
   609
	if ( music ) {
slouken@173
   610
		/* Stop the music if it's currently playing */
slouken@173
   611
		SDL_LockAudio();
slouken@173
   612
		if ( music == music_playing ) {
slouken@173
   613
			/* Wait for any fade out to finish */
slouken@173
   614
			while ( music->fading == MIX_FADING_OUT ) {
slouken@173
   615
				SDL_UnlockAudio();
slouken@173
   616
				SDL_Delay(100);
slouken@173
   617
				SDL_LockAudio();
slouken@173
   618
			}
slouken@173
   619
			if ( music == music_playing ) {
slouken@173
   620
				music_internal_halt();
megastep@4
   621
			}
slouken@0
   622
		}
slouken@173
   623
		SDL_UnlockAudio();
slouken@0
   624
		switch (music->type) {
slouken@0
   625
#ifdef CMD_MUSIC
slouken@0
   626
			case MUS_CMD:
slouken@0
   627
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   628
				break;
slouken@0
   629
#endif
slouken@0
   630
#ifdef WAV_MUSIC
slouken@0
   631
			case MUS_WAV:
slouken@0
   632
				WAVStream_FreeSong(music->data.wave);
slouken@0
   633
				break;
slouken@0
   634
#endif
slouken@249
   635
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
   636
			case MUS_MOD:
slouken@0
   637
				MikMod_FreeSong(music->data.module);
slouken@0
   638
				break;
slouken@0
   639
#endif
slouken@0
   640
#ifdef MID_MUSIC
slouken@0
   641
			case MUS_MID:
slouken@98
   642
#ifdef USE_NATIVE_MIDI
slouken@98
   643
  				if ( native_midi_ok ) {
slouken@98
   644
					native_midi_freesong(music->data.nativemidi);
slouken@106
   645
				} MIDI_ELSE
slouken@98
   646
#endif
slouken@106
   647
#ifdef USE_TIMIDITY_MIDI
slouken@98
   648
				if ( timidity_ok ) {
slouken@98
   649
					Timidity_FreeSong(music->data.midi);
slouken@98
   650
				}
slouken@106
   651
#endif
slouken@0
   652
				break;
slouken@0
   653
#endif
slouken@63
   654
#ifdef OGG_MUSIC
slouken@63
   655
			case MUS_OGG:
slouken@63
   656
				OGG_delete(music->data.ogg);
slouken@63
   657
				break;
slouken@63
   658
#endif
slouken@0
   659
#ifdef MP3_MUSIC
slouken@26
   660
			case MUS_MP3:
slouken@0
   661
				SMPEG_delete(music->data.mp3);
slouken@0
   662
				break;
slouken@0
   663
#endif
slouken@0
   664
			default:
slouken@0
   665
				/* Unknown music type?? */
slouken@0
   666
				break;
slouken@0
   667
		}
slouken@0
   668
		free(music);
slouken@0
   669
	}
slouken@0
   670
}
slouken@0
   671
slouken@177
   672
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   673
   music, if 'music' is NULL.
slouken@177
   674
*/
slouken@177
   675
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   676
{
slouken@177
   677
	Mix_MusicType type = MUS_NONE;
slouken@177
   678
slouken@177
   679
	if ( music ) {
slouken@177
   680
		type = music->type;
slouken@177
   681
	} else {
slouken@177
   682
		SDL_LockAudio();
slouken@177
   683
		if ( music_playing ) {
slouken@177
   684
			type = music_playing->type;
slouken@177
   685
		}
slouken@177
   686
		SDL_UnlockAudio();
slouken@177
   687
	}
slouken@177
   688
	return(type);
slouken@177
   689
}
slouken@177
   690
slouken@173
   691
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   692
 */
slouken@173
   693
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   694
{
slouken@173
   695
	int retval = 0;
megastep@10
   696
slouken@173
   697
	/* Note the music we're playing */
slouken@173
   698
	if ( music_playing ) {
slouken@173
   699
		music_internal_halt();
slouken@173
   700
	}
slouken@173
   701
	music_playing = music;
slouken@173
   702
slouken@173
   703
	/* Set the initial volume */
icculus@237
   704
	if ( music->type != MUS_MOD ) {
icculus@237
   705
		music_internal_initialize_volume();
slouken@173
   706
	}
slouken@173
   707
slouken@173
   708
	/* Set up for playback */
slouken@0
   709
	switch (music->type) {
slouken@0
   710
#ifdef CMD_MUSIC
slouken@173
   711
	    case MUS_CMD:
slouken@173
   712
		MusicCMD_Start(music->data.cmd);
slouken@173
   713
		break;
slouken@0
   714
#endif
slouken@0
   715
#ifdef WAV_MUSIC
slouken@173
   716
	    case MUS_WAV:
slouken@173
   717
		WAVStream_Start(music->data.wave);
slouken@173
   718
		break;
slouken@0
   719
#endif
slouken@249
   720
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
   721
	    case MUS_MOD:
slouken@173
   722
		Player_Start(music->data.module);
icculus@237
   723
		/* Player_SetVolume() does nothing before Player_Start() */
icculus@237
   724
		music_internal_initialize_volume();
slouken@173
   725
		break;
slouken@0
   726
#endif
slouken@0
   727
#ifdef MID_MUSIC
slouken@173
   728
	    case MUS_MID:
slouken@98
   729
#ifdef USE_NATIVE_MIDI
slouken@173
   730
		if ( native_midi_ok ) {
slouken@173
   731
			native_midi_start(music->data.nativemidi);
slouken@173
   732
		} MIDI_ELSE
slouken@98
   733
#endif
slouken@106
   734
#ifdef USE_TIMIDITY_MIDI
slouken@173
   735
		if ( timidity_ok ) {
slouken@173
   736
			Timidity_Start(music->data.midi);
slouken@173
   737
		}
slouken@106
   738
#endif
slouken@173
   739
		break;
slouken@0
   740
#endif
slouken@63
   741
#ifdef OGG_MUSIC
slouken@173
   742
	    case MUS_OGG:
slouken@173
   743
		OGG_play(music->data.ogg);
slouken@173
   744
		break;
slouken@63
   745
#endif
slouken@0
   746
#ifdef MP3_MUSIC
slouken@173
   747
	    case MUS_MP3:
slouken@173
   748
		SMPEG_enableaudio(music->data.mp3,1);
slouken@173
   749
		SMPEG_enablevideo(music->data.mp3,0);
slouken@179
   750
		SMPEG_play(music_playing->data.mp3);
slouken@173
   751
		break;
slouken@0
   752
#endif
slouken@173
   753
	    default:
slouken@173
   754
		Mix_SetError("Can't play unknown music type");
slouken@173
   755
		retval = -1;
slouken@173
   756
		break;
slouken@0
   757
	}
slouken@173
   758
slouken@173
   759
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   760
	if ( retval == 0 ) {
slouken@173
   761
		if ( position > 0.0 ) {
slouken@173
   762
			if ( music_internal_position(position) < 0 ) {
slouken@173
   763
				Mix_SetError("Position not implemented for music type");
slouken@173
   764
				retval = -1;
slouken@173
   765
			}
slouken@173
   766
		} else {
slouken@173
   767
			music_internal_position(0.0);
slouken@173
   768
		}
slouken@173
   769
	}
slouken@173
   770
slouken@173
   771
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   772
	if ( retval < 0 ) {
slouken@173
   773
		music_playing = NULL;
slouken@173
   774
	}
slouken@173
   775
	return(retval);
slouken@173
   776
}
slouken@173
   777
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   778
{
slouken@173
   779
	int retval;
slouken@173
   780
slouken@173
   781
	/* Don't play null pointers :-) */
slouken@173
   782
	if ( music == NULL ) {
slouken@173
   783
		Mix_SetError("music parameter was NULL");
slouken@173
   784
		return(-1);
slouken@173
   785
	}
slouken@173
   786
slouken@173
   787
	/* Setup the data */
slouken@173
   788
	if ( ms ) {
slouken@173
   789
		music->fading = MIX_FADING_IN;
slouken@173
   790
	} else {
slouken@173
   791
		music->fading = MIX_NO_FADING;
slouken@173
   792
	}
slouken@173
   793
	music->fade_step = 0;
slouken@173
   794
	music->fade_steps = ms/ms_per_step;
slouken@173
   795
slouken@173
   796
	/* Play the puppy */
slouken@173
   797
	SDL_LockAudio();
slouken@173
   798
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
   799
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
   800
		SDL_UnlockAudio();
slouken@173
   801
		SDL_Delay(100);
slouken@173
   802
		SDL_LockAudio();
slouken@173
   803
	}
slouken@173
   804
	music_active = 1;
slouken@173
   805
	music_loops = loops;
slouken@173
   806
	retval = music_internal_play(music, position);
slouken@173
   807
	SDL_UnlockAudio();
slouken@173
   808
slouken@173
   809
	return(retval);
slouken@173
   810
}
slouken@173
   811
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
   812
{
slouken@173
   813
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
   814
}
slouken@173
   815
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
   816
{
slouken@173
   817
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
   818
}
megastep@10
   819
slouken@173
   820
/* Set the playing music position */
slouken@173
   821
int music_internal_position(double position)
megastep@10
   822
{
slouken@173
   823
	int retval = 0;
slouken@173
   824
slouken@173
   825
	switch (music_playing->type) {
slouken@249
   826
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
   827
	    case MUS_MOD:
slouken@173
   828
		Player_SetPosition((UWORD)position);
slouken@173
   829
		break;
slouken@173
   830
#endif
slouken@173
   831
#ifdef OGG_MUSIC
slouken@173
   832
	    case MUS_OGG:
slouken@173
   833
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
   834
		break;
slouken@173
   835
#endif
slouken@173
   836
#ifdef MP3_MUSIC
slouken@173
   837
	    case MUS_MP3:
slouken@176
   838
		if ( position > 0.0 ) {
slouken@173
   839
			SMPEG_skip(music_playing->data.mp3, position);
slouken@179
   840
		} else {
slouken@179
   841
			SMPEG_rewind(music_playing->data.mp3);
slouken@179
   842
			SMPEG_play(music_playing->data.mp3);
slouken@173
   843
		}
slouken@173
   844
		break;
slouken@173
   845
#endif
slouken@173
   846
	    default:
slouken@173
   847
		/* TODO: Implement this for other music backends */
slouken@173
   848
		retval = -1;
slouken@173
   849
		break;
megastep@10
   850
	}
slouken@173
   851
	return(retval);
megastep@4
   852
}
slouken@155
   853
int Mix_SetMusicPosition(double position)
slouken@154
   854
{
slouken@173
   855
	int retval;
slouken@173
   856
slouken@173
   857
	SDL_LockAudio();
slouken@173
   858
	if ( music_playing ) {
slouken@173
   859
		retval = music_internal_position(position);
slouken@173
   860
		if ( retval < 0 ) {
slouken@173
   861
			Mix_SetError("Position not implemented for music type");
slouken@154
   862
		}
slouken@173
   863
	} else {
slouken@173
   864
		Mix_SetError("Music isn't playing");
slouken@173
   865
		retval = -1;
slouken@154
   866
	}
slouken@173
   867
	SDL_UnlockAudio();
slouken@154
   868
slouken@173
   869
	return(retval);
slouken@154
   870
}
slouken@154
   871
icculus@237
   872
/* Set the music's initial volume */
icculus@237
   873
static void music_internal_initialize_volume(void)
icculus@237
   874
{
icculus@237
   875
	if ( music_playing->fading == MIX_FADING_IN ) {
icculus@237
   876
		music_internal_volume(0);
icculus@237
   877
	} else {
icculus@237
   878
		music_internal_volume(music_volume);
icculus@237
   879
	}
icculus@237
   880
}
icculus@237
   881
slouken@0
   882
/* Set the music volume */
slouken@173
   883
static void music_internal_volume(int volume)
slouken@173
   884
{
slouken@173
   885
	switch (music_playing->type) {
slouken@173
   886
#ifdef CMD_MUSIC
slouken@173
   887
	    case MUS_CMD:
slouken@173
   888
		MusicCMD_SetVolume(volume);
slouken@173
   889
		break;
slouken@173
   890
#endif
slouken@173
   891
#ifdef WAV_MUSIC
slouken@173
   892
	    case MUS_WAV:
slouken@173
   893
		WAVStream_SetVolume(volume);
slouken@173
   894
		break;
slouken@173
   895
#endif
slouken@249
   896
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
   897
	    case MUS_MOD:
slouken@173
   898
		Player_SetVolume((SWORD)volume);
slouken@173
   899
		break;
slouken@173
   900
#endif
slouken@173
   901
#ifdef MID_MUSIC
slouken@173
   902
	    case MUS_MID:
slouken@173
   903
#ifdef USE_NATIVE_MIDI
slouken@173
   904
		if ( native_midi_ok ) {
slouken@173
   905
			native_midi_setvolume(volume);
slouken@173
   906
		} MIDI_ELSE
slouken@173
   907
#endif
slouken@173
   908
#ifdef USE_TIMIDITY_MIDI
slouken@173
   909
		if ( timidity_ok ) {
slouken@173
   910
			Timidity_SetVolume(volume);
slouken@173
   911
		}
slouken@173
   912
#endif
slouken@173
   913
		break;
slouken@173
   914
#endif
slouken@173
   915
#ifdef OGG_MUSIC
slouken@173
   916
	    case MUS_OGG:
slouken@173
   917
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
   918
		break;
slouken@173
   919
#endif
slouken@173
   920
#ifdef MP3_MUSIC
slouken@173
   921
	    case MUS_MP3:
slouken@173
   922
		SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
   923
		break;
slouken@173
   924
#endif
slouken@173
   925
	    default:
slouken@173
   926
		/* Unknown music type?? */
slouken@173
   927
		break;
slouken@173
   928
	}
slouken@173
   929
}
slouken@0
   930
int Mix_VolumeMusic(int volume)
slouken@0
   931
{
slouken@0
   932
	int prev_volume;
slouken@0
   933
slouken@0
   934
	prev_volume = music_volume;
megastep@4
   935
	if ( volume < 0 ) {
slouken@163
   936
		return prev_volume;
megastep@4
   937
	}
megastep@4
   938
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
   939
		volume = SDL_MIX_MAXVOLUME;
megastep@4
   940
	}
megastep@4
   941
	music_volume = volume;
slouken@173
   942
	SDL_LockAudio();
slouken@173
   943
	if ( music_playing ) {
slouken@173
   944
		music_internal_volume(music_volume);
slouken@0
   945
	}
slouken@173
   946
	SDL_UnlockAudio();
slouken@0
   947
	return(prev_volume);
slouken@0
   948
}
slouken@0
   949
slouken@173
   950
/* Halt playing of music */
slouken@173
   951
static void music_internal_halt(void)
megastep@7
   952
{
megastep@7
   953
	switch (music_playing->type) {
megastep@7
   954
#ifdef CMD_MUSIC
slouken@173
   955
	    case MUS_CMD:
megastep@7
   956
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
   957
		break;
megastep@7
   958
#endif
megastep@7
   959
#ifdef WAV_MUSIC
slouken@173
   960
	    case MUS_WAV:
megastep@7
   961
		WAVStream_Stop();
megastep@7
   962
		break;
megastep@7
   963
#endif
slouken@249
   964
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
   965
	    case MUS_MOD:
megastep@7
   966
		Player_Stop();
megastep@7
   967
		break;
megastep@7
   968
#endif
megastep@7
   969
#ifdef MID_MUSIC
slouken@173
   970
	    case MUS_MID:
slouken@98
   971
#ifdef USE_NATIVE_MIDI
slouken@98
   972
		if ( native_midi_ok ) {
slouken@98
   973
			native_midi_stop();
slouken@106
   974
		} MIDI_ELSE
slouken@98
   975
#endif
slouken@106
   976
#ifdef USE_TIMIDITY_MIDI
slouken@98
   977
		if ( timidity_ok ) {
slouken@98
   978
			Timidity_Stop();
slouken@98
   979
		}
slouken@106
   980
#endif
megastep@7
   981
		break;
megastep@7
   982
#endif
slouken@63
   983
#ifdef OGG_MUSIC
slouken@173
   984
	    case MUS_OGG:
slouken@63
   985
		OGG_stop(music_playing->data.ogg);
slouken@63
   986
		break;
slouken@63
   987
#endif
megastep@7
   988
#ifdef MP3_MUSIC
slouken@173
   989
	    case MUS_MP3:
megastep@7
   990
		SMPEG_stop(music_playing->data.mp3);
megastep@7
   991
		break;
megastep@7
   992
#endif
slouken@173
   993
	    default:
megastep@7
   994
		/* Unknown music type?? */
megastep@7
   995
		return;
megastep@7
   996
	}
megastep@7
   997
	music_playing->fading = MIX_NO_FADING;
slouken@17
   998
	music_playing = NULL;
megastep@7
   999
}
slouken@0
  1000
int Mix_HaltMusic(void)
slouken@0
  1001
{
slouken@173
  1002
	SDL_LockAudio();
slouken@173
  1003
	if ( music_playing ) {
slouken@173
  1004
		music_internal_halt();
slouken@0
  1005
	}
slouken@173
  1006
	SDL_UnlockAudio();
slouken@173
  1007
slouken@0
  1008
	return(0);
slouken@0
  1009
}
slouken@0
  1010
megastep@4
  1011
/* Progressively stop the music */
megastep@4
  1012
int Mix_FadeOutMusic(int ms)
megastep@4
  1013
{
slouken@173
  1014
	int retval = 0;
slouken@173
  1015
slouken@173
  1016
	SDL_LockAudio();
slouken@173
  1017
	if ( music_playing && (music_playing->fading == MIX_NO_FADING) ) {
slouken@173
  1018
		music_playing->fading = MIX_FADING_OUT;
slouken@173
  1019
		music_playing->fade_step = 0;
slouken@173
  1020
		music_playing->fade_steps = ms/ms_per_step;
slouken@173
  1021
		retval = 1;
megastep@4
  1022
	}
slouken@173
  1023
	SDL_UnlockAudio();
slouken@173
  1024
slouken@173
  1025
	return(retval);
megastep@4
  1026
}
megastep@4
  1027
megastep@4
  1028
Mix_Fading Mix_FadingMusic(void)
megastep@4
  1029
{
slouken@173
  1030
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
  1031
slouken@173
  1032
	SDL_LockAudio();
slouken@173
  1033
	if ( music_playing ) {
slouken@173
  1034
		fading = music_playing->fading;
slouken@173
  1035
	}
slouken@173
  1036
	SDL_UnlockAudio();
slouken@173
  1037
slouken@173
  1038
	return(fading);
megastep@4
  1039
}
megastep@4
  1040
slouken@0
  1041
/* Pause/Resume the music stream */
slouken@0
  1042
void Mix_PauseMusic(void)
slouken@0
  1043
{
slouken@173
  1044
	music_active = 0;
slouken@0
  1045
}
megastep@4
  1046
slouken@0
  1047
void Mix_ResumeMusic(void)
slouken@0
  1048
{
slouken@173
  1049
	music_active = 1;
slouken@0
  1050
}
slouken@0
  1051
slouken@0
  1052
void Mix_RewindMusic(void)
slouken@0
  1053
{
slouken@173
  1054
	Mix_SetMusicPosition(0.0);
slouken@0
  1055
}
slouken@0
  1056
megastep@13
  1057
int Mix_PausedMusic(void)
megastep@13
  1058
{
slouken@17
  1059
	return (music_active == 0);
megastep@13
  1060
}
megastep@13
  1061
slouken@0
  1062
/* Check the status of the music */
slouken@173
  1063
static int music_internal_playing()
slouken@173
  1064
{
slouken@173
  1065
	int playing = 1;
slouken@173
  1066
	switch (music_playing->type) {
slouken@173
  1067
#ifdef CMD_MUSIC
slouken@173
  1068
	    case MUS_CMD:
slouken@173
  1069
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
  1070
			playing = 0;
slouken@173
  1071
		}
slouken@173
  1072
		break;
slouken@173
  1073
#endif
slouken@173
  1074
#ifdef WAV_MUSIC
slouken@173
  1075
	    case MUS_WAV:
slouken@173
  1076
		if ( ! WAVStream_Active() ) {
slouken@173
  1077
			playing = 0;
slouken@173
  1078
		}
slouken@173
  1079
		break;
slouken@173
  1080
#endif
slouken@249
  1081
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1082
	    case MUS_MOD:
slouken@173
  1083
		if ( ! Player_Active() ) {
slouken@173
  1084
			playing = 0;
slouken@173
  1085
		}
slouken@173
  1086
		break;
slouken@173
  1087
#endif
slouken@173
  1088
#ifdef MID_MUSIC
slouken@173
  1089
	    case MUS_MID:
slouken@173
  1090
#ifdef USE_NATIVE_MIDI
slouken@173
  1091
		if ( native_midi_ok ) {
slouken@173
  1092
			if ( ! native_midi_active() )
slouken@173
  1093
				playing = 0;
slouken@173
  1094
		} MIDI_ELSE
slouken@173
  1095
#endif
slouken@173
  1096
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1097
		if ( timidity_ok ) {
slouken@173
  1098
			if ( ! Timidity_Active() )
slouken@173
  1099
				playing = 0;
slouken@173
  1100
		}
slouken@173
  1101
#endif
slouken@173
  1102
		break;
slouken@173
  1103
#endif
slouken@173
  1104
#ifdef OGG_MUSIC
slouken@173
  1105
	    case MUS_OGG:
slouken@173
  1106
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1107
			playing = 0;
slouken@173
  1108
		}
slouken@173
  1109
		break;
slouken@173
  1110
#endif
slouken@173
  1111
#ifdef MP3_MUSIC
slouken@173
  1112
	    case MUS_MP3:
slouken@173
  1113
		if ( SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1114
			playing = 0;
slouken@173
  1115
		break;
slouken@173
  1116
#endif
slouken@173
  1117
	    default:
slouken@173
  1118
		playing = 0;
slouken@173
  1119
		break;
slouken@173
  1120
	}
slouken@173
  1121
	return(playing);
slouken@173
  1122
}
megastep@13
  1123
int Mix_PlayingMusic(void)
slouken@0
  1124
{
slouken@173
  1125
	int playing = 0;
slouken@173
  1126
slouken@173
  1127
	SDL_LockAudio();
slouken@173
  1128
	if ( music_playing ) {
slouken@173
  1129
		playing = music_internal_playing();
slouken@0
  1130
	}
slouken@173
  1131
	SDL_UnlockAudio();
slouken@173
  1132
slouken@173
  1133
	return(playing);
slouken@0
  1134
}
slouken@0
  1135
slouken@0
  1136
/* Set the external music playback command */
slouken@0
  1137
int Mix_SetMusicCMD(const char *command)
slouken@0
  1138
{
slouken@0
  1139
	Mix_HaltMusic();
slouken@0
  1140
	if ( music_cmd ) {
slouken@0
  1141
		free(music_cmd);
slouken@0
  1142
		music_cmd = NULL;
slouken@0
  1143
	}
slouken@0
  1144
	if ( command ) {
slouken@0
  1145
		music_cmd = (char *)malloc(strlen(command)+1);
slouken@0
  1146
		if ( music_cmd == NULL ) {
slouken@0
  1147
			return(-1);
slouken@0
  1148
		}
slouken@0
  1149
		strcpy(music_cmd, command);
slouken@0
  1150
	}
slouken@0
  1151
	return(0);
slouken@0
  1152
}
slouken@0
  1153
slouken@249
  1154
#ifdef LIBMIKMOD_MUSIC
slouken@249
  1155
static int _pl_synchro_value;
slouken@249
  1156
void Player_SetSynchroValue(int i)
slouken@249
  1157
{
slouken@249
  1158
	fprintf(stderr,"SDL_mixer: Player_SetSynchroValue is not supported.\n");
slouken@249
  1159
	_pl_synchro_value = i;
slouken@249
  1160
}
slouken@249
  1161
slouken@249
  1162
int Player_GetSynchroValue(void)
slouken@249
  1163
{
slouken@249
  1164
	fprintf(stderr,"SDL_mixer: Player_GetSynchroValue is not supported.\n");
slouken@249
  1165
	return _pl_synchro_value;
slouken@249
  1166
}
slouken@249
  1167
#endif
slouken@249
  1168
slouken@154
  1169
int Mix_SetSynchroValue(int i)
slouken@154
  1170
{
slouken@154
  1171
	if ( music_playing && ! music_stopped ) {
slouken@154
  1172
		switch (music_playing->type) {
slouken@249
  1173
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1174
		    case MUS_MOD:
slouken@173
  1175
			if ( ! Player_Active() ) {
slouken@173
  1176
				return(-1);
slouken@173
  1177
			}
slouken@173
  1178
			Player_SetSynchroValue(i);
slouken@173
  1179
			return 0;
slouken@173
  1180
			break;
slouken@154
  1181
#endif
slouken@173
  1182
		    default:
slouken@173
  1183
			return(-1);
slouken@173
  1184
			break;
slouken@154
  1185
		}
slouken@154
  1186
		return(-1);
slouken@154
  1187
	}
slouken@154
  1188
	return(-1);
slouken@154
  1189
}
slouken@154
  1190
slouken@154
  1191
int Mix_GetSynchroValue(void)
slouken@154
  1192
{
slouken@154
  1193
	if ( music_playing && ! music_stopped ) {
slouken@154
  1194
		switch (music_playing->type) {
slouken@249
  1195
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@173
  1196
		    case MUS_MOD:
slouken@173
  1197
			if ( ! Player_Active() ) {
slouken@173
  1198
				return(-1);
slouken@173
  1199
			}
slouken@173
  1200
			return Player_GetSynchroValue();
slouken@173
  1201
			break;
slouken@154
  1202
#endif
slouken@173
  1203
		    default:
slouken@173
  1204
			return(-1);
slouken@173
  1205
			break;
slouken@154
  1206
		}
slouken@154
  1207
		return(-1);
slouken@154
  1208
	}
slouken@154
  1209
	return(-1);
slouken@154
  1210
}
slouken@154
  1211
slouken@154
  1212
slouken@0
  1213
/* Uninitialize the music players */
slouken@0
  1214
void close_music(void)
slouken@0
  1215
{
slouken@0
  1216
	Mix_HaltMusic();
slouken@0
  1217
#ifdef CMD_MUSIC
slouken@0
  1218
	Mix_SetMusicCMD(NULL);
slouken@0
  1219
#endif
slouken@249
  1220
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@0
  1221
	MikMod_Exit();
slouken@249
  1222
# ifndef LIBMIKMOD_MUSIC
slouken@101
  1223
	MikMod_UnregisterAllLoaders();
slouken@101
  1224
	MikMod_UnregisterAllDrivers();
slouken@249
  1225
# endif
slouken@0
  1226
#endif
slouken@0
  1227
}
slouken@0
  1228
slouken@249
  1229
# ifdef LIBMIKMOD_MUSIC
slouken@249
  1230
typedef struct
slouken@249
  1231
{
slouken@249
  1232
	MREADER mr;
slouken@249
  1233
	int offset;
slouken@249
  1234
	int eof;
slouken@249
  1235
	SDL_RWops *rw;
slouken@249
  1236
} LMM_MREADER;
slouken@249
  1237
BOOL LMM_Seek(struct MREADER *mr,long to,int dir)
slouken@249
  1238
{
slouken@249
  1239
	int at;
slouken@249
  1240
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1241
	if(dir==SEEK_SET)
slouken@249
  1242
		to+=lmmmr->offset;
slouken@249
  1243
	at=SDL_RWseek(lmmmr->rw, to, dir);
slouken@249
  1244
	return at<lmmmr->offset;
slouken@249
  1245
}
slouken@249
  1246
long LMM_Tell(struct MREADER *mr)
slouken@249
  1247
{
slouken@249
  1248
	int at;
slouken@249
  1249
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1250
	at=SDL_RWtell(lmmmr->rw)-lmmmr->offset;
slouken@249
  1251
	return at;
slouken@249
  1252
}
slouken@249
  1253
BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
slouken@249
  1254
{
slouken@249
  1255
	int got;
slouken@249
  1256
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1257
	got=SDL_RWread(lmmmr->rw, buf, sz, 1);
slouken@249
  1258
	return got;
slouken@249
  1259
}
slouken@249
  1260
int LMM_Get(struct MREADER *mr)
slouken@249
  1261
{
slouken@249
  1262
	unsigned char c;
slouken@249
  1263
	int i=EOF;
slouken@249
  1264
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1265
	if(SDL_RWread(lmmmr->rw,&c,1,1))
slouken@249
  1266
		i=c;
slouken@249
  1267
	return i;
slouken@249
  1268
}
slouken@249
  1269
BOOL LMM_Eof(struct MREADER *mr)
slouken@249
  1270
{
slouken@249
  1271
	int offset;
slouken@249
  1272
	LMM_MREADER* lmmmr=(LMM_MREADER*)mr;
slouken@249
  1273
	offset=LMM_Tell(mr);
slouken@249
  1274
	return offset>=lmmmr->eof;
slouken@249
  1275
}
slouken@249
  1276
MODULE *MikMod_LoadSongRW(SDL_RWops *rw, int maxchan)
slouken@249
  1277
{
slouken@249
  1278
	LMM_MREADER lmmmr={
slouken@249
  1279
		LMM_Seek,
slouken@249
  1280
		LMM_Tell,
slouken@249
  1281
		LMM_Read,
slouken@249
  1282
		LMM_Get,
slouken@249
  1283
		LMM_Eof,
slouken@249
  1284
		0,
slouken@249
  1285
		0,
slouken@249
  1286
		rw
slouken@249
  1287
	};
slouken@249
  1288
	MODULE *m;
slouken@249
  1289
	lmmmr.offset=SDL_RWtell(rw);
slouken@249
  1290
	SDL_RWseek(rw,0,SEEK_END);
slouken@249
  1291
	lmmmr.eof=SDL_RWtell(rw);
slouken@249
  1292
	SDL_RWseek(rw,lmmmr.offset,SEEK_SET);
slouken@249
  1293
	m=Player_LoadGeneric((MREADER*)&lmmmr,maxchan,0);
slouken@249
  1294
	return m;
slouken@249
  1295
}
slouken@249
  1296
# endif
slouken@249
  1297
slouken@226
  1298
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw) {
slouken@246
  1299
	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
slouken@246
  1300
	/*		    or there are too many formats supported by MikMod or MikMod does */
slouken@246
  1301
	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
slouken@246
  1302
	/*		    the check can be uncommented */
slouken@226
  1303
	Mix_Music *music;
slouken@246
  1304
	int start;
slouken@226
  1305
slouken@226
  1306
	/* Figure out what kind of file this is */
slouken@246
  1307
	start = SDL_RWtell(rw);
slouken@246
  1308
	if (SDL_RWread(rw,magic,1,4)!=4) {
slouken@226
  1309
		Mix_SetError("Couldn't read from RWops");
slouken@226
  1310
		return NULL;
slouken@226
  1311
	}
slouken@246
  1312
	SDL_RWseek(rw, start, SEEK_SET);
slouken@246
  1313
	magic[4]='\0';
slouken@226
  1314
slouken@226
  1315
	/* Allocate memory for the music structure */
slouken@226
  1316
	music=(Mix_Music *)malloc(sizeof(Mix_Music));
slouken@226
  1317
	if (music==NULL ) {
slouken@226
  1318
		Mix_SetError("Out of memory");
slouken@226
  1319
		return(NULL);
slouken@226
  1320
	}
slouken@226
  1321
	music->error = 0;
slouken@226
  1322
slouken@246
  1323
#ifdef OGG_MUSIC
slouken@246
  1324
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@246
  1325
	if ( strcmp((char *)magic, "OggS") == 0 ) {
slouken@246
  1326
		music->type = MUS_OGG;
slouken@246
  1327
		music->data.ogg = OGG_new_RW(rw);
slouken@246
  1328
		if ( music->data.ogg == NULL ) {
slouken@246
  1329
			music->error = 1;
slouken@246
  1330
		}
slouken@246
  1331
	} else
slouken@246
  1332
#endif
slouken@256
  1333
#ifdef MP3_MUSIC
slouken@256
  1334
	if ( magic[0] == 0xFF && (magic[1] & 0xF0) == 0xF0 ) {
slouken@256
  1335
		SMPEG_Info info;
slouken@256
  1336
		music->type = MUS_MP3;
slouken@256
  1337
		music->data.mp3 = SMPEG_new_rwops(rw, &info, 0);
slouken@256
  1338
		if ( !info.has_audio ) {
slouken@256
  1339
			Mix_SetError("MPEG file does not have any audio stream.");
slouken@256
  1340
			music->error = 1;
slouken@256
  1341
		} else {
slouken@256
  1342
			SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@256
  1343
		}
slouken@256
  1344
	} else
slouken@256
  1345
#endif
slouken@249
  1346
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
slouken@226
  1347
	if (1) {
slouken@226
  1348
		music->type=MUS_MOD;
slouken@226
  1349
		music->data.module=MikMod_LoadSongRW(rw,64);
slouken@226
  1350
		if (music->data.module==NULL) {
slouken@226
  1351
			Mix_SetError("%s",MikMod_strerror(MikMod_errno));
slouken@226
  1352
			music->error=1;
slouken@226
  1353
		}
slouken@226
  1354
	} else
slouken@226
  1355
#endif
slouken@226
  1356
	{
slouken@226
  1357
		Mix_SetError("Unrecognized music format");
slouken@226
  1358
		music->error=1;
slouken@226
  1359
	}
slouken@226
  1360
	if (music->error) {
slouken@226
  1361
		free(music);
slouken@226
  1362
		music=NULL;
slouken@226
  1363
	}
slouken@226
  1364
	return(music);
slouken@226
  1365
}