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/
|
|
|
|
|
|
|
|
|
|
#include "DSoundStream.h"
|
2009-01-29 00:57:55 +00:00
|
|
|
|
#include "../main.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
|
#include <dxerr.h>
|
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;
|
|
|
|
|
|
|
|
|
|
//buffer description
|
|
|
|
|
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
|
|
|
|
|
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS; //VIKTIGT //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
|
|
|
|
|
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-01-29 00:57:55 +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.
|
|
|
|
|
{
|
|
|
|
|
void* ptr1, * ptr2;
|
|
|
|
|
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-01-29 00:57:55 +00:00
|
|
|
|
((DSound *)args)->SoundLoop();
|
|
|
|
|
|
|
|
|
|
return 0; //huzzah! :D
|
2008-12-08 05:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
|
void DSound::SoundLoop() {
|
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
|
currentPos = 0;
|
|
|
|
|
lastPos = 0;
|
|
|
|
|
|
|
|
|
|
// Prefill buffer?
|
|
|
|
|
//writeDataToBuffer(0,realtimeBuffer,bufferSize);
|
|
|
|
|
// dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, (void **)&p2, &num2, 0);
|
|
|
|
|
dsBuffer->Play(0, 0, DSBPLAY_LOOPING);
|
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
|
while (!threadData) {
|
2008-12-08 05:25:12 +00:00
|
|
|
|
// No blocking inside the csection
|
2009-01-29 00:57:55 +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");
|
|
|
|
|
(*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-01-29 00:57:55 +00:00
|
|
|
|
soundCriticalSection->Leave();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
//no security attributes, automatic resetting, init state nonset, untitled
|
2009-01-29 00:57:55 +00:00
|
|
|
|
soundSyncEvent = new Common::Event();
|
|
|
|
|
soundSyncEvent->Init();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
|
|
//vi initierar den...........
|
2009-01-29 00:57:55 +00:00
|
|
|
|
soundCriticalSection = new Common::CriticalSection();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
|
|
//vi vill ha access till DSOUND s<>...
|
|
|
|
|
if (FAILED(DirectSoundCreate8(0, &ds, 0)))
|
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
|
|
|
|
if(hWnd)
|
|
|
|
|
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-01-29 00:57:55 +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-01-29 00:57:55 +00:00
|
|
|
|
soundCriticalSection->Enter();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
threadData = 1;
|
|
|
|
|
// kick the thread if it's waiting
|
2009-01-29 00:57:55 +00:00
|
|
|
|
soundSyncEvent->Set();
|
|
|
|
|
soundCriticalSection->Leave();
|
|
|
|
|
delete soundCriticalSection;
|
|
|
|
|
delete thread;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
|
|
dsBuffer->Release();
|
|
|
|
|
ds->Release();
|
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
|
soundSyncEvent->Shutdown();
|
|
|
|
|
delete soundSyncEvent;
|
|
|
|
|
soundSyncEvent = NULL;
|
|
|
|
|
thread = NULL;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-29 00:57:55 +00:00
|
|
|
|
/* Unused, is it needed?
|
|
|
|
|
int DSound::GetCurSample()
|
2008-12-08 05:25:12 +00:00
|
|
|
|
{
|
2009-01-29 00:57:55 +00:00
|
|
|
|
soundCriticalSection->Enter();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
int playCursor;
|
|
|
|
|
dsBuffer->GetCurrentPosition((DWORD*)&playCursor, 0);
|
|
|
|
|
playCursor = ModBufferSize(playCursor - lastPos) + totalRenderedBytes;
|
2009-01-29 00:57:55 +00:00
|
|
|
|
soundCriticalSection->Leave();
|
|
|
|
|
return playCursor;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
}
|
2009-01-29 00:57:55 +00:00
|
|
|
|
*/
|