2009-01-29 00:57:55 +00:00
|
|
|
// Copyright (C) 2003-2009 Dolphin Project.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
#include <dxerr.h>
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "DSoundStream.h"
|
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
extern bool log_ai;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
bool DSound::CreateBuffer()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
PCMWAVEFORMAT pcmwf;
|
|
|
|
DSBUFFERDESC dsbdesc;
|
|
|
|
|
|
|
|
memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));
|
|
|
|
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
|
|
|
|
|
|
|
|
pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
pcmwf.wf.nChannels = 2;
|
|
|
|
pcmwf.wf.nSamplesPerSec = sampleRate;
|
|
|
|
pcmwf.wf.nBlockAlign = 4;
|
|
|
|
pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
|
|
|
|
pcmwf.wBitsPerSample = 16;
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
// Fill out DSound buffer description.
|
2008-12-08 05:25:12 +00:00
|
|
|
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
|
2009-02-20 22:04:52 +00:00
|
|
|
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS;
|
2008-12-08 05:25:12 +00:00
|
|
|
dsbdesc.dwBufferBytes = bufferSize = BUFSIZE;
|
2009-01-29 00:57:55 +00:00
|
|
|
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
HRESULT res = ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL);
|
|
|
|
if (SUCCEEDED(res))
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
|
|
|
dsBuffer->SetCurrentPosition(0);
|
2009-01-29 00:57:55 +00:00
|
|
|
return true;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2009-02-20 22:04:52 +00:00
|
|
|
else
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
// Failed.
|
2009-01-29 00:57:55 +00:00
|
|
|
PanicAlert("Sound buffer creation failed: %s", DXGetErrorString(res));
|
2008-12-08 05:25:12 +00:00
|
|
|
dsBuffer = NULL;
|
2009-01-29 00:57:55 +00:00
|
|
|
return false;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
bool DSound::WriteDataToBuffer(DWORD dwOffset, // Our own write cursor.
|
2008-12-08 05:25:12 +00:00
|
|
|
char* soundData, // Start of our data.
|
|
|
|
DWORD dwSoundBytes) // Size of block to copy.
|
|
|
|
{
|
2009-02-14 01:07:20 +00:00
|
|
|
// I want to record the regular audio to, how do I do that?
|
2009-02-20 22:04:52 +00:00
|
|
|
// Well, it's gonna be a bit tricky. For future work :)
|
2009-02-14 01:07:20 +00:00
|
|
|
//std::string Data = ArrayToString((const u8*)soundData, dwSoundBytes);
|
|
|
|
//Console::Print("Data: %s\n\n", Data.c_str());
|
|
|
|
//if (log_ai) g_wave_writer.AddStereoSamples((const short*)soundData, dwSoundBytes);
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
void *ptr1, *ptr2;
|
2008-12-08 05:25:12 +00:00
|
|
|
DWORD numBytes1, numBytes2;
|
|
|
|
// Obtain memory address of write block. This will be in two parts if the block wraps around.
|
|
|
|
HRESULT hr = dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
|
|
|
|
|
|
|
|
// If the buffer was lost, restore and retry lock.
|
|
|
|
if (DSERR_BUFFERLOST == hr)
|
|
|
|
{
|
|
|
|
dsBuffer->Restore();
|
|
|
|
hr = dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
|
|
|
|
}
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
memcpy(ptr1, soundData, numBytes1);
|
|
|
|
if (ptr2 != 0)
|
|
|
|
memcpy(ptr2, soundData + numBytes1, numBytes2);
|
|
|
|
|
|
|
|
// Release the data back to DirectSound.
|
|
|
|
dsBuffer->Unlock(ptr1, numBytes1, ptr2, numBytes2);
|
2009-01-29 00:57:55 +00:00
|
|
|
return true;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
return false;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
// The audio thread.
|
|
|
|
DWORD WINAPI soundThread(void* args)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-02-20 22:04:52 +00:00
|
|
|
(reinterpret_cast<DSound *>(args))->SoundLoop();
|
|
|
|
return 0;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
void DSound::SoundLoop()
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
currentPos = 0;
|
|
|
|
lastPos = 0;
|
|
|
|
dsBuffer->Play(0, 0, DSBPLAY_LOOPING);
|
2009-02-20 22:04:52 +00:00
|
|
|
while (!threadData)
|
|
|
|
{
|
2008-12-08 05:25:12 +00:00
|
|
|
// No blocking inside the csection
|
2009-02-23 19:47:58 +00:00
|
|
|
soundCriticalSection.Enter();
|
2008-12-08 05:25:12 +00:00
|
|
|
dsBuffer->GetCurrentPosition((DWORD*)¤tPos, 0);
|
2009-02-09 19:50:06 +00:00
|
|
|
int numBytesToRender = FIX128(ModBufferSize(currentPos - lastPos));
|
2008-12-08 05:25:12 +00:00
|
|
|
if (numBytesToRender >= 256)
|
|
|
|
{
|
|
|
|
if (numBytesToRender > sizeof(realtimeBuffer))
|
2009-01-29 00:57:55 +00:00
|
|
|
PanicAlert("soundThread: too big render call");
|
2009-02-20 22:04:52 +00:00
|
|
|
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
|
2008-12-08 05:25:12 +00:00
|
|
|
WriteDataToBuffer(lastPos, (char*)realtimeBuffer, numBytesToRender);
|
|
|
|
currentPos = ModBufferSize(lastPos + numBytesToRender);
|
|
|
|
totalRenderedBytes += numBytesToRender;
|
|
|
|
lastPos = currentPos;
|
|
|
|
}
|
|
|
|
|
2009-02-23 19:47:58 +00:00
|
|
|
soundCriticalSection.Leave();
|
|
|
|
if (threadData)
|
|
|
|
break;
|
|
|
|
soundSyncEvent.Wait();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dsBuffer->Stop();
|
|
|
|
}
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
bool DSound::Start()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-02-23 19:47:58 +00:00
|
|
|
soundSyncEvent.Init();
|
2008-12-08 05:25:12 +00:00
|
|
|
if (FAILED(DirectSoundCreate8(0, &ds, 0)))
|
2009-02-20 22:04:52 +00:00
|
|
|
return false;
|
2009-02-23 19:47:58 +00:00
|
|
|
if (hWnd)
|
2009-01-29 00:57:55 +00:00
|
|
|
ds->SetCooperativeLevel((HWND)hWnd, DSSCL_NORMAL);
|
2008-12-08 05:25:12 +00:00
|
|
|
if (!CreateBuffer())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DWORD num1;
|
|
|
|
short* p1;
|
|
|
|
dsBuffer->Lock(0, bufferSize, (void* *)&p1, &num1, 0, 0, 0);
|
|
|
|
memset(p1, 0, num1);
|
|
|
|
dsBuffer->Unlock(p1, num1, 0, 0);
|
|
|
|
totalRenderedBytes = -bufferSize;
|
2009-01-29 00:57:55 +00:00
|
|
|
thread = new Common::Thread(soundThread, (void *)this);
|
2008-12-08 05:25:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
void DSound::Update()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-02-23 19:47:58 +00:00
|
|
|
soundSyncEvent.Set();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
void DSound::Stop()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-02-23 19:47:58 +00:00
|
|
|
soundCriticalSection.Enter();
|
2008-12-08 05:25:12 +00:00
|
|
|
threadData = 1;
|
|
|
|
// kick the thread if it's waiting
|
2009-02-23 19:47:58 +00:00
|
|
|
soundSyncEvent.Set();
|
|
|
|
soundCriticalSection.Leave();
|
2009-01-29 00:57:55 +00:00
|
|
|
delete thread;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
dsBuffer->Release();
|
|
|
|
ds->Release();
|
|
|
|
|
2009-02-23 19:47:58 +00:00
|
|
|
soundSyncEvent.Shutdown();
|
2009-01-29 00:57:55 +00:00
|
|
|
thread = NULL;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|