Audio system update - HLE plugin submitted, homebrew has sound, and also Mario movies!! (this was very unexpected). This also acts as a frame limiter. Might provide an option to turn it off in the future.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@227 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
hrydgard
2008-08-16 21:58:07 +00:00
parent dcd5ffeb7a
commit aa3fee8c60
57 changed files with 4438 additions and 104 deletions

View File

@ -0,0 +1,127 @@
#include <ao/ao.h>
#include <pthread.h>
#include "AOSoundStream.h"
namespace AOSound
{
pthread_t thread;
StreamCallback callback;
char *buffer;
int buf_size;
ao_device *device;
ao_sample_format format;
int default_driver;
int bufferSize;
int totalRenderedBytes;
int sampleRate;
volatile int threadData;
int currentPos;
int lastPos;
short realtimeBuffer[1024 * 1024];
int AOSound_GetSampleRate()
{
return sampleRate;
}
bool WriteDataToBuffer(int dwOffset,char* soundData, int dwSoundBytes)
{
//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);
return(true);
}
return(false);*/
if(soundData[0] != 0)
ao_play(device, soundData, dwSoundBytes);
return true;
}
void* soundThread(void*)
{
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);
while (!threadData)
{
// No blocking inside the csection
//dsBuffer->GetCurrentPosition((DWORD*)&currentPos, 0);
int numBytesToRender = 256;
if (numBytesToRender >= 256)
{
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, 44100, 2);
WriteDataToBuffer(0, (char*)realtimeBuffer, numBytesToRender);
//currentPos = ModBufferSize(lastPos + numBytesToRender);
//totalRenderedBytes += numBytesToRender;
//lastPos = currentPos;
}
//WaitForSingleObject(soundSyncEvent, MAXWAIT);
}
//dsBuffer->Stop();
return(0); //hurra!
}
bool AOSound_StartSound(int _sampleRate, StreamCallback _callback)
{
callback = _callback;
threadData = 0;
sampleRate = _sampleRate;
//no security attributes, automatic resetting, init state nonset, untitled
//soundSyncEvent = CreateEvent(0, false, false, 0);
ao_initialize();
default_driver = ao_default_driver_id();
format.bits = 16;
format.channels = 2;
format.rate = sampleRate;
format.byte_format = AO_FMT_LITTLE;
//vi vill ha access till DSOUND så...
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
fprintf(stderr, "Error opening device.\n");
return 1;
}
buf_size = format.bits/8 * format.channels * format.rate;
buffer = (char*)calloc(buf_size, sizeof(char));
pthread_create(&thread, NULL, soundThread, (void *)NULL);
return(true);
}
void AOSound_StopSound()
{
ao_close(device);
ao_shutdown();
}
}

View File

@ -0,0 +1,35 @@
// Copyright (C) 2003-2008 Dolphin Project.
// 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/
#ifndef __AOSOUNDSTREAM_H__
#define __AOSOUNDSTREAM_H__
namespace AOSound
{
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
bool AOSound_StartSound(int sampleRate, StreamCallback _callback);
void AOSound_UpdateSound();
void AOSound_StopSound();
float AOSound_GetTimer();
int AOSound_GetCurSample();
int AOSound_GetSampleRate();
}
#endif //__AOSOUNDSTREAM_H__

View File

@ -0,0 +1,254 @@
// Copyright (C) 2003-2008 Dolphin Project.
// 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 "stdafx.h"
#include <mmsystem.h>
#include <dsound.h>
#include "dsoundstream.h"
namespace DSound
{
#define BUFSIZE 32768
#define MAXWAIT 70 //ms
CRITICAL_SECTION soundCriticalSection;
HANDLE soundSyncEvent;
HANDLE hThread;
StreamCallback callback;
//lite mojs
IDirectSound8* ds;
IDirectSoundBuffer* dsBuffer;
//tja.. beh<65>vs
int bufferSize; //i bytes
int totalRenderedBytes;
int sampleRate;
//med den h<>r synkar vi st<73>ngning..
//0=vi spelar ov<6F>sen, 1=st<73>ng tr<74>den NU!
volatile int threadData;
inline int FIX128(int x)
{
return(x & (~127));
}
int DSound_GetSampleRate()
{
return(sampleRate);
}
bool CreateBuffer()
{
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;
dsbdesc.lpwfxFormat = (WAVEFORMATEX*)&pcmwf;
if (SUCCEEDED(ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL)))
{
dsBuffer->SetCurrentPosition(0);
return(true);
}
else
{
// Failed.
dsBuffer = NULL;
return(false);
}
}
bool WriteDataToBuffer(DWORD dwOffset, // Our own write cursor.
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);
return(true);
}
return(false);
}
inline int ModBufferSize(int x)
{
return((x + bufferSize) % bufferSize);
}
int currentPos;
int lastPos;
short realtimeBuffer[1024 * 1024];
//Sj<53>lva tr<74>den
DWORD WINAPI soundThread(void*)
{
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);
while (!threadData)
{
// No blocking inside the csection
EnterCriticalSection(&soundCriticalSection);
dsBuffer->GetCurrentPosition((DWORD*)&currentPos, 0);
int numBytesToRender = FIX128(ModBufferSize(currentPos - lastPos));
if (numBytesToRender >= 256)
{
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
WriteDataToBuffer(lastPos, (char*)realtimeBuffer, numBytesToRender);
currentPos = ModBufferSize(lastPos + numBytesToRender);
totalRenderedBytes += numBytesToRender;
lastPos = currentPos;
}
LeaveCriticalSection(&soundCriticalSection);
WaitForSingleObject(soundSyncEvent, MAXWAIT);
}
dsBuffer->Stop();
return(0); //hurra!
}
bool DSound_StartSound(HWND window, int _sampleRate, StreamCallback _callback)
{
callback = _callback;
threadData = 0;
sampleRate = _sampleRate;
//no security attributes, automatic resetting, init state nonset, untitled
soundSyncEvent = CreateEvent(0, false, false, 0);
//vi initierar den...........
InitializeCriticalSection(&soundCriticalSection);
//vi vill ha access till DSOUND s<>...
if (FAILED(DirectSoundCreate8(0, &ds, 0)))
{
return(false);
}
ds->SetCooperativeLevel(window, DSSCL_NORMAL);
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;
DWORD h;
hThread = CreateThread(0, 0, soundThread, 0, 0, &h);
SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
return(true);
}
void DSound_UpdateSound()
{
SetEvent(soundSyncEvent);
}
void DSound_StopSound()
{
EnterCriticalSection(&soundCriticalSection);
threadData = 1;
//kick the thread if it's waiting
SetEvent(soundSyncEvent);
LeaveCriticalSection(&soundCriticalSection);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
dsBuffer->Release();
ds->Release();
CloseHandle(soundSyncEvent);
}
int DSound_GetCurSample()
{
EnterCriticalSection(&soundCriticalSection);
int playCursor;
dsBuffer->GetCurrentPosition((DWORD*)&playCursor, 0);
playCursor = ModBufferSize(playCursor - lastPos) + totalRenderedBytes;
LeaveCriticalSection(&soundCriticalSection);
return(playCursor);
}
float DSound_GetTimer()
{
return((float)DSound_GetCurSample() * (1.0f / (4.0f * sampleRate)));
}
}

View File

@ -0,0 +1,35 @@
// Copyright (C) 2003-2008 Dolphin Project.
// 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/
#ifndef __SOUNDSTREAM_H__
#define __SOUNDSTREAM_H__
namespace DSound
{
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
bool DSound_StartSound(HWND window, int sampleRate, StreamCallback _callback);
void DSound_UpdateSound();
void DSound_StopSound();
float DSound_GetTimer();
int DSound_GetCurSample();
int DSound_GetSampleRate();
}
#endif //__SOUNDSTREAM_H__

View File

@ -0,0 +1,99 @@
// Copyright (C) 2003-2008 Dolphin Project.
// 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/
// This queue solution is temporary. I'll implement something more efficient later.
#include <queue>
#include "../Config.h"
#include "../Globals.h"
#include "../DSPHandler.h"
#include "Thread.h"
#include "Mixer.h"
#ifdef _WIN32
#include "../PCHW/DSoundStream.h"
#endif
namespace {
Common::CriticalSection push_sync;
// On real hardware, this fifo is much, much smaller. But timing is also tighter than under Windows, so...
int queue_maxlength = 1024 * 8;
std::queue<s16> sample_queue;
} // namespace
volatile bool mixer_HLEready = false;
volatile int queue_size = 0;
void Mixer(short *buffer, int numSamples, int bits, int rate, int channels)
{
// silence
memset(buffer, 0, numSamples * 2 * sizeof(short));
// first get th DTK Music
if (g_Config.m_EnableDTKMusic)
{
g_dspInitialize.pGetAudioStreaming(buffer, numSamples);
}
//if this was called directly from the HLE, and not by timeout
if (g_Config.m_EnableHLEAudio && (mixer_HLEready || g_Config.m_AntiGap))
{
IUCode* pUCode = CDSPHandler::GetInstance().GetUCode();
if (pUCode != NULL)
pUCode->MixAdd(buffer, numSamples);
}
push_sync.Enter();
int count = 0;
while (sample_queue.size() && count < numSamples * 2) {
int x = buffer[count];
short sample = sample_queue.front();
x += sample_queue.front();
if (x > 32767) x = 32767;
if (x < -32767) x = -32767;
buffer[count] = x;
count++;
sample_queue.pop();
queue_size--;
}
push_sync.Leave();
}
void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate) {
// static FILE *f;
// if (!f)
// f = fopen("d:\\hello.raw", "wb");
// fwrite(buffer, num_stereo_samples * 4, 1, f);
static int fraction = 0;
while (queue_size > queue_maxlength / 2) {
#ifdef _WIN32
DSound::DSound_UpdateSound();
#endif
Sleep(0);
}
push_sync.Enter();
for (int i = 0; i < num_stereo_samples * 2; i++) {
while (fraction < 65536) {
sample_queue.push(buffer[i]);
queue_size++;
fraction += (65536ULL * sample_rate) / 48000;
}
fraction &= 0xFFFF;
}
push_sync.Leave();
}

View File

@ -0,0 +1,30 @@
// Copyright (C) 2003-2008 Dolphin Project.
// 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/
#ifndef _MIXER_H
#define _MIXER_H
extern volatile bool mixer_HLEready;
// Called from audio threads
void Mixer(short* buffer, int numSamples, int bits, int rate, int channels);
// Called from main thread
void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate);
#endif