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

379 lines
11 KiB
C++
Raw Normal View History

2003-02-16 04:02:21 +00:00
#include "../../global.h"
2002-12-13 08:33:25 +00:00
#include "DSoundHelpers.h"
2002-12-21 07:21:49 +00:00
#include "../../RageUtil.h"
#include "../../RageLog.h"
2002-12-13 08:33:25 +00:00
2003-02-20 21:57:28 +00:00
#define DIRECTSOUND_VERSION 0x0700
2002-12-21 07:21:49 +00:00
#include <dsound.h>
2003-01-08 00:44:17 +00:00
#include <math.h>
2002-12-21 07:37:34 +00:00
#pragma comment(lib, "dsound.lib")
#pragma comment(lib, "dxguid.lib")
2003-02-17 02:37:38 +00:00
BOOL CALLBACK DSound::EnumCallback(LPGUID lpGuid, LPCSTR lpcstrDescription, LPCSTR lpcstrModule, LPVOID lpContext)
{
LOG->Info("DirectSound Driver: %s (%s)", lpcstrDescription, lpcstrModule);
if(lpGuid)
LOG->Info(" ID: {%8.8x-%4.4x-%4.4x-%6.6x}", lpGuid->Data1, lpGuid->Data2, lpGuid->Data3, lpGuid->Data4);
return TRUE;
}
2002-12-21 07:21:49 +00:00
DSound::DSound()
2002-12-13 08:33:25 +00:00
{
2002-12-21 07:21:49 +00:00
HRESULT hr;
2002-12-13 08:33:25 +00:00
#ifndef _XBOX
// Initialize COM
if( FAILED( hr = CoInitialize( NULL ) ) )
RageException::ThrowNonfatal(hr_ssprintf(hr, "CoInitialize"));
#endif
// Create IDirectSound using the primary sound device
if( FAILED( hr = DirectSoundCreate( NULL, &ds, NULL ) ) )
RageException::ThrowNonfatal(hr_ssprintf(hr, "DirectSoundCreate"));
#ifndef _XBOX
2003-02-17 02:37:38 +00:00
DirectSoundEnumerate(EnumCallback, 0);
2002-12-21 07:21:49 +00:00
/* Try to set primary mixing privileges */
hr = ds->SetCooperativeLevel(GetDesktopWindow(), DSSCL_PRIORITY);
#endif
2002-12-13 08:33:25 +00:00
}
2002-12-21 07:21:49 +00:00
DSound::~DSound()
2002-12-13 08:33:25 +00:00
{
ds->Release();
#ifndef _XBOX
CoUninitialize();
#endif
2002-12-21 07:21:49 +00:00
}
bool DSound::IsEmulated() const
{
#ifndef _XBOX
2002-12-21 07:21:49 +00:00
/* Don't bother wasting time trying to create buffers if we're
* emulated. This also gives us better diagnostic information. */
DSCAPS Caps;
Caps.dwSize = sizeof(Caps);
HRESULT hr;
if(FAILED(hr = ds->GetCaps(&Caps)))
2002-12-21 07:21:49 +00:00
{
LOG->Warn(hr_ssprintf(hr, "ds->GetCaps failed"));
2002-12-21 07:21:49 +00:00
/* This is strange, so let's be conservative. */
return true;
}
return !!(Caps.dwFlags & DSCAPS_EMULDRIVER);
#else
return false;
#endif
2002-12-21 07:21:49 +00:00
}
DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware,
int channels_, int samplerate_, int samplebits_, int writeahead_)
2002-12-21 07:21:49 +00:00
{
channels = channels_;
samplerate = samplerate_;
samplebits = samplebits_;
2002-12-31 01:08:35 +00:00
writeahead = writeahead_;
2002-12-21 07:21:49 +00:00
buffer_locked = false;
last_cursor_pos = write_cursor = LastPosition = buffer_bytes_filled = 0;
2002-12-21 07:21:49 +00:00
2002-12-31 01:08:35 +00:00
/* The size of the actual DSound buffer. This can be large; we generally
* won't fill it completely. */
buffersize = 1024*64;
2002-12-21 07:21:49 +00:00
2002-12-13 08:33:25 +00:00
WAVEFORMATEX waveformat;
memset(&waveformat, 0, sizeof(waveformat));
waveformat.cbSize = sizeof(waveformat);
waveformat.wFormatTag = WAVE_FORMAT_PCM;
2003-04-25 04:10:18 +00:00
bool NeedCtrlFrequency = false;
if(samplerate == DYNAMIC_SAMPLERATE)
{
samplerate = 44100;
NeedCtrlFrequency = true;
}
2002-12-21 07:21:49 +00:00
int bytes = samplebits/8;
waveformat.wBitsPerSample = WORD(samplebits);
2002-12-13 08:33:25 +00:00
waveformat.nChannels = WORD(channels);
waveformat.nSamplesPerSec = DWORD(samplerate);
waveformat.nBlockAlign = WORD(bytes*channels);
waveformat.nAvgBytesPerSec = samplerate * bytes*channels;
2002-12-21 07:21:49 +00:00
/* Try to create the secondary buffer */
DSBUFFERDESC format;
memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);
#ifdef _XBOX
format.dwFlags = DSBCAPS_CTRLVOLUME;
#else
format.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLVOLUME;
#endif
#ifndef _XBOX
2003-01-08 01:36:06 +00:00
/* Don't use DSBCAPS_STATIC. It's meant for static buffers, and we
* only use streaming buffers. */
2002-12-21 07:21:49 +00:00
if(hardware == HW_HARDWARE)
format.dwFlags |= DSBCAPS_LOCHARDWARE;
2003-01-08 01:36:06 +00:00
else
2002-12-21 07:21:49 +00:00
format.dwFlags |= DSBCAPS_LOCSOFTWARE;
#endif
2003-04-25 04:10:18 +00:00
if(NeedCtrlFrequency)
format.dwFlags |= DSBCAPS_CTRLFREQUENCY;
2002-12-21 07:21:49 +00:00
format.dwBufferBytes = buffersize;
#ifndef _XBOX
2002-12-21 07:21:49 +00:00
format.dwReserved = 0;
#endif
2002-12-21 07:21:49 +00:00
format.lpwfxFormat = &waveformat;
/* Query IID_IDirectSoundBuffer instead of IID_IDirectSoundBuffer8. -Chris */
// IDirectSoundBuffer *sndbuf_buf;
// HRESULT hr = ds.GetDS()->CreateSoundBuffer(&format, &sndbuf_buf, NULL);
// if (FAILED(hr))
// RageException::ThrowNonfatal(hr_ssprintf(hr, "CreateSoundBuffer failed"));
//
// sndbuf_buf->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*) &buf);
// ASSERT(buf);
HRESULT hr = ds.GetDS()->CreateSoundBuffer(&format, &buf, NULL);
2002-12-21 07:21:49 +00:00
if (FAILED(hr))
2002-12-21 08:15:35 +00:00
RageException::ThrowNonfatal(hr_ssprintf(hr, "CreateSoundBuffer failed"));
2002-12-21 07:21:49 +00:00
#ifndef _XBOX
2003-01-08 01:36:06 +00:00
/* I'm not sure this should ever be needed, but ... */
DSBCAPS bcaps;
bcaps.dwSize=sizeof(bcaps);
hr = buf->GetCaps(&bcaps);
if(FAILED(hr))
RageException::Throw(hr_ssprintf(hr, "buf->GetCaps"));
if(int(bcaps.dwBufferBytes) != buffersize)
{
LOG->Warn("bcaps.dwBufferBytes (%i) != buffersize(%i); adjusting",
bcaps.dwBufferBytes, buffersize);
buffersize = bcaps.dwBufferBytes;
writeahead = min(writeahead, buffersize);
}
#endif
}
2003-04-25 04:10:18 +00:00
void DSoundBuf::SetSampleRate(int hz)
{
samplerate = hz;
HRESULT hr = buf->SetFrequency(hz);
2003-07-09 20:40:28 +00:00
if(FAILED(hr))
RageException::Throw(hr_ssprintf(hr, "buf->SetFrequency(%i)", hz));
2003-04-25 04:10:18 +00:00
}
void DSoundBuf::SetVolume(float vol)
{
ASSERT(vol >= 0);
ASSERT(vol <= 1);
2003-02-19 23:49:57 +00:00
if( vol == 0 )
vol = 0.001f; // fix log10f(0) == -INF
2003-01-08 00:44:17 +00:00
float vl2 = log10f(vol) / log10f(2); /* vol log 2 */
2003-01-27 23:09:40 +00:00
/* Volume is a multiplier; SetVolume wants attenuation in thousands of a
2003-01-08 00:44:17 +00:00
* decibel. */
if(vol != 1)
buf->SetVolume(max(int(1000 * vl2), DSBVOLUME_MIN));
2002-12-21 07:21:49 +00:00
}
/* Determine if "pos" is between "start" and "end", for a circular buffer. */
bool contained(int start, int end, int pos)
{
if(end >= start) /* start ... pos ... end */
return start <= pos && pos <= end;
else
return pos >= start || pos <= end;
}
2002-12-21 07:21:49 +00:00
DSoundBuf::~DSoundBuf()
{
buf->Release();
}
bool DSoundBuf::get_output_buf(char **buffer, unsigned *bufsiz, int *play_pos, int chunksize)
{
ASSERT(!buffer_locked);
DWORD cursorstart, junk, cursorend;
2002-12-21 07:21:49 +00:00
HRESULT result;
/* It's easiest to think of the cursor as a block, starting and ending at
* the two values returned by GetCurrentPosition, that we can't write to. */
result = buf->GetCurrentPosition(&cursorstart, &cursorend);
#ifndef _XBOX
2002-12-21 07:21:49 +00:00
if ( result == DSERR_BUFFERLOST ) {
buf->Restore();
result = buf->GetCurrentPosition(&cursorstart, &cursorend);
2002-12-21 07:21:49 +00:00
}
if ( result != DS_OK ) {
LOG->Warn(hr_ssprintf(result, "DirectSound::GetCurrentPosition failed"));
return false;
}
#endif
2002-12-21 07:21:49 +00:00
2002-12-31 01:08:35 +00:00
/* XXX We can figure out if we've underrun, and increase the write-ahead
* when it happens. Two problems:
* 1. It's ugly to wait until we actually underrun. (We could store the
* write-ahead, though.)
* 2. We don't want a random underrun (eg. virus scanner starts) to
* permanently increase our write-ahead. We want the smallest possible
* that will give us reliable audio in normal conditions. I'm not sure
* of a robust way to do this.
2002-12-31 01:08:35 +00:00
*
* Also, writeahead should be a static (all buffers write ahead the same
* amount); writeahead in the ctor should be a hint only (initial value),
* and the sound driver should query a sound to get the current writeahead
* in GetLatencySeconds().
*/
2002-12-31 01:08:35 +00:00
{
int first_byte_filled = write_cursor-buffer_bytes_filled;
if(first_byte_filled < 0) first_byte_filled += buffersize; /* unwrap */
2002-12-31 01:08:35 +00:00
int current_cursor = cursorstart;
if(current_cursor < first_byte_filled) current_cursor += buffersize;
2002-12-31 01:08:35 +00:00
/* The number of bytes that have been played since the last time we got here: */
int bytes_played = current_cursor - first_byte_filled;
buffer_bytes_filled -= bytes_played;
/* Data between the play cursor and the write cursor is committed to be
* played. If we don't actually have data there, we've underrun. */
if(!contained(first_byte_filled, write_cursor, cursorstart) ||
!contained(first_byte_filled, write_cursor, cursorend))
{
LOG->Trace("underrun: %i..%i filled but cursor at %i..%i (missed it by %i)",
2003-03-17 20:01:05 +00:00
first_byte_filled, write_cursor, cursorstart, cursorend,
(cursorend - write_cursor + buffersize) % buffersize);
// (cursorend - first_byte_filled + buffersize) % buffersize);
/* Pretend the space between the play and write cursor is filled
* with data, and continue filling from there. */
int no_write_zone_size = cursorend - cursorstart;
if(no_write_zone_size < 0) no_write_zone_size += buffersize; /* unwrap */
buffer_bytes_filled = no_write_zone_size;
write_cursor = cursorend;
/* Don't register another buffer underrun until the play cursor
* passes the new write cursor. */
}
2002-12-31 01:08:35 +00:00
}
/* If we already have enough bytes written ahead, stop. */
if(buffer_bytes_filled > writeahead)
2002-12-31 01:08:35 +00:00
return false;
int num_bytes_empty = buffersize-buffer_bytes_filled;
/* num_bytes_empty is the amount of free buffer space. If it's
2002-12-21 07:21:49 +00:00
* too small, come back later. */
if(num_bytes_empty < chunksize)
return false;
/* I don't want to deal with DSound's split-circular-buffer locking stuff, so clamp
2002-12-21 07:21:49 +00:00
* the writing space at the end of the physical buffer. */
num_bytes_empty = min(num_bytes_empty, buffersize - write_cursor);
/* Don't fill more than one chunk at a time. This reduces the maximum
* amount of time until we give data; that way, if we're short on time,
* we'll give some data soon instead of lots of data later. */
num_bytes_empty = min(num_bytes_empty, chunksize);
// LOG->Trace("gave %i at %i (%i, %i) %i filled", num_bytes_empty, write_cursor, cursor, write, buffer_bytes_filled);
2002-12-31 01:08:35 +00:00
2002-12-21 07:21:49 +00:00
/* Lock the audio buffer. */
result = buf->Lock(write_cursor, num_bytes_empty, (LPVOID *)buffer, (DWORD *) bufsiz, NULL, &junk, 0);
#ifndef _XBOX
2002-12-21 07:21:49 +00:00
if ( result == DSERR_BUFFERLOST ) {
buf->Restore();
result = buf->Lock(write_cursor, num_bytes_empty, (LPVOID *)buffer, (DWORD *) bufsiz, NULL, &junk, 0);
}
#endif
2002-12-21 07:21:49 +00:00
if ( result != DS_OK ) {
LOG->Warn(hr_ssprintf(result, "Couldn't lock the DirectSound buffer."));
return false;
}
write_cursor += num_bytes_empty;
if(write_cursor >= buffersize) write_cursor -= buffersize;
buffer_bytes_filled += num_bytes_empty;
2002-12-21 07:21:49 +00:00
*play_pos = last_cursor_pos;
/* Increment last_cursor_pos to point at where the data we're about to
* ask for will actually be played. */
2002-12-29 00:19:05 +00:00
last_cursor_pos += num_bytes_empty / samplesize();
2002-12-21 07:21:49 +00:00
buffer_locked = true;
return true;
}
void DSoundBuf::release_output_buf(char *buffer, unsigned bufsiz)
{
buf->Unlock(buffer, bufsiz, NULL, 0);
buffer_locked = false;
}
int DSoundBuf::GetPosition() const
{
DWORD cursor, junk;
buf->GetCurrentPosition(&cursor, &junk);
2002-12-29 00:19:05 +00:00
int cursor_frames = int(cursor) / samplesize();
int write_cursor_frames = write_cursor / samplesize();
2002-12-21 07:21:49 +00:00
2002-12-29 00:19:05 +00:00
int frames_behind = write_cursor_frames - cursor_frames;
2002-12-21 07:21:49 +00:00
if(frames_behind <= 0)
frames_behind += buffersize_frames(); /* unwrap */
int ret = last_cursor_pos - frames_behind;
/* Failsafe: never return a value smaller than we've already returned.
* This can happen once in a while in underrun conditions. */
ret = max(LastPosition, ret);
LastPosition = ret;
return ret;
}
void DSoundBuf::Play()
{
2002-12-29 00:19:05 +00:00
buf->Play(0, 0, DSBPLAY_LOOPING);
2002-12-21 07:21:49 +00:00
}
void DSoundBuf::Stop()
{
buf->Stop();
buf->SetCurrentPosition(0);
last_cursor_pos = LastPosition = write_cursor = buffer_bytes_filled = 0;
2002-12-21 07:21:49 +00:00
}
void DSoundBuf::Reset()
{
/* Nothing is playing. Reset the sample count; this is just to
* prevent eventual overflow. */
last_cursor_pos = LastPosition = 0;
2002-12-13 08:33:25 +00:00
}
/*
* Copyright (c) 2002 by the person(s) listed below. All rights reserved.
*
* Glenn Maynard
*/