2006-10-17 20:52:09 +00:00
|
|
|
/*
|
|
|
|
* This is an alternate backend for the music system.
|
|
|
|
* It uses SDL_mixer to provide a more reliable playback,
|
|
|
|
* and allow processing of multiple audio formats.
|
|
|
|
*
|
|
|
|
* -- MD2211 (2006-04-24)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SDL/SDL.h>
|
2010-09-02 00:07:37 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2006-10-17 20:52:09 +00:00
|
|
|
#include <SDL/SDL_mixer.h>
|
2010-09-02 00:07:37 +00:00
|
|
|
#else
|
|
|
|
#include <SDL_mixer/SDL_mixer.h>
|
|
|
|
#endif
|
2006-10-17 20:52:09 +00:00
|
|
|
#include <string.h>
|
2008-01-23 17:25:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2006-10-17 20:52:09 +00:00
|
|
|
#include "args.h"
|
2010-07-18 20:28:15 +00:00
|
|
|
#include "hmp.h"
|
2007-09-18 13:37:39 +00:00
|
|
|
#include "digi_mixer_music.h"
|
2006-10-17 20:52:09 +00:00
|
|
|
#include "cfile.h"
|
2008-01-23 17:25:09 +00:00
|
|
|
#include "u_mem.h"
|
2006-10-17 20:52:09 +00:00
|
|
|
|
2010-11-28 15:49:32 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
extern int digi_win32_play_midi_song( char * filename, int loop );
|
|
|
|
#endif
|
2006-10-17 20:52:09 +00:00
|
|
|
Mix_Music *current_music = NULL;
|
2010-10-29 15:40:21 +00:00
|
|
|
static unsigned char *current_music_hndlbuf = NULL;
|
|
|
|
|
2006-10-17 20:52:09 +00:00
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
/*
|
2010-08-22 13:27:47 +00:00
|
|
|
* Plays a music file from an absolute path or a relative path
|
2008-01-23 17:25:09 +00:00
|
|
|
*/
|
2006-10-17 20:52:09 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
int mix_play_file(char *filename, int loop, void (*hook_finished_track)())
|
|
|
|
{
|
2010-06-27 14:30:12 +00:00
|
|
|
SDL_RWops *rw = NULL;
|
|
|
|
PHYSFS_file *filehandle = NULL;
|
2010-10-29 15:40:21 +00:00
|
|
|
char full_path[PATH_MAX];
|
|
|
|
char *fptr;
|
|
|
|
unsigned int bufsize = 0;
|
2007-03-12 21:56:41 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
mix_free_music(); // stop and free what we're already playing, if anything
|
2007-03-12 21:56:41 +00:00
|
|
|
|
2010-06-14 10:43:30 +00:00
|
|
|
fptr = strrchr(filename, '.');
|
2008-01-23 17:25:09 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (fptr == NULL)
|
|
|
|
return 0;
|
2008-01-23 17:25:09 +00:00
|
|
|
|
2010-11-28 15:49:32 +00:00
|
|
|
// It's a .hmp!
|
2010-06-14 08:13:16 +00:00
|
|
|
if (!stricmp(fptr, ".hmp"))
|
|
|
|
{
|
2010-11-28 15:49:32 +00:00
|
|
|
#ifdef _WIN32 // on _WIN32, play natively
|
|
|
|
return digi_win32_play_midi_song( filename, loop );
|
|
|
|
#else // otherwise convert and load to current_music
|
2010-10-29 15:40:21 +00:00
|
|
|
hmp2mid(filename, ¤t_music_hndlbuf, &bufsize);
|
|
|
|
rw = SDL_RWFromConstMem(current_music_hndlbuf,bufsize*sizeof(char));
|
|
|
|
current_music = Mix_LoadMUS_RW(rw);
|
2010-11-28 15:49:32 +00:00
|
|
|
#endif
|
2010-06-14 08:13:16 +00:00
|
|
|
}
|
2007-03-12 21:56:41 +00:00
|
|
|
|
2010-07-05 07:41:30 +00:00
|
|
|
// try loading music via given filename
|
2010-10-29 15:40:21 +00:00
|
|
|
if (!current_music)
|
|
|
|
current_music = Mix_LoadMUS(filename);
|
2010-07-05 07:41:30 +00:00
|
|
|
|
2010-10-29 15:40:21 +00:00
|
|
|
// no luck. so it might be in Searchpath. So try to build absolute path
|
2010-07-05 07:41:30 +00:00
|
|
|
if (!current_music)
|
2010-08-22 13:27:47 +00:00
|
|
|
{
|
|
|
|
PHYSFSX_getRealPath(filename, full_path);
|
|
|
|
current_music = Mix_LoadMUS(full_path);
|
|
|
|
if (current_music)
|
|
|
|
filename = full_path; // used later for possible error reporting
|
|
|
|
}
|
|
|
|
|
2010-10-29 15:40:21 +00:00
|
|
|
// still nothin'? Let's open via PhysFS in case it's located inside an archive
|
2010-08-22 13:27:47 +00:00
|
|
|
if (!current_music)
|
2010-06-14 08:13:16 +00:00
|
|
|
{
|
2010-10-29 15:40:21 +00:00
|
|
|
filehandle = PHYSFS_openRead(filename);
|
|
|
|
if (filehandle != NULL)
|
|
|
|
{
|
|
|
|
current_music_hndlbuf = d_realloc(current_music_hndlbuf, sizeof(char *)*PHYSFS_fileLength(filehandle));
|
|
|
|
bufsize = PHYSFS_read(filehandle, current_music_hndlbuf, sizeof(char), PHYSFS_fileLength(filehandle));
|
|
|
|
rw = SDL_RWFromConstMem(current_music_hndlbuf,bufsize*sizeof(char));
|
|
|
|
PHYSFS_close(filehandle);
|
|
|
|
current_music = Mix_LoadMUS_RW(rw);
|
|
|
|
}
|
2010-06-14 08:13:16 +00:00
|
|
|
}
|
2006-10-17 20:52:09 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
if (current_music)
|
|
|
|
{
|
2010-11-28 15:49:32 +00:00
|
|
|
Mix_PlayMusic(current_music, (loop ? -1 : 1));
|
2010-06-14 08:13:16 +00:00
|
|
|
Mix_HookMusicFinished(hook_finished_track ? hook_finished_track : mix_free_music);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-22 13:27:47 +00:00
|
|
|
con_printf(CON_CRITICAL,"Music %s could not be loaded\n", filename);
|
2010-10-29 15:40:21 +00:00
|
|
|
mix_stop_music();
|
2010-06-14 08:13:16 +00:00
|
|
|
}
|
2008-03-23 08:42:24 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-23 08:42:24 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
// What to do when stopping song playback
|
|
|
|
void mix_free_music()
|
2008-03-23 08:42:24 +00:00
|
|
|
{
|
2010-06-14 08:13:16 +00:00
|
|
|
Mix_HaltMusic();
|
|
|
|
if (current_music)
|
2008-03-23 08:42:24 +00:00
|
|
|
{
|
2010-06-14 08:13:16 +00:00
|
|
|
Mix_FreeMusic(current_music);
|
|
|
|
current_music = NULL;
|
2008-03-23 08:42:24 +00:00
|
|
|
}
|
2010-09-02 14:00:26 +00:00
|
|
|
if (current_music_hndlbuf)
|
|
|
|
{
|
|
|
|
d_free(current_music_hndlbuf);
|
|
|
|
current_music_hndlbuf = NULL;
|
|
|
|
}
|
2010-06-14 08:13:16 +00:00
|
|
|
}
|
2008-03-23 08:42:24 +00:00
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
void mix_set_music_volume(int vol)
|
|
|
|
{
|
2010-08-19 15:54:19 +00:00
|
|
|
vol *= MIX_MAX_VOLUME/8;
|
2010-06-14 08:13:16 +00:00
|
|
|
Mix_VolumeMusic(vol);
|
2008-03-23 08:42:24 +00:00
|
|
|
}
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
void mix_stop_music()
|
|
|
|
{
|
|
|
|
Mix_HaltMusic();
|
2010-09-02 14:00:26 +00:00
|
|
|
if (current_music_hndlbuf)
|
|
|
|
{
|
|
|
|
d_free(current_music_hndlbuf);
|
|
|
|
current_music_hndlbuf = NULL;
|
|
|
|
}
|
2008-01-10 20:35:59 +00:00
|
|
|
}
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
void mix_pause_music()
|
|
|
|
{
|
|
|
|
Mix_PauseMusic();
|
2006-10-17 20:52:09 +00:00
|
|
|
}
|
|
|
|
|
2010-06-14 08:13:16 +00:00
|
|
|
void mix_resume_music()
|
|
|
|
{
|
|
|
|
Mix_ResumeMusic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mix_pause_resume_music()
|
|
|
|
{
|
|
|
|
if (Mix_PausedMusic())
|
|
|
|
Mix_ResumeMusic();
|
|
|
|
else if (Mix_PlayingMusic())
|
|
|
|
Mix_PauseMusic();
|
2006-10-17 20:52:09 +00:00
|
|
|
}
|