Files
itgmania212121/src/RageSoundMixBuffer.cpp
T

104 lines
3.2 KiB
C++
Raw Normal View History

2011-03-17 01:47:30 -04:00
#include "global.h"
#include "RageSoundMixBuffer.h"
2023-04-19 14:22:59 +02:00
#include <cmath>
2023-04-20 12:34:12 +02:00
#include <cstdint>
2025-02-04 08:13:28 -08:00
#include <algorithm>
#include <vector>
2011-03-17 01:47:30 -04:00
2025-02-04 08:13:28 -08:00
RageSoundMixBuffer::RageSoundMixBuffer() : m_iOffset(0) {}
RageSoundMixBuffer::~RageSoundMixBuffer() {}
2011-03-17 01:47:30 -04:00
/* write() will start mixing iOffset samples into the buffer. Be careful; this is
* measured in samples, not frames, so if the data is stereo, multiply by two. */
void RageSoundMixBuffer::SetWriteOffset( int iOffset )
{
m_iOffset = static_cast<int>(iOffset);
2011-03-17 01:47:30 -04:00
}
2024-06-08 21:36:40 -07:00
void RageSoundMixBuffer::Extend(unsigned iSamples)
2011-03-17 01:47:30 -04:00
{
2025-02-04 08:13:28 -08:00
const unsigned realsize = iSamples + m_iOffset;
if (m_pMixbuf.size() < realsize)
2011-03-17 01:47:30 -04:00
{
2025-02-04 08:13:28 -08:00
m_pMixbuf.resize(realsize, 0.0f);
2011-03-17 01:47:30 -04:00
}
}
void RageSoundMixBuffer::write( const float *pBuf, unsigned iSize, int iSourceStride, int iDestStride )
{
if( iSize == 0 )
return;
2024-05-14 00:23:43 -07:00
// iSize = 3, iDestStride = 2 uses 4 frames. Don't allocate the stride of the last sample.
2011-03-17 01:47:30 -04:00
Extend( iSize * iDestStride - (iDestStride-1) );
// Load the audio at m_iOffset into the buffer.
2025-02-04 08:13:28 -08:00
float *pDestBuf = &m_pMixbuf[m_iOffset];
2011-03-17 01:47:30 -04:00
while( iSize )
{
*pDestBuf += *pBuf;
pBuf += iSourceStride;
pDestBuf += iDestStride;
--iSize;
}
}
2024-10-05 20:48:43 -07:00
void RageSoundMixBuffer::read( int16_t *pBuf )
2011-03-17 01:47:30 -04:00
{
2025-02-04 08:13:28 -08:00
for (unsigned iPos = 0; iPos < m_pMixbuf.size(); ++iPos)
2011-03-17 01:47:30 -04:00
{
// do the read
2011-03-17 01:47:30 -04:00
float iOut = m_pMixbuf[iPos];
// ensure volume is within expected levels to prevent clipping
2024-09-03 04:52:20 -07:00
iOut = std::clamp( iOut, -1.0f, +1.0f );
// round rather than truncate to minimize distortion
pBuf[iPos] = static_cast<int16_t>(std::round(iOut * INT16_MAX));
2011-03-17 01:47:30 -04:00
}
2025-02-04 08:13:28 -08:00
m_pMixbuf.clear();
2011-03-17 01:47:30 -04:00
}
void RageSoundMixBuffer::read( float *pBuf )
{
2025-02-04 08:13:28 -08:00
std::copy(m_pMixbuf.begin(), m_pMixbuf.end(), pBuf);
m_pMixbuf.clear();
2011-03-17 01:47:30 -04:00
}
void RageSoundMixBuffer::read_deinterlace( float **pBufs, int channels )
{
2025-02-04 08:13:28 -08:00
for (unsigned i = 0; i < m_pMixbuf.size() / channels; ++i)
{
2025-02-04 08:13:28 -08:00
for (int ch = 0; ch < channels; ++ch)
{
pBufs[ch][i] = m_pMixbuf[channels * i + ch];
}
}
2025-02-04 08:13:28 -08:00
m_pMixbuf.clear();
}
2011-03-17 01:47:30 -04:00
/*
* Copyright (c) 2002-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/