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
This commit is contained in:
@@ -4,17 +4,28 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
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<float*>(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<std::uint64_t>(iSamples) + static_cast<std::uint64_t>(m_iOffset);
|
||||
if( m_iBufSize < realsize )
|
||||
{
|
||||
m_pMixbuf = (float *) realloc( m_pMixbuf, sizeof(float) * realsize );
|
||||
m_pMixbuf = static_cast<float*>(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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user