Files
itgmania212121/stepmania/src/RageSoundMixBuffer.cpp
T

117 lines
3.2 KiB
C++
Raw Normal View History

2004-10-24 03:05:57 +00:00
#include "global.h"
#include "RageSoundMixBuffer.h"
#include "RageUtil.h"
2006-05-14 07:04:28 +00:00
#if defined(MACOSX)
#include "archutils/Darwin/VectorHelper.h"
2006-05-14 08:12:37 +00:00
#ifdef USE_VEC
2007-01-28 22:26:49 +00:00
static bool g_bVector = Vector::CheckForVector();
2006-05-14 07:04:28 +00:00
#endif
2006-05-14 08:12:37 +00:00
#endif
2006-05-14 07:04:28 +00:00
2004-10-24 03:05:57 +00:00
RageSoundMixBuffer::RageSoundMixBuffer()
{
2004-10-24 03:14:24 +00:00
m_iBufSize = m_iBufUsed = 0;
m_pMixbuf = NULL;
m_iOffset = 0;
2004-10-24 03:05:57 +00:00
}
RageSoundMixBuffer::~RageSoundMixBuffer()
{
2004-10-24 03:14:24 +00:00
free( m_pMixbuf );
2004-10-24 03:05:57 +00:00
}
2004-10-25 00:48:16 +00: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. */
2004-10-24 03:14:24 +00:00
void RageSoundMixBuffer::SetWriteOffset( int iOffset )
2004-10-24 03:05:57 +00:00
{
2004-10-24 03:14:24 +00:00
m_iOffset = iOffset;
}
2004-10-24 03:05:57 +00:00
2004-10-25 00:48:16 +00:00
void RageSoundMixBuffer::Extend( unsigned iSamples )
2004-10-24 03:14:24 +00:00
{
2004-10-25 00:48:16 +00:00
const unsigned realsize = iSamples+m_iOffset;
2004-10-24 03:14:24 +00:00
if( m_iBufSize < realsize )
2004-10-24 03:05:57 +00:00
{
2007-01-27 07:14:58 +00:00
m_pMixbuf = (float *) realloc( m_pMixbuf, sizeof(float) * realsize );
2004-10-24 03:14:24 +00:00
m_iBufSize = realsize;
2004-10-24 03:05:57 +00:00
}
2004-10-24 03:14:24 +00:00
if( m_iBufUsed < realsize )
2004-10-24 03:05:57 +00:00
{
2007-01-27 07:14:58 +00:00
memset( m_pMixbuf + m_iBufUsed, 0, (realsize - m_iBufUsed) * sizeof(float) );
2004-10-24 03:14:24 +00:00
m_iBufUsed = realsize;
2004-10-24 03:05:57 +00:00
}
2004-10-25 00:48:16 +00:00
}
2007-01-27 07:14:58 +00:00
void RageSoundMixBuffer::write( const float *pBuf, unsigned iSize, int iSourceStride, int iDestStride )
2004-10-25 00:48:16 +00:00
{
2006-12-10 04:06:33 +00:00
if( iSize == 0 )
return;
2006-12-09 22:43:44 +00:00
/* iSize = 3, iDestStride = 2 uses 4 frames. Don't allocate the stride of the
* last sample. */
Extend( iSize * iDestStride - (iDestStride-1) );
2004-10-24 03:05:57 +00:00
/* Scale volume and add. */
2007-01-27 07:14:58 +00:00
float *pDestBuf = m_pMixbuf+m_iOffset;
#ifdef USE_VEC
2006-12-09 22:43:44 +00:00
if( g_bVector && iSourceStride == 1 && iDestStride == 1 )
2006-05-14 07:04:28 +00:00
{
Vector::FastSoundWrite( pDestBuf, pBuf, iSize );
2006-05-14 07:04:28 +00:00
return;
}
#endif
2007-01-27 07:14:58 +00:00
2006-12-09 22:43:44 +00:00
while( iSize )
2004-10-25 00:48:16 +00:00
{
2007-01-27 07:14:58 +00:00
*pDestBuf += *pBuf;
2006-12-09 22:43:44 +00:00
pBuf += iSourceStride;
pDestBuf += iDestStride;
--iSize;
2004-10-25 00:48:16 +00:00
}
2004-10-24 03:05:57 +00:00
}
2006-12-08 03:59:54 +00:00
void RageSoundMixBuffer::read( int16_t *pBuf )
2004-10-24 03:05:57 +00:00
{
2006-12-08 03:59:54 +00:00
for( unsigned iPos = 0; iPos < m_iBufUsed; ++iPos )
2004-10-24 03:05:57 +00:00
{
2007-01-27 07:14:58 +00:00
float iOut = m_pMixbuf[iPos];
iOut = clamp( iOut, -1.0f, +1.0f );
pBuf[iPos] = lrintf(iOut * 32767);
2004-10-24 03:05:57 +00:00
}
2004-10-24 03:14:24 +00:00
m_iBufUsed = 0;
2004-10-24 03:05:57 +00:00
}
2006-12-08 03:59:54 +00:00
void RageSoundMixBuffer::read( float *pBuf )
2004-10-24 03:05:57 +00:00
{
2007-01-27 13:29:36 +00:00
memcpy( pBuf, m_pMixbuf, m_iBufUsed*sizeof(float) );
2004-10-24 03:14:24 +00:00
m_iBufUsed = 0;
2004-10-24 03:05:57 +00: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.
*/