From a7477a6c56ee5c3dad01a1d5d9dc35e8956040d0 Mon Sep 17 00:00:00 2001 From: sukibaby <163092272+sukibaby@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:24:27 -0800 Subject: [PATCH] Better type choices, comment improvements, adding missing braces, removal of dead code. --- src/RageSoundMixBuffer.cpp | 13 ++++++++++--- src/RageSoundMixBuffer.h | 6 ++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/RageSoundMixBuffer.cpp b/src/RageSoundMixBuffer.cpp index 84d571c504..31cdc06b7c 100644 --- a/src/RageSoundMixBuffer.cpp +++ b/src/RageSoundMixBuffer.cpp @@ -13,7 +13,7 @@ RageSoundMixBuffer::~RageSoundMixBuffer() {} * measured in samples, not frames, so if the data is stereo, multiply by two. */ void RageSoundMixBuffer::SetWriteOffset( int iOffset ) { - m_iOffset = iOffset; + m_iOffset = static_cast(iOffset); } void RageSoundMixBuffer::Extend(unsigned iSamples) @@ -33,7 +33,7 @@ void RageSoundMixBuffer::write( const float *pBuf, unsigned iSize, int iSourceSt // iSize = 3, iDestStride = 2 uses 4 frames. Don't allocate the stride of the last sample. Extend( iSize * iDestStride - (iDestStride-1) ); - // Scale volume and add. + // Load the audio at m_iOffset into the buffer. float *pDestBuf = &m_pMixbuf[m_iOffset]; while( iSize ) @@ -49,9 +49,12 @@ void RageSoundMixBuffer::read( int16_t *pBuf ) { for (unsigned iPos = 0; iPos < m_pMixbuf.size(); ++iPos) { + // do the read float iOut = m_pMixbuf[iPos]; + // ensure volume is within expected levels to prevent clipping iOut = std::clamp( iOut, -1.0f, +1.0f ); - pBuf[iPos] = static_cast(iOut * INT16_MAX); + // round rather than truncate to minimize distortion + pBuf[iPos] = static_cast(std::round(iOut * INT16_MAX)); } m_pMixbuf.clear(); } @@ -65,8 +68,12 @@ void RageSoundMixBuffer::read( float *pBuf ) void RageSoundMixBuffer::read_deinterlace( float **pBufs, int channels ) { for (unsigned i = 0; i < m_pMixbuf.size() / channels; ++i) + { for (int ch = 0; ch < channels; ++ch) + { pBufs[ch][i] = m_pMixbuf[channels * i + ch]; + } + } m_pMixbuf.clear(); } diff --git a/src/RageSoundMixBuffer.h b/src/RageSoundMixBuffer.h index d080cc92bb..a3f0752afa 100644 --- a/src/RageSoundMixBuffer.h +++ b/src/RageSoundMixBuffer.h @@ -15,13 +15,11 @@ public: void read(int16_t* pBuf); void read(float* pBuf); void read_deinterlace(float** pBufs, int channels); - inline int64_t size() const { return m_iBufUsed; } + inline size_t size() const { return m_pMixbuf.size(); } private: std::vector m_pMixbuf; - int64_t m_iBufSize; - int64_t m_iBufUsed; - int64_t m_iOffset; + unsigned m_iOffset; }; #endif // RAGESOUNDMIXBUFFER_H