diff --git a/source/exhumed/src/cd.cpp b/source/exhumed/src/cd.cpp index db5d46ed1..06e6482fb 100644 --- a/source/exhumed/src/cd.cpp +++ b/source/exhumed/src/cd.cpp @@ -16,9 +16,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ //------------------------------------------------------------------------- #include "ns.h" +#include "build.h" #include "compat.h" #include "baselayer.h" #include "cd.h" +#include "fx_man.h" +#include "sound.h" +#include "exhumed.h" #include #include @@ -26,6 +30,11 @@ BEGIN_PS_NS extern short word_9AC30; +static char *pTrack = NULL; +int trackhandle = -1; +int nLastVolumeSet = 0; + + int cd_check_device_present() { return 1; @@ -47,31 +56,113 @@ int initcdaudio() void setCDaudiovolume(int val) { - + if (trackhandle > 0) { + FX_SetPan(trackhandle, val, val, val); + } } int playCDtrack(int nTrack) { - return 1; + if (nTrack < 2) { + return 0; + } + + char filebuf[128]; + + // prefer flac if available + sprintf(filebuf, "exhumed%02d.flac", nTrack); + int32_t hFile = kopen4load(filebuf, 0); + if (hFile < 0) + { + // try ogg vorbis now + sprintf(filebuf, "exhumed%02d.ogg", nTrack); + hFile = kopen4load(filebuf, 0); + if (hFile < 0) { + return 0; + } + } + + int32_t nFileLen = kfilelength(hFile); + + pTrack = (char*)Xaligned_alloc(16, nFileLen); + int nRead = kread(hFile, pTrack, nFileLen); + + kclose(hFile); + + trackhandle = FX_Play(pTrack, nRead, -1, 0, 0, 255, 255, 255, FX_MUSIC_PRIORITY, fix16_one, MUSIC_ID); + if (trackhandle < 0) + { + if (pTrack) + { + Xaligned_free(pTrack); + pTrack = NULL; + } + return 0; + } + + setCDaudiovolume(gMusicVolume); + + nCDTrackLength = 1; + + return nCDTrackLength; } void StartfadeCDaudio() { - + if (CDplaying()) { + nLastVolumeSet = gMusicVolume; + } } int StepFadeCDaudio() { + if (!CDplaying()) { return 0; + } + + if (nLastVolumeSet <= 0) { + return 0; + } + + nLastVolumeSet -= 8; + + if (nLastVolumeSet <= 0) { + nLastVolumeSet = 0; + } + + setCDaudiovolume(nLastVolumeSet); + + if (nLastVolumeSet == 0) { + StopCD(); + } + + return 1; } int CDplaying() { + if (trackhandle > 0 && pTrack) { // better way to do this? return 1; + } + else { + return 0; + } } void StopCD() { + if (trackhandle > 0) { + FX_StopSound(trackhandle); + trackhandle = -1; + } + nCDTrackLength = 0; + + if (pTrack) + { + Xaligned_free(pTrack); + pTrack = NULL; + } } + END_PS_NS diff --git a/source/exhumed/src/cd.h b/source/exhumed/src/cd.h index 9a2300cbc..9a206fe9d 100644 --- a/source/exhumed/src/cd.h +++ b/source/exhumed/src/cd.h @@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. BEGIN_PS_NS +extern int trackhandle; + int initcdaudio(); void setCDaudiovolume(int val); int playCDtrack(int nTrack); diff --git a/source/exhumed/src/cdaudio.cpp b/source/exhumed/src/cdaudio.cpp index fb9fa425a..088a2a7c3 100644 --- a/source/exhumed/src/cdaudio.cpp +++ b/source/exhumed/src/cdaudio.cpp @@ -17,12 +17,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. //------------------------------------------------------------------------- #include "ns.h" #include "cdaudio.h" +#include "cd.h" +#include "exhumed.h" + BEGIN_PS_NS int fadecdaudio() { -/* TODO StartfadeCDaudio(); while (1) @@ -34,7 +36,7 @@ int fadecdaudio() WaitTicks(1); } } -*/ + return 1; } diff --git a/source/exhumed/src/exhumed.cpp b/source/exhumed/src/exhumed.cpp index 1143c87aa..f25900cf5 100644 --- a/source/exhumed/src/exhumed.cpp +++ b/source/exhumed/src/exhumed.cpp @@ -874,9 +874,9 @@ void timerhandler() lastfps = fps; fps = 0; - if (nCDTrackLength > 0) { - nCDTrackLength--; - } +// if (nCDTrackLength > 0) { +// nCDTrackLength--; +// } } if (!bInMove) OSD_DispatchQueued(); @@ -2291,7 +2291,7 @@ LOOP3: lPlayerXVel = 0; lPlayerYVel = 0; movefifopos = movefifoend; - nCDTrackLength = 0; +// nCDTrackLength = 0; RefreshStatus(); if (bSerialPlay) { diff --git a/source/exhumed/src/menu.cpp b/source/exhumed/src/menu.cpp index 26654c0d2..5f17e42d3 100644 --- a/source/exhumed/src/menu.cpp +++ b/source/exhumed/src/menu.cpp @@ -923,7 +923,7 @@ void menu_AdjustVolume() } // TODO SetMusicVolume(); -// TODO setCDaudiovolume(gMusicVolume); + setCDaudiovolume(gMusicVolume); continue; } @@ -955,8 +955,8 @@ void menu_AdjustVolume() gMusicVolume += 4; } -// TODO SetMusicVolume(); -// TODO setCDaudiovolume(gMusicVolume); +// SetMusicVolume(); + setCDaudiovolume(gMusicVolume); continue; } diff --git a/source/exhumed/src/sound.cpp b/source/exhumed/src/sound.cpp index 963e0924d..fface1859 100644 --- a/source/exhumed/src/sound.cpp +++ b/source/exhumed/src/sound.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "snake.h" #include "trigdat.h" #include "sequence.h" +#include "cd.h" BEGIN_PS_NS @@ -295,6 +296,12 @@ void CalcASSPan(int nPan, int nVolume, int *pLeft, int *pRight) void ASSCallback(intptr_t num) { + if ((int32_t)num == MUSIC_ID) { + trackhandle = -1; + StopCD(); + return; + } + // TODO: add mutex? if ((int32_t)num == -1) handle = -1; @@ -1174,7 +1181,7 @@ void StopAllSounds(void) for (int i = 0; i < kMaxActiveSounds; i++) { - if (sActiveSound[i].f_e >= 0) + if (sActiveSound[i].f_e >= 0 && sActiveSound[i].f_e != trackhandle) FX_StopSound(sActiveSound[i].f_e); // AIL_end_sample(sActiveSound[i].f_e); } diff --git a/source/exhumed/src/sound.h b/source/exhumed/src/sound.h index b1387f1c0..0eea988e8 100644 --- a/source/exhumed/src/sound.h +++ b/source/exhumed/src/sound.h @@ -29,6 +29,8 @@ BEGIN_PS_NS #define kCreepyCount 150 +#define MUSIC_ID (-65536) + enum { kSound0 = 0, kSound1,