mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Attempt to move mixer to audio common, it's a bit more complicated than I expected
so please check I didn't break anything in hle git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2756 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
@ -433,6 +433,14 @@
|
||||
RelativePath=".\Src\SoundStream.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\Mixer.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\Mixer.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Src\WaveFile.cpp"
|
||||
>
|
||||
|
@ -18,16 +18,18 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "AOSoundStream.h"
|
||||
#include "Mixer.h"
|
||||
|
||||
#if defined(HAVE_AO) && HAVE_AO
|
||||
|
||||
void AOSound::SoundLoop()
|
||||
{
|
||||
uint_32 numBytesToRender = 256;
|
||||
ao_initialize();
|
||||
default_driver = ao_default_driver_id();
|
||||
format.bits = 16;
|
||||
format.channels = 2;
|
||||
format.rate = sampleRate;
|
||||
format.rate = m_mixer->GetSampleRate();
|
||||
format.byte_format = AO_FMT_LITTLE;
|
||||
|
||||
device = ao_open_live(default_driver, &format, NULL /* no options */);
|
||||
@ -43,14 +45,21 @@ void AOSound::SoundLoop()
|
||||
|
||||
while (!threadData)
|
||||
{
|
||||
soundCriticalSection->Enter();
|
||||
soundCriticalSection.Enter();
|
||||
|
||||
uint_32 numBytesToRender = 256;
|
||||
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
|
||||
m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
|
||||
ao_play(device, (char*)realtimeBuffer, numBytesToRender);
|
||||
soundCriticalSection->Leave();
|
||||
soundSyncEvent->Wait();
|
||||
|
||||
soundCriticalSection.Leave();
|
||||
|
||||
if (! threadData)
|
||||
soundSyncEvent.Wait();
|
||||
}
|
||||
|
||||
ao_close(device);
|
||||
device = NULL;
|
||||
ao_shutdown();
|
||||
|
||||
}
|
||||
|
||||
void *soundThread(void *args)
|
||||
@ -63,34 +72,28 @@ bool AOSound::Start()
|
||||
{
|
||||
memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
|
||||
|
||||
soundSyncEvent = new Common::Event();
|
||||
soundSyncEvent->Init();
|
||||
|
||||
soundCriticalSection = new Common::CriticalSection(1);
|
||||
|
||||
soundSyncEvent.Init();
|
||||
|
||||
thread = new Common::Thread(soundThread, (void *)this);
|
||||
return true;
|
||||
}
|
||||
|
||||
void AOSound::Update()
|
||||
{
|
||||
soundSyncEvent->Set();
|
||||
soundSyncEvent.Set();
|
||||
}
|
||||
|
||||
void AOSound::Stop()
|
||||
{
|
||||
soundCriticalSection->Enter();
|
||||
soundCriticalSection.Enter();
|
||||
threadData = 1;
|
||||
soundSyncEvent->Set();
|
||||
soundCriticalSection->Leave();
|
||||
soundSyncEvent->Shutdown();
|
||||
delete soundCriticalSection;
|
||||
delete thread;
|
||||
delete soundSyncEvent;
|
||||
soundSyncEvent.Set();
|
||||
soundCriticalSection.Leave();
|
||||
|
||||
delete thread;
|
||||
thread = NULL;
|
||||
soundSyncEvent.Shutdown();
|
||||
|
||||
ao_close(device);
|
||||
device = NULL;
|
||||
ao_shutdown();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -30,8 +30,8 @@ class AOSound : public SoundStream
|
||||
{
|
||||
#if defined(HAVE_AO) && HAVE_AO
|
||||
Common::Thread *thread;
|
||||
Common::CriticalSection *soundCriticalSection;
|
||||
Common::Event *soundSyncEvent;
|
||||
Common::CriticalSection soundCriticalSection;
|
||||
Common::Event soundSyncEvent;
|
||||
|
||||
int buf_size;
|
||||
|
||||
@ -42,9 +42,8 @@ class AOSound : public SoundStream
|
||||
short realtimeBuffer[1024 * 1024];
|
||||
|
||||
public:
|
||||
AOSound(int _sampleRate, StreamCallback _callback) :
|
||||
SoundStream(_sampleRate, _callback) {}
|
||||
|
||||
AOSound(CMixer *mixer) : SoundStream(mixer) {}
|
||||
|
||||
virtual ~AOSound() {}
|
||||
|
||||
virtual bool Start();
|
||||
@ -63,14 +62,10 @@ public:
|
||||
|
||||
virtual void Update();
|
||||
|
||||
virtual int GetSampleRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
#else
|
||||
public:
|
||||
AOSound(int _sampleRate, StreamCallback _callback) :
|
||||
SoundStream(_sampleRate, _callback) {}
|
||||
AOSound(CMixer *mixer) :
|
||||
SoundStream(mixer) {}
|
||||
#endif
|
||||
};
|
||||
|
||||
|
51
Source/Core/AudioCommon/Src/AudioCommon.cpp
Normal file
51
Source/Core/AudioCommon/Src/AudioCommon.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "AudioCommon.h"
|
||||
#include "Mixer.h"
|
||||
#include "AOSoundStream.h"
|
||||
#include "DSoundStream.h"
|
||||
#include "NullSoundStream.h"
|
||||
|
||||
|
||||
namespace AudioCommon {
|
||||
|
||||
SoundStream *InitSoundStream(std::string backend, CMixer *mixer) {
|
||||
|
||||
if (!mixer) {
|
||||
mixer = new CMixer();
|
||||
}
|
||||
|
||||
if (backend == "DSound") {
|
||||
if (DSound::isValid())
|
||||
soundStream = new DSound(mixer, g_dspInitialize.hWnd);
|
||||
}
|
||||
else if (backend == "AOSound") {
|
||||
if (AOSound::isValid())
|
||||
soundStream = new AOSound(mixer);
|
||||
}
|
||||
else if (backend == "NullSound") {
|
||||
soundStream = new NullSound(mixer);
|
||||
}
|
||||
else {
|
||||
PanicAlert("Cannot recognize backend %s", backend.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (soundStream) {
|
||||
if (!soundStream->Start()) {
|
||||
PanicAlert("Could not initialize backend %s, falling back to NULL",
|
||||
backend.c_str());
|
||||
delete soundStream;
|
||||
soundStream = new NullSound(mixer);
|
||||
soundStream->Start();
|
||||
}
|
||||
}
|
||||
else {
|
||||
PanicAlert("Sound backend %s is not valid, falling back to NULL",
|
||||
backend.c_str());
|
||||
delete soundStream;
|
||||
soundStream = new NullSound(mixer);
|
||||
soundStream->Start();
|
||||
}
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
} // Namespace
|
18
Source/Core/AudioCommon/Src/AudioCommon.h
Normal file
18
Source/Core/AudioCommon/Src/AudioCommon.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef _AUDIO_COMMON_H
|
||||
#define _AUDIO_COMMON_H
|
||||
|
||||
#include "Common.h"
|
||||
#include "pluginspecs_dsp.h"
|
||||
#include "SoundStream.h"
|
||||
|
||||
class CMixer;
|
||||
|
||||
extern DSPInitialize g_dspInitialize;
|
||||
extern SoundStream *soundStream;
|
||||
|
||||
namespace AudioCommon {
|
||||
|
||||
SoundStream *InitSoundStream(std::string backend, CMixer *mixer = NULL);
|
||||
} // Namespace
|
||||
|
||||
#endif // AUDIO_COMMON
|
@ -19,7 +19,7 @@
|
||||
#include <dxerr.h>
|
||||
#include "DSoundStream.h"
|
||||
|
||||
extern bool log_ai;
|
||||
//extern bool log_ai;
|
||||
|
||||
bool DSound::CreateBuffer()
|
||||
{
|
||||
@ -114,7 +114,7 @@ void DSound::SoundLoop()
|
||||
{
|
||||
if (numBytesToRender > sizeof(realtimeBuffer))
|
||||
PanicAlert("soundThread: too big render call");
|
||||
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
|
||||
m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
|
||||
WriteDataToBuffer(lastPos, (char*)realtimeBuffer, numBytesToRender);
|
||||
currentPos = ModBufferSize(lastPos + numBytesToRender);
|
||||
totalRenderedBytes += numBytesToRender;
|
||||
|
@ -64,12 +64,10 @@ class DSound : public SoundStream
|
||||
DWORD dwSoundBytes);
|
||||
|
||||
public:
|
||||
DSound(int _sampleRate, StreamCallback _callback) :
|
||||
SoundStream(_sampleRate, _callback) {}
|
||||
|
||||
DSound(int _sampleRate, StreamCallback _callback, void *_hWnd) :
|
||||
SoundStream(_sampleRate, _callback), hWnd(_hWnd) {}
|
||||
|
||||
DSound(CMixer *mixer, void *hWnd = NULL) : SoundStream(mixer) {}
|
||||
|
||||
DSound(CMixer *mixer) : SoundStream(mixer) {}
|
||||
|
||||
virtual ~DSound() {}
|
||||
|
||||
virtual bool Start();
|
||||
@ -81,8 +79,8 @@ public:
|
||||
|
||||
#else
|
||||
public:
|
||||
DSound(int _sampleRate, StreamCallback _callback, void *hWnd = NULL) :
|
||||
SoundStream(_sampleRate, _callback) {}
|
||||
DSound(CMixer *mixer, void *hWnd = NULL) :
|
||||
SoundStream(mixer) {}
|
||||
#endif
|
||||
};
|
||||
|
||||
|
160
Source/Core/AudioCommon/Src/Mixer.cpp
Normal file
160
Source/Core/AudioCommon/Src/Mixer.cpp
Normal file
@ -0,0 +1,160 @@
|
||||
// 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> // System
|
||||
|
||||
#include "Thread.h" // Common
|
||||
|
||||
#include "Mixer.h"
|
||||
#include "FixedSizeQueue.h"
|
||||
#include "AudioCommon.h"
|
||||
|
||||
void CMixer::Mix(short *samples, int numSamples)
|
||||
{
|
||||
// silence
|
||||
memset(samples, 0, numSamples * 2 * sizeof(short));
|
||||
|
||||
if (g_dspInitialize.pEmulatorState) {
|
||||
if (*g_dspInitialize.pEmulatorState != 0)
|
||||
return;
|
||||
}
|
||||
|
||||
Premix(samples, numSamples);
|
||||
|
||||
push_sync.Enter();
|
||||
int count = 0;
|
||||
while (m_queueSize > queue_minlength && count < numSamples * 2) {
|
||||
int x = samples[count];
|
||||
x += sample_queue.front();
|
||||
if (x > 32767) x = 32767;
|
||||
if (x < -32767) x = -32767;
|
||||
samples[count++] = x;
|
||||
sample_queue.pop();
|
||||
x = samples[count];
|
||||
x += sample_queue.front();
|
||||
if (x > 32767) x = 32767;
|
||||
if (x < -32767) x = -32767;
|
||||
samples[count++] = x;
|
||||
sample_queue.pop();
|
||||
m_queueSize-=2;
|
||||
}
|
||||
push_sync.Leave();
|
||||
}
|
||||
|
||||
|
||||
void CMixer::PushSamples(short *samples, int num_stereo_samples)
|
||||
{
|
||||
if (!soundStream)
|
||||
return;
|
||||
|
||||
if (m_queueSize == 0)
|
||||
{
|
||||
m_queueSize = queue_minlength;
|
||||
for (int i = 0; i < queue_minlength; i++)
|
||||
sample_queue.push((s16)0);
|
||||
}
|
||||
|
||||
static int PV1l=0,PV2l=0,PV3l=0,PV4l=0;
|
||||
static int PV1r=0,PV2r=0,PV3r=0,PV4r=0;
|
||||
static int acc=0;
|
||||
|
||||
#ifdef _WIN32
|
||||
if (GetAsyncKeyState(VK_TAB))
|
||||
return;
|
||||
#endif
|
||||
|
||||
// Write Other Audio
|
||||
if (m_throttle) {
|
||||
/* This is only needed for non-AX sound, currently directly
|
||||
streamed and DTK sound. For AX we call SoundStream::Update in
|
||||
AXTask() for example. */
|
||||
while (m_queueSize > queue_maxlength / 2) {
|
||||
// Urgh.
|
||||
if (g_dspInitialize.pEmulatorState) {
|
||||
if (*g_dspInitialize.pEmulatorState != 0)
|
||||
return;
|
||||
}
|
||||
soundStream->Update();
|
||||
Common::SleepCurrentThread(0);
|
||||
}
|
||||
|
||||
push_sync.Enter();
|
||||
while (num_stereo_samples)
|
||||
{
|
||||
acc += m_sampleRate;
|
||||
while (num_stereo_samples && (acc >= 48000))
|
||||
{
|
||||
PV4l=PV3l;
|
||||
PV3l=PV2l;
|
||||
PV2l=PV1l;
|
||||
PV1l=*(samples++); //32bit processing
|
||||
PV4r=PV3r;
|
||||
PV3r=PV2r;
|
||||
PV2r=PV1r;
|
||||
PV1r=*(samples++); //32bit processing
|
||||
num_stereo_samples--;
|
||||
acc-=48000;
|
||||
}
|
||||
|
||||
// defaults to nearest
|
||||
s32 DataL = PV1l;
|
||||
s32 DataR = PV1r;
|
||||
|
||||
if (m_mode == 1) //linear
|
||||
{
|
||||
DataL = PV1l + ((PV2l - PV1l)*acc)/48000;
|
||||
DataR = PV1r + ((PV2r - PV1r)*acc)/48000;
|
||||
}
|
||||
else if (m_mode == 2) //cubic
|
||||
{
|
||||
s32 a0l = PV1l - PV2l - PV4l + PV3l;
|
||||
s32 a0r = PV1r - PV2r - PV4r + PV3r;
|
||||
s32 a1l = PV4l - PV3l - a0l;
|
||||
s32 a1r = PV4r - PV3r - a0r;
|
||||
s32 a2l = PV1l - PV4l;
|
||||
s32 a2r = PV1r - PV4r;
|
||||
s32 a3l = PV2l;
|
||||
s32 a3r = PV2r;
|
||||
|
||||
s32 t0l = ((a0l )*acc)/48000;
|
||||
s32 t0r = ((a0r )*acc)/48000;
|
||||
s32 t1l = ((t0l+a1l)*acc)/48000;
|
||||
s32 t1r = ((t0r+a1r)*acc)/48000;
|
||||
s32 t2l = ((t1l+a2l)*acc)/48000;
|
||||
s32 t2r = ((t1r+a2r)*acc)/48000;
|
||||
s32 t3l = ((t2l+a3l));
|
||||
s32 t3r = ((t2r+a3r));
|
||||
|
||||
DataL = t3l;
|
||||
DataR = t3r;
|
||||
}
|
||||
|
||||
int l = DataL, r = DataR;
|
||||
if (l < -32767) l = -32767;
|
||||
if (r < -32767) r = -32767;
|
||||
if (l > 32767) l = 32767;
|
||||
if (r > 32767) r = 32767;
|
||||
sample_queue.push(l);
|
||||
sample_queue.push(r);
|
||||
m_queueSize += 2;
|
||||
}
|
||||
push_sync.Leave();
|
||||
}
|
||||
}
|
||||
|
67
Source/Core/AudioCommon/Src/Mixer.h
Normal file
67
Source/Core/AudioCommon/Src/Mixer.h
Normal file
@ -0,0 +1,67 @@
|
||||
// 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
|
||||
|
||||
#include "FixedSizeQueue.h"
|
||||
|
||||
// On real hardware, this fifo is much, much smaller. But timing is also
|
||||
// tighter than under Windows, so...
|
||||
#define queue_minlength 1024 * 4
|
||||
#define queue_maxlength 1024 * 28
|
||||
|
||||
class CMixer {
|
||||
|
||||
public:
|
||||
CMixer() : m_sampleRate(48000),m_bits(16),m_channels(2), m_mode(2), m_HLEready(false) {}
|
||||
|
||||
// Called from audio threads
|
||||
void Mix(short *sample, int numSamples);
|
||||
|
||||
// Called from main thread
|
||||
void PushSamples(short* samples, int num_stereo_samples);
|
||||
|
||||
virtual void Premix(short *samples, int numSamples) {}
|
||||
|
||||
int GetSampleRate() {return m_sampleRate;}
|
||||
|
||||
void SetThrottle(bool use) { m_throttle = use;}
|
||||
|
||||
// FIXME do we need this
|
||||
bool IsHLEReady() { return m_HLEready;}
|
||||
void SetHLEReady(bool ready) { m_HLEready = ready;}
|
||||
//////
|
||||
|
||||
protected:
|
||||
int m_sampleRate;
|
||||
int m_bits;
|
||||
int m_channels;
|
||||
|
||||
int m_mode;
|
||||
bool m_HLEready;
|
||||
int m_queueSize;
|
||||
|
||||
bool m_throttle;
|
||||
private:
|
||||
Common::CriticalSection push_sync;
|
||||
FixedSizeQueue<s16, queue_maxlength> sample_queue;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -23,8 +23,7 @@
|
||||
class NullSound : public SoundStream
|
||||
{
|
||||
public:
|
||||
NullSound(int _sampleRate, StreamCallback _callback) :
|
||||
SoundStream(_sampleRate, _callback) {}
|
||||
NullSound(CMixer *mixer) : SoundStream(mixer) {}
|
||||
|
||||
virtual ~NullSound() {}
|
||||
|
||||
@ -35,7 +34,8 @@ public:
|
||||
virtual bool Start() { return true; }
|
||||
|
||||
virtual void Update() {
|
||||
(*callback)(NULL, 256 >> 2, 16, sampleRate, 2);
|
||||
m_mixer->Mix(NULL, 256 >> 2);
|
||||
//(*callback)(NULL, 256 >> 2, 16, sampleRate, 2);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -5,6 +5,8 @@ Import('env')
|
||||
files = [
|
||||
'AOSoundStream.cpp',
|
||||
'WaveFile.cpp',
|
||||
'Mixer.cpp',
|
||||
'AudioCommon.cpp',
|
||||
]
|
||||
|
||||
env_audiocommon = env.Clone()
|
||||
|
@ -19,32 +19,27 @@
|
||||
#define __SOUNDSTREAM_H__
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
|
||||
#include "Mixer.h"
|
||||
|
||||
class SoundStream
|
||||
{
|
||||
protected:
|
||||
int sampleRate;
|
||||
StreamCallback callback;
|
||||
|
||||
CMixer *m_mixer;
|
||||
// We set this to shut down the sound thread.
|
||||
// 0=keep playing, 1=stop playing NOW.
|
||||
volatile int threadData;
|
||||
|
||||
public:
|
||||
SoundStream(int _sampleRate, StreamCallback _callback) :
|
||||
sampleRate(_sampleRate), callback(_callback), threadData(0) {}
|
||||
virtual ~SoundStream() {}
|
||||
SoundStream(CMixer *mixer) : m_mixer(mixer), threadData(0) {}
|
||||
virtual ~SoundStream() { delete m_mixer;}
|
||||
|
||||
static bool isValid() { return false; }
|
||||
virtual bool usesMixer() const { return false; }
|
||||
virtual bool Start() { return false; }
|
||||
virtual void SoundLoop() {}
|
||||
virtual void Stop() {}
|
||||
virtual void Update() {}
|
||||
|
||||
virtual int GetSampleRate() const { return sampleRate; }
|
||||
static bool isValid() { return false; }
|
||||
virtual CMixer *GetMixer() const { return m_mixer; }
|
||||
virtual bool Start() { return false; }
|
||||
virtual void SoundLoop() {}
|
||||
virtual void Stop() {}
|
||||
virtual void Update() {}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user