Files
itgmania212121/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp
T

212 lines
6.2 KiB
C++
Raw Normal View History

2003-02-16 04:02:21 +00:00
#include "global.h"
2002-12-21 07:28:03 +00:00
#include "RageSoundDriver_WaveOut.h"
#if defined(_MSC_VER)
2002-12-21 07:37:34 +00:00
#pragma comment(lib, "winmm.lib")
#endif
2002-12-21 07:28:03 +00:00
2003-01-24 22:28:23 +00:00
#include "RageTimer.h"
#include "RageLog.h"
#include "RageSound.h"
#include "RageUtil.h"
2003-07-26 23:12:38 +00:00
#include "RageSoundManager.h"
#include "PrefsManager.h"
2006-09-18 22:45:42 +00:00
#include "archutils/Win32/ErrorStrings.h"
2002-12-21 07:28:03 +00:00
2006-11-18 20:33:35 +00:00
REGISTER_SOUND_DRIVER_CLASS( WaveOut );
2002-12-21 07:28:03 +00:00
const int channels = 2;
2004-01-12 04:00:34 +00:00
const int bytes_per_frame = channels*2; /* 16-bit */
2002-12-28 16:07:19 +00:00
const int buffersize_frames = 1024*8; /* in frames */
2004-01-12 04:00:34 +00:00
const int buffersize = buffersize_frames * bytes_per_frame; /* in bytes */
2002-12-21 07:28:03 +00:00
const int num_chunks = 8;
const int chunksize_frames = buffersize_frames / num_chunks;
const int chunksize = buffersize / num_chunks; /* in bytes */
2002-12-21 07:28:03 +00:00
2006-01-22 01:00:06 +00:00
static RString wo_ssprintf( MMRESULT err, const char *szFmt, ...)
2002-12-21 08:23:16 +00:00
{
2005-06-04 00:16:27 +00:00
char szBuf[MAXERRORLENGTH];
waveOutGetErrorText( err, szBuf, MAXERRORLENGTH );
2002-12-21 08:23:16 +00:00
2006-11-19 18:39:24 +00:00
va_list va;
va_start( va, szFmt );
RString s = vssprintf( szFmt, va );
va_end( va );
2002-12-21 08:23:16 +00:00
2005-06-04 00:16:27 +00:00
return s += ssprintf( "(%s)", szBuf );
2002-12-21 08:23:16 +00:00
}
2006-12-07 02:16:51 +00:00
int RageSoundDriver_WaveOut::MixerThread_start( void *p )
2002-12-21 07:28:03 +00:00
{
2006-12-07 02:16:51 +00:00
((RageSoundDriver_WaveOut *) p)->MixerThread();
2002-12-21 07:28:03 +00:00
return 0;
}
2006-12-07 02:16:51 +00:00
void RageSoundDriver_WaveOut::MixerThread()
2002-12-21 07:28:03 +00:00
{
if( !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) )
LOG->Warn( werr_ssprintf(GetLastError(), "Failed to set sound thread priority") );
2002-12-21 08:32:25 +00:00
2005-06-04 00:16:27 +00:00
while( !m_bShutdown )
{
while( GetData() )
2002-12-21 07:28:03 +00:00
;
2005-06-04 00:16:27 +00:00
WaitForSingleObject( m_hSoundEvent, 10 );
2002-12-21 07:28:03 +00:00
}
2003-11-01 06:41:14 +00:00
2005-06-04 00:16:27 +00:00
waveOutReset( m_hWaveOut );
2002-12-21 07:28:03 +00:00
}
2006-12-07 02:16:51 +00:00
bool RageSoundDriver_WaveOut::GetData()
2002-12-21 07:28:03 +00:00
{
/* Look for a free buffer. */
int b;
for( b = 0; b < num_chunks; ++b )
2005-06-04 00:16:27 +00:00
if( m_aBuffers[b].dwFlags & WHDR_DONE )
break;
if( b == num_chunks )
return false;
2002-12-21 07:28:03 +00:00
/* Call the callback. */
2005-06-04 00:16:27 +00:00
this->Mix( (int16_t *) m_aBuffers[b].lpData, chunksize_frames, m_iLastCursorPos, GetPosition( NULL ) );
2002-12-21 07:28:03 +00:00
2005-06-04 00:16:27 +00:00
MMRESULT ret = waveOutWrite( m_hWaveOut, &m_aBuffers[b], sizeof(m_aBuffers[b]) );
2002-12-21 07:28:03 +00:00
if(ret != MMSYSERR_NOERROR)
2002-12-21 18:32:06 +00:00
RageException::Throw(wo_ssprintf(ret, "waveOutWrite failed"));
2002-12-21 07:28:03 +00:00
2005-06-04 00:16:27 +00:00
/* Increment m_iLastCursorPos. */
m_iLastCursorPos += chunksize_frames;
2002-12-21 07:28:03 +00:00
return true;
}
2006-12-07 02:16:51 +00:00
void RageSoundDriver_WaveOut::SetupDecodingThread()
2002-12-21 07:28:03 +00:00
{
if( !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) )
LOG->Warn( werr_ssprintf(GetLastError(), "Failed to set sound thread priority") );
2002-12-21 07:28:03 +00:00
}
2006-12-07 02:16:51 +00:00
int64_t RageSoundDriver_WaveOut::GetPosition( const RageSoundBase *pSound ) const
2002-12-21 07:28:03 +00:00
{
MMTIME tm;
tm.wType = TIME_SAMPLES;
2005-06-04 00:16:27 +00:00
MMRESULT ret = waveOutGetPosition( m_hWaveOut, &tm, sizeof(tm) );
if( ret != MMSYSERR_NOERROR )
RageException::Throw( wo_ssprintf(ret, "waveOutGetPosition failed") );
2002-12-21 07:28:03 +00:00
return tm.u.sample;
}
2006-12-07 02:16:51 +00:00
RageSoundDriver_WaveOut::RageSoundDriver_WaveOut()
2002-12-21 07:28:03 +00:00
{
2005-06-04 00:16:27 +00:00
m_bShutdown = false;
m_iLastCursorPos = 0;
2002-12-21 07:28:03 +00:00
2005-06-04 00:16:27 +00:00
m_hSoundEvent = CreateEvent( NULL, false, true, NULL );
2002-12-21 07:28:03 +00:00
2005-06-04 00:16:27 +00:00
m_hWaveOut = NULL;
2004-11-30 21:36:36 +00:00
}
2002-12-21 07:28:03 +00:00
2006-12-07 02:16:51 +00:00
RString RageSoundDriver_WaveOut::Init()
2004-11-30 21:36:36 +00:00
{
m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
if( m_iSampleRate == 0 )
m_iSampleRate = 44100;
2004-11-30 21:36:36 +00:00
WAVEFORMATEX fmt;
2002-12-21 07:28:03 +00:00
fmt.wFormatTag = WAVE_FORMAT_PCM;
2006-11-19 18:39:24 +00:00
fmt.nChannels = channels;
2002-12-21 07:28:03 +00:00
fmt.cbSize = 0;
fmt.nSamplesPerSec = m_iSampleRate;
2002-12-21 07:28:03 +00:00
fmt.wBitsPerSample = 16;
fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign;
2005-06-04 00:16:27 +00:00
MMRESULT ret = waveOutOpen( &m_hWaveOut, WAVE_MAPPER, &fmt, (DWORD_PTR) m_hSoundEvent, NULL, CALLBACK_EVENT );
2004-11-30 21:36:36 +00:00
if( ret != MMSYSERR_NOERROR )
return wo_ssprintf( ret, "waveOutOpen failed" );
2002-12-21 07:28:03 +00:00
2005-06-04 00:16:27 +00:00
ZERO( m_aBuffers );
2002-12-21 07:28:03 +00:00
for(int b = 0; b < num_chunks; ++b)
{
2005-06-04 00:16:27 +00:00
m_aBuffers[b].dwBufferLength = chunksize;
m_aBuffers[b].lpData = new char[chunksize];
ret = waveOutPrepareHeader( m_hWaveOut, &m_aBuffers[b], sizeof(m_aBuffers[b]) );
2004-11-30 21:36:36 +00:00
if( ret != MMSYSERR_NOERROR )
return wo_ssprintf( ret, "waveOutPrepareHeader failed" );
2005-06-04 00:16:27 +00:00
m_aBuffers[b].dwFlags |= WHDR_DONE;
2002-12-21 07:28:03 +00:00
}
2002-12-28 16:07:19 +00:00
LOG->Info( "WaveOut software mixing at %i hz", m_iSampleRate );
/* We have a very large writeahead; make sure we have a large enough decode
* buffer to recover cleanly from underruns. */
SetDecodeBufferSize( buffersize_frames * 3/2 );
2004-04-02 21:32:25 +00:00
StartDecodeThread();
2005-06-04 00:16:27 +00:00
MixingThread.SetName( "Mixer thread" );
2003-08-05 01:32:51 +00:00
MixingThread.Create( MixerThread_start, this );
2004-11-30 21:36:36 +00:00
2006-01-22 01:00:06 +00:00
return RString();
2002-12-21 07:28:03 +00:00
}
2006-12-07 02:16:51 +00:00
RageSoundDriver_WaveOut::~RageSoundDriver_WaveOut()
2002-12-21 07:28:03 +00:00
{
/* Signal the mixing thread to quit. */
2004-11-30 21:36:36 +00:00
if( MixingThread.IsCreated() )
{
2005-06-04 00:16:27 +00:00
m_bShutdown = true;
SetEvent( m_hSoundEvent );
LOG->Trace( "Shutting down mixer thread ..." );
2004-11-30 21:36:36 +00:00
MixingThread.Wait();
2005-06-04 00:16:27 +00:00
LOG->Trace( "Mixer thread shut down." );
2004-11-30 21:36:36 +00:00
}
2005-06-04 00:16:27 +00:00
if( m_hWaveOut != NULL )
2004-11-30 21:36:36 +00:00
{
2005-06-04 00:16:27 +00:00
for( int b = 0; b < num_chunks && m_aBuffers[b].lpData != NULL; ++b )
2004-11-30 21:36:36 +00:00
{
2005-06-04 00:16:27 +00:00
waveOutUnprepareHeader( m_hWaveOut, &m_aBuffers[b], sizeof(m_aBuffers[b]) );
delete [] m_aBuffers[b].lpData;
2004-11-30 21:36:36 +00:00
}
2005-06-04 00:16:27 +00:00
waveOutClose( m_hWaveOut );
2004-11-30 21:36:36 +00:00
}
2002-12-21 07:28:03 +00:00
2005-06-04 00:16:27 +00:00
CloseHandle( m_hSoundEvent );
2002-12-21 07:28:03 +00:00
}
2006-12-07 02:16:51 +00:00
float RageSoundDriver_WaveOut::GetPlayLatency() const
2002-12-21 07:28:03 +00:00
{
2004-01-12 04:00:34 +00:00
/* If we have a 1000-byte buffer, and we fill 100 bytes at a time, we
* almost always have between 900 and 1000 bytes filled; on average, 950. */
return (buffersize_frames - chunksize_frames/2) * (1.0f / m_iSampleRate);
2002-12-21 07:28:03 +00:00
}
/*
2004-05-15 20:28:17 +00:00
* (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.
2002-12-21 07:28:03 +00:00
*/