2003-02-16 04:02:21 +00:00
|
|
|
#include "global.h"
|
2002-12-21 07:28:03 +00:00
|
|
|
#include "RageSoundDriver_WaveOut.h"
|
|
|
|
|
|
2005-05-11 04:43:54 +00:00
|
|
|
#if defined(_MSC_VER)
|
2002-12-21 07:37:34 +00:00
|
|
|
#pragma comment(lib, "winmm.lib")
|
2005-05-11 04:43:54 +00:00
|
|
|
#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"
|
2005-10-24 02:37:04 +00:00
|
|
|
#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;
|
2004-03-18 04:19:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2005-06-04 00:16:27 +00:00
|
|
|
int RageSound_WaveOut::MixerThread_start( void *p )
|
2002-12-21 07:28:03 +00:00
|
|
|
{
|
|
|
|
|
((RageSound_WaveOut *) p)->MixerThread();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RageSound_WaveOut::MixerThread()
|
|
|
|
|
{
|
2004-03-18 04:19: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 )
|
2004-03-18 04:19:03 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2003-01-07 03:55:43 +00:00
|
|
|
bool RageSound_WaveOut::GetData()
|
2002-12-21 07:28:03 +00:00
|
|
|
{
|
|
|
|
|
/* Look for a free buffer. */
|
|
|
|
|
int b;
|
2004-03-18 04:19:03 +00:00
|
|
|
for( b = 0; b < num_chunks; ++b )
|
2005-06-04 00:16:27 +00:00
|
|
|
if( m_aBuffers[b].dwFlags & WHDR_DONE )
|
|
|
|
|
break;
|
2004-03-18 04:19:03 +00:00
|
|
|
if( b == num_chunks )
|
|
|
|
|
return false;
|
2002-12-21 07:28:03 +00:00
|
|
|
|
2004-03-18 04:19: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;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-18 04:19:03 +00:00
|
|
|
void RageSound_WaveOut::SetupDecodingThread()
|
2002-12-21 07:28:03 +00:00
|
|
|
{
|
2004-03-18 04:19: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
|
|
|
}
|
|
|
|
|
|
2005-06-04 00:16:27 +00:00
|
|
|
int64_t RageSound_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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageSound_WaveOut::RageSound_WaveOut()
|
|
|
|
|
{
|
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-01-22 01:00:06 +00:00
|
|
|
RString RageSound_WaveOut::Init()
|
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;
|
2005-10-24 02:37:04 +00:00
|
|
|
m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
|
|
|
|
|
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
|
|
|
|
2005-10-24 02:37:04 +00:00
|
|
|
LOG->Info( "WaveOut software mixing at %i hz", m_iSampleRate );
|
|
|
|
|
|
2004-04-06 23:18:40 +00:00
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageSound_WaveOut::~RageSound_WaveOut()
|
|
|
|
|
{
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RageSound_WaveOut::GetPlayLatency() const
|
|
|
|
|
{
|
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. */
|
2005-10-24 02:37:04 +00:00
|
|
|
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
|
|
|
*/
|