music.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 07 Oct 2018 00:21:02 +0300
branchSDL-1.2
changeset 873 d159f97ef3e7
parent 871 284d10d87cc9
child 883 2ff5d3653db2
permissions -rw-r--r--
backport fix for bugs 1914, 1915. (from 2.0 branch commit 030181ff9f59).
slouken@0
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@518
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@140
    22
/* $Id$ */
slouken@138
    23
slouken@3
    24
#include <stdlib.h>
slouken@3
    25
#include <string.h>
slouken@125
    26
#include <ctype.h>
icculus@280
    27
#include <assert.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
#ifdef CMD_MUSIC
slouken@0
    35
#include "music_cmd.h"
slouken@0
    36
#endif
slouken@0
    37
#ifdef WAV_MUSIC
slouken@0
    38
#include "wavestream.h"
slouken@0
    39
#endif
slouken@481
    40
#ifdef MODPLUG_MUSIC
slouken@481
    41
#include "music_modplug.h"
slouken@481
    42
#endif
slouken@411
    43
#ifdef MOD_MUSIC
slouken@411
    44
#include "music_mod.h"
slouken@0
    45
#endif
slouken@0
    46
#ifdef MID_MUSIC
slouken@106
    47
#  ifdef USE_TIMIDITY_MIDI
slouken@106
    48
#    include "timidity.h"
slouken@106
    49
#  endif
chewi@506
    50
#  ifdef USE_FLUIDSYNTH_MIDI
chewi@506
    51
#    include "fluidsynth.h"
chewi@506
    52
#  endif
slouken@106
    53
#  ifdef USE_NATIVE_MIDI
slouken@106
    54
#    include "native_midi.h"
slouken@106
    55
#  endif
slouken@0
    56
#endif
slouken@63
    57
#ifdef OGG_MUSIC
slouken@63
    58
#include "music_ogg.h"
slouken@63
    59
#endif
slouken@0
    60
#ifdef MP3_MUSIC
slouken@314
    61
#include "dynamic_mp3.h"
slouken@357
    62
#endif
slouken@357
    63
#ifdef MP3_MAD_MUSIC
slouken@357
    64
#include "music_mad.h"
slouken@357
    65
#endif
slouken@382
    66
#ifdef FLAC_MUSIC
slouken@382
    67
#include "music_flac.h"
slouken@382
    68
#endif
slouken@0
    69
slouken@357
    70
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
    71
static SDL_AudioSpec used_mixer;
slouken@0
    72
#endif
slouken@0
    73
slouken@357
    74
slouken@42
    75
int volatile music_active = 1;
slouken@42
    76
static int volatile music_stopped = 0;
slouken@0
    77
static int music_loops = 0;
slouken@0
    78
static char *music_cmd = NULL;
slouken@42
    79
static Mix_Music * volatile music_playing = NULL;
slouken@17
    80
static int music_volume = MIX_MAX_VOLUME;
megastep@4
    81
slouken@0
    82
struct _Mix_Music {
slouken@177
    83
	Mix_MusicType type;
slouken@0
    84
	union {
slouken@0
    85
#ifdef CMD_MUSIC
slouken@0
    86
		MusicCMD *cmd;
slouken@0
    87
#endif
slouken@0
    88
#ifdef WAV_MUSIC
slouken@0
    89
		WAVStream *wave;
slouken@0
    90
#endif
slouken@481
    91
#ifdef MODPLUG_MUSIC
slouken@481
    92
		modplug_data *modplug;
slouken@481
    93
#endif
slouken@411
    94
#ifdef MOD_MUSIC
slouken@411
    95
		struct MODULE *module;
slouken@0
    96
#endif
slouken@0
    97
#ifdef MID_MUSIC
slouken@106
    98
#ifdef USE_TIMIDITY_MIDI
slouken@0
    99
		MidiSong *midi;
slouken@106
   100
#endif
chewi@506
   101
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   102
		FluidSynthMidiSong *fluidsynthmidi;
chewi@506
   103
#endif
slouken@98
   104
#ifdef USE_NATIVE_MIDI
slouken@98
   105
		NativeMidiSong *nativemidi;
slouken@98
   106
#endif
slouken@0
   107
#endif
slouken@63
   108
#ifdef OGG_MUSIC
slouken@63
   109
		OGG_music *ogg;
slouken@63
   110
#endif
slouken@0
   111
#ifdef MP3_MUSIC
slouken@0
   112
		SMPEG *mp3;
slouken@0
   113
#endif
slouken@357
   114
#ifdef MP3_MAD_MUSIC
slouken@357
   115
		mad_data *mp3_mad;
slouken@357
   116
#endif
slouken@382
   117
#ifdef FLAC_MUSIC
slouken@382
   118
		FLAC_music *flac;
slouken@382
   119
#endif
slouken@0
   120
	} data;
megastep@4
   121
	Mix_Fading fading;
slouken@17
   122
	int fade_step;
slouken@17
   123
	int fade_steps;
slouken@0
   124
	int error;
slouken@0
   125
};
slouken@29
   126
#ifdef MID_MUSIC
slouken@106
   127
#ifdef USE_TIMIDITY_MIDI
slouken@0
   128
static int timidity_ok;
slouken@142
   129
static int samplesize;
slouken@106
   130
#endif
chewi@506
   131
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   132
static int fluidsynth_ok;
chewi@506
   133
#endif
slouken@98
   134
#ifdef USE_NATIVE_MIDI
slouken@98
   135
static int native_midi_ok;
slouken@98
   136
#endif
slouken@29
   137
#endif
slouken@0
   138
slouken@17
   139
/* Used to calculate fading steps */
slouken@17
   140
static int ms_per_step;
slouken@17
   141
icculus@390
   142
/* rcg06042009 report available decoders at runtime. */
icculus@390
   143
static const char **music_decoders = NULL;
icculus@390
   144
static int num_decoders = 0;
icculus@390
   145
chewi@506
   146
/* Semicolon-separated SoundFont paths */
chewi@506
   147
#ifdef MID_MUSIC
chewi@506
   148
char* soundfont_paths = NULL;
chewi@506
   149
#endif
chewi@506
   150
slouken@466
   151
int Mix_GetNumMusicDecoders(void)
icculus@390
   152
{
icculus@390
   153
	return(num_decoders);
icculus@390
   154
}
icculus@390
   155
icculus@390
   156
const char *Mix_GetMusicDecoder(int index)
icculus@390
   157
{
icculus@390
   158
	if ((index < 0) || (index >= num_decoders)) {
icculus@390
   159
		return NULL;
icculus@390
   160
	}
icculus@390
   161
	return(music_decoders[index]);
icculus@390
   162
}
icculus@390
   163
icculus@390
   164
static void add_music_decoder(const char *decoder)
icculus@390
   165
{
sezeroz@873
   166
	void *ptr = SDL_realloc(music_decoders, (num_decoders + 1) * sizeof (const char *));
icculus@390
   167
	if (ptr == NULL) {
icculus@390
   168
		return;  /* oh well, go on without it. */
icculus@390
   169
	}
slouken@411
   170
	music_decoders = (const char **) ptr;
icculus@390
   171
	music_decoders[num_decoders++] = decoder;
icculus@390
   172
}
icculus@390
   173
megastep@10
   174
/* Local low-level functions prototypes */
icculus@237
   175
static void music_internal_initialize_volume(void);
slouken@173
   176
static void music_internal_volume(int volume);
slouken@173
   177
static int  music_internal_play(Mix_Music *music, double position);
slouken@173
   178
static int  music_internal_position(double position);
slouken@173
   179
static int  music_internal_playing();
slouken@173
   180
static void music_internal_halt(void);
megastep@7
   181
slouken@29
   182
slouken@29
   183
/* Support for hooking when the music has finished */
slouken@29
   184
static void (*music_finished_hook)(void) = NULL;
slouken@29
   185
slouken@29
   186
void Mix_HookMusicFinished(void (*music_finished)(void))
slouken@29
   187
{
slouken@29
   188
	SDL_LockAudio();
slouken@29
   189
	music_finished_hook = music_finished;
slouken@29
   190
	SDL_UnlockAudio();
slouken@29
   191
}
slouken@29
   192
slouken@29
   193
icculus@281
   194
/* If music isn't playing, halt it if no looping is required, restart it */
icculus@281
   195
/* otherwhise. NOP if the music is playing */
icculus@281
   196
static int music_halt_or_loop (void)
icculus@281
   197
{
icculus@281
   198
	/* Restart music if it has to loop */
slouken@528
   199
	
icculus@281
   200
	if (!music_internal_playing()) 
icculus@281
   201
	{
slouken@541
   202
#ifdef USE_NATIVE_MIDI
slouken@533
   203
		/* Native MIDI handles looping internally */
slouken@533
   204
		if (music_playing->type == MUS_MID && native_midi_ok) {
slouken@533
   205
			music_loops = 0;
slouken@533
   206
		}
slouken@541
   207
#endif
slouken@533
   208
icculus@281
   209
		/* Restart music if it has to loop at a high level */
slouken@533
   210
		if (music_loops)
icculus@281
   211
		{
slouken@533
   212
			Mix_Fading current_fade;
slouken@533
   213
			--music_loops;
slouken@533
   214
			current_fade = music_playing->fading;
icculus@281
   215
			music_internal_play(music_playing, 0.0);
icculus@281
   216
			music_playing->fading = current_fade;
icculus@281
   217
		} 
icculus@281
   218
		else 
icculus@281
   219
		{
icculus@281
   220
			music_internal_halt();
icculus@281
   221
			if (music_finished_hook)
icculus@281
   222
				music_finished_hook();
icculus@281
   223
			
icculus@281
   224
			return 0;
icculus@281
   225
		}
icculus@281
   226
	}
icculus@281
   227
	
icculus@281
   228
	return 1;
icculus@281
   229
}
icculus@281
   230
icculus@281
   231
icculus@281
   232
slouken@0
   233
/* Mixing function */
slouken@0
   234
void music_mixer(void *udata, Uint8 *stream, int len)
slouken@0
   235
{
slouken@407
   236
	int left = 0;
slouken@407
   237
slouken@173
   238
	if ( music_playing && music_active ) {
megastep@13
   239
		/* Handle fading */
slouken@17
   240
		if ( music_playing->fading != MIX_NO_FADING ) {
slouken@17
   241
			if ( music_playing->fade_step++ < music_playing->fade_steps ) {
slouken@173
   242
				int volume;
slouken@26
   243
				int fade_step = music_playing->fade_step;
slouken@26
   244
				int fade_steps = music_playing->fade_steps;
slouken@17
   245
megastep@4
   246
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   247
					volume = (music_volume * (fade_steps-fade_step)) / fade_steps;
slouken@17
   248
				} else { /* Fading in */
slouken@173
   249
					volume = (music_volume * fade_step) / fade_steps;
slouken@17
   250
				}
slouken@173
   251
				music_internal_volume(volume);
slouken@17
   252
			} else {
slouken@17
   253
				if ( music_playing->fading == MIX_FADING_OUT ) {
slouken@173
   254
					music_internal_halt();
slouken@173
   255
					if ( music_finished_hook ) {
slouken@173
   256
						music_finished_hook();
slouken@173
   257
					}
megastep@4
   258
					return;
megastep@4
   259
				}
megastep@4
   260
				music_playing->fading = MIX_NO_FADING;
megastep@4
   261
			}
megastep@4
   262
		}
icculus@281
   263
		
slouken@528
   264
		music_halt_or_loop();
slouken@528
   265
		if (!music_internal_playing())
slouken@528
   266
			return;
slouken@528
   267
slouken@0
   268
		switch (music_playing->type) {
slouken@0
   269
#ifdef CMD_MUSIC
slouken@0
   270
			case MUS_CMD:
slouken@0
   271
				/* The playing is done externally */
slouken@0
   272
				break;
slouken@0
   273
#endif
slouken@0
   274
#ifdef WAV_MUSIC
slouken@0
   275
			case MUS_WAV:
slouken@407
   276
				left = WAVStream_PlaySome(stream, len);
slouken@0
   277
				break;
slouken@0
   278
#endif
slouken@481
   279
#ifdef MODPLUG_MUSIC
slouken@481
   280
			case MUS_MODPLUG:
slouken@481
   281
				left = modplug_playAudio(music_playing->data.modplug, stream, len);
slouken@481
   282
				break;
slouken@481
   283
#endif
slouken@411
   284
#ifdef MOD_MUSIC
slouken@0
   285
			case MUS_MOD:
slouken@411
   286
				left = MOD_playAudio(music_playing->data.module, stream, len);
slouken@0
   287
				break;
slouken@0
   288
#endif
slouken@0
   289
#ifdef MID_MUSIC
chewi@506
   290
			case MUS_MID:
slouken@529
   291
#ifdef USE_NATIVE_MIDI
slouken@529
   292
  				if ( native_midi_ok ) {
slouken@529
   293
					/* Native midi is handled asynchronously */
slouken@529
   294
					goto skip;
slouken@529
   295
	  			}
slouken@529
   296
#endif
chewi@506
   297
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   298
				if ( fluidsynth_ok ) {
chewi@506
   299
					fluidsynth_playsome(music_playing->data.fluidsynthmidi, stream, len);
slouken@528
   300
					goto skip;
chewi@506
   301
				}
chewi@506
   302
#endif
slouken@106
   303
#ifdef USE_TIMIDITY_MIDI
slouken@98
   304
				if ( timidity_ok ) {
slouken@98
   305
					int samples = len / samplesize;
slouken@98
   306
  					Timidity_PlaySome(stream, samples);
slouken@528
   307
					goto skip;
slouken@98
   308
				}
chewi@506
   309
#endif
slouken@0
   310
				break;
slouken@0
   311
#endif
slouken@63
   312
#ifdef OGG_MUSIC
slouken@63
   313
			case MUS_OGG:
icculus@281
   314
				
slouken@407
   315
				left = OGG_playAudio(music_playing->data.ogg, stream, len);
slouken@63
   316
				break;
slouken@63
   317
#endif
slouken@382
   318
#ifdef FLAC_MUSIC
slouken@382
   319
			case MUS_FLAC:
slouken@407
   320
				left = FLAC_playAudio(music_playing->data.flac, stream, len);
slouken@382
   321
				break;
slouken@382
   322
#endif
slouken@0
   323
#ifdef MP3_MUSIC
slouken@26
   324
			case MUS_MP3:
slouken@407
   325
				left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
slouken@0
   326
				break;
slouken@0
   327
#endif
slouken@357
   328
#ifdef MP3_MAD_MUSIC
slouken@357
   329
			case MUS_MP3_MAD:
slouken@407
   330
				left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
slouken@357
   331
				break;
slouken@357
   332
#endif
slouken@0
   333
			default:
slouken@0
   334
				/* Unknown music type?? */
slouken@0
   335
				break;
slouken@0
   336
		}
slouken@528
   337
	}
slouken@407
   338
slouken@528
   339
skip:
slouken@528
   340
	/* Handle seamless music looping */
slouken@528
   341
	if (left > 0 && left < len) {
icculus@509
   342
		music_halt_or_loop();
slouken@528
   343
		if (music_internal_playing())
icculus@509
   344
			music_mixer(udata, stream+(len-left), left);
slouken@407
   345
	}
slouken@0
   346
}
slouken@0
   347
slouken@0
   348
/* Initialize the music players with a certain desired audio format */
slouken@0
   349
int open_music(SDL_AudioSpec *mixer)
slouken@0
   350
{
slouken@0
   351
#ifdef WAV_MUSIC
slouken@479
   352
	if ( WAVStream_Init(mixer) == 0 ) {
icculus@390
   353
		add_music_decoder("WAVE");
slouken@0
   354
	}
slouken@0
   355
#endif
slouken@481
   356
#ifdef MODPLUG_MUSIC
icculus@484
   357
	if ( modplug_init(mixer) == 0 ) {
slouken@481
   358
		add_music_decoder("MODPLUG");
slouken@481
   359
	}
slouken@481
   360
#endif
slouken@411
   361
#ifdef MOD_MUSIC
slouken@479
   362
	if ( MOD_init(mixer) == 0 ) {
slouken@411
   363
		add_music_decoder("MIKMOD");
slouken@0
   364
	}
slouken@0
   365
#endif
slouken@0
   366
#ifdef MID_MUSIC
slouken@106
   367
#ifdef USE_TIMIDITY_MIDI
slouken@142
   368
	samplesize = mixer->size / mixer->samples;
slouken@100
   369
	if ( Timidity_Init(mixer->freq, mixer->format,
slouken@102
   370
	                    mixer->channels, mixer->samples) == 0 ) {
slouken@100
   371
		timidity_ok = 1;
icculus@390
   372
		add_music_decoder("TIMIDITY");
slouken@0
   373
	} else {
slouken@0
   374
		timidity_ok = 0;
slouken@0
   375
	}
slouken@106
   376
#endif
chewi@506
   377
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   378
	if ( fluidsynth_init(mixer) == 0 ) {
chewi@506
   379
		fluidsynth_ok = 1;
chewi@506
   380
		add_music_decoder("FLUIDSYNTH");
chewi@506
   381
	} else {
chewi@506
   382
		fluidsynth_ok = 0;
chewi@506
   383
	}
chewi@506
   384
#endif
slouken@100
   385
#ifdef USE_NATIVE_MIDI
chewi@506
   386
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   387
	native_midi_ok = !fluidsynth_ok;
chewi@506
   388
	if ( native_midi_ok )
chewi@506
   389
#endif
slouken@106
   390
#ifdef USE_TIMIDITY_MIDI
chewi@506
   391
		native_midi_ok = !timidity_ok;
slouken@415
   392
	if ( !native_midi_ok ) {
slouken@415
   393
		native_midi_ok = (getenv("SDL_NATIVE_MUSIC") != NULL);
slouken@415
   394
	}
slouken@106
   395
	if ( native_midi_ok )
slouken@106
   396
#endif
slouken@101
   397
		native_midi_ok = native_midi_detect();
icculus@390
   398
	if ( native_midi_ok )
icculus@390
   399
		add_music_decoder("NATIVEMIDI");
slouken@100
   400
#endif
slouken@0
   401
#endif
slouken@63
   402
#ifdef OGG_MUSIC
slouken@479
   403
	if ( OGG_init(mixer) == 0 ) {
icculus@390
   404
		add_music_decoder("OGG");
slouken@63
   405
	}
slouken@63
   406
#endif
slouken@382
   407
#ifdef FLAC_MUSIC
slouken@479
   408
	if ( FLAC_init(mixer) == 0 ) {
icculus@390
   409
		add_music_decoder("FLAC");
slouken@382
   410
	}
slouken@382
   411
#endif
slouken@357
   412
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
slouken@0
   413
	/* Keep a copy of the mixer */
slouken@0
   414
	used_mixer = *mixer;
icculus@390
   415
	add_music_decoder("MP3");
slouken@0
   416
#endif
icculus@390
   417
slouken@42
   418
	music_playing = NULL;
megastep@7
   419
	music_stopped = 0;
slouken@0
   420
	Mix_VolumeMusic(SDL_MIX_MAXVOLUME);
slouken@0
   421
slouken@26
   422
	/* Calculate the number of ms for each callback */
slouken@44
   423
	ms_per_step = (int) (((float)mixer->samples * 1000.0) / mixer->freq);
slouken@17
   424
slouken@0
   425
	return(0);
slouken@0
   426
}
slouken@0
   427
slouken@125
   428
/* Portable case-insensitive string compare function */
slouken@125
   429
int MIX_string_equals(const char *str1, const char *str2)
slouken@125
   430
{
slouken@125
   431
	while ( *str1 && *str2 ) {
slouken@125
   432
		if ( toupper((unsigned char)*str1) !=
slouken@125
   433
		     toupper((unsigned char)*str2) )
slouken@125
   434
			break;
slouken@125
   435
		++str1;
slouken@125
   436
		++str2;
slouken@125
   437
	}
slouken@125
   438
	return (!*str1 && !*str2);
slouken@125
   439
}
slouken@125
   440
slouken@542
   441
static int detect_mp3(Uint8 *magic)
slouken@542
   442
{
slouken@542
   443
	if ( strncmp((char *)magic, "ID3", 3) == 0 ) {
slouken@542
   444
		return 1;
slouken@542
   445
	}
slouken@542
   446
slouken@542
   447
	/* Detection code lifted from SMPEG */
slouken@542
   448
	if(((magic[0] & 0xff) != 0xff) || // No sync bits
slouken@542
   449
	   ((magic[1] & 0xf0) != 0xf0) || //
slouken@542
   450
	   ((magic[2] & 0xf0) == 0x00) || // Bitrate is 0
slouken@542
   451
	   ((magic[2] & 0xf0) == 0xf0) || // Bitrate is 15
slouken@542
   452
	   ((magic[2] & 0x0c) == 0x0c) || // Frequency is 3
slouken@542
   453
	   ((magic[1] & 0x06) == 0x00)) { // Layer is 4
slouken@542
   454
		return(0);
slouken@542
   455
	}
slouken@542
   456
	return 1;
slouken@542
   457
}
slouken@542
   458
slouken@542
   459
/* MUS_MOD can't be auto-detected. If no other format was detected, MOD is
slouken@542
   460
 * assumed and MUS_MOD will be returned, meaning that the format might not
slouken@542
   461
 * actually be MOD-based.
slouken@542
   462
 *
slouken@542
   463
 * Returns MUS_NONE in case of errors. */
slouken@542
   464
static Mix_MusicType detect_music_type(SDL_RWops *rw)
slouken@542
   465
{
slouken@542
   466
	Uint8 magic[5];
slouken@542
   467
	Uint8 moremagic[9];
slouken@542
   468
slouken@542
   469
	int start = SDL_RWtell(rw);
slouken@542
   470
	if (SDL_RWread(rw, magic, 1, 4) != 4 || SDL_RWread(rw, moremagic, 1, 8) != 8 ) {
slouken@542
   471
		Mix_SetError("Couldn't read from RWops");
slouken@542
   472
		return MUS_NONE;
slouken@542
   473
	}
slouken@542
   474
	SDL_RWseek(rw, start, RW_SEEK_SET);
slouken@542
   475
	magic[4]='\0';
slouken@542
   476
	moremagic[8] = '\0';
slouken@542
   477
slouken@542
   478
	/* WAVE files have the magic four bytes "RIFF"
slouken@542
   479
	   AIFF files have the magic 12 bytes "FORM" XXXX "AIFF" */
slouken@542
   480
	if (((strcmp((char *)magic, "RIFF") == 0) && (strcmp((char *)(moremagic+4), "WAVE") == 0)) ||
slouken@542
   481
		(strcmp((char *)magic, "FORM") == 0)) {
slouken@542
   482
		return MUS_WAV;
slouken@542
   483
	}
slouken@542
   484
slouken@542
   485
	/* Ogg Vorbis files have the magic four bytes "OggS" */
slouken@542
   486
	if (strcmp((char *)magic, "OggS") == 0) {
slouken@542
   487
		return MUS_OGG;
slouken@542
   488
	}
slouken@542
   489
slouken@542
   490
	/* FLAC files have the magic four bytes "fLaC" */
slouken@542
   491
	if (strcmp((char *)magic, "fLaC") == 0) {
slouken@542
   492
		return MUS_FLAC;
slouken@542
   493
	}
slouken@542
   494
slouken@542
   495
	/* MIDI files have the magic four bytes "MThd" */
slouken@542
   496
	if (strcmp((char *)magic, "MThd") == 0) {
slouken@542
   497
		return MUS_MID;
slouken@542
   498
	}
slouken@542
   499
slouken@542
   500
	if (detect_mp3(magic)) {
slouken@542
   501
		return MUS_MP3;
slouken@542
   502
	}
slouken@542
   503
slouken@542
   504
	/* Assume MOD format.
slouken@542
   505
	 *
slouken@542
   506
	 * Apparently there is no way to check if the file is really a MOD,
slouken@542
   507
	 * or there are too many formats supported by MikMod/ModPlug, or
slouken@542
   508
	 * MikMod/ModPlug does this check by itself. */
slouken@542
   509
	return MUS_MOD;
slouken@542
   510
}
slouken@542
   511
slouken@0
   512
/* Load a music file */
slouken@0
   513
Mix_Music *Mix_LoadMUS(const char *file)
slouken@0
   514
{
slouken@542
   515
	SDL_RWops *rw;
slouken@542
   516
	Mix_Music *music;
slouken@542
   517
	Mix_MusicType type;
slouken@542
   518
	char *ext = strrchr(file, '.');
slouken@542
   519
slouken@542
   520
#ifdef CMD_MUSIC
slouken@542
   521
	if ( music_cmd ) {
slouken@542
   522
		/* Allocate memory for the music structure */
slouken@561
   523
		music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
slouken@542
   524
		if ( music == NULL ) {
slouken@542
   525
			Mix_SetError("Out of memory");
slouken@542
   526
			return(NULL);
slouken@542
   527
		}
slouken@542
   528
		music->error = 0;
slouken@542
   529
		music->type = MUS_CMD;
slouken@542
   530
		music->data.cmd = MusicCMD_LoadSong(music_cmd, file);
slouken@542
   531
		if ( music->data.cmd == NULL ) {
slouken@561
   532
			SDL_free(music);
sezeroz@870
   533
			music = NULL;
slouken@542
   534
		}
slouken@542
   535
		return music;
slouken@542
   536
	}
slouken@542
   537
#endif
slouken@542
   538
slouken@542
   539
	rw = SDL_RWFromFile(file, "rb");
slouken@542
   540
	if ( rw == NULL ) {
slouken@542
   541
		Mix_SetError("Couldn't open '%s'", file);
slouken@542
   542
		return NULL;
slouken@542
   543
	}
slouken@542
   544
slouken@542
   545
	/* Use the extension as a first guess on the file type */
slouken@542
   546
	type = MUS_NONE;
slouken@542
   547
	ext = strrchr(file, '.');
slouken@542
   548
	/* No need to guard these with #ifdef *_MUSIC stuff,
slouken@542
   549
	 * since we simply call Mix_LoadMUSType_RW() later */
slouken@542
   550
	if ( ext ) {
slouken@542
   551
		++ext; /* skip the dot in the extension */
slouken@542
   552
		if ( MIX_string_equals(ext, "WAV") ) {
slouken@542
   553
			type = MUS_WAV;
slouken@542
   554
		} else if ( MIX_string_equals(ext, "MID") ||
slouken@542
   555
		            MIX_string_equals(ext, "MIDI") ||
slouken@542
   556
		            MIX_string_equals(ext, "KAR") ) {
slouken@542
   557
			type = MUS_MID;
slouken@542
   558
		} else if ( MIX_string_equals(ext, "OGG") ) {
slouken@542
   559
			type = MUS_OGG;
slouken@542
   560
		} else if ( MIX_string_equals(ext, "FLAC") ) {
slouken@542
   561
			type = MUS_FLAC;
slouken@542
   562
		} else 	if ( MIX_string_equals(ext, "MPG") ||
slouken@542
   563
		             MIX_string_equals(ext, "MPEG") ||
slouken@542
   564
		             MIX_string_equals(ext, "MP3") ||
slouken@542
   565
		             MIX_string_equals(ext, "MAD") ) {
slouken@542
   566
			type = MUS_MP3;
slouken@542
   567
		}
slouken@542
   568
	}
slouken@542
   569
	if ( type == MUS_NONE ) {
slouken@542
   570
		type = detect_music_type(rw);
slouken@542
   571
	}
slouken@542
   572
slouken@542
   573
	/* We need to know if a specific error occurs; if not, we'll set a
slouken@542
   574
	 * generic one, so we clear the current one. */
slouken@542
   575
	Mix_SetError("");
slouken@542
   576
	music = Mix_LoadMUSType_RW(rw, type, SDL_TRUE);
slouken@542
   577
	if ( music == NULL && Mix_GetError()[0] == '\0' ) {
slouken@542
   578
		SDL_FreeRW(rw);
slouken@542
   579
		Mix_SetError("Couldn't open '%s'", file);
slouken@542
   580
	}
slouken@542
   581
	return music;
slouken@542
   582
}
slouken@542
   583
slouken@542
   584
Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw)
slouken@542
   585
{
slouken@542
   586
	return Mix_LoadMUSType_RW(rw, MUS_NONE, SDL_FALSE);
slouken@542
   587
}
slouken@542
   588
slouken@542
   589
Mix_Music *Mix_LoadMUSType_RW(SDL_RWops *rw, Mix_MusicType type, int freesrc)
slouken@542
   590
{
slouken@0
   591
	Mix_Music *music;
slouken@0
   592
slouken@542
   593
	if (!rw) {
slouken@542
   594
		Mix_SetError("RWops pointer is NULL");
slouken@542
   595
		return NULL;
slouken@542
   596
	}
slouken@542
   597
slouken@542
   598
	/* If the caller wants auto-detection, figure out what kind of file
slouken@542
   599
	 * this is. */
slouken@542
   600
	if (type == MUS_NONE) {
slouken@542
   601
		if ((type = detect_music_type(rw)) == MUS_NONE) {
slouken@542
   602
			/* Don't call Mix_SetError() here since detect_music_type()
slouken@542
   603
			 * does that. */
slouken@542
   604
			return NULL;
slouken@0
   605
		}
slouken@0
   606
	}
slouken@125
   607
slouken@0
   608
	/* Allocate memory for the music structure */
slouken@561
   609
	music = (Mix_Music *)SDL_malloc(sizeof(Mix_Music));
slouken@542
   610
	if (music == NULL ) {
slouken@26
   611
		Mix_SetError("Out of memory");
slouken@542
   612
		return NULL;
slouken@0
   613
	}
slouken@0
   614
	music->error = 0;
slouken@0
   615
slouken@542
   616
	switch (type) {
slouken@542
   617
#ifdef WAV_MUSIC
slouken@542
   618
	case MUS_WAV:
slouken@542
   619
		/* The WAVE loader needs the first 4 bytes of the header */
slouken@542
   620
		{
slouken@542
   621
			Uint8 magic[5];
slouken@542
   622
			int start = SDL_RWtell(rw);
slouken@542
   623
			if (SDL_RWread(rw, magic, 1, 4) != 4) {
slouken@542
   624
				Mix_SetError("Couldn't read from RWops");
sezeroz@871
   625
				SDL_free(music);
slouken@542
   626
				return MUS_NONE;
slouken@542
   627
			}
slouken@542
   628
			SDL_RWseek(rw, start, RW_SEEK_SET);
slouken@542
   629
			magic[4] = '\0';
slouken@542
   630
			music->type = MUS_WAV;
slouken@542
   631
			music->data.wave = WAVStream_LoadSong_RW(rw, (char *)magic, freesrc);
slouken@542
   632
		}
slouken@542
   633
		if (music->data.wave == NULL) {
slouken@0
   634
			music->error = 1;
slouken@0
   635
		}
slouken@542
   636
		break;
slouken@0
   637
#endif
slouken@542
   638
#ifdef OGG_MUSIC
slouken@542
   639
	case MUS_OGG:
slouken@542
   640
		music->type = MUS_OGG;
slouken@542
   641
		music->data.ogg = OGG_new_RW(rw, freesrc);
slouken@542
   642
		if ( music->data.ogg == NULL ) {
slouken@0
   643
			music->error = 1;
slouken@0
   644
		}
slouken@542
   645
		break;
slouken@542
   646
#endif
slouken@542
   647
#ifdef FLAC_MUSIC
slouken@542
   648
	case MUS_FLAC:
slouken@542
   649
		music->type = MUS_FLAC;
slouken@542
   650
		music->data.flac = FLAC_new_RW(rw, freesrc);
slouken@542
   651
		if ( music->data.flac == NULL ) {
slouken@542
   652
			music->error = 1;
slouken@542
   653
		}
slouken@542
   654
		break;
slouken@542
   655
#endif
slouken@542
   656
#ifdef MP3_MUSIC
slouken@542
   657
	case MUS_MP3:
slouken@542
   658
		if ( Mix_Init(MIX_INIT_MP3) ) {
slouken@542
   659
			SMPEG_Info info;
slouken@542
   660
			music->type = MUS_MP3;
slouken@542
   661
			music->data.mp3 = smpeg.SMPEG_new_rwops(rw, &info, 0);
slouken@542
   662
			if ( !info.has_audio ) {
slouken@542
   663
				Mix_SetError("MPEG file does not have any audio stream.");
slouken@542
   664
				music->error = 1;
slouken@542
   665
			} else {
slouken@542
   666
				smpeg.SMPEG_actualSpec(music->data.mp3, &used_mixer);
slouken@542
   667
			}
slouken@542
   668
		} else {
slouken@542
   669
			music->error = 1;
slouken@542
   670
		}
slouken@542
   671
		break;
slouken@542
   672
#elif defined(MP3_MAD_MUSIC)
slouken@542
   673
	case MUS_MP3:
slouken@542
   674
		music->type = MUS_MP3_MAD;
slouken@542
   675
		music->data.mp3_mad = mad_openFileRW(rw, &used_mixer, freesrc);
slouken@542
   676
		if (music->data.mp3_mad == 0) {
slouken@542
   677
			Mix_SetError("Could not initialize MPEG stream.");
slouken@542
   678
			music->error = 1;
slouken@542
   679
		}
slouken@542
   680
		break;
slouken@0
   681
#endif
slouken@0
   682
#ifdef MID_MUSIC
slouken@542
   683
	case MUS_MID:
slouken@0
   684
		music->type = MUS_MID;
slouken@98
   685
#ifdef USE_NATIVE_MIDI
slouken@542
   686
		if ( native_midi_ok ) {
slouken@542
   687
			music->data.nativemidi = native_midi_loadsong_RW(rw, freesrc);
slouken@98
   688
	  		if ( music->data.nativemidi == NULL ) {
slouken@98
   689
		  		Mix_SetError("%s", native_midi_error());
slouken@98
   690
			  	music->error = 1;
slouken@98
   691
			}
slouken@542
   692
			break;
slouken@542
   693
		}
chewi@506
   694
#endif
chewi@506
   695
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   696
		if ( fluidsynth_ok ) {
slouken@542
   697
			music->data.fluidsynthmidi = fluidsynth_loadsong_RW(rw, freesrc);
chewi@506
   698
			if ( music->data.fluidsynthmidi == NULL ) {
chewi@506
   699
				music->error = 1;
chewi@506
   700
			}
slouken@542
   701
			break;
chewi@506
   702
		}
slouken@98
   703
#endif
slouken@106
   704
#ifdef USE_TIMIDITY_MIDI
slouken@0
   705
		if ( timidity_ok ) {
slouken@542
   706
			music->data.midi = Timidity_LoadSong_RW(rw, freesrc);
slouken@0
   707
			if ( music->data.midi == NULL ) {
slouken@26
   708
				Mix_SetError("%s", Timidity_Error());
slouken@0
   709
				music->error = 1;
slouken@0
   710
			}
slouken@98
   711
		} else {
slouken@26
   712
			Mix_SetError("%s", Timidity_Error());
slouken@0
   713
			music->error = 1;
slouken@0
   714
		}
slouken@106
   715
#endif
slouken@542
   716
		break;
slouken@0
   717
#endif
slouken@542
   718
#if defined(MODPLUG_MUSIC) || defined(MOD_MUSIC)
slouken@542
   719
	case MUS_MOD:
slouken@542
   720
		music->error = 1;
slouken@542
   721
#ifdef MODPLUG_MUSIC
slouken@542
   722
		if ( music->error ) {
slouken@542
   723
			music->type = MUS_MODPLUG;
slouken@542
   724
			music->data.modplug = modplug_new_RW(rw, freesrc);
slouken@542
   725
			if ( music->data.modplug ) {
slouken@542
   726
				music->error = 0;
slouken@542
   727
			}
slouken@63
   728
		}
slouken@481
   729
#endif
slouken@411
   730
#ifdef MOD_MUSIC
slouken@542
   731
		if ( music->error ) {
slouken@542
   732
			music->type = MUS_MOD;
slouken@542
   733
			music->data.module = MOD_new_RW(rw, freesrc);
slouken@542
   734
			if ( music->data.module ) {
slouken@542
   735
				music->error = 0;
slouken@542
   736
			}
slouken@0
   737
		}
slouken@0
   738
#endif
slouken@542
   739
		break;
slouken@542
   740
#endif
slouken@542
   741
slouken@542
   742
	default:
slouken@26
   743
		Mix_SetError("Unrecognized music format");
slouken@542
   744
		music->error=1;
slouken@542
   745
	} /* switch (want) */
chewi@506
   746
slouken@542
   747
slouken@542
   748
	if (music->error) {
slouken@561
   749
		SDL_free(music);
slouken@542
   750
		music=NULL;
slouken@0
   751
	}
slouken@0
   752
	return(music);
slouken@0
   753
}
slouken@0
   754
slouken@0
   755
/* Free a music chunk previously loaded */
slouken@0
   756
void Mix_FreeMusic(Mix_Music *music)
slouken@0
   757
{
slouken@0
   758
	if ( music ) {
slouken@173
   759
		/* Stop the music if it's currently playing */
slouken@173
   760
		SDL_LockAudio();
slouken@173
   761
		if ( music == music_playing ) {
slouken@173
   762
			/* Wait for any fade out to finish */
slouken@173
   763
			while ( music->fading == MIX_FADING_OUT ) {
slouken@173
   764
				SDL_UnlockAudio();
slouken@173
   765
				SDL_Delay(100);
slouken@173
   766
				SDL_LockAudio();
slouken@173
   767
			}
slouken@173
   768
			if ( music == music_playing ) {
slouken@173
   769
				music_internal_halt();
megastep@4
   770
			}
slouken@0
   771
		}
slouken@173
   772
		SDL_UnlockAudio();
slouken@0
   773
		switch (music->type) {
slouken@0
   774
#ifdef CMD_MUSIC
slouken@0
   775
			case MUS_CMD:
slouken@0
   776
				MusicCMD_FreeSong(music->data.cmd);
slouken@0
   777
				break;
slouken@0
   778
#endif
slouken@0
   779
#ifdef WAV_MUSIC
slouken@0
   780
			case MUS_WAV:
slouken@0
   781
				WAVStream_FreeSong(music->data.wave);
slouken@0
   782
				break;
slouken@0
   783
#endif
slouken@481
   784
#ifdef MODPLUG_MUSIC
slouken@481
   785
			case MUS_MODPLUG:
slouken@481
   786
				modplug_delete(music->data.modplug);
slouken@481
   787
				break;
slouken@481
   788
#endif
slouken@411
   789
#ifdef MOD_MUSIC
slouken@0
   790
			case MUS_MOD:
slouken@411
   791
				MOD_delete(music->data.module);
slouken@0
   792
				break;
slouken@0
   793
#endif
slouken@0
   794
#ifdef MID_MUSIC
slouken@0
   795
			case MUS_MID:
slouken@98
   796
#ifdef USE_NATIVE_MIDI
slouken@98
   797
  				if ( native_midi_ok ) {
slouken@98
   798
					native_midi_freesong(music->data.nativemidi);
chewi@506
   799
					goto skip;
chewi@506
   800
				}
chewi@506
   801
#endif
chewi@506
   802
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   803
				if ( fluidsynth_ok ) {
chewi@506
   804
					fluidsynth_freesong(music->data.fluidsynthmidi);
chewi@506
   805
					goto skip;
chewi@506
   806
				}
slouken@98
   807
#endif
slouken@106
   808
#ifdef USE_TIMIDITY_MIDI
slouken@98
   809
				if ( timidity_ok ) {
slouken@98
   810
					Timidity_FreeSong(music->data.midi);
chewi@506
   811
					goto skip;
slouken@98
   812
				}
slouken@106
   813
#endif
slouken@0
   814
				break;
slouken@0
   815
#endif
slouken@63
   816
#ifdef OGG_MUSIC
slouken@63
   817
			case MUS_OGG:
slouken@63
   818
				OGG_delete(music->data.ogg);
slouken@63
   819
				break;
slouken@63
   820
#endif
slouken@382
   821
#ifdef FLAC_MUSIC
slouken@382
   822
			case MUS_FLAC:
slouken@382
   823
				FLAC_delete(music->data.flac);
slouken@382
   824
				break;
slouken@382
   825
#endif
slouken@0
   826
#ifdef MP3_MUSIC
slouken@26
   827
			case MUS_MP3:
slouken@314
   828
				smpeg.SMPEG_delete(music->data.mp3);
slouken@0
   829
				break;
slouken@0
   830
#endif
slouken@357
   831
#ifdef MP3_MAD_MUSIC
slouken@357
   832
			case MUS_MP3_MAD:
slouken@357
   833
				mad_closeFile(music->data.mp3_mad);
slouken@357
   834
				break;
slouken@357
   835
#endif
slouken@0
   836
			default:
slouken@0
   837
				/* Unknown music type?? */
slouken@0
   838
				break;
slouken@0
   839
		}
chewi@506
   840
chewi@506
   841
    skip:
slouken@561
   842
		SDL_free(music);
slouken@0
   843
	}
slouken@0
   844
}
slouken@0
   845
slouken@177
   846
/* Find out the music format of a mixer music, or the currently playing
slouken@177
   847
   music, if 'music' is NULL.
slouken@177
   848
*/
slouken@177
   849
Mix_MusicType Mix_GetMusicType(const Mix_Music *music)
slouken@177
   850
{
slouken@177
   851
	Mix_MusicType type = MUS_NONE;
slouken@177
   852
slouken@177
   853
	if ( music ) {
slouken@177
   854
		type = music->type;
slouken@177
   855
	} else {
slouken@177
   856
		SDL_LockAudio();
slouken@177
   857
		if ( music_playing ) {
slouken@177
   858
			type = music_playing->type;
slouken@177
   859
		}
slouken@177
   860
		SDL_UnlockAudio();
slouken@177
   861
	}
slouken@177
   862
	return(type);
slouken@177
   863
}
slouken@177
   864
slouken@173
   865
/* Play a music chunk.  Returns 0, or -1 if there was an error.
slouken@173
   866
 */
slouken@173
   867
static int music_internal_play(Mix_Music *music, double position)
slouken@0
   868
{
slouken@173
   869
	int retval = 0;
megastep@10
   870
slouken@547
   871
#if defined(__MACOSX__) && defined(USE_NATIVE_MIDI)
slouken@531
   872
	/* This fixes a bug with native MIDI on Mac OS X, where you
slouken@531
   873
	   can't really stop and restart MIDI from the audio callback.
slouken@529
   874
	*/
slouken@531
   875
	if ( music == music_playing && music->type == MUS_MID && native_midi_ok ) {
slouken@531
   876
		/* Just a seek suffices to restart playing */
slouken@531
   877
		music_internal_position(position);
slouken@531
   878
		return 0;
slouken@529
   879
	}
slouken@531
   880
#endif
slouken@529
   881
slouken@173
   882
	/* Note the music we're playing */
slouken@173
   883
	if ( music_playing ) {
slouken@173
   884
		music_internal_halt();
slouken@173
   885
	}
slouken@173
   886
	music_playing = music;
slouken@173
   887
slouken@173
   888
	/* Set the initial volume */
icculus@237
   889
	if ( music->type != MUS_MOD ) {
icculus@237
   890
		music_internal_initialize_volume();
slouken@173
   891
	}
slouken@173
   892
slouken@173
   893
	/* Set up for playback */
slouken@0
   894
	switch (music->type) {
slouken@0
   895
#ifdef CMD_MUSIC
slouken@173
   896
	    case MUS_CMD:
slouken@173
   897
		MusicCMD_Start(music->data.cmd);
slouken@173
   898
		break;
slouken@0
   899
#endif
slouken@0
   900
#ifdef WAV_MUSIC
slouken@173
   901
	    case MUS_WAV:
slouken@173
   902
		WAVStream_Start(music->data.wave);
slouken@173
   903
		break;
slouken@0
   904
#endif
slouken@481
   905
#ifdef MODPLUG_MUSIC
slouken@481
   906
	    case MUS_MODPLUG:
slouken@481
   907
		/* can't set volume until file is loaded, so finally set it now */
slouken@481
   908
		music_internal_initialize_volume();
slouken@481
   909
		modplug_play(music->data.modplug);
slouken@481
   910
		break;
slouken@481
   911
#endif
slouken@411
   912
#ifdef MOD_MUSIC
slouken@173
   913
	    case MUS_MOD:
slouken@411
   914
		MOD_play(music->data.module);
icculus@237
   915
		/* Player_SetVolume() does nothing before Player_Start() */
icculus@237
   916
		music_internal_initialize_volume();
slouken@173
   917
		break;
slouken@0
   918
#endif
slouken@0
   919
#ifdef MID_MUSIC
slouken@173
   920
	    case MUS_MID:
slouken@98
   921
#ifdef USE_NATIVE_MIDI
slouken@173
   922
		if ( native_midi_ok ) {
slouken@533
   923
			native_midi_start(music->data.nativemidi, music_loops);
chewi@506
   924
			goto skip;
chewi@506
   925
		}
chewi@506
   926
#endif
chewi@506
   927
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
   928
		if (fluidsynth_ok ) {
chewi@506
   929
			fluidsynth_start(music->data.fluidsynthmidi);
chewi@506
   930
			goto skip;
chewi@506
   931
		}
slouken@98
   932
#endif
slouken@106
   933
#ifdef USE_TIMIDITY_MIDI
slouken@173
   934
		if ( timidity_ok ) {
slouken@173
   935
			Timidity_Start(music->data.midi);
chewi@506
   936
			goto skip;
slouken@173
   937
		}
slouken@106
   938
#endif
slouken@173
   939
		break;
slouken@0
   940
#endif
slouken@63
   941
#ifdef OGG_MUSIC
slouken@173
   942
	    case MUS_OGG:
slouken@173
   943
		OGG_play(music->data.ogg);
slouken@173
   944
		break;
slouken@63
   945
#endif
slouken@382
   946
#ifdef FLAC_MUSIC
slouken@382
   947
	    case MUS_FLAC:
slouken@382
   948
		FLAC_play(music->data.flac);
slouken@382
   949
		break;
slouken@382
   950
#endif
slouken@0
   951
#ifdef MP3_MUSIC
slouken@173
   952
	    case MUS_MP3:
slouken@314
   953
		smpeg.SMPEG_enableaudio(music->data.mp3,1);
slouken@314
   954
		smpeg.SMPEG_enablevideo(music->data.mp3,0);
slouken@314
   955
		smpeg.SMPEG_play(music_playing->data.mp3);
slouken@173
   956
		break;
slouken@0
   957
#endif
slouken@357
   958
#ifdef MP3_MAD_MUSIC
slouken@357
   959
	    case MUS_MP3_MAD:
slouken@357
   960
		mad_start(music->data.mp3_mad);
slouken@357
   961
		break;
slouken@357
   962
#endif
slouken@173
   963
	    default:
slouken@173
   964
		Mix_SetError("Can't play unknown music type");
slouken@173
   965
		retval = -1;
slouken@173
   966
		break;
slouken@0
   967
	}
slouken@173
   968
chewi@506
   969
skip:
slouken@173
   970
	/* Set the playback position, note any errors if an offset is used */
slouken@173
   971
	if ( retval == 0 ) {
slouken@173
   972
		if ( position > 0.0 ) {
slouken@173
   973
			if ( music_internal_position(position) < 0 ) {
slouken@173
   974
				Mix_SetError("Position not implemented for music type");
slouken@173
   975
				retval = -1;
slouken@173
   976
			}
slouken@173
   977
		} else {
slouken@173
   978
			music_internal_position(0.0);
slouken@173
   979
		}
slouken@173
   980
	}
slouken@173
   981
slouken@173
   982
	/* If the setup failed, we're not playing any music anymore */
slouken@173
   983
	if ( retval < 0 ) {
slouken@173
   984
		music_playing = NULL;
slouken@173
   985
	}
slouken@173
   986
	return(retval);
slouken@173
   987
}
slouken@173
   988
int Mix_FadeInMusicPos(Mix_Music *music, int loops, int ms, double position)
slouken@173
   989
{
slouken@173
   990
	int retval;
slouken@173
   991
slouken@499
   992
	if ( ms_per_step == 0 ) {
slouken@499
   993
		SDL_SetError("Audio device hasn't been opened");
slouken@499
   994
		return(-1);
slouken@499
   995
	}
slouken@499
   996
slouken@173
   997
	/* Don't play null pointers :-) */
slouken@173
   998
	if ( music == NULL ) {
slouken@173
   999
		Mix_SetError("music parameter was NULL");
slouken@173
  1000
		return(-1);
slouken@173
  1001
	}
slouken@173
  1002
slouken@173
  1003
	/* Setup the data */
slouken@173
  1004
	if ( ms ) {
slouken@173
  1005
		music->fading = MIX_FADING_IN;
slouken@173
  1006
	} else {
slouken@173
  1007
		music->fading = MIX_NO_FADING;
slouken@173
  1008
	}
slouken@173
  1009
	music->fade_step = 0;
slouken@173
  1010
	music->fade_steps = ms/ms_per_step;
slouken@173
  1011
slouken@173
  1012
	/* Play the puppy */
slouken@173
  1013
	SDL_LockAudio();
slouken@173
  1014
	/* If the current music is fading out, wait for the fade to complete */
slouken@173
  1015
	while ( music_playing && (music_playing->fading == MIX_FADING_OUT) ) {
slouken@173
  1016
		SDL_UnlockAudio();
slouken@173
  1017
		SDL_Delay(100);
slouken@173
  1018
		SDL_LockAudio();
slouken@173
  1019
	}
slouken@533
  1020
	if (loops == 1) {
slouken@533
  1021
		/* Loop is the number of times to play the audio */
slouken@533
  1022
		loops = 0;
slouken@533
  1023
	}
slouken@173
  1024
	music_loops = loops;
slouken@173
  1025
	retval = music_internal_play(music, position);
sezeroz@864
  1026
	music_active = (retval == 0);
slouken@173
  1027
	SDL_UnlockAudio();
slouken@173
  1028
slouken@173
  1029
	return(retval);
slouken@173
  1030
}
slouken@173
  1031
int Mix_FadeInMusic(Mix_Music *music, int loops, int ms)
slouken@173
  1032
{
slouken@173
  1033
	return Mix_FadeInMusicPos(music, loops, ms, 0.0);
slouken@173
  1034
}
slouken@173
  1035
int Mix_PlayMusic(Mix_Music *music, int loops)
slouken@173
  1036
{
slouken@173
  1037
	return Mix_FadeInMusicPos(music, loops, 0, 0.0);
megastep@10
  1038
}
megastep@10
  1039
slouken@173
  1040
/* Set the playing music position */
slouken@173
  1041
int music_internal_position(double position)
megastep@10
  1042
{
slouken@173
  1043
	int retval = 0;
slouken@173
  1044
slouken@173
  1045
	switch (music_playing->type) {
slouken@481
  1046
#ifdef MODPLUG_MUSIC
slouken@481
  1047
	    case MUS_MODPLUG:
slouken@481
  1048
		modplug_jump_to_time(music_playing->data.modplug, position);
slouken@481
  1049
		break;
slouken@481
  1050
#endif
slouken@411
  1051
#ifdef MOD_MUSIC
slouken@173
  1052
	    case MUS_MOD:
slouken@411
  1053
		MOD_jump_to_time(music_playing->data.module, position);
slouken@173
  1054
		break;
slouken@173
  1055
#endif
slouken@173
  1056
#ifdef OGG_MUSIC
slouken@173
  1057
	    case MUS_OGG:
slouken@173
  1058
		OGG_jump_to_time(music_playing->data.ogg, position);
slouken@173
  1059
		break;
slouken@173
  1060
#endif
slouken@382
  1061
#ifdef FLAC_MUSIC
slouken@382
  1062
	    case MUS_FLAC:
slouken@382
  1063
		FLAC_jump_to_time(music_playing->data.flac, position);
slouken@382
  1064
		break;
slouken@382
  1065
#endif
slouken@173
  1066
#ifdef MP3_MUSIC
slouken@173
  1067
	    case MUS_MP3:
slouken@565
  1068
		smpeg.SMPEG_rewind(music_playing->data.mp3);
slouken@565
  1069
		smpeg.SMPEG_play(music_playing->data.mp3);
slouken@176
  1070
		if ( position > 0.0 ) {
slouken@314
  1071
			smpeg.SMPEG_skip(music_playing->data.mp3, (float)position);
slouken@173
  1072
		}
slouken@173
  1073
		break;
slouken@173
  1074
#endif
slouken@357
  1075
#ifdef MP3_MAD_MUSIC
slouken@357
  1076
	    case MUS_MP3_MAD:
slouken@357
  1077
		mad_seek(music_playing->data.mp3_mad, position);
slouken@357
  1078
		break;
slouken@357
  1079
#endif
slouken@173
  1080
	    default:
slouken@173
  1081
		/* TODO: Implement this for other music backends */
slouken@173
  1082
		retval = -1;
slouken@173
  1083
		break;
megastep@10
  1084
	}
slouken@173
  1085
	return(retval);
megastep@4
  1086
}
slouken@155
  1087
int Mix_SetMusicPosition(double position)
slouken@154
  1088
{
slouken@173
  1089
	int retval;
slouken@173
  1090
slouken@173
  1091
	SDL_LockAudio();
slouken@173
  1092
	if ( music_playing ) {
slouken@173
  1093
		retval = music_internal_position(position);
slouken@173
  1094
		if ( retval < 0 ) {
slouken@173
  1095
			Mix_SetError("Position not implemented for music type");
slouken@154
  1096
		}
slouken@173
  1097
	} else {
slouken@173
  1098
		Mix_SetError("Music isn't playing");
slouken@173
  1099
		retval = -1;
slouken@154
  1100
	}
slouken@173
  1101
	SDL_UnlockAudio();
slouken@154
  1102
slouken@173
  1103
	return(retval);
slouken@154
  1104
}
slouken@154
  1105
icculus@237
  1106
/* Set the music's initial volume */
icculus@237
  1107
static void music_internal_initialize_volume(void)
icculus@237
  1108
{
icculus@237
  1109
	if ( music_playing->fading == MIX_FADING_IN ) {
icculus@237
  1110
		music_internal_volume(0);
icculus@237
  1111
	} else {
icculus@237
  1112
		music_internal_volume(music_volume);
icculus@237
  1113
	}
icculus@237
  1114
}
icculus@237
  1115
slouken@0
  1116
/* Set the music volume */
slouken@173
  1117
static void music_internal_volume(int volume)
slouken@173
  1118
{
slouken@173
  1119
	switch (music_playing->type) {
slouken@173
  1120
#ifdef CMD_MUSIC
slouken@173
  1121
	    case MUS_CMD:
slouken@173
  1122
		MusicCMD_SetVolume(volume);
slouken@173
  1123
		break;
slouken@173
  1124
#endif
slouken@173
  1125
#ifdef WAV_MUSIC
slouken@173
  1126
	    case MUS_WAV:
slouken@173
  1127
		WAVStream_SetVolume(volume);
slouken@173
  1128
		break;
slouken@173
  1129
#endif
slouken@481
  1130
#ifdef MODPLUG_MUSIC
slouken@481
  1131
	    case MUS_MODPLUG:
slouken@481
  1132
		modplug_setvolume(music_playing->data.modplug, volume);
slouken@481
  1133
		break;
slouken@481
  1134
#endif
slouken@411
  1135
#ifdef MOD_MUSIC
slouken@173
  1136
	    case MUS_MOD:
slouken@411
  1137
		MOD_setvolume(music_playing->data.module, volume);
slouken@173
  1138
		break;
slouken@173
  1139
#endif
slouken@173
  1140
#ifdef MID_MUSIC
slouken@173
  1141
	    case MUS_MID:
slouken@173
  1142
#ifdef USE_NATIVE_MIDI
slouken@173
  1143
		if ( native_midi_ok ) {
slouken@173
  1144
			native_midi_setvolume(volume);
chewi@506
  1145
			return;
chewi@506
  1146
		}
chewi@506
  1147
#endif
chewi@506
  1148
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1149
		if ( fluidsynth_ok ) {
chewi@506
  1150
			fluidsynth_setvolume(music_playing->data.fluidsynthmidi, volume);
chewi@506
  1151
			return;
chewi@506
  1152
		}
slouken@173
  1153
#endif
slouken@173
  1154
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1155
		if ( timidity_ok ) {
slouken@173
  1156
			Timidity_SetVolume(volume);
chewi@506
  1157
			return;
slouken@173
  1158
		}
slouken@173
  1159
#endif
slouken@173
  1160
		break;
slouken@173
  1161
#endif
slouken@173
  1162
#ifdef OGG_MUSIC
slouken@173
  1163
	    case MUS_OGG:
slouken@173
  1164
		OGG_setvolume(music_playing->data.ogg, volume);
slouken@173
  1165
		break;
slouken@173
  1166
#endif
slouken@382
  1167
#ifdef FLAC_MUSIC
slouken@382
  1168
	    case MUS_FLAC:
slouken@382
  1169
		FLAC_setvolume(music_playing->data.flac, volume);
slouken@382
  1170
		break;
slouken@382
  1171
#endif
slouken@173
  1172
#ifdef MP3_MUSIC
slouken@173
  1173
	    case MUS_MP3:
slouken@314
  1174
		smpeg.SMPEG_setvolume(music_playing->data.mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
slouken@173
  1175
		break;
slouken@173
  1176
#endif
slouken@357
  1177
#ifdef MP3_MAD_MUSIC
slouken@357
  1178
	    case MUS_MP3_MAD:
slouken@357
  1179
		mad_setVolume(music_playing->data.mp3_mad, volume);
slouken@357
  1180
		break;
slouken@357
  1181
#endif
slouken@173
  1182
	    default:
slouken@173
  1183
		/* Unknown music type?? */
slouken@173
  1184
		break;
slouken@173
  1185
	}
slouken@173
  1186
}
slouken@0
  1187
int Mix_VolumeMusic(int volume)
slouken@0
  1188
{
slouken@0
  1189
	int prev_volume;
slouken@0
  1190
slouken@0
  1191
	prev_volume = music_volume;
megastep@4
  1192
	if ( volume < 0 ) {
slouken@163
  1193
		return prev_volume;
megastep@4
  1194
	}
megastep@4
  1195
	if ( volume > SDL_MIX_MAXVOLUME ) {
megastep@4
  1196
		volume = SDL_MIX_MAXVOLUME;
megastep@4
  1197
	}
megastep@4
  1198
	music_volume = volume;
slouken@173
  1199
	SDL_LockAudio();
slouken@173
  1200
	if ( music_playing ) {
slouken@173
  1201
		music_internal_volume(music_volume);
slouken@0
  1202
	}
slouken@173
  1203
	SDL_UnlockAudio();
slouken@0
  1204
	return(prev_volume);
slouken@0
  1205
}
slouken@0
  1206
slouken@173
  1207
/* Halt playing of music */
slouken@173
  1208
static void music_internal_halt(void)
megastep@7
  1209
{
megastep@7
  1210
	switch (music_playing->type) {
megastep@7
  1211
#ifdef CMD_MUSIC
slouken@173
  1212
	    case MUS_CMD:
megastep@7
  1213
		MusicCMD_Stop(music_playing->data.cmd);
megastep@7
  1214
		break;
megastep@7
  1215
#endif
megastep@7
  1216
#ifdef WAV_MUSIC
slouken@173
  1217
	    case MUS_WAV:
megastep@7
  1218
		WAVStream_Stop();
megastep@7
  1219
		break;
megastep@7
  1220
#endif
slouken@481
  1221
#ifdef MODPLUG_MUSIC
slouken@481
  1222
	    case MUS_MODPLUG:
slouken@481
  1223
		modplug_stop(music_playing->data.modplug);
slouken@481
  1224
		break;
slouken@481
  1225
#endif
slouken@411
  1226
#ifdef MOD_MUSIC
slouken@173
  1227
	    case MUS_MOD:
slouken@411
  1228
		MOD_stop(music_playing->data.module);
megastep@7
  1229
		break;
megastep@7
  1230
#endif
megastep@7
  1231
#ifdef MID_MUSIC
slouken@173
  1232
	    case MUS_MID:
slouken@98
  1233
#ifdef USE_NATIVE_MIDI
slouken@98
  1234
		if ( native_midi_ok ) {
slouken@98
  1235
			native_midi_stop();
chewi@506
  1236
			goto skip;
chewi@506
  1237
		}
chewi@506
  1238
#endif
chewi@506
  1239
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1240
		if ( fluidsynth_ok ) {
chewi@506
  1241
			fluidsynth_stop(music_playing->data.fluidsynthmidi);
chewi@506
  1242
			goto skip;
chewi@506
  1243
		}
slouken@98
  1244
#endif
slouken@106
  1245
#ifdef USE_TIMIDITY_MIDI
slouken@98
  1246
		if ( timidity_ok ) {
slouken@98
  1247
			Timidity_Stop();
chewi@506
  1248
			goto skip;
slouken@98
  1249
		}
slouken@106
  1250
#endif
megastep@7
  1251
		break;
megastep@7
  1252
#endif
slouken@63
  1253
#ifdef OGG_MUSIC
slouken@173
  1254
	    case MUS_OGG:
slouken@63
  1255
		OGG_stop(music_playing->data.ogg);
slouken@63
  1256
		break;
slouken@63
  1257
#endif
slouken@382
  1258
#ifdef FLAC_MUSIC
slouken@382
  1259
	    case MUS_FLAC:
slouken@382
  1260
		FLAC_stop(music_playing->data.flac);
slouken@382
  1261
		break;
slouken@382
  1262
#endif
megastep@7
  1263
#ifdef MP3_MUSIC
slouken@173
  1264
	    case MUS_MP3:
slouken@314
  1265
		smpeg.SMPEG_stop(music_playing->data.mp3);
megastep@7
  1266
		break;
megastep@7
  1267
#endif
slouken@357
  1268
#ifdef MP3_MAD_MUSIC
slouken@357
  1269
	    case MUS_MP3_MAD:
slouken@357
  1270
		mad_stop(music_playing->data.mp3_mad);
slouken@357
  1271
		break;
slouken@357
  1272
#endif
slouken@173
  1273
	    default:
megastep@7
  1274
		/* Unknown music type?? */
megastep@7
  1275
		return;
megastep@7
  1276
	}
chewi@506
  1277
chewi@506
  1278
skip:
megastep@7
  1279
	music_playing->fading = MIX_NO_FADING;
slouken@17
  1280
	music_playing = NULL;
megastep@7
  1281
}
slouken@0
  1282
int Mix_HaltMusic(void)
slouken@0
  1283
{
slouken@173
  1284
	SDL_LockAudio();
slouken@173
  1285
	if ( music_playing ) {
slouken@173
  1286
		music_internal_halt();
gabomdq@597
  1287
		if ( music_finished_hook ) {
gabomdq@597
  1288
			music_finished_hook();
gabomdq@597
  1289
		}
slouken@0
  1290
	}
slouken@173
  1291
	SDL_UnlockAudio();
slouken@173
  1292
slouken@0
  1293
	return(0);
slouken@0
  1294
}
slouken@0
  1295
megastep@4
  1296
/* Progressively stop the music */
megastep@4
  1297
int Mix_FadeOutMusic(int ms)
megastep@4
  1298
{
slouken@173
  1299
	int retval = 0;
slouken@173
  1300
slouken@499
  1301
	if ( ms_per_step == 0 ) {
slouken@499
  1302
		SDL_SetError("Audio device hasn't been opened");
slouken@499
  1303
		return 0;
slouken@499
  1304
	}
slouken@499
  1305
icculus@280
  1306
	if (ms <= 0) {  /* just halt immediately. */
icculus@280
  1307
		Mix_HaltMusic();
icculus@280
  1308
		return 1;
icculus@280
  1309
	}
icculus@280
  1310
slouken@173
  1311
	SDL_LockAudio();
icculus@280
  1312
	if ( music_playing) {
icculus@280
  1313
                int fade_steps = (ms + ms_per_step - 1)/ms_per_step;
icculus@280
  1314
                if ( music_playing->fading == MIX_NO_FADING ) {
icculus@280
  1315
	        	music_playing->fade_step = 0;
icculus@280
  1316
                } else {
icculus@280
  1317
                        int step;
icculus@280
  1318
                        int old_fade_steps = music_playing->fade_steps;
icculus@280
  1319
                        if ( music_playing->fading == MIX_FADING_OUT ) {
icculus@280
  1320
                                step = music_playing->fade_step;
icculus@280
  1321
                        } else {
icculus@280
  1322
                                step = old_fade_steps
icculus@280
  1323
                                        - music_playing->fade_step + 1;
icculus@280
  1324
                        }
icculus@280
  1325
                        music_playing->fade_step = (step * fade_steps)
icculus@280
  1326
                                / old_fade_steps;
icculus@280
  1327
                }
slouken@173
  1328
		music_playing->fading = MIX_FADING_OUT;
icculus@280
  1329
		music_playing->fade_steps = fade_steps;
slouken@173
  1330
		retval = 1;
megastep@4
  1331
	}
slouken@173
  1332
	SDL_UnlockAudio();
slouken@173
  1333
slouken@173
  1334
	return(retval);
megastep@4
  1335
}
megastep@4
  1336
megastep@4
  1337
Mix_Fading Mix_FadingMusic(void)
megastep@4
  1338
{
slouken@173
  1339
	Mix_Fading fading = MIX_NO_FADING;
slouken@173
  1340
slouken@173
  1341
	SDL_LockAudio();
slouken@173
  1342
	if ( music_playing ) {
slouken@173
  1343
		fading = music_playing->fading;
slouken@173
  1344
	}
slouken@173
  1345
	SDL_UnlockAudio();
slouken@173
  1346
slouken@173
  1347
	return(fading);
megastep@4
  1348
}
megastep@4
  1349
slouken@0
  1350
/* Pause/Resume the music stream */
slouken@0
  1351
void Mix_PauseMusic(void)
slouken@0
  1352
{
slouken@173
  1353
	music_active = 0;
slouken@0
  1354
}
megastep@4
  1355
slouken@0
  1356
void Mix_ResumeMusic(void)
slouken@0
  1357
{
slouken@173
  1358
	music_active = 1;
slouken@0
  1359
}
slouken@0
  1360
slouken@0
  1361
void Mix_RewindMusic(void)
slouken@0
  1362
{
slouken@173
  1363
	Mix_SetMusicPosition(0.0);
slouken@0
  1364
}
slouken@0
  1365
megastep@13
  1366
int Mix_PausedMusic(void)
megastep@13
  1367
{
slouken@17
  1368
	return (music_active == 0);
megastep@13
  1369
}
megastep@13
  1370
slouken@0
  1371
/* Check the status of the music */
slouken@173
  1372
static int music_internal_playing()
slouken@173
  1373
{
slouken@173
  1374
	int playing = 1;
icculus@510
  1375
icculus@510
  1376
	if (music_playing == NULL) {
icculus@510
  1377
		return 0;
icculus@510
  1378
	}
icculus@510
  1379
slouken@173
  1380
	switch (music_playing->type) {
slouken@173
  1381
#ifdef CMD_MUSIC
slouken@173
  1382
	    case MUS_CMD:
slouken@173
  1383
		if (!MusicCMD_Active(music_playing->data.cmd)) {
slouken@173
  1384
			playing = 0;
slouken@173
  1385
		}
slouken@173
  1386
		break;
slouken@173
  1387
#endif
slouken@173
  1388
#ifdef WAV_MUSIC
slouken@173
  1389
	    case MUS_WAV:
slouken@173
  1390
		if ( ! WAVStream_Active() ) {
slouken@173
  1391
			playing = 0;
slouken@173
  1392
		}
slouken@173
  1393
		break;
slouken@173
  1394
#endif
slouken@481
  1395
#ifdef MODPLUG_MUSIC
slouken@481
  1396
	    case MUS_MODPLUG:
slouken@481
  1397
		if ( ! modplug_playing(music_playing->data.modplug) ) {
slouken@481
  1398
			playing = 0;
slouken@481
  1399
		}
slouken@481
  1400
		break;
slouken@481
  1401
#endif
slouken@411
  1402
#ifdef MOD_MUSIC
slouken@173
  1403
	    case MUS_MOD:
slouken@411
  1404
		if ( ! MOD_playing(music_playing->data.module) ) {
slouken@173
  1405
			playing = 0;
slouken@173
  1406
		}
slouken@173
  1407
		break;
slouken@173
  1408
#endif
slouken@173
  1409
#ifdef MID_MUSIC
slouken@173
  1410
	    case MUS_MID:
slouken@173
  1411
#ifdef USE_NATIVE_MIDI
slouken@173
  1412
		if ( native_midi_ok ) {
slouken@173
  1413
			if ( ! native_midi_active() )
slouken@173
  1414
				playing = 0;
chewi@506
  1415
			goto skip;
chewi@506
  1416
		}
chewi@506
  1417
#endif
chewi@506
  1418
#ifdef USE_FLUIDSYNTH_MIDI
chewi@506
  1419
		if ( fluidsynth_ok ) {
chewi@506
  1420
			if ( ! fluidsynth_active(music_playing->data.fluidsynthmidi) )
chewi@506
  1421
				playing = 0;
chewi@506
  1422
			goto skip;
chewi@506
  1423
		}
slouken@173
  1424
#endif
slouken@173
  1425
#ifdef USE_TIMIDITY_MIDI
slouken@173
  1426
		if ( timidity_ok ) {
slouken@173
  1427
			if ( ! Timidity_Active() )
slouken@173
  1428
				playing = 0;
chewi@506
  1429
			goto skip;
slouken@173
  1430
		}
slouken@173
  1431
#endif
slouken@173
  1432
		break;
slouken@173
  1433
#endif
slouken@173
  1434
#ifdef OGG_MUSIC
slouken@173
  1435
	    case MUS_OGG:
slouken@173
  1436
		if ( ! OGG_playing(music_playing->data.ogg) ) {
slouken@173
  1437
			playing = 0;
slouken@173
  1438
		}
slouken@173
  1439
		break;
slouken@173
  1440
#endif
slouken@382
  1441
#ifdef FLAC_MUSIC
slouken@382
  1442
	    case MUS_FLAC:
slouken@382
  1443
		if ( ! FLAC_playing(music_playing->data.flac) ) {
slouken@382
  1444
			playing = 0;
slouken@382
  1445
		}
slouken@382
  1446
		break;
slouken@382
  1447
#endif
slouken@173
  1448
#ifdef MP3_MUSIC
slouken@173
  1449
	    case MUS_MP3:
slouken@314
  1450
		if ( smpeg.SMPEG_status(music_playing->data.mp3) != SMPEG_PLAYING )
slouken@173
  1451
			playing = 0;
slouken@173
  1452
		break;
slouken@173
  1453
#endif
slouken@357
  1454
#ifdef MP3_MAD_MUSIC
slouken@357
  1455
	    case MUS_MP3_MAD:
slouken@357
  1456
		if (!mad_isPlaying(music_playing->data.mp3_mad)) {
slouken@357
  1457
			playing = 0;
slouken@357
  1458
		}
slouken@357
  1459
		break;
slouken@357
  1460
#endif
slouken@173
  1461
	    default:
slouken@173
  1462
		playing = 0;
slouken@173
  1463
		break;
slouken@173
  1464
	}
chewi@506
  1465
chewi@506
  1466
skip:
slouken@173
  1467
	return(playing);
slouken@173
  1468
}
megastep@13
  1469
int Mix_PlayingMusic(void)
slouken@0
  1470
{
slouken@173
  1471
	int playing = 0;
slouken@173
  1472
slouken@173
  1473
	SDL_LockAudio();
slouken@173
  1474
	if ( music_playing ) {
slouken@529
  1475
		playing = music_loops || music_internal_playing();
slouken@0
  1476
	}
slouken@173
  1477
	SDL_UnlockAudio();
slouken@173
  1478
slouken@173
  1479
	return(playing);
slouken@0
  1480
}
slouken@0
  1481
slouken@0
  1482
/* Set the external music playback command */
slouken@0
  1483
int Mix_SetMusicCMD(const char *command)
slouken@0
  1484
{
slouken@0
  1485
	Mix_HaltMusic();
slouken@0
  1486
	if ( music_cmd ) {
slouken@561
  1487
		SDL_free(music_cmd);
slouken@0
  1488
		music_cmd = NULL;
slouken@0
  1489
	}
slouken@0
  1490
	if ( command ) {
slouken@561
  1491
		music_cmd = (char *)SDL_malloc(strlen(command)+1);
slouken@0
  1492
		if ( music_cmd == NULL ) {
slouken@0
  1493
			return(-1);
slouken@0
  1494
		}
slouken@0
  1495
		strcpy(music_cmd, command);
slouken@0
  1496
	}
slouken@0
  1497
	return(0);
slouken@0
  1498
}
slouken@0
  1499
slouken@154
  1500
int Mix_SetSynchroValue(int i)
slouken@154
  1501
{
slouken@419
  1502
	/* Not supported by any players at this time */
slouken@154
  1503
	return(-1);
slouken@154
  1504
}
slouken@154
  1505
slouken@154
  1506
int Mix_GetSynchroValue(void)
slouken@154
  1507
{
slouken@419
  1508
	/* Not supported by any players at this time */
slouken@154
  1509
	return(-1);
slouken@154
  1510
}
slouken@154
  1511
slouken@154
  1512
slouken@0
  1513
/* Uninitialize the music players */
slouken@0
  1514
void close_music(void)
slouken@0
  1515
{
slouken@0
  1516
	Mix_HaltMusic();
slouken@0
  1517
#ifdef CMD_MUSIC
slouken@0
  1518
	Mix_SetMusicCMD(NULL);
slouken@0
  1519
#endif
slouken@481
  1520
#ifdef MODPLUG_MUSIC
slouken@481
  1521
	modplug_exit();
slouken@481
  1522
#endif
slouken@411
  1523
#ifdef MOD_MUSIC
slouken@411
  1524
	MOD_exit();
slouken@0
  1525
#endif
patmandin@264
  1526
#ifdef MID_MUSIC
patmandin@264
  1527
# ifdef USE_TIMIDITY_MIDI
patmandin@264
  1528
	Timidity_Close();
patmandin@264
  1529
# endif
patmandin@264
  1530
#endif
icculus@390
  1531
icculus@390
  1532
	/* rcg06042009 report available decoders at runtime. */
slouken@561
  1533
	SDL_free(music_decoders);
icculus@390
  1534
	music_decoders = NULL;
icculus@390
  1535
	num_decoders = 0;
slouken@499
  1536
slouken@499
  1537
	ms_per_step = 0;
slouken@0
  1538
}
slouken@0
  1539
chewi@506
  1540
int Mix_SetSoundFonts(const char *paths)
chewi@506
  1541
{
chewi@506
  1542
#ifdef MID_MUSIC
chewi@506
  1543
	if (soundfont_paths) {
slouken@561
  1544
		SDL_free(soundfont_paths);
chewi@506
  1545
		soundfont_paths = NULL;
chewi@506
  1546
	}
chewi@506
  1547
chewi@506
  1548
	if (paths) {
slouken@561
  1549
		if (!(soundfont_paths = SDL_strdup(paths))) {
chewi@506
  1550
			Mix_SetError("Insufficient memory to set SoundFonts");
chewi@506
  1551
			return 0;
chewi@506
  1552
		}
chewi@506
  1553
	}
chewi@506
  1554
#endif
chewi@506
  1555
	return 1;
chewi@506
  1556
}
chewi@506
  1557
chewi@506
  1558
#ifdef MID_MUSIC
dimitris@508
  1559
const char* Mix_GetSoundFonts(void)
chewi@506
  1560
{
chewi@506
  1561
	const char* force = getenv("SDL_FORCE_SOUNDFONTS");
chewi@506
  1562
chewi@506
  1563
	if (!soundfont_paths || (force && force[0] == '1')) {
chewi@506
  1564
		return getenv("SDL_SOUNDFONTS");
chewi@506
  1565
	} else {
chewi@506
  1566
		return soundfont_paths;
chewi@506
  1567
	}
chewi@506
  1568
}
chewi@506
  1569
chewi@506
  1570
int Mix_EachSoundFont(int (*function)(const char*, void*), void *data)
chewi@506
  1571
{
chewi@506
  1572
	char *context, *path, *paths;
chewi@506
  1573
	const char* cpaths = Mix_GetSoundFonts();
chewi@506
  1574
chewi@506
  1575
	if (!cpaths) {
chewi@506
  1576
		Mix_SetError("No SoundFonts have been requested");
chewi@506
  1577
		return 0;
chewi@506
  1578
	}
chewi@506
  1579
slouken@561
  1580
	if (!(paths = SDL_strdup(cpaths))) {
chewi@506
  1581
		Mix_SetError("Insufficient memory to iterate over SoundFonts");
chewi@506
  1582
		return 0;
chewi@506
  1583
	}
chewi@506
  1584
icculus@513
  1585
#if defined(__MINGW32__) || defined(__MINGW64__)
icculus@513
  1586
	for (path = strtok(paths, ";"); path; path = strtok(NULL, ";")) {
icculus@513
  1587
#elif defined(_WIN32)
chewi@506
  1588
	for (path = strtok_s(paths, ";", &context); path; path = strtok_s(NULL, ";", &context)) {
chewi@506
  1589
#else
chewi@506
  1590
	for (path = strtok_r(paths, ":;", &context); path; path = strtok_r(NULL, ":;", &context)) {
chewi@506
  1591
#endif
chewi@506
  1592
		if (!function(path, data)) {
slouken@561
  1593
			SDL_free(paths);
chewi@506
  1594
			return 0;
chewi@506
  1595
		}
chewi@506
  1596
	}
chewi@506
  1597
slouken@561
  1598
	SDL_free(paths);
chewi@506
  1599
	return 1;
chewi@506
  1600
}
chewi@506
  1601
#endif