From 2f5512d22799ac484986b48a0b12538e964cb148 Mon Sep 17 00:00:00 2001 From: Christian Semmler Date: Thu, 18 Jan 2024 13:04:07 -0500 Subject: [PATCH] Fixes/improvements --- .../legoomni/src/video/legometerpresenter.cpp | 2 +- LEGO1/omni/include/mxmusicmanager.h | 7 +- LEGO1/omni/include/mxstreamcontroller.h | 2 +- LEGO1/omni/src/audio/mxmusicmanager.cpp | 109 +++++++++++------- util/compat.h | 5 + 5 files changed, 78 insertions(+), 47 deletions(-) diff --git a/LEGO1/lego/legoomni/src/video/legometerpresenter.cpp b/LEGO1/lego/legoomni/src/video/legometerpresenter.cpp index 5be9d0d6..f347545d 100644 --- a/LEGO1/lego/legoomni/src/video/legometerpresenter.cpp +++ b/LEGO1/lego/legoomni/src/video/legometerpresenter.cpp @@ -8,7 +8,7 @@ DECOMP_SIZE_ASSERT(LegoMeterPresenter, 0x94) // GLOBAL: LEGO1 0x1010207c // STRING: LEGO1 0x10101fb4 -const char* g_filterIndex = "FILTER_INDEX"; +const char* g_filterIndex = "FILLER_INDEX"; // GLOBAL: LEGO1 0x10102094 // STRING: LEGO1 0x10101f70 diff --git a/LEGO1/omni/include/mxmusicmanager.h b/LEGO1/omni/include/mxmusicmanager.h index f26a4c9b..48c2df69 100644 --- a/LEGO1/omni/include/mxmusicmanager.h +++ b/LEGO1/omni/include/mxmusicmanager.h @@ -4,6 +4,8 @@ #include "decomp.h" #include "mxaudiomanager.h" +#include + // VTABLE: LEGO1 0x100dc930 // SIZE 0x58 class MxMusicManager : public MxAudioManager { @@ -18,7 +20,7 @@ class MxMusicManager : public MxAudioManager { inline MxBool GetMIDIInitialized() { return m_midiInitialized; } inline void GetMIDIVolume(DWORD& p_volume) { - if (midiOutGetVolume(m_midiStreamH, &p_volume)) { + if (midiOutGetVolume((HMIDIOUT) m_midiStreamH, &p_volume)) { p_volume = CalculateVolume(100); } } @@ -35,6 +37,9 @@ class MxMusicManager : public MxAudioManager { MxS32 CalculateVolume(MxS32 p_volume); void SetMIDIVolume(); + static void CALLBACK + MidiCallbackProc(HMIDIOUT p_hmo, UINT p_wMsg, DWORD_PTR p_dwInstance, DWORD_PTR p_dwParam1, DWORD_PTR p_dwParam2); + HMIDISTRM m_midiStreamH; // 0x30 MxBool m_midiInitialized; // 0x34 MxU32 m_bufferSize; // 0x38 diff --git a/LEGO1/omni/include/mxstreamcontroller.h b/LEGO1/omni/include/mxstreamcontroller.h index e852d099..57caae8e 100644 --- a/LEGO1/omni/include/mxstreamcontroller.h +++ b/LEGO1/omni/include/mxstreamcontroller.h @@ -119,6 +119,6 @@ class MxStreamController : public MxCore { // List::~List // TEMPLATE: LEGO1 0x100c1bc0 -// List::insert +// list >::insert #endif // MXSTREAMCONTROLLER_H diff --git a/LEGO1/omni/src/audio/mxmusicmanager.cpp b/LEGO1/omni/src/audio/mxmusicmanager.cpp index 485aa21b..b936ac30 100644 --- a/LEGO1/omni/src/audio/mxmusicmanager.cpp +++ b/LEGO1/omni/src/audio/mxmusicmanager.cpp @@ -66,38 +66,46 @@ void MxMusicManager::Destroy(MxBool p_fromDestructor) MxResult MxMusicManager::ResetStream() { MxResult result = FAILURE; + if (m_midiInitialized) { if (m_bufferCurrentSize == 0) { if (m_loopCount != -1) { m_loopCount += -1; + if (!m_loopCount) { DeinitializeMIDI(); - return result; + goto done; } } + ResetBuffer(); } - do { - if (m_midiHdrP->dwFlags & (MHDR_DONE | MHDR_PREPARED)) { - if (midiOutUnprepareHeader((HMIDIOUT) m_midiStreamH, m_midiHdrP, sizeof(MIDIHDR))) - break; - memset(m_midiHdrP, 0, sizeof(MIDIHDR)); + + if (m_midiHdrP->dwFlags & (MHDR_DONE | MHDR_PREPARED)) { + if (midiOutUnprepareHeader((HMIDIOUT) m_midiStreamH, m_midiHdrP, sizeof(MIDIHDR)) != MMSYSERR_NOERROR) + goto done; + + memset(m_midiHdrP, 0, sizeof(MIDIHDR)); + } + + m_bufferCurrentOffset += 4; + DWORD length = *((DWORD*) m_bufferCurrentOffset); + m_bufferCurrentOffset += sizeof(DWORD); + + m_midiHdrP->lpData = (LPSTR) m_bufferCurrentOffset; + m_midiHdrP->dwBufferLength = length; + m_midiHdrP->dwBytesRecorded = length; + + if (!midiOutPrepareHeader((HMIDIOUT) m_midiStreamH, m_midiHdrP, sizeof(MIDIHDR))) { + if (!midiStreamOut(m_midiStreamH, m_midiHdrP, sizeof(MIDIHDR))) { + result = SUCCESS; + m_bufferCurrentOffset += length; + m_bufferCurrentSize--; } - m_bufferCurrentOffset += 4; - DWORD length = *((DWORD*) m_bufferCurrentOffset); - m_bufferCurrentOffset += 4; - m_midiHdrP->lpData = (LPSTR) m_bufferCurrentOffset; - m_midiHdrP->dwBufferLength = length; - m_midiHdrP->dwBytesRecorded = length; - if (!midiOutPrepareHeader((HMIDIOUT) m_midiStreamH, m_midiHdrP, sizeof(MIDIHDR))) { - if (!midiStreamOut(m_midiStreamH, m_midiHdrP, sizeof(MIDIHDR))) { - result = SUCCESS; - m_bufferCurrentOffset += length; - m_bufferCurrentSize--; - } - } - } while (FALSE); + } } + +done: return result; } @@ -121,11 +129,16 @@ void MxMusicManager::SetMIDIVolume() } // FUNCTION: LEGO1 0x100c0820 -static void CALLBACK -MidiCallbackProc(HMIDIOUT p_hmo, UINT p_wMsg, MxMusicManager* p_dwInstance, MxU32* p_dwParam1, MxU32* p_dwParam2) +void CALLBACK MxMusicManager::MidiCallbackProc( + HMIDIOUT p_hmo, + UINT p_wMsg, + DWORD_PTR p_dwInstance, + DWORD_PTR p_dwParam1, + DWORD_PTR p_dwParam2 +) { if (p_wMsg == MOM_DONE) - p_dwInstance->ResetStream(); + ((MxMusicManager*) p_dwInstance)->ResetStream(); } // FUNCTION: LEGO1 0x100c0840 @@ -194,54 +207,62 @@ MxS32 MxMusicManager::CalculateVolume(MxS32 p_volume) undefined4 MxMusicManager::InitializeMIDI(MxU8* p_data, MxS32 p_loopCount) { MxResult result = FAILURE; + m_criticalSection.Enter(); - do { - if (m_midiInitialized) - break; + + if (!m_midiInitialized) { MxU32 total = midiOutGetNumDevs(); MxU32 device = 0; + for (; device < total; device++) { MIDIOUTCAPSA caps; midiOutGetDevCapsA(device, &caps, sizeof(MIDIOUTCAPSA)); if (caps.wTechnology == MOD_FMSYNTH) break; } - if (device == total) + + if (device >= total) device = -1; - if (midiStreamOpen( - (LPHMIDIOUT) &m_midiStreamH, - &device, - 1, - (DWORD) MidiCallbackProc, - (DWORD) this, - CALLBACK_FUNCTION - )) - break; + + if (midiStreamOpen(&m_midiStreamH, &device, 1, (DWORD) MidiCallbackProc, (DWORD) this, CALLBACK_FUNCTION) != + MMSYSERR_NOERROR) + goto done; + GetMIDIVolume(m_midiVolume); + m_midiHdrP = new MIDIHDR(); if (!m_midiHdrP) - break; + goto done; + memset(m_midiHdrP, 0, sizeof(MIDIHDR)); + MIDIPROPTIMEDIV timediv; timediv.cbStruct = 8; m_bufferOffset = p_data; m_bufferOffset += 0x14; timediv.dwTimeDiv = *((DWORD*) m_bufferOffset); - if (midiStreamProperty(m_midiStreamH, (LPBYTE) &timediv, MIDIPROP_SET | MIDIPROP_TIMEDIV)) - break; + + if (midiStreamProperty(m_midiStreamH, (LPBYTE) &timediv, MIDIPROP_SET | MIDIPROP_TIMEDIV) != MMSYSERR_NOERROR) + goto done; + m_bufferOffset += 0x14; m_bufferSize = *((MxU32*) m_bufferOffset); - m_bufferOffset += 0x4; + m_bufferOffset += sizeof(MxU32); m_loopCount = p_loopCount; m_midiInitialized = TRUE; + ResetBuffer(); if (ResetStream() != SUCCESS) - break; + goto done; + SetMIDIVolume(); - if (midiStreamRestart(m_midiStreamH)) - break; + if (midiStreamRestart(m_midiStreamH) != MMSYSERR_NOERROR) + goto done; + result = SUCCESS; - } while (FALSE); + } + +done: m_criticalSection.Leave(); return result; } diff --git a/util/compat.h b/util/compat.h index b38a1c7c..a5b7a487 100644 --- a/util/compat.h +++ b/util/compat.h @@ -15,6 +15,11 @@ #define COMPAT_CONST #endif +// DWORD_PTR didn't exist in older Windows SDKs +#if (defined(_MSC_VER) && _MSC_VER < 1100) +typedef unsigned long DWORD_PTR, *PDWORD_PTR; +#endif + // Disable "identifier was truncated to '255' characters" warning. // Impossible to avoid this if using STL map or set. // This removes most (but not all) occurrences of the warning.