From 69e4adf5b321e64b4be9db1a313c91cabe50dfd5 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Tue, 11 Apr 2023 16:37:01 +0300 Subject: [PATCH] Add HQ music support See #239 --- src/audio.cc | 28 +++++----------------------- src/audio.h | 2 +- src/audio_file.cc | 28 +++++----------------------- src/audio_file.h | 2 +- src/game_sound.cc | 8 ++------ src/sound.cc | 10 +++++----- src/sound.h | 2 +- src/sound_effects_cache.cc | 2 +- src/sound_effects_cache.h | 2 +- 9 files changed, 22 insertions(+), 62 deletions(-) diff --git a/src/audio.cc b/src/audio.cc index ee71201..77000c6 100644 --- a/src/audio.cc +++ b/src/audio.cc @@ -58,7 +58,7 @@ static int audioSoundDecoderReadHandler(void* data, void* buffer, unsigned int s // AudioOpen // 0x41A2EC -int audioOpen(const char* fname, int flags) +int audioOpen(const char* fname, int* channels, int* sampleRate) { char path[80]; snprintf(path, sizeof(path), "%s", fname); @@ -70,28 +70,7 @@ int audioOpen(const char* fname, int flags) compression = 0; } - char mode[4]; - memset(mode, 0, 4); - - // NOTE: Original implementation is slightly different, it uses separate - // variable to track index where to set 't' and 'b'. - char* pm = mode; - if (flags & 1) { - *pm++ = 'w'; - } else if (flags & 2) { - *pm++ = 'w'; - *pm++ = '+'; - } else { - *pm++ = 'r'; - } - - if (flags & 0x100) { - *pm++ = 't'; - } else if (flags & 0x200) { - *pm++ = 'b'; - } - - File* stream = fileOpen(path, mode); + File* stream = fileOpen(path, "rb"); if (stream == NULL) { debugPrint("AudioOpen: Couldn't open %s for read\n", path); return -1; @@ -121,6 +100,9 @@ int audioOpen(const char* fname, int flags) audioFile->flags |= AUDIO_COMPRESSED; audioFile->soundDecoder = soundDecoderInit(audioSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize)); audioFile->fileSize *= 2; + + *channels = audioFile->channels; + *sampleRate = audioFile->sampleRate; } else { audioFile->fileSize = fileGetSize(stream); } diff --git a/src/audio.h b/src/audio.h index 2b96ac9..752b914 100644 --- a/src/audio.h +++ b/src/audio.h @@ -5,7 +5,7 @@ namespace fallout { typedef bool(AudioQueryCompressedFunc)(char* filePath); -int audioOpen(const char* fname, int mode); +int audioOpen(const char* fname, int* channels, int* sampleRate); int audioClose(int handle); int audioRead(int handle, void* buffer, unsigned int size); long audioSeek(int handle, long offset, int origin); diff --git a/src/audio_file.cc b/src/audio_file.cc index d1dce84..8333c5a 100644 --- a/src/audio_file.cc +++ b/src/audio_file.cc @@ -57,7 +57,7 @@ static int audioFileSoundDecoderReadHandler(void* data, void* buffer, unsigned i } // 0x41A88C -int audioFileOpen(const char* fname, int flags) +int audioFileOpen(const char* fname, int* channels, int* sampleRate) { char path[COMPAT_MAX_PATH]; strcpy(path, fname); @@ -69,28 +69,7 @@ int audioFileOpen(const char* fname, int flags) compression = 0; } - char mode[4]; - memset(mode, '\0', 4); - - // NOTE: Original implementation is slightly different, it uses separate - // variable to track index where to set 't' and 'b'. - char* pm = mode; - if (flags & 0x01) { - *pm++ = 'w'; - } else if (flags & 0x02) { - *pm++ = 'w'; - *pm++ = '+'; - } else { - *pm++ = 'r'; - } - - if (flags & 0x0100) { - *pm++ = 't'; - } else if (flags & 0x0200) { - *pm++ = 'b'; - } - - FILE* stream = compat_fopen(path, mode); + FILE* stream = compat_fopen(path, "rb"); if (stream == NULL) { return -1; } @@ -119,6 +98,9 @@ int audioFileOpen(const char* fname, int flags) audioFile->flags |= AUDIO_FILE_COMPRESSED; audioFile->soundDecoder = soundDecoderInit(audioFileSoundDecoderReadHandler, audioFile->stream, &(audioFile->channels), &(audioFile->sampleRate), &(audioFile->fileSize)); audioFile->fileSize *= 2; + + *channels = audioFile->channels; + *sampleRate = audioFile->sampleRate; } else { audioFile->fileSize = getFileSize(stream); } diff --git a/src/audio_file.h b/src/audio_file.h index f00734b..a43bdc7 100644 --- a/src/audio_file.h +++ b/src/audio_file.h @@ -5,7 +5,7 @@ namespace fallout { typedef bool(AudioFileQueryCompressedFunc)(char* filePath); -int audioFileOpen(const char* fname, int flags); +int audioFileOpen(const char* fname, int* channels, int* sampleRate); int audioFileClose(int handle); int audioFileRead(int handle, void* buf, unsigned int size); long audioFileSeek(int handle, long offset, int origin); diff --git a/src/game_sound.cc b/src/game_sound.cc index e7a838c..b1033ed 100644 --- a/src/game_sound.cc +++ b/src/game_sound.cc @@ -157,7 +157,7 @@ static int _gsound_speech_volume_get_set(int volume); static void speechPause(); static void speechResume(); static void _gsound_bkg_proc(); -static int gameSoundFileOpen(const char* fname, int access); +static int gameSoundFileOpen(const char* fname, int* channels, int* sampleRate); static long _gsound_write_(); static long gameSoundFileTellNotImplemented(int handle); static int gameSoundFileWrite(int handle, const void* buf, unsigned int size); @@ -1548,12 +1548,8 @@ void _gsound_bkg_proc() } // 0x451A08 -int gameSoundFileOpen(const char* fname, int flags) +int gameSoundFileOpen(const char* fname, int* channels, int* sampleRate) { - if ((flags & 2) != 0) { - return -1; - } - File* stream = fileOpen(fname, "rb"); if (stream == NULL) { return -1; diff --git a/src/sound.cc b/src/sound.cc index 353b605..5b6114b 100644 --- a/src/sound.cc +++ b/src/sound.cc @@ -1,5 +1,6 @@ #include "sound.h" +#include #include #include #include @@ -8,7 +9,6 @@ #ifdef _WIN32 #include #else -#include #include #endif @@ -49,7 +49,7 @@ static long soundFileSize(int fileHandle); static long soundTellData(int fileHandle); static int soundWriteData(int fileHandle, const void* buf, unsigned int size); static int soundReadData(int fileHandle, void* buf, unsigned int size); -static int soundOpenData(const char* filePath, int flags); +static int soundOpenData(const char* filePath, int* channels, int* sampleRate); static long soundSeekData(int fileHandle, long offset, int origin); static int soundCloseData(int fileHandle); static char* soundFileManglerDefaultImpl(char* fname); @@ -223,9 +223,9 @@ static int soundReadData(int fileHandle, void* buf, unsigned int size) } // 0x4AC768 -static int soundOpenData(const char* filePath, int flags) +static int soundOpenData(const char* filePath, int* channels, int* sampleRate) { - return open(filePath, flags); + return open(filePath, _O_RDONLY | _O_BINARY); } // 0x4AC774 @@ -608,7 +608,7 @@ int soundLoad(Sound* sound, char* filePath) return gSoundLastError; } - sound->io.fd = sound->io.open(gSoundFileNameMangler(filePath), 0x0200); + sound->io.fd = sound->io.open(gSoundFileNameMangler(filePath), &(sound->channels), &(sound->rate)); if (sound->io.fd == -1) { gSoundLastError = SOUND_FILE_NOT_FOUND; return gSoundLastError; diff --git a/src/sound.h b/src/sound.h index ab81e63..427b5f8 100644 --- a/src/sound.h +++ b/src/sound.h @@ -46,7 +46,7 @@ typedef enum SoundError { SOUND_ERR_COUNT, } SoundError; -typedef int SoundOpenProc(const char* filePath, int flags); +typedef int SoundOpenProc(const char* filePath, int* channels, int* sampleRate); typedef int SoundCloseProc(int fileHandle); typedef int SoundReadProc(int fileHandle, void* buf, unsigned int size); typedef int SoundWriteProc(int fileHandle, const void* buf, unsigned int size); diff --git a/src/sound_effects_cache.cc b/src/sound_effects_cache.cc index 26aead6..30f91ae 100644 --- a/src/sound_effects_cache.cc +++ b/src/sound_effects_cache.cc @@ -154,7 +154,7 @@ void soundEffectsCacheFlush() // sfxc_cached_open // 0x4A915C -int soundEffectsCacheFileOpen(const char* fname, int mode) +int soundEffectsCacheFileOpen(const char* fname, int* channels, int* sampleRate) { if (_sfxc_files_open >= SOUND_EFFECTS_MAX_COUNT) { return -1; diff --git a/src/sound_effects_cache.h b/src/sound_effects_cache.h index e6e0972..579c856 100644 --- a/src/sound_effects_cache.h +++ b/src/sound_effects_cache.h @@ -11,7 +11,7 @@ int soundEffectsCacheInit(int cache_size, const char* effectsPath); void soundEffectsCacheExit(); int soundEffectsCacheInitialized(); void soundEffectsCacheFlush(); -int soundEffectsCacheFileOpen(const char* fname, int mode); +int soundEffectsCacheFileOpen(const char* fname, int* channels, int* sampleRate); int soundEffectsCacheFileClose(int handle); int soundEffectsCacheFileRead(int handle, void* buf, unsigned int size); int soundEffectsCacheFileWrite(int handle, const void* buf, unsigned int size);