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:
25
Source/Plugins/Plugin_DSP_HLE/Src/HLEMixer.cpp
Normal file
25
Source/Plugins/Plugin_DSP_HLE/Src/HLEMixer.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "Config.h" // Local
|
||||
#include "Globals.h"
|
||||
#include "DSPHandler.h"
|
||||
#include "HLEMixer.h"
|
||||
|
||||
void HLEMixer::MixUCode(short *samples, int numSamples) {
|
||||
//if this was called directly from the HLE, and not by timeout
|
||||
if (g_Config.m_EnableHLEAudio && IsHLEReady()) {
|
||||
IUCode* pUCode = CDSPHandler::GetInstance().GetUCode();
|
||||
if (pUCode != NULL)
|
||||
pUCode->MixAdd(samples, numSamples);
|
||||
}
|
||||
}
|
||||
|
||||
void HLEMixer::Premix(short *samples, int numSamples) {
|
||||
|
||||
// first get the DTK Music
|
||||
if (g_Config.m_EnableDTKMusic) {
|
||||
g_dspInitialize.pGetAudioStreaming(samples, numSamples);
|
||||
}
|
||||
|
||||
MixUCode(samples, numSamples);
|
||||
}
|
||||
|
||||
|
16
Source/Plugins/Plugin_DSP_HLE/Src/HLEMixer.h
Normal file
16
Source/Plugins/Plugin_DSP_HLE/Src/HLEMixer.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef HLEMIXER_H
|
||||
#define HLEMIXER_H
|
||||
#include "AudioCommon.h"
|
||||
#include "Mixer.h"
|
||||
|
||||
class HLEMixer : public CMixer
|
||||
{
|
||||
public:
|
||||
void MixUCode(short *samples, int numSamples);
|
||||
|
||||
virtual void Premix(short *samples, int numSamples);
|
||||
};
|
||||
|
||||
#endif // HLEMIXER_H
|
||||
|
||||
|
@ -1,200 +0,0 @@
|
||||
// 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 "../Config.h" // Local
|
||||
#include "../Globals.h"
|
||||
#include "../DSPHandler.h"
|
||||
#include "../Debugger/File.h"
|
||||
#include "../main.h"
|
||||
|
||||
#include "Mixer.h"
|
||||
#include "FixedSizeQueue.h"
|
||||
|
||||
namespace {
|
||||
|
||||
Common::CriticalSection push_sync;
|
||||
|
||||
// On real hardware, this fifo is much, much smaller. But timing is also tighter than under Windows, so...
|
||||
const int queue_minlength = 1024 * 4;
|
||||
const int queue_maxlength = 1024 * 28;
|
||||
|
||||
FixedSizeQueue<s16, queue_maxlength> 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));
|
||||
|
||||
if (g_dspInitialize.pEmulatorState) {
|
||||
if (*g_dspInitialize.pEmulatorState != 0)
|
||||
return;
|
||||
}
|
||||
|
||||
// first get the DTK Music
|
||||
if (g_Config.m_EnableDTKMusic)
|
||||
{
|
||||
g_dspInitialize.pGetAudioStreaming(buffer, numSamples);
|
||||
}
|
||||
|
||||
Mixer_MixUCode(buffer, numSamples, bits, rate, channels);
|
||||
|
||||
push_sync.Enter();
|
||||
int count = 0;
|
||||
while (queue_size > queue_minlength && count < numSamples * 2) {
|
||||
int x = buffer[count];
|
||||
x += sample_queue.front();
|
||||
if (x > 32767) x = 32767;
|
||||
if (x < -32767) x = -32767;
|
||||
buffer[count++] = x;
|
||||
sample_queue.pop();
|
||||
x = buffer[count];
|
||||
x += sample_queue.front();
|
||||
if (x > 32767) x = 32767;
|
||||
if (x < -32767) x = -32767;
|
||||
buffer[count++] = x;
|
||||
sample_queue.pop();
|
||||
queue_size-=2;
|
||||
}
|
||||
push_sync.Leave();
|
||||
}
|
||||
|
||||
void Mixer_MixUCode(short *buffer, int numSamples, int bits, int rate,
|
||||
int channels) {
|
||||
//if this was called directly from the HLE, and not by timeout
|
||||
if (g_Config.m_EnableHLEAudio && mixer_HLEready)
|
||||
{
|
||||
IUCode* pUCode = CDSPHandler::GetInstance().GetUCode();
|
||||
if (pUCode != NULL)
|
||||
pUCode->MixAdd(buffer, numSamples);
|
||||
}
|
||||
}
|
||||
|
||||
void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate)
|
||||
{
|
||||
if (!soundStream)
|
||||
return;
|
||||
|
||||
if (queue_size == 0)
|
||||
{
|
||||
queue_size = 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 (g_Config.m_EnableThrottle)
|
||||
{
|
||||
/* 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 (queue_size > queue_maxlength / 2) {
|
||||
// Urgh.
|
||||
if (g_dspInitialize.pEmulatorState) {
|
||||
if (*g_dspInitialize.pEmulatorState != 0)
|
||||
return;
|
||||
}
|
||||
soundStream->Update();
|
||||
Common::SleepCurrentThread(0);
|
||||
}
|
||||
|
||||
//convert into config option?
|
||||
const int mode = 2;
|
||||
|
||||
push_sync.Enter();
|
||||
while (num_stereo_samples)
|
||||
{
|
||||
acc += sample_rate;
|
||||
while (num_stereo_samples && (acc >= 48000))
|
||||
{
|
||||
PV4l=PV3l;
|
||||
PV3l=PV2l;
|
||||
PV2l=PV1l;
|
||||
PV1l=*(buffer++); //32bit processing
|
||||
PV4r=PV3r;
|
||||
PV3r=PV2r;
|
||||
PV2r=PV1r;
|
||||
PV1r=*(buffer++); //32bit processing
|
||||
num_stereo_samples--;
|
||||
acc-=48000;
|
||||
}
|
||||
|
||||
// defaults to nearest
|
||||
s32 DataL = PV1l;
|
||||
s32 DataR = PV1r;
|
||||
|
||||
if (mode == 1) //linear
|
||||
{
|
||||
DataL = PV1l + ((PV2l - PV1l)*acc)/48000;
|
||||
DataR = PV1r + ((PV2r - PV1r)*acc)/48000;
|
||||
}
|
||||
else if (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);
|
||||
queue_size += 2;
|
||||
}
|
||||
push_sync.Leave();
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
// 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);
|
||||
|
||||
void Mixer_MixUCode(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
|
||||
|
@ -8,11 +8,10 @@ name = "Plugin_DSP_HLE"
|
||||
files = [
|
||||
'DSPHandler.cpp',
|
||||
'MailHandler.cpp',
|
||||
'HLEMixer.cpp',
|
||||
'main.cpp',
|
||||
'Config.cpp',
|
||||
'Globals.cpp',
|
||||
# 'PCHW/AOSoundStream.cpp',
|
||||
'PCHW/Mixer.cpp',
|
||||
'Debugger/File.cpp',
|
||||
'UCodes/UCode_AX.cpp',
|
||||
'UCodes/UCode_AXWii.cpp',
|
||||
|
@ -25,7 +25,7 @@ extern CDebugger* m_frame;
|
||||
#include <sstream>
|
||||
|
||||
#include "../Globals.h"
|
||||
#include "../PCHW/Mixer.h"
|
||||
#include "Mixer.h"
|
||||
#include "../MailHandler.h"
|
||||
|
||||
#include "UCodes.h"
|
||||
@ -513,7 +513,7 @@ bool CUCode_AX::AXTask(u32& _uMail)
|
||||
m_addressPBs = Memory_Read_U32(uAddress);
|
||||
uAddress += 4;
|
||||
|
||||
mixer_HLEready = true;
|
||||
soundStream->GetMixer()->SetHLEReady(true);
|
||||
SaveLog("%08x : AXLIST PB address: %08x", uAddress, m_addressPBs);
|
||||
|
||||
SaveLog("Update the SoundThread to be in sync");
|
||||
|
@ -23,8 +23,8 @@
|
||||
extern CDebugger * m_frame;
|
||||
#endif
|
||||
|
||||
#include "../PCHW/Mixer.h"
|
||||
#include "../MailHandler.h"
|
||||
#include "Mixer.h"
|
||||
|
||||
#include "UCodes.h"
|
||||
#include "UCode_AXStructs.h"
|
||||
@ -324,7 +324,7 @@ bool CUCode_AXWii::AXTask(u32& _uMail)
|
||||
case 0x0004: // PBs are here now
|
||||
m_addressPBs = Memory_Read_U32(uAddress);
|
||||
lCUCode_AX->m_addressPBs = m_addressPBs; // for the sake of logging
|
||||
mixer_HLEready = true;
|
||||
soundStream->GetMixer()->SetHLEReady(true);
|
||||
SaveLog("%08x : AXLIST PB address: %08x", uAddress, m_addressPBs);
|
||||
soundStream->Update();
|
||||
uAddress += 4;
|
||||
|
@ -21,7 +21,7 @@
|
||||
#include "UCode_AX_ADPCM.h"
|
||||
#include "UCode_AX.h"
|
||||
#include "../main.h"
|
||||
|
||||
#include "Mixer.h"
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Externals
|
||||
@ -107,7 +107,7 @@ inline void WriteBackPBsWii(u32 pbs_address, ParamBlockType& _pPBs, int _num)
|
||||
template<class ParamBlockType>
|
||||
inline void MixAddVoice(ParamBlockType &pb, int *templbuffer, int *temprbuffer, int _iSize, bool Wii)
|
||||
{
|
||||
ratioFactor = 32000.0f / (float)soundStream->GetSampleRate();
|
||||
ratioFactor = 32000.0f / (float)soundStream->GetMixer()->GetSampleRate();
|
||||
|
||||
DoVoiceHacks(pb, Wii);
|
||||
|
||||
@ -115,7 +115,6 @@ inline void MixAddVoice(ParamBlockType &pb, int *templbuffer, int *temprbuffer,
|
||||
if (pb.running)
|
||||
{
|
||||
|
||||
// =======================================================================================
|
||||
// Read initial parameters
|
||||
// ------------
|
||||
//constants
|
||||
|
@ -24,7 +24,7 @@
|
||||
#include "../MailHandler.h"
|
||||
|
||||
#include "../main.h"
|
||||
#include "../PCHW/Mixer.h"
|
||||
#include "Mixer.h"
|
||||
|
||||
|
||||
CUCode_Zelda::CUCode_Zelda(CMailHandler& _rMailHandler)
|
||||
@ -157,8 +157,7 @@ void CUCode_Zelda::ExecuteList()
|
||||
tmp[2] = Read32();
|
||||
|
||||
// We're ready to mix
|
||||
mixer_HLEready = true;
|
||||
|
||||
soundStream->GetMixer()->SetHLEReady(true);
|
||||
DEBUG_LOG(DSPHLE, "Update the SoundThread to be in sync");
|
||||
soundStream->Update(); //do it in this thread to avoid sync problems
|
||||
|
||||
|
@ -28,16 +28,17 @@ CDebugger* m_frame = NULL;
|
||||
|
||||
#include "ChunkFile.h"
|
||||
#include "WaveFile.h"
|
||||
#include "PCHW/Mixer.h"
|
||||
#include "HLEMixer.h"
|
||||
#include "DSPHandler.h"
|
||||
#include "Config.h"
|
||||
#include "Setup.h"
|
||||
#include "StringUtil.h"
|
||||
|
||||
#include "AudioCommon.h"
|
||||
#include "AOSoundStream.h"
|
||||
#include "DSoundStream.h"
|
||||
#include "NullSoundStream.h"
|
||||
|
||||
|
||||
// Declarations and definitions
|
||||
PLUGIN_GLOBALS* globals = NULL;
|
||||
DSPInitialize g_dspInitialize;
|
||||
@ -192,6 +193,7 @@ void DllConfig(HWND _hParent)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void Initialize(void *init)
|
||||
{
|
||||
g_dspInitialize = *(DSPInitialize*)init;
|
||||
@ -201,45 +203,9 @@ void Initialize(void *init)
|
||||
|
||||
CDSPHandler::CreateInstance();
|
||||
|
||||
if (g_Config.sBackend == "DSound")
|
||||
{
|
||||
if (DSound::isValid())
|
||||
soundStream = new DSound(48000, Mixer, g_dspInitialize.hWnd);
|
||||
}
|
||||
else if (g_Config.sBackend == "AOSound")
|
||||
{
|
||||
if (AOSound::isValid())
|
||||
soundStream = new AOSound(48000, Mixer);
|
||||
}
|
||||
else if (g_Config.sBackend == "NullSound")
|
||||
{
|
||||
soundStream = new NullSound(48000, Mixer_MixUCode);
|
||||
}
|
||||
else
|
||||
{
|
||||
PanicAlert("Cannot recognize backend %s", g_Config.sBackend.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (soundStream)
|
||||
{
|
||||
if (!soundStream->Start())
|
||||
{
|
||||
PanicAlert("Could not initialize backend %s, falling back to NULL",
|
||||
g_Config.sBackend.c_str());
|
||||
delete soundStream;
|
||||
soundStream = new NullSound(48000, Mixer);
|
||||
soundStream->Start();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PanicAlert("Sound backend %s is not valid, falling back to NULL",
|
||||
g_Config.sBackend.c_str());
|
||||
delete soundStream;
|
||||
soundStream = new NullSound(48000, Mixer);
|
||||
soundStream->Start();
|
||||
}
|
||||
soundStream = AudioCommon::InitSoundStream(g_Config.sBackend,
|
||||
new HLEMixer());
|
||||
soundStream->GetMixer()->SetThrottle(g_Config.m_EnableThrottle);
|
||||
|
||||
// Start the sound recording
|
||||
if (log_ai)
|
||||
@ -251,15 +217,20 @@ void Initialize(void *init)
|
||||
|
||||
void DSP_StopSoundStream()
|
||||
{
|
||||
// fprintf(stderr, "in dsp stop\n");
|
||||
if (!soundStream)
|
||||
PanicAlert("Can't stop non running SoundStream!");
|
||||
soundStream->Stop();
|
||||
delete soundStream;
|
||||
soundStream = NULL;
|
||||
// fprintf(stderr, "in dsp stop end\n");
|
||||
|
||||
}
|
||||
|
||||
void Shutdown()
|
||||
{
|
||||
// FIXME: called before stop is finished????
|
||||
// fprintf(stderr, "in dsp shutdown\n");
|
||||
// Check that soundstream already is stopped.
|
||||
if (soundStream)
|
||||
PanicAlert("SoundStream alive in DSP::Shutdown!");
|
||||
@ -384,7 +355,7 @@ void DSP_SendAIBuffer(unsigned int address, int sample_rate)
|
||||
return;
|
||||
}
|
||||
|
||||
if (soundStream->usesMixer())
|
||||
if (soundStream->GetMixer())
|
||||
{
|
||||
short samples[16] = {0}; // interleaved stereo
|
||||
if (address)
|
||||
@ -398,7 +369,7 @@ void DSP_SendAIBuffer(unsigned int address, int sample_rate)
|
||||
if (log_ai)
|
||||
g_wave_writer.AddStereoSamples(samples, 8);
|
||||
}
|
||||
Mixer_PushSamples(samples, 32 / 4, sample_rate);
|
||||
soundStream->GetMixer()->PushSamples(samples, 32 / 4);
|
||||
}
|
||||
|
||||
// SoundStream is updated only when necessary (there is no 70 ms limit
|
||||
|
Reference in New Issue
Block a user