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

173 lines
4.4 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"
2002-12-21 07:37:34 +00:00
#pragma comment(lib, "winmm.lib")
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"
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-21 07:28:03 +00:00
const int samplerate = 44100;
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
2002-12-21 08:23:16 +00:00
static CString wo_ssprintf( MMRESULT err, const char *fmt, ...)
{
char buf[MAXERRORLENGTH];
waveOutGetErrorText(err, buf, MAXERRORLENGTH);
va_list va;
va_start(va, fmt);
CString s = vssprintf( fmt, va );
va_end(va);
return s += ssprintf( "(%s)", buf );
}
2002-12-21 07:28:03 +00:00
int RageSound_WaveOut::MixerThread_start(void *p)
{
((RageSound_WaveOut *) p)->MixerThread();
return 0;
}
void RageSound_WaveOut::MixerThread()
{
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
* assigns it; we might get here before that happens, though. */
while(!SOUNDMAN && !shutdown) Sleep(10);
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
while( !shutdown )
{
while( GetData() )
2002-12-21 07:28:03 +00:00
;
2002-12-21 09:23:45 +00:00
WaitForSingleObject(sound_event, 10);
2002-12-21 07:28:03 +00:00
}
2003-11-01 06:41:14 +00:00
waveOutReset(wo);
2002-12-21 07:28:03 +00:00
}
bool RageSound_WaveOut::GetData()
2002-12-21 07:28:03 +00:00
{
LockMutex L(SOUNDMAN->lock);
/* Look for a free buffer. */
int b;
for( b = 0; b < num_chunks; ++b )
2002-12-21 07:28:03 +00:00
if(buffers[b].dwFlags & WHDR_DONE) break;
if( b == num_chunks )
return false;
2002-12-21 07:28:03 +00:00
/* Call the callback. */
this->Mix( (int16_t *) buffers[b].lpData, chunksize_frames, last_cursor_pos, GetPosition( NULL ) );
2002-12-21 07:28:03 +00:00
MMRESULT ret = waveOutWrite(wo, &buffers[b], sizeof(buffers[b]));
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
2002-12-21 08:24:34 +00:00
/* Increment last_cursor_pos. */
2002-12-21 07:28:03 +00:00
last_cursor_pos += chunksize_frames;
return true;
}
void RageSound_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
}
2004-01-19 22:21:57 +00:00
int64_t RageSound_WaveOut::GetPosition( const RageSoundBase *snd ) const
2002-12-21 07:28:03 +00:00
{
LockMutex L(SOUNDMAN->lock);
MMTIME tm;
tm.wType = TIME_SAMPLES;
MMRESULT ret = waveOutGetPosition(wo, &tm, sizeof(tm));
2002-12-21 08:23:16 +00:00
if(ret != MMSYSERR_NOERROR)
2002-12-21 18:32:06 +00:00
RageException::Throw(wo_ssprintf(ret, "waveOutGetPosition failed"));
2002-12-21 07:28:03 +00:00
return tm.u.sample;
}
RageSound_WaveOut::RageSound_WaveOut()
{
shutdown = false;
last_cursor_pos = 0;
sound_event = CreateEvent(NULL, false, true, NULL);
WAVEFORMATEX fmt;
fmt.wFormatTag = WAVE_FORMAT_PCM;
fmt.nChannels = channels;
fmt.cbSize = 0;
fmt.nSamplesPerSec = samplerate;
fmt.wBitsPerSample = 16;
fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign;
2002-12-28 16:07:19 +00:00
MMRESULT ret = waveOutOpen(&wo, WAVE_MAPPER, &fmt,
2002-12-21 07:28:03 +00:00
(DWORD_PTR) sound_event, NULL, CALLBACK_EVENT);
if(ret != MMSYSERR_NOERROR)
2002-12-21 08:23:16 +00:00
RageException::ThrowNonfatal(wo_ssprintf(ret, "waveOutOpen failed"));
2002-12-21 07:28:03 +00:00
for(int b = 0; b < num_chunks; ++b)
{
memset(&buffers[b], 0, sizeof(buffers[b]));
buffers[b].dwBufferLength = chunksize;
buffers[b].lpData = new char[chunksize];
ret = waveOutPrepareHeader(wo, &buffers[b], sizeof(buffers[b]));
if(ret != MMSYSERR_NOERROR)
2002-12-21 08:23:16 +00:00
RageException::ThrowNonfatal(wo_ssprintf(ret, "waveOutPrepareHeader failed"));
2002-12-21 07:28:03 +00:00
buffers[b].dwFlags |= WHDR_DONE;
}
2002-12-28 16:07:19 +00:00
2003-08-05 01:32:51 +00:00
MixingThread.SetName("Mixer thread");
MixingThread.Create( MixerThread_start, this );
2002-12-21 07:28:03 +00:00
}
RageSound_WaveOut::~RageSound_WaveOut()
{
/* Signal the mixing thread to quit. */
shutdown = true;
2003-11-01 06:41:14 +00:00
SetEvent( sound_event );
2002-12-21 07:28:03 +00:00
LOG->Trace("Shutting down mixer thread ...");
2003-08-05 01:32:51 +00:00
MixingThread.Wait();
2002-12-21 07:28:03 +00:00
LOG->Trace("Mixer thread shut down.");
CloseHandle(sound_event);
waveOutClose(wo);
for(int b = 0; b < num_chunks; ++b)
2003-11-01 07:01:36 +00:00
{
waveOutUnprepareHeader( wo, &buffers[b], sizeof(buffers[b]) );
2002-12-21 07:28:03 +00:00
delete [] buffers[b].lpData;
2003-11-01 07:01:36 +00:00
}
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. */
return (buffersize_frames - chunksize_frames/2) * (1.0f / samplerate);
2002-12-21 07:28:03 +00:00
}
/*
2004-01-12 23:20:52 +00:00
* Copyright (c) 2002-2004 by the person(s) listed below. All rights reserved.
2002-12-21 07:28:03 +00:00
*
* Glenn Maynard
*/