X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=cd_shared.c;h=aa33079dd2ea55710e50481907f3a85cb5918734;hb=4eb6393c19467e2a1e45913397aaf11380d1cd83;hp=fd5ee0eda0864867d562b332e76781fb666096ae;hpb=ff46d6ff516fda192c5adc55a5c9b82007545bd2;p=xonotic%2Fdarkplaces.git diff --git a/cd_shared.c b/cd_shared.c index fd5ee0ed..aa33079d 100644 --- a/cd_shared.c +++ b/cd_shared.c @@ -21,12 +21,18 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // rights reserved. #include "quakedef.h" +#include "cdaudio.h" +#include "sound.h" + +#define MAXTRACKS 256 // Prototypes of the system dependent functions extern void CDAudio_SysEject (void); extern void CDAudio_SysCloseDoor (void); extern int CDAudio_SysGetAudioDiskInfo (void); -extern int CDAudio_SysPlay (qbyte track); +extern float CDAudio_SysGetVolume (void); +extern void CDAudio_SysSetVolume (float volume); +extern int CDAudio_SysPlay (unsigned char track); extern int CDAudio_SysStop (void); extern int CDAudio_SysPause (void); extern int CDAudio_SysResume (void); @@ -36,21 +42,26 @@ extern int CDAudio_SysStartup (void); extern void CDAudio_SysShutdown (void); // used by menu to ghost CD audio slider -cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0"}; +cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0","indicates if CD Audio system is active"}; static qboolean wasPlaying = false; static qboolean initialized = false; static qboolean enabled = false; static float cdvolume; -static qbyte remap[100]; -static qbyte maxTrack; +typedef char filename_t[MAX_QPATH]; +static filename_t remap[MAXTRACKS]; +static unsigned char maxTrack; +static int faketrack = -1; + +static float saved_vol = 1.0f; // exported variables qboolean cdValid = false; qboolean cdPlaying = false; qboolean cdPlayLooping = false; -qbyte cdPlayTrack; +unsigned char cdPlayTrack; +cl_cdstate_t cd; static void CDAudio_Eject (void) { @@ -86,30 +97,100 @@ static int CDAudio_GetAudioDiskInfo (void) } -void CDAudio_Play (qbyte track, qboolean looping) +void CDAudio_Play_byName (const char *trackname, qboolean looping) { + unsigned int track; + sfx_t* sfx; + + Host_StartVideo(); + if (!enabled) return; - if (!cdValid) + if(strspn(trackname, "0123456789") == strlen(trackname)) { - CDAudio_GetAudioDiskInfo(); - if (!cdValid) - return; + track = (unsigned char) atoi(trackname); + if(track > 0 && track < MAXTRACKS) + if(*remap[track]) + trackname = remap[track]; } - track = remap[track]; - if (track < 1 || track > maxTrack) + if(strspn(trackname, "0123456789") == strlen(trackname)) { - Con_DPrintf("CDAudio: Bad track number %u.\n", track); - return; + track = (unsigned char) atoi(trackname); + if (track < 1) + { + Con_Printf("CDAudio: Bad track number %u.\n", track); + return; + } } + else + track = 0; - if (cdPlaying && cdPlayTrack == track) + if (cdPlaying && cdPlayTrack == track && faketrack == -1) return; + CDAudio_Stop (); - if (CDAudio_SysPlay(track) == -1) - return; + // Try playing a fake track (sound file) first + if(track >= 1) + { + sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false); + if (sfx == NULL || !S_IsSoundPrecached (sfx)) + sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false); + if (sfx == NULL || !S_IsSoundPrecached (sfx)) + sfx = S_PrecacheSound (va ("cdtracks/track%02u", track), false, false); + if (sfx == NULL || !S_IsSoundPrecached (sfx)) + sfx = S_PrecacheSound (va ("cdtracks/track%03u", track), false, false); + } + else + { + sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false); + if (sfx == NULL || !S_IsSoundPrecached (sfx)) + sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false); + } + if (sfx != NULL) + { + faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0); + if (faketrack != -1) + { + if (looping) + S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true); + S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true); + if(track >= 1) + Con_Printf ("Fake CD track %u playing...\n", track); + else + Con_Printf ("BGM track %s playing...\n", trackname); + } + } + + // If we can't play a fake CD track, try the real one + if (faketrack == -1) + { + if(track < 1) + { + Con_Print("Could not load BGM track.\n"); + return; + } + + if (!cdValid) + { + CDAudio_GetAudioDiskInfo(); + if (!cdValid) + { + Con_Print ("No CD in player.\n"); + return; + } + } + + if (track > maxTrack) + { + Con_Printf("CDAudio: Bad track number %u.\n", track); + return; + } + + if (CDAudio_SysPlay(track) == -1) + return; + } cdPlayLooping = looping; cdPlayTrack = track; @@ -119,13 +200,24 @@ void CDAudio_Play (qbyte track, qboolean looping) CDAudio_Pause (); } +void CDAudio_Play (unsigned char track, qboolean looping) +{ + char buf[20]; + dpsnprintf(buf, sizeof(buf), "%d", (int) track); + CDAudio_Play_byName(buf, looping); +} void CDAudio_Stop (void) { - if (!enabled || !cdPlaying) + if (!enabled) return; - if (CDAudio_SysStop() == -1) + if (faketrack != -1) + { + S_StopChannel (faketrack, true); + faketrack = -1; + } + else if (cdPlaying && (CDAudio_SysStop() == -1)) return; wasPlaying = false; @@ -137,7 +229,9 @@ void CDAudio_Pause (void) if (!enabled || !cdPlaying) return; - if (CDAudio_SysPause() == -1) + if (faketrack != -1) + S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true); + else if (CDAudio_SysPause() == -1) return; wasPlaying = cdPlaying; @@ -147,10 +241,12 @@ void CDAudio_Pause (void) void CDAudio_Resume (void) { - if (!enabled || !cdValid || !wasPlaying) + if (!enabled || cdPlaying || !wasPlaying) return; - if (CDAudio_SysResume() == -1) + if (faketrack != -1) + S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false); + else if (CDAudio_SysResume() == -1) return; cdPlaying = true; } @@ -166,6 +262,9 @@ static void CD_f (void) command = Cmd_Argv (1); + if (strcasecmp(command, "remap") != 0) + Host_StartVideo(); + if (strcasecmp(command, "on") == 0) { enabled = true; @@ -185,8 +284,8 @@ static void CD_f (void) enabled = true; if (cdPlaying) CDAudio_Stop(); - for (n = 0; n < 100; n++) - remap[n] = n; + for (n = 0; n < MAXTRACKS; n++) + *remap[n] = 0; // empty string, that is, unremapped CDAudio_GetAudioDiskInfo(); return; } @@ -196,13 +295,13 @@ static void CD_f (void) ret = Cmd_Argc() - 2; if (ret <= 0) { - for (n = 1; n < 100; n++) - if (remap[n] != n) - Con_Printf(" %u -> %u\n", n, remap[n]); + for (n = 1; n < MAXTRACKS; n++) + if (*remap[n]) + Con_Printf(" %u -> %s\n", n, remap[n]); return; } for (n = 1; n <= ret; n++) - remap[n] = atoi(Cmd_Argv (n+1)); + strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap)); return; } @@ -212,25 +311,15 @@ static void CD_f (void) return; } - if (!cdValid) - { - CDAudio_GetAudioDiskInfo(); - if (!cdValid) - { - Con_Print("No CD in player.\n"); - return; - } - } - if (strcasecmp(command, "play") == 0) { - CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false); + CDAudio_Play_byName(Cmd_Argv (2), false); return; } if (strcasecmp(command, "loop") == 0) { - CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true); + CDAudio_Play_byName(Cmd_Argv (2), true); return; } @@ -254,7 +343,7 @@ static void CD_f (void) if (strcasecmp(command, "eject") == 0) { - if (cdPlaying) + if (cdPlaying && faketrack == -1) CDAudio_Stop(); CDAudio_Eject(); cdValid = false; @@ -263,7 +352,11 @@ static void CD_f (void) if (strcasecmp(command, "info") == 0) { - Con_Printf("%u tracks\n", maxTrack); + CDAudio_GetAudioDiskInfo (); + if (cdValid) + Con_Printf("%u tracks on CD.\n", maxTrack); + else + Con_Print ("No CD in player.\n"); if (cdPlaying) Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack); else if (wasPlaying) @@ -271,30 +364,54 @@ static void CD_f (void) Con_Printf("Volume is %f\n", cdvolume); return; } + + Con_Printf("CD commands:\n"); + Con_Printf("cd on - enables CD audio system\n"); + Con_Printf("cd off - stops and disables CD audio system\n"); + Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)"); + Con_Printf("cd remap [remap2] [remap3] [...] - chooses (possibly emulated) CD tracks to play when a map asks for a particular track, this has many uses\n"); + Con_Printf("cd close - closes CD tray\n"); + Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n"); + Con_Printf("cd play - plays selected track in remapping table\n"); + Con_Printf("cd loop - plays and repeats selected track in remapping table\n"); + Con_Printf("cd stop - stops playing current CD track\n"); + Con_Printf("cd pause - pauses CD playback\n"); + Con_Printf("cd resume - unpauses CD playback\n"); + Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n"); } -void CDAudio_Update (void) +void CDAudio_SetVolume (float newvol) { - if (!enabled) + // If the volume hasn't changed + if (newvol == cdvolume) return; - if (bgmvolume.value != cdvolume) + // If the CD has been muted + if (newvol == 0.0f) + CDAudio_Pause (); + else { - if (cdvolume) - { - Cvar_SetValueQuick (&bgmvolume, 0.0); - cdvolume = bgmvolume.value; - CDAudio_Pause (); - } - else - { - Cvar_SetValueQuick (&bgmvolume, 1.0); - cdvolume = bgmvolume.value; + // If the CD has been unmuted + if (cdvolume == 0.0f) CDAudio_Resume (); - } + + if (faketrack != -1) + S_SetChannelVolume (faketrack, newvol); + CDAudio_SysSetVolume (newvol); } - CDAudio_SysUpdate(); + cdvolume = newvol; +} + +void CDAudio_Update (void) +{ + if (!enabled) + return; + + CDAudio_SetVolume (bgmvolume.value); + + if (faketrack == -1) + CDAudio_SysUpdate(); } int CDAudio_Init (void) @@ -304,37 +421,49 @@ int CDAudio_Init (void) if (cls.state == ca_dedicated) return -1; - if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe")) +// COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support + if (COM_CheckParm("-nocdaudio")) return -1; CDAudio_SysInit(); - for (i = 0; i < 100; i++) - remap[i] = i; + for (i = 0; i < MAXTRACKS; i++) + *remap[i] = 0; Cvar_RegisterVariable(&cdaudioinitialized); Cvar_SetValueQuick(&cdaudioinitialized, true); enabled = true; - Cmd_AddCommand("cd", CD_f); + Cmd_AddCommand("cd", CD_f, "execute a CD drive command (cd on/off/reset/remap/close/play/loop/stop/pause/resume/eject/info) - use cd by itself for usage"); return 0; } int CDAudio_Startup (void) { - if (CDAudio_SysStartup() == -1) + if (COM_CheckParm("-nocdaudio")) return -1; + CDAudio_SysStartup (); + if (CDAudio_GetAudioDiskInfo()) { - Con_DPrint("CDAudio_Init: No CD in player.\n"); + Con_Print("CDAudio_Init: No CD in player.\n"); cdValid = false; } + saved_vol = CDAudio_SysGetVolume (); + if (saved_vol < 0.0f) + { + Con_Print ("Can't get initial CD volume\n"); + saved_vol = 1.0f; + } + else + Con_Printf ("Initial CD volume: %g\n", saved_vol); + initialized = true; - Con_DPrint("CD Audio Initialized\n"); + Con_Print("CD Audio Initialized\n"); return 0; } @@ -343,6 +472,9 @@ void CDAudio_Shutdown (void) { if (!initialized) return; + + CDAudio_SysSetVolume (saved_vol); + CDAudio_Stop(); CDAudio_SysShutdown(); initialized = false;