music.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 09 Nov 2003 20:11:30 +0000
changeset 237 5fb7a859f158
parent 226 3691a375f2e6
child 241 503416fca921
permissions -rw-r--r--
(Courtesy of Jerome Zago on the SDL mailing list...)


+++++ Synopsis:
Mix_FadeInMusic(Music, 0, chunksize) following Mix_VolumeMusic(0) doesn't take
into account the latter for low values of "chunksize" when "Music" is a mod.

+++++ How to reproduce the problem:
#####
$ cat testmusic.c
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
if (argc != 4) {
fprintf(stderr, "Usage: %s <music file> <chunksize> <ms>\n", argv[0]);
return 1;
}

if (SDL_Init(SDL_INIT_AUDIO) >= 0) {
if (Mix_OpenAudio(44100, AUDIO_S16, 1, atoi(argv[2])) >= 0) {
Mix_Music* Music = Mix_LoadMUS(argv[1]);
if (Music) {
Mix_VolumeMusic(0);
Mix_FadeInMusic(Music, 0, atoi(argv[3]));
printf("Now playing\n");
SDL_Delay(2000);
Mix_FreeMusic(Music);
}
}
}
SDL_Quit();
return 0;
};

$ cc -g -O2 -Wall -I/usr/include/SDL -D_REENTRANT -L/usr/lib -lSDL \
-lpthread -lSDL_mixer testmusic.c -o testmusic

$ ./testmusic /usr/local/share/heroes/mod/heroes01.xm 512 10
Now playing
[ I hear the music => BAD ]

$ ./testmusic /usr/local/share/heroes/mod/heroes01.xm 512 11
Now playing
[ I don't hear the music => GOOD ]

$ ./testmusic /usr/local/share/heroes/mod/heroes01.xm 1024 22
Now playing
[ I hear the music => BAD ]

$ ./testmusic /usr/local/share/heroes/mod/heroes01.xm 1024 23
Now playing
[ I don't hear the music => GOOD ]

$ ./testmusic /usr/local/share/heroes/mod/heroes01.xm 2048 45
Now playing
[ I hear the music => BAD ]

$ ./testmusic /usr/local/share/heroes/mod/heroes01.xm 2048 46
Now playing
[ I don't hear the music => GOOD ]
#####

+++++ Comments
heroes01.xm comes from
http://prdownloads.sourceforge.net/heroes/heroes-sound-tracks-1.0.tar.bz2.
All the other modules I've tested show the same behaviour.

+++++ Analysis (using SDL_mixer source code):
It appears that the lowest value of chunksize for which the volume is taken
into account is equal to ms_per_step = (int) (((float)mixer->samples *
1000.0) / mixer->freq) [file music.c, function open_music()].
This also means that music->fade_steps [function Mix_FadeInMusicPos()] = 0
when and only when the volume isn't taken into account.
In this case, note that music_internal_volume() is never called from
music_mixer(), since (music_playing->fade_step++ < music_playing->fade_steps)
is never verified. This might explain the bug.

+++++ Testing environment:
- Mandrake Linux 9.1 i386
- SDL_mixer 1.2.4 (Mandrake package), 1.2.5 or CVS (both built from scratch)
- Athlon XP 1500+
- SB Live!

More details upon request. Thanks for your time.


--------

This is related to my previous message:
[SDL] Strange behaviour of Mix_FadeInMusic (1.2.5) on mods
http://www.libsdl.org/pipermail/sdl/2003-November/057749.html

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