2006-11-16 03:02:42 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003-2004 Ryan C. Gordon. and James Bentler
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
Originally written by Ryan C. Gordon. (icculus@clutteredmind.org)
|
|
|
|
Adapted to work with JonoF's port by James Bentler (bentler@cs.umn.edu)
|
|
|
|
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* A reimplementation of Jim Dose's FX_MAN routines, using SDL_mixer 1.2.
|
|
|
|
* Whee. FX_MAN is also known as the "Apogee Sound System", or "ASS" for
|
|
|
|
* short. How strangely appropriate that seems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2007-08-17 03:16:46 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2006-11-16 03:02:42 +00:00
|
|
|
|
|
|
|
#include "duke3d.h"
|
|
|
|
#include "cache1d.h"
|
|
|
|
|
|
|
|
#if (!defined __WATCOMC__)
|
|
|
|
#define cdecl
|
|
|
|
#endif
|
|
|
|
|
2008-04-25 01:46:19 +00:00
|
|
|
#define _NEED_SDLMIXER 1
|
|
|
|
#include "sdl_inc.h"
|
2006-11-16 03:02:42 +00:00
|
|
|
#include "music.h"
|
|
|
|
|
|
|
|
#define __FX_TRUE (1 == 1)
|
|
|
|
#define __FX_FALSE (!__FX_TRUE)
|
|
|
|
|
|
|
|
#define DUKESND_DEBUG "DUKESND_DEBUG"
|
|
|
|
|
|
|
|
#ifndef min
|
|
|
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef max
|
|
|
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int MUSIC_ErrorCode = MUSIC_Ok;
|
|
|
|
|
|
|
|
static char warningMessage[80];
|
|
|
|
static char errorMessage[80];
|
|
|
|
static FILE *debug_file = NULL;
|
|
|
|
static int initialized_debugging = 0;
|
2007-12-24 05:07:52 +00:00
|
|
|
static int external_midi = 0;
|
2006-11-16 03:02:42 +00:00
|
|
|
|
2008-03-16 06:37:47 +00:00
|
|
|
static int music_initialized = 0;
|
|
|
|
static int music_context = 0;
|
|
|
|
static int music_loopflag = MUSIC_PlayOnce;
|
|
|
|
static Mix_Music *music_musicchunk = NULL;
|
|
|
|
static char *music_songdata = NULL;
|
2007-08-17 03:16:46 +00:00
|
|
|
|
2006-11-16 03:02:42 +00:00
|
|
|
// This gets called all over the place for information and debugging messages.
|
|
|
|
// If the user set the DUKESND_DEBUG environment variable, the messages
|
|
|
|
// go to the file that is specified in that variable. Otherwise, they
|
|
|
|
// are ignored for the expense of the function call. If DUKESND_DEBUG is
|
|
|
|
// set to "-" (without the quotes), then the output goes to stdout.
|
|
|
|
static void musdebug(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (debug_file)
|
|
|
|
{
|
|
|
|
fprintf(debug_file, "DUKEMUS: ");
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(debug_file, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(debug_file, "\n");
|
|
|
|
fflush(debug_file);
|
|
|
|
} // if
|
|
|
|
} // musdebug
|
|
|
|
|
|
|
|
static void init_debugging(void)
|
|
|
|
{
|
|
|
|
const char *envr;
|
|
|
|
|
|
|
|
if (initialized_debugging)
|
|
|
|
return;
|
|
|
|
|
|
|
|
envr = getenv(DUKESND_DEBUG);
|
|
|
|
if (envr != NULL)
|
|
|
|
{
|
2007-02-12 21:08:31 +00:00
|
|
|
if (Bstrcmp(envr, "-") == 0)
|
2006-11-16 03:02:42 +00:00
|
|
|
debug_file = stdout;
|
|
|
|
else
|
|
|
|
debug_file = fopen(envr, "w");
|
|
|
|
|
|
|
|
if (debug_file == NULL)
|
|
|
|
fprintf(stderr, "DUKESND: -WARNING- Could not open debug file!\n");
|
|
|
|
else
|
|
|
|
setbuf(debug_file, NULL);
|
|
|
|
} // if
|
|
|
|
|
|
|
|
initialized_debugging = 1;
|
|
|
|
} // init_debugging
|
|
|
|
|
|
|
|
static void setErrorMessage(const char *msg)
|
|
|
|
{
|
2007-02-12 21:08:31 +00:00
|
|
|
Bstrncpy(errorMessage, msg, sizeof(errorMessage));
|
2006-11-16 03:02:42 +00:00
|
|
|
// strncpy() doesn't add the null char if there isn't room...
|
|
|
|
errorMessage[sizeof(errorMessage) - 1] = '\0';
|
|
|
|
musdebug("Error message set to [%s].", errorMessage);
|
|
|
|
} // setErrorMessage
|
|
|
|
|
|
|
|
// The music functions...
|
|
|
|
|
|
|
|
char *MUSIC_ErrorString(int ErrorNumber)
|
|
|
|
{
|
|
|
|
switch (ErrorNumber)
|
|
|
|
{
|
|
|
|
case MUSIC_Warning:
|
|
|
|
return(warningMessage);
|
|
|
|
|
|
|
|
case MUSIC_Error:
|
|
|
|
return(errorMessage);
|
|
|
|
|
|
|
|
case MUSIC_Ok:
|
|
|
|
return("OK; no error.");
|
|
|
|
|
|
|
|
case MUSIC_ASSVersion:
|
|
|
|
return("Incorrect sound library version.");
|
|
|
|
|
|
|
|
case MUSIC_SoundCardError:
|
|
|
|
return("General sound card error.");
|
|
|
|
|
|
|
|
case MUSIC_InvalidCard:
|
|
|
|
return("Invalid sound card.");
|
|
|
|
|
|
|
|
case MUSIC_MidiError:
|
|
|
|
return("MIDI error.");
|
|
|
|
|
|
|
|
case MUSIC_MPU401Error:
|
|
|
|
return("MPU401 error.");
|
|
|
|
|
|
|
|
case MUSIC_TaskManError:
|
|
|
|
return("Task Manager error.");
|
|
|
|
|
|
|
|
//case MUSIC_FMNotDetected:
|
|
|
|
// return("FM not detected error.");
|
|
|
|
|
|
|
|
case MUSIC_DPMI_Error:
|
|
|
|
return("DPMI error.");
|
|
|
|
|
|
|
|
default:
|
|
|
|
return("Unknown error.");
|
|
|
|
} // switch
|
|
|
|
|
|
|
|
assert(0); // shouldn't hit this point.
|
|
|
|
return(NULL);
|
|
|
|
} // MUSIC_ErrorString
|
|
|
|
|
|
|
|
int MUSIC_Init(int SoundCard, int Address)
|
|
|
|
{
|
2007-12-24 05:07:52 +00:00
|
|
|
// Use an external MIDI player if the user has specified to do so
|
2008-03-17 07:08:56 +00:00
|
|
|
char *command = getenv("EDUKE32_MUSIC_CMD");
|
2008-04-25 01:46:19 +00:00
|
|
|
const SDL_version *linked = Mix_Linked_Version();
|
|
|
|
|
|
|
|
if (SDL_VERSIONNUM(linked->major,linked->minor,linked->patch) < MIX_REQUIREDVERSION)
|
|
|
|
{
|
|
|
|
// reject running with SDL_Mixer versions older than what is stated in sdl_inc.h
|
|
|
|
initprintf("You need at least v%d.%d.%d of SDL_mixer for music\n",SDL_MIXER_MIN_X,SDL_MIXER_MIN_Y,SDL_MIXER_MIN_Z);
|
|
|
|
return(MUSIC_Error);
|
|
|
|
}
|
|
|
|
|
2007-12-24 05:07:52 +00:00
|
|
|
external_midi = (command != NULL && command[0] != 0);
|
2008-04-25 01:46:19 +00:00
|
|
|
if (external_midi)
|
2007-12-24 05:07:52 +00:00
|
|
|
Mix_SetMusicCMD(command);
|
|
|
|
|
2006-11-16 03:02:42 +00:00
|
|
|
init_debugging();
|
|
|
|
|
|
|
|
musdebug("INIT! card=>%d, address=>%d...", SoundCard, Address);
|
|
|
|
|
|
|
|
if (music_initialized)
|
|
|
|
{
|
|
|
|
setErrorMessage("Music system is already initialized.");
|
|
|
|
return(MUSIC_Error);
|
|
|
|
} // if
|
|
|
|
|
|
|
|
SoundCard = 1;
|
|
|
|
|
|
|
|
music_initialized = 1;
|
|
|
|
return(MUSIC_Ok);
|
|
|
|
} // MUSIC_Init
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_Shutdown(void)
|
|
|
|
{
|
|
|
|
musdebug("shutting down sound subsystem.");
|
|
|
|
|
2007-12-24 05:07:52 +00:00
|
|
|
// TODO - make sure this is being called from the menu -- SA
|
2008-04-25 01:46:19 +00:00
|
|
|
if (external_midi)
|
2007-12-24 05:07:52 +00:00
|
|
|
Mix_SetMusicCMD(NULL);
|
|
|
|
|
2006-11-16 03:02:42 +00:00
|
|
|
MUSIC_StopSong();
|
|
|
|
music_context = 0;
|
|
|
|
music_initialized = 0;
|
|
|
|
music_loopflag = MUSIC_PlayOnce;
|
2007-02-13 01:28:50 +00:00
|
|
|
|
2006-11-16 03:02:42 +00:00
|
|
|
return(MUSIC_Ok);
|
|
|
|
} // MUSIC_Shutdown
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_SetMaxFMMidiChannel(int channel)
|
|
|
|
{
|
|
|
|
musdebug("STUB ... MUSIC_SetMaxFMMidiChannel(%d).\n", channel);
|
|
|
|
} // MUSIC_SetMaxFMMidiChannel
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_SetVolume(int volume)
|
|
|
|
{
|
|
|
|
volume = max(0, volume);
|
|
|
|
volume = min(volume, 255);
|
|
|
|
|
|
|
|
Mix_VolumeMusic(volume >> 1); // convert 0-255 to 0-128.
|
|
|
|
} // MUSIC_SetVolume
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_SetMidiChannelVolume(int channel, int volume)
|
|
|
|
{
|
|
|
|
musdebug("STUB ... MUSIC_SetMidiChannelVolume(%d, %d).\n", channel, volume);
|
|
|
|
} // MUSIC_SetMidiChannelVolume
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_ResetMidiChannelVolumes(void)
|
|
|
|
{
|
|
|
|
musdebug("STUB ... MUSIC_ResetMidiChannelVolumes().\n");
|
|
|
|
} // MUSIC_ResetMidiChannelVolumes
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_GetVolume(void)
|
|
|
|
{
|
|
|
|
return(Mix_VolumeMusic(-1) << 1); // convert 0-128 to 0-255.
|
|
|
|
} // MUSIC_GetVolume
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_SetLoopFlag(int loopflag)
|
|
|
|
{
|
|
|
|
music_loopflag = loopflag;
|
|
|
|
} // MUSIC_SetLoopFlag
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_SongPlaying(void)
|
|
|
|
{
|
|
|
|
return((Mix_PlayingMusic()) ? __FX_TRUE : __FX_FALSE);
|
|
|
|
} // MUSIC_SongPlaying
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_Continue(void)
|
|
|
|
{
|
|
|
|
if (Mix_PausedMusic())
|
|
|
|
Mix_ResumeMusic();
|
|
|
|
} // MUSIC_Continue
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_Pause(void)
|
|
|
|
{
|
|
|
|
Mix_PauseMusic();
|
|
|
|
} // MUSIC_Pause
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_StopSong(void)
|
|
|
|
{
|
|
|
|
//if (!fx_initialized)
|
|
|
|
if (!Mix_QuerySpec(NULL, NULL, NULL))
|
|
|
|
{
|
|
|
|
setErrorMessage("Need FX system initialized, too. Sorry.");
|
|
|
|
return(MUSIC_Error);
|
|
|
|
} // if
|
|
|
|
|
|
|
|
if ((Mix_PlayingMusic()) || (Mix_PausedMusic()))
|
|
|
|
Mix_HaltMusic();
|
|
|
|
|
|
|
|
if (music_musicchunk)
|
|
|
|
Mix_FreeMusic(music_musicchunk);
|
2008-03-16 06:37:47 +00:00
|
|
|
if (music_songdata)
|
2008-04-25 01:46:19 +00:00
|
|
|
Bfree(music_songdata);
|
2006-11-16 03:02:42 +00:00
|
|
|
|
|
|
|
music_musicchunk = NULL;
|
2008-03-16 06:37:47 +00:00
|
|
|
music_songdata = NULL;
|
2007-02-13 01:28:50 +00:00
|
|
|
|
2006-11-16 03:02:42 +00:00
|
|
|
return(MUSIC_Ok);
|
|
|
|
} // MUSIC_StopSong
|
|
|
|
|
|
|
|
// Duke3D-specific. --ryan.
|
2008-03-14 21:04:28 +00:00
|
|
|
void PlayMusic(char *_filename)
|
2006-11-16 03:02:42 +00:00
|
|
|
{
|
2007-12-12 17:42:14 +00:00
|
|
|
int handle;
|
|
|
|
int size;
|
|
|
|
int rc;
|
2006-11-16 03:02:42 +00:00
|
|
|
|
2008-03-14 21:04:28 +00:00
|
|
|
MUSIC_StopSong();
|
2008-03-16 06:37:47 +00:00
|
|
|
|
2008-03-14 21:04:28 +00:00
|
|
|
handle = kopen4load(_filename, 0);
|
|
|
|
if (handle == -1)
|
|
|
|
return;
|
2006-11-16 03:02:42 +00:00
|
|
|
|
2008-03-14 21:04:28 +00:00
|
|
|
size = kfilelength(handle);
|
|
|
|
if (size == -1)
|
|
|
|
{
|
|
|
|
kclose(handle);
|
|
|
|
return;
|
|
|
|
} // if
|
2006-11-16 03:02:42 +00:00
|
|
|
|
2008-03-16 06:42:21 +00:00
|
|
|
music_songdata = Bcalloc(size,sizeof(char));
|
2008-03-16 06:37:47 +00:00
|
|
|
if (music_songdata == NULL)
|
2008-03-14 21:04:28 +00:00
|
|
|
{
|
|
|
|
kclose(handle);
|
|
|
|
return;
|
|
|
|
} // if
|
2008-03-08 19:45:48 +00:00
|
|
|
|
2008-03-16 06:37:47 +00:00
|
|
|
rc = kread(handle, music_songdata, size);
|
2008-03-14 21:04:28 +00:00
|
|
|
kclose(handle);
|
|
|
|
if (rc != size)
|
|
|
|
{
|
2008-03-16 06:37:47 +00:00
|
|
|
Bfree(music_songdata);
|
|
|
|
music_songdata = NULL;
|
2008-03-14 21:04:28 +00:00
|
|
|
return;
|
|
|
|
} // if
|
2006-11-16 03:02:42 +00:00
|
|
|
|
2008-03-16 06:42:21 +00:00
|
|
|
music_musicchunk = Mix_LoadMUS_RW(SDL_RWFromMem((char *) music_songdata, size));
|
|
|
|
|
|
|
|
if (music_musicchunk != NULL)
|
2006-11-16 03:02:42 +00:00
|
|
|
{
|
2008-03-16 06:42:21 +00:00
|
|
|
// !!! FIXME: I set the music to loop. Hope that's okay. --ryan.
|
|
|
|
Musicsize = size;
|
|
|
|
Mix_PlayMusic(music_musicchunk, -1);
|
|
|
|
} // if
|
2006-11-16 03:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_SetContext(int context)
|
|
|
|
{
|
|
|
|
musdebug("STUB ... MUSIC_SetContext().\n");
|
|
|
|
music_context = context;
|
|
|
|
} // MUSIC_SetContext
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_GetContext(void)
|
|
|
|
{
|
|
|
|
return(music_context);
|
|
|
|
} // MUSIC_GetContext
|
|
|
|
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
void MUSIC_SetSongTick(unsigned int PositionInTicks)
|
2006-11-16 03:02:42 +00:00
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(PositionInTicks);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_SetSongTick().\n");
|
|
|
|
} // MUSIC_SetSongTick
|
|
|
|
|
|
|
|
|
2007-12-12 17:42:14 +00:00
|
|
|
void MUSIC_SetSongTime(unsigned int milliseconds)
|
2006-11-16 03:02:42 +00:00
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(milliseconds);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_SetSongTime().\n");
|
|
|
|
}// MUSIC_SetSongTime
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_SetSongPosition(int measure, int beat, int tick)
|
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(measure);
|
|
|
|
UNREFERENCED_PARAMETER(beat);
|
|
|
|
UNREFERENCED_PARAMETER(tick);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_SetSongPosition().\n");
|
|
|
|
} // MUSIC_SetSongPosition
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_GetSongPosition(songposition *pos)
|
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(pos);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_GetSongPosition().\n");
|
|
|
|
} // MUSIC_GetSongPosition
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_GetSongLength(songposition *pos)
|
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(pos);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_GetSongLength().\n");
|
|
|
|
} // MUSIC_GetSongLength
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_FadeVolume(int tovolume, int milliseconds)
|
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(tovolume);
|
2006-11-16 03:02:42 +00:00
|
|
|
Mix_FadeOutMusic(milliseconds);
|
|
|
|
return(MUSIC_Ok);
|
|
|
|
} // MUSIC_FadeVolume
|
|
|
|
|
|
|
|
|
|
|
|
int MUSIC_FadeActive(void)
|
|
|
|
{
|
|
|
|
return((Mix_FadingMusic() == MIX_FADING_OUT) ? __FX_TRUE : __FX_FALSE);
|
|
|
|
} // MUSIC_FadeActive
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_StopFade(void)
|
|
|
|
{
|
|
|
|
musdebug("STUB ... MUSIC_StopFade().\n");
|
|
|
|
} // MUSIC_StopFade
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_RerouteMidiChannel(int channel, int cdecl(*function)(int event, int c1, int c2))
|
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(channel);
|
|
|
|
UNREFERENCED_PARAMETER(function);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_RerouteMidiChannel().\n");
|
|
|
|
} // MUSIC_RerouteMidiChannel
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_RegisterTimbreBank(unsigned char *timbres)
|
|
|
|
{
|
2008-03-22 10:23:57 +00:00
|
|
|
UNREFERENCED_PARAMETER(timbres);
|
2006-11-16 03:02:42 +00:00
|
|
|
musdebug("STUB ... MUSIC_RegisterTimbreBank().\n");
|
|
|
|
} // MUSIC_RegisterTimbreBank
|
|
|
|
|
|
|
|
|
|
|
|
void MUSIC_Update(void)
|
|
|
|
{}
|