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