From 9e3c7fc207d4a8d5b154bcda337814b61dfb5934 Mon Sep 17 00:00:00 2001 From: sukibaby <163092272+sukibaby@users.noreply.github.com> Date: Sat, 8 Jun 2024 21:36:40 -0700 Subject: [PATCH] Update RageSoundMixBuffer 1) Set a buffer of 2MB instead of setting the buffer to nullptr and depend on it being grown by assets being loaded at the game launch 2) Update some C style code to C++ style 3) Improve Extend feature to support handling a larger number of samples, and error handling in case of a memory allocation failure --- src/RageSoundMixBuffer.cpp | 31 +++++++++++++++++++++++-------- src/RageSoundMixBuffer.h | 8 ++++---- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/RageSoundMixBuffer.cpp b/src/RageSoundMixBuffer.cpp index f70546e8a9..99d1081284 100644 --- a/src/RageSoundMixBuffer.cpp +++ b/src/RageSoundMixBuffer.cpp @@ -4,17 +4,28 @@ #include #include +#include RageSoundMixBuffer::RageSoundMixBuffer() { - m_iBufSize = m_iBufUsed = 0; - m_pMixbuf = nullptr; + // See how many samples we can stuff into 2MB. + size_t BUF_SIZE = 2 * 1024 * 1024 / sizeof(float); + + m_pMixbuf = static_cast(std::malloc(BUF_SIZE * sizeof(float))); + if (m_pMixbuf == nullptr) { + ASSERT_M(false, "Failed to allocate memory for the sound mixing buffer"); + } + + m_iBufSize = BUF_SIZE; + std::memset(m_pMixbuf, 0, m_iBufSize * sizeof(float)); + m_iBufUsed = 0; m_iOffset = 0; } + RageSoundMixBuffer::~RageSoundMixBuffer() { - free( m_pMixbuf ); + std::free(m_pMixbuf); } /* write() will start mixing iOffset samples into the buffer. Be careful; this is @@ -24,18 +35,22 @@ void RageSoundMixBuffer::SetWriteOffset( int iOffset ) m_iOffset = iOffset; } -void RageSoundMixBuffer::Extend( unsigned iSamples ) +void RageSoundMixBuffer::Extend(unsigned iSamples) { - const unsigned realsize = iSamples+m_iOffset; + const std::uint64_t realsize = static_cast(iSamples) + static_cast(m_iOffset); if( m_iBufSize < realsize ) { - m_pMixbuf = (float *) realloc( m_pMixbuf, sizeof(float) * realsize ); + m_pMixbuf = static_cast(std::realloc(m_pMixbuf, sizeof(float) * realsize)); + if (m_pMixbuf == nullptr) + { + ASSERT_M(false, "Failed to re-allocate memory for the sound mixing buffer."); + } m_iBufSize = realsize; } if( m_iBufUsed < realsize ) { - memset( m_pMixbuf + m_iBufUsed, 0, (realsize - m_iBufUsed) * sizeof(float) ); + std::memset(m_pMixbuf + m_iBufUsed, 0, (realsize - m_iBufUsed) * sizeof(float)); m_iBufUsed = realsize; } } @@ -73,7 +88,7 @@ void RageSoundMixBuffer::read( std::int16_t *pBuf ) void RageSoundMixBuffer::read( float *pBuf ) { - memcpy( pBuf, m_pMixbuf, m_iBufUsed*sizeof(float) ); + std::memcpy( pBuf, m_pMixbuf, m_iBufUsed * sizeof(float) ); m_iBufUsed = 0; } diff --git a/src/RageSoundMixBuffer.h b/src/RageSoundMixBuffer.h index 020f0a39d3..0f47e377fc 100644 --- a/src/RageSoundMixBuffer.h +++ b/src/RageSoundMixBuffer.h @@ -11,10 +11,10 @@ public: RageSoundMixBuffer(); ~RageSoundMixBuffer(); - /* Mix the given buffer of samples. */ + // Mix the given buffer of samples. void write( const float *pBuf, unsigned iSize, int iSourceStride = 1, int iDestStride = 1 ); - /* Extend the buffer as if write() was called with a buffer of silence. */ + // Extend the buffer as if write() was called with a buffer of silence. void Extend( unsigned iSamples ); void read( std::int16_t *pBuf ); @@ -26,8 +26,8 @@ public: private: float *m_pMixbuf; - unsigned m_iBufSize; /* actual allocated samples */ - unsigned m_iBufUsed; /* used samples */ + std::uint64_t m_iBufSize; // actual allocated samples + std::uint64_t m_iBufUsed; // used samples int m_iOffset; };