From 4e8cd33eacd3b3e8a251557e98400056c3a149ab Mon Sep 17 00:00:00 2001 From: Mark Olsen Date: Thu, 16 Feb 2023 03:41:31 +0000 Subject: [PATCH] Some general sound code cleanup: Removed unused functions and variables, marked internally-used-only functions and variables as static and removed stray semicolons after functions. --- common/audio.h | 8 -- common/soundio.cpp | 187 +++++++++++++------------------------- common/soundio_null.cpp | 71 ++++++--------- common/soundio_openal.cpp | 151 ++++++++++-------------------- 4 files changed, 140 insertions(+), 277 deletions(-) diff --git a/common/audio.h b/common/audio.h index f8fe92ff..b292518a 100644 --- a/common/audio.h +++ b/common/audio.h @@ -118,13 +118,9 @@ typedef enum /*=========================================================================*/ /* The following prototypes are for the file: SOUNDIO.CPP */ /*=========================================================================*/ -int File_Stream_Sample(char const* filename, bool real_time_start = false); int File_Stream_Sample_Vol(char const* filename, int volume, bool real_time_start = false); void Sound_Callback(void); -void maintenance_callback(void); void* Load_Sample(char const* filename); -int Load_Sample_Into_Buffer(char const* filename, void* buffer, int size); -int Sample_Read(int fh, void* buffer, int size); void Free_Sample(void const* sample); bool Audio_Init(int bits_per_sample, bool stereo, int rate, bool reverse_channels); void Sound_End(void); @@ -133,13 +129,9 @@ bool Sample_Status(int handle); bool Is_Sample_Playing(void const* sample); void Stop_Sample_Playing(void const* sample); int Play_Sample(void const* sample, int priority = 0xFF, int volume = 0xFF, signed short panloc = 0x0); -int Play_Sample_Handle(void const* sample, int priority, int volume, signed short panloc, int id); -int Set_Sound_Vol(int volume); int Set_Score_Vol(int volume); void Fade_Sample(int handle, int ticks); -int Get_Free_Sample_Handle(int priority); int Get_Digi_Handle(void); -int Sample_Length(void const* sample); void Restore_Sound_Buffers(void); bool Set_Primary_Buffer_Format(void); bool Start_Primary_Sound_Buffer(bool forced); diff --git a/common/soundio.cpp b/common/soundio.cpp index d49e5b97..01a68ba5 100644 --- a/common/soundio.cpp +++ b/common/soundio.cpp @@ -37,44 +37,47 @@ enum INVALID_FILE_HANDLE = -1, }; -bool ReverseChannels; -LockedDataType LockedData; +static bool ReverseChannels; +static LockedDataType LockedData; LPDIRECTSOUND SoundObject; -LPDIRECTSOUNDBUFFER DumpBuffer; +static LPDIRECTSOUNDBUFFER DumpBuffer; LPDIRECTSOUNDBUFFER PrimaryBufferPtr; -WAVEFORMATEX DsBuffFormat; -WAVEFORMATEX PrimaryBuffFormat; -DSBUFFERDESC BufferDesc; -DSBUFFERDESC PrimaryBufferDesc; -CRITICAL_SECTION GlobalAudioCriticalSection; -void* SoundThreadHandle; -bool SoundThreadActive; -bool StartingFileStream; -MemoryFlagType StreamBufferFlag; +static WAVEFORMATEX DsBuffFormat; +static WAVEFORMATEX PrimaryBuffFormat; +static DSBUFFERDESC BufferDesc; +static DSBUFFERDESC PrimaryBufferDesc; +static CRITICAL_SECTION GlobalAudioCriticalSection; +static void* SoundThreadHandle; +static bool SoundThreadActive; +static bool StartingFileStream; +static MemoryFlagType StreamBufferFlag; //int Misc; -UINT SoundTimerHandle; -void* FileStreamBuffer; -bool volatile AudioDone; +static UINT SoundTimerHandle; +static void* FileStreamBuffer; +static bool volatile AudioDone; SFX_Type SoundType; Sample_Type SampleType; // Forward declare some internal functions. bool Attempt_Audio_Restore(LPDIRECTSOUNDBUFFER sound_buffer); +static int Get_Free_Sample_Handle(int priority); +static int Play_Sample_Handle(void const* sample, int priority, int volume, signed short panloc, int id); +static int Sample_Read(int fh, void* buffer, int size); void CALLBACK Sound_Timer_Callback(UINT uID = 0, UINT uMsg = 0, DWORD_PTR dwUser = 0, DWORD_PTR dw1 = 0, DWORD_PTR dw2 = 0); -int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void** dest, int size); -int Sample_Copy(SampleTrackerType* st, - void** source, - int* ssize, - void** alternate, - int* altsize, - void* dest, - int size, - SCompressType sound_comp, - void* trailer, - int16_t* trailersize); - -int Convert_HMI_To_Direct_Sound_Volume(int vol) +static int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void** dest, int size); +static int Sample_Copy(SampleTrackerType* st, + void** source, + int* ssize, + void** alternate, + int* altsize, + void* dest, + int size, + SCompressType sound_comp, + void* trailer, + int16_t* trailersize); + +static int Convert_HMI_To_Direct_Sound_Volume(int vol) { // Complete silence. if (vol <= 0) { @@ -94,7 +97,7 @@ int Convert_HMI_To_Direct_Sound_Volume(int vol) return int(-(v + -1.0)); } -void Maintenance_Callback() +static void Maintenance_Callback() { SampleTrackerType* st = LockedData.SampleTracker; HRESULT ret; @@ -260,7 +263,7 @@ void Maintenance_Callback() } } -void Init_Locked_Data() +static void Init_Locked_Data() { LockedData.DigiHandle = INVALID_AUDIO_HANDLE; LockedData.ServiceSomething = 0; @@ -273,7 +276,7 @@ void Init_Locked_Data() LockedData._int = 0; } -bool File_Callback(short id, short* odd, void** buffer, int* size) +static bool File_Callback(short id, short* odd, void** buffer, int* size) { if (id == INVALID_AUDIO_HANDLE) { return false; @@ -384,11 +387,11 @@ bool File_Callback(short id, short* odd, void** buffer, int* size) return false; } -int __cdecl Stream_Sample_Vol(void* buffer, - int size, - bool (*callback)(short int, short int*, void**, int*), - int volume, - int handle) +static int __cdecl Stream_Sample_Vol(void* buffer, + int size, + bool (*callback)(short int, short int*, void**, int*), + int volume, + int handle) { if (AudioDone || buffer == nullptr || size == 0 || LockedData.DigiHandle == INVALID_AUDIO_HANDLE) { return INVALID_AUDIO_HANDLE; @@ -414,12 +417,7 @@ int __cdecl Stream_Sample_Vol(void* buffer, return playid; } -int File_Stream_Sample(const char* filename, bool real_time_start) -{ - return File_Stream_Sample_Vol(filename, VOLUME_MAX, real_time_start); -} - -void File_Stream_Preload(int index) +static void File_Stream_Preload(int index) { SampleTrackerType* st = &LockedData.SampleTracker[index]; int maxnum = (LockedData.StreamBufferCount / 2) + 4; @@ -598,25 +596,7 @@ void* Load_Sample(char const* filename) return data; } -long Load_Sample_Into_Buffer(char const* filename, void* buffer, long size) -{ - if (buffer == nullptr || size == 0 || LockedData.DigiHandle == INVALID_AUDIO_HANDLE || !filename - || !Find_File(filename)) { - return 0; - } - - int handle = Open_File(filename, 1); - - if (handle == INVALID_FILE_HANDLE) { - return 0; - } - - int sample_size = Sample_Read(handle, buffer, size); - Close_File(handle); - return sample_size; -} - -int Sample_Read(int fh, void* buffer, int size) +static int Sample_Read(int fh, void* buffer, int size) { if (buffer == nullptr || fh == INVALID_AUDIO_HANDLE || size <= sizeof(AUDHeaderType)) { return 0; @@ -641,7 +621,7 @@ void Free_Sample(void const* sample) } } -void CALLBACK Sound_Timer_Callback(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) +static void CALLBACK Sound_Timer_Callback(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) { if (!AudioDone) { EnterCriticalSection(&GlobalAudioCriticalSection); @@ -650,7 +630,7 @@ void CALLBACK Sound_Timer_Callback(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_ } } -void Sound_Thread(void* a1) +static void Sound_Thread(void* a1) { // TODO : Find a alternative solution, this is the original code, and likely causes lockups on modern systems. DuplicateHandle( @@ -677,7 +657,7 @@ bool Set_Primary_Buffer_Format() return false; } -int Print_Sound_Error(char* sound_error, void* window) +static int Print_Sound_Error(char* sound_error, void* window) { return MessageBoxA((HWND)window, sound_error, "DirectSound Audio Error", MB_OK | MB_ICONWARNING); } @@ -979,7 +959,7 @@ void Stop_Sample_Playing(void const* sample) } } -int Get_Free_Sample_Handle(int priority) +static int Get_Free_Sample_Handle(int priority) { int index = 0; @@ -1029,7 +1009,7 @@ int Play_Sample(void const* sample, int priority, int volume, signed short panlo return Play_Sample_Handle(sample, priority, volume, panloc, Get_Free_Sample_Handle(priority)); } -bool Attempt_Audio_Restore(LPDIRECTSOUNDBUFFER sound_buffer) +static bool Attempt_Audio_Restore(LPDIRECTSOUNDBUFFER sound_buffer) { HRESULT return_code = 0; DWORD play_status = 0; @@ -1050,7 +1030,7 @@ bool Attempt_Audio_Restore(LPDIRECTSOUNDBUFFER sound_buffer) return return_code != DSERR_BUFFERLOST; } -int Attempt_To_Play_Buffer(int id) +static int Attempt_To_Play_Buffer(int id) { HRESULT return_code; SampleTrackerType* st = &LockedData.SampleTracker[id]; @@ -1093,7 +1073,7 @@ int Attempt_To_Play_Buffer(int id) } extern bool Any_Locked(); -int Play_Sample_Handle(void const* sample, int priority, int volume, signed short panloc, int id) +static int Play_Sample_Handle(void const* sample, int priority, int volume, signed short panloc, int id) { HRESULT return_code; DWORD status; @@ -1323,13 +1303,6 @@ void Restore_Sound_Buffers() } } -int Set_Sound_Vol(int vol) -{ - int oldvol = LockedData.SoundVolume; - LockedData.SoundVolume = vol; - return oldvol; -} - int Set_Score_Vol(int volume) { int old = LockedData.ScoreVolume; @@ -1360,49 +1333,11 @@ void Fade_Sample(int index, int ticks) } } -void Unfade_Sample(int index, int ticks) -{ - if (Sample_Status(index)) { - SampleTrackerType* st = &LockedData.SampleTracker[index]; - - if (ticks > 0 && !st->Loading) { - st->Reducer -= ((st->Volume / ticks) + 1); - } else { - st->Reducer = 0; - } - } -} - int Get_Digi_Handle() { return LockedData.DigiHandle; } -unsigned Sample_Length(void* sample) -{ - if (sample == nullptr) { - return 0; - } - - AUDHeaderType header; - memcpy(&header, sample, sizeof(header)); - unsigned time = header.UncompSize; - - if (header.Flags & 2) { - time /= 2; - } - - if (header.Flags & 1) { - time /= 2; - } - - if (header.Rate / 60 > 0) { - time /= header.Rate / 60; - } - - return time; -} - bool Start_Primary_Sound_Buffer(bool forced) { if (PrimaryBufferPtr == nullptr || !GameInFocus) { @@ -1440,7 +1375,7 @@ void Stop_Primary_Sound_Buffer() } } -void Suspend_Audio_Thread() +static void Suspend_Audio_Thread() { if (SoundThreadActive) { timeKillEvent(SoundTimerHandle); @@ -1449,7 +1384,7 @@ void Suspend_Audio_Thread() } } -void Resume_Audio_Thread() +static void Resume_Audio_Thread() { if (!SoundThreadActive) { SoundTimerHandle = timeSetEvent(TIMER_DELAY, TIMER_RESOLUTION, Sound_Timer_Callback, SoundThreadActive, 1); @@ -1457,7 +1392,7 @@ void Resume_Audio_Thread() } } -int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void** dest, int size) +static int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void** dest, int size) { int out = 0; @@ -1498,16 +1433,16 @@ int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void* return out; } -int Sample_Copy(SampleTrackerType* st, - void** source, - int* ssize, - void** alternate, - int* altsize, - void* dest, - int size, - SCompressType scomp, - void* trailer, - int16_t* trailersize) +static int Sample_Copy(SampleTrackerType* st, + void** source, + int* ssize, + void** alternate, + int* altsize, + void* dest, + int size, + SCompressType scomp, + void* trailer, + int16_t* trailersize) { int datasize = 0; diff --git a/common/soundio_null.cpp b/common/soundio_null.cpp index 1e01680d..afbd6600 100644 --- a/common/soundio_null.cpp +++ b/common/soundio_null.cpp @@ -6,80 +6,67 @@ bool StreamLowImpact = false; SFX_Type SoundType; Sample_Type SampleType; -int File_Stream_Sample(char const* filename, bool real_time_start) -{ - return 1; -}; int File_Stream_Sample_Vol(char const* filename, int volume, bool real_time_start) { return 1; -}; -void Sound_Callback(void){}; -void maintenance_callback(void){}; +} +void Sound_Callback(void) +{ +} void* Load_Sample(char const* filename) { return nullptr; -}; -int Load_Sample_Into_Buffer(char const* filename, void* buffer, int size) -{ - return 0; } -int Sample_Read(int fh, void* buffer, int size) +void Free_Sample(void const* sample) { - return 0; -}; -void Free_Sample(void const* sample){}; +} bool Audio_Init(int bits_per_sample, bool stereo, int rate, bool reverse_channels) { return 0; -}; -void Sound_End(void){}; -void Stop_Sample(int handle){}; +} +void Sound_End(void) +{ +} +void Stop_Sample(int handle) +{ +} bool Sample_Status(int handle) { return 0; -}; +} bool Is_Sample_Playing(void const* sample) { return 0; -}; -void Stop_Sample_Playing(void const* sample){}; -int Play_Sample(void const* sample, int priority, int volume, signed short panloc) +} +void Stop_Sample_Playing(void const* sample) { - return 1; -}; -int Play_Sample_Handle(void const* sample, int priority, int volume, signed short panloc, int id) +} +int Play_Sample(void const* sample, int priority, int volume, signed short panloc) { return 1; -}; -int Set_Sound_Vol(int volume) -{ - return 0; -}; +} int Set_Score_Vol(int volume) { return 0; -}; -void Fade_Sample(int handle, int ticks){}; -int Get_Free_Sample_Handle(int priority) +} +void Fade_Sample(int handle, int ticks) { - return 1; -}; +} int Get_Digi_Handle(void) { return 1; } -int Sample_Length(void const* sample) +void Restore_Sound_Buffers(void) { - return 0; -}; -void Restore_Sound_Buffers(void){}; +} bool Set_Primary_Buffer_Format(void) { return 0; -}; +} bool Start_Primary_Sound_Buffer(bool forced) { return 0; -}; -void Stop_Primary_Sound_Buffer(void){}; +} +void Stop_Primary_Sound_Buffer(void) +{ +} diff --git a/common/soundio_openal.cpp b/common/soundio_openal.cpp index 46d0d0df..669d91be 100644 --- a/common/soundio_openal.cpp +++ b/common/soundio_openal.cpp @@ -203,24 +203,26 @@ struct LockedDataType unsigned SoundVolume; unsigned ScoreVolume; int VolumeLock; -} LockedData; +}; void (*Audio_Focus_Loss_Function)() = nullptr; +static struct LockedDataType LockedData; SFX_Type SoundType; Sample_Type SampleType; -void* FileStreamBuffer = nullptr; +static void* FileStreamBuffer = nullptr; bool StreamLowImpact = false; -bool StartingFileStream = false; -bool volatile AudioDone = false; +static bool StartingFileStream = false; +static bool volatile AudioDone = false; ALCcontext* OpenALContext = nullptr; extern bool GameInFocus; static uint8_t ChunkBuffer[BUFFER_CHUNK_SIZE]; -unsigned int SoundTimerHandle; - bool Any_Locked(); // From each games winstub.cpp at the moment. -void Maintenance_Callback(); +static int Get_Free_Sample_Handle(int priority); +static void Maintenance_Callback(); +static int Play_Sample_Handle(const void* sample, int priority, int volume, signed short panloc, int id); +static int Sample_Read(int fh, void* buffer, int size); static ALenum Get_OpenAL_Format(int bits, int channels) { @@ -282,7 +284,7 @@ static void Init_Locked_Data() LockedData.ScoreVolume = VOLUME_MAX; } -int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void** dest, int size) +static int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void** dest, int size) { int out = 0; @@ -323,16 +325,16 @@ int Simple_Copy(void** source, int* ssize, void** alternate, int* altsize, void* return out; } -int Sample_Copy(SampleTrackerType* st, - void** source, - int* ssize, - void** alternate, - int* altsize, - void* dest, - int size, - SCompressType scomp, - void* trailer, - int16_t* trailersize) +static int Sample_Copy(SampleTrackerType* st, + void** source, + int* ssize, + void** alternate, + int* altsize, + void* dest, + int size, + SCompressType scomp, + void* trailer, + int16_t* trailersize) { int datasize = 0; @@ -411,12 +413,8 @@ int Sample_Copy(SampleTrackerType* st, return datasize; } -int File_Stream_Sample(const char* filename, bool real_time_start) -{ - return File_Stream_Sample_Vol(filename, VOLUME_MAX, real_time_start); -} - -int Stream_Sample_Vol(void* buffer, int size, bool (*callback)(short, short*, void**, int*), int volume, int handle) +static int +Stream_Sample_Vol(void* buffer, int size, bool (*callback)(short, short*, void**, int*), int volume, int handle) { if (AudioDone || buffer == nullptr || size == 0 || LockedData.DigiHandle == INVALID_AUDIO_HANDLE) { return INVALID_AUDIO_HANDLE; @@ -442,7 +440,7 @@ int Stream_Sample_Vol(void* buffer, int size, bool (*callback)(short, short*, vo return playid; } -bool File_Callback(short id, short* odd, void** buffer, int* size) +static bool File_Callback(short id, short* odd, void** buffer, int* size) { if (id == INVALID_AUDIO_HANDLE) { return false; @@ -510,7 +508,7 @@ bool File_Callback(short id, short* odd, void** buffer, int* size) return false; } -void File_Stream_Preload(int index) +static void File_Stream_Preload(int index) { SampleTrackerType* st = &LockedData.SampleTracker[index]; int maxnum = (LockedData.StreamBufferCount / 2) + 4; @@ -611,7 +609,7 @@ int File_Stream_Sample_Vol(char const* filename, int volume, bool real_time_star } return INVALID_AUDIO_HANDLE; -}; +} void Sound_Callback() { @@ -663,9 +661,9 @@ void Sound_Callback() } } } -}; +} -void Maintenance_Callback() +static void Maintenance_Callback() { if (AudioDone) { return; @@ -760,7 +758,7 @@ void Maintenance_Callback() --LockedData.VolumeLock; } -}; +} void* Load_Sample(char const* filename) { @@ -784,27 +782,9 @@ void* Load_Sample(char const* filename) } return data; -}; - -int Load_Sample_Into_Buffer(char const* filename, void* buffer, int size) -{ - if (buffer == nullptr || size == 0 || LockedData.DigiHandle == INVALID_AUDIO_HANDLE || !filename - || !Find_File(filename)) { - return 0; - } - - int handle = Open_File(filename, 1); - - if (handle == INVALID_FILE_HANDLE) { - return 0; - } - - int sample_size = Sample_Read(handle, buffer, size); - Close_File(handle); - return sample_size; } -int Sample_Read(int fh, void* buffer, int size) +static int Sample_Read(int fh, void* buffer, int size) { if (buffer == nullptr || fh == INVALID_AUDIO_HANDLE || size <= sizeof(AUDHeaderType)) { return 0; @@ -819,14 +799,14 @@ int Sample_Read(int fh, void* buffer, int size) memcpy(buffer, &header, sizeof(AUDHeaderType)); return actual_bytes_read; -}; +} void Free_Sample(const void* sample) { if (sample != nullptr) { free((void*)sample); } -}; +} bool Audio_Init(int bits_per_sample, bool stereo, int rate, bool reverse_channels) { @@ -886,7 +866,7 @@ bool Audio_Init(int bits_per_sample, bool stereo, int rate, bool reverse_channel AudioDone = false; return true; -}; +} void Sound_End() { @@ -914,7 +894,7 @@ void Sound_End() } AudioDone = true; -}; +} void Stop_Sample(int index) { @@ -953,7 +933,7 @@ void Stop_Sample(int index) st->QueueBuffer = nullptr; } } -}; +} bool Sample_Status(int index) { @@ -983,7 +963,7 @@ bool Sample_Status(int index) alGetSourcei(st->OpenALSource, AL_SOURCE_STATE, &val); return val == AL_PLAYING; -}; +} bool Is_Sample_Playing(const void* sample) { @@ -998,7 +978,7 @@ bool Is_Sample_Playing(const void* sample) } return false; -}; +} void Stop_Sample_Playing(const void* sample) { @@ -1010,14 +990,14 @@ void Stop_Sample_Playing(const void* sample) } } } -}; +} int Play_Sample(const void* sample, int priority, int volume, signed short panloc) { return Play_Sample_Handle(sample, priority, volume, panloc, Get_Free_Sample_Handle(priority)); -}; +} -int Attempt_To_Play_Buffer(int id) +static int Attempt_To_Play_Buffer(int id) { SampleTrackerType* st = &LockedData.SampleTracker[id]; @@ -1029,7 +1009,7 @@ int Attempt_To_Play_Buffer(int id) return id; } -int Play_Sample_Handle(const void* sample, int priority, int volume, signed short panloc, int id) +static int Play_Sample_Handle(const void* sample, int priority, int volume, signed short panloc, int id) { if (Any_Locked()) { return INVALID_AUDIO_HANDLE; @@ -1160,15 +1140,7 @@ int Play_Sample_Handle(const void* sample, int priority, int volume, signed shor } return INVALID_AUDIO_HANDLE; -}; - -int Set_Sound_Vol(int volume) -{ - - int oldvol = LockedData.SoundVolume; - LockedData.SoundVolume = volume; - return oldvol; -}; +} int Set_Score_Vol(int volume) { @@ -1184,7 +1156,7 @@ int Set_Score_Vol(int volume) } return old; -}; +} void Fade_Sample(int index, int ticks) { @@ -1197,9 +1169,9 @@ void Fade_Sample(int index, int ticks) Stop_Sample(index); } } -}; +} -int Get_Free_Sample_Handle(int priority) +static int Get_Free_Sample_Handle(int priority) { int index = 0; @@ -1242,44 +1214,21 @@ int Get_Free_Sample_Handle(int priority) LockedData.SampleTracker[index].IsScore = false; return index; -}; +} int Get_Digi_Handle() { return LockedData.DigiHandle; } -int Sample_Length(const void* sample) +void Restore_Sound_Buffers() { - if (sample == nullptr) { - return 0; - } - - AUDHeaderType header; - memcpy(&header, sample, sizeof(header)); - unsigned time = header.UncompSize; - - if (header.Flags & 2) { - time /= 2; - } - - if (header.Flags & 1) { - time /= 2; - } - - if (header.Rate / 60 > 0) { - time /= header.Rate / 60; - } - - return time; -}; - -void Restore_Sound_Buffers(){}; +} bool Set_Primary_Buffer_Format() { return true; -}; +} bool Start_Primary_Sound_Buffer(bool forced) { @@ -1290,7 +1239,7 @@ bool Start_Primary_Sound_Buffer(bool forced) alcProcessContext(OpenALContext); return true; -}; +} void Stop_Primary_Sound_Buffer() { @@ -1301,4 +1250,4 @@ void Stop_Primary_Sound_Buffer() if (OpenALContext != nullptr) { alcSuspendContext(OpenALContext); } -}; +}