Files
itgmania212121/stepmania/src/RageSoundMixBuffer.cpp
T

93 lines
2.7 KiB
C++
Raw Normal View History

2004-10-24 03:05:57 +00:00
#include "global.h"
#include "RageSoundMixBuffer.h"
#include "RageUtil.h"
RageSoundMixBuffer::RageSoundMixBuffer()
{
2004-10-24 03:14:24 +00:00
m_iBufSize = m_iBufUsed = 0;
m_pMixbuf = NULL;
m_iOffset = 0;
SetVolume( 1.0f );
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
}
void RageSoundMixBuffer::SetVolume( float f )
{
2004-10-24 03:14:24 +00:00
m_iVolumeFactor = int(256*f);
2004-10-24 03:05:57 +00:00
}
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-24 03:14:24 +00:00
void RageSoundMixBuffer::write( const int16_t *buf, unsigned size )
{
const unsigned realsize = size+m_iOffset;
if( m_iBufSize < realsize )
2004-10-24 03:05:57 +00:00
{
2004-10-24 03:14:24 +00:00
m_pMixbuf = (int32_t *) realloc( m_pMixbuf, sizeof(int32_t) * realsize );
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
{
2004-10-24 03:14:24 +00:00
memset( m_pMixbuf + m_iBufUsed, 0, (realsize - m_iBufUsed) * sizeof(int32_t) );
m_iBufUsed = realsize;
2004-10-24 03:05:57 +00:00
}
/* Scale volume and add. */
2004-10-24 03:14:24 +00:00
for( unsigned pos = 0; pos < size; ++pos )
m_pMixbuf[pos+m_iOffset] += buf[pos] * m_iVolumeFactor;
2004-10-24 03:05:57 +00:00
}
2004-10-24 03:14:24 +00:00
void RageSoundMixBuffer::read( int16_t *buf )
2004-10-24 03:05:57 +00:00
{
2004-10-24 03:14:24 +00:00
for( unsigned pos = 0; pos < m_iBufUsed; ++pos )
2004-10-24 03:05:57 +00:00
{
2004-10-24 03:14:24 +00:00
int32_t out = (m_pMixbuf[pos]) / 256;
2004-10-24 03:05:57 +00:00
buf[pos] = (int16_t) clamp( out, -32768, 32767 );
}
2004-10-24 03:14:24 +00:00
m_iBufUsed = 0;
2004-10-24 03:05:57 +00:00
}
void RageSoundMixBuffer::read( float *buf )
{
const int Minimum = -32768 * 256;
const int Maximum = 32767 * 256;
2004-10-24 03:14:24 +00:00
for( unsigned pos = 0; pos < m_iBufUsed; ++pos )
buf[pos] = SCALE( (float)m_pMixbuf[pos], Minimum, Maximum, -1.0f, 1.0f );
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
}
/*
* 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.
*/