2008-08-16 21:58:07 +00:00
|
|
|
// 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"
|
2008-11-21 02:53:54 +00:00
|
|
|
#include "../Logging/Console.h"
|
2008-08-16 21:58:07 +00:00
|
|
|
#include "Thread.h"
|
|
|
|
#include "Mixer.h"
|
2008-09-28 17:34:40 +00:00
|
|
|
#include "FixedSizeQueue.h"
|
|
|
|
|
2008-08-16 21:58:07 +00:00
|
|
|
#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...
|
2008-09-28 17:34:40 +00:00
|
|
|
const int queue_minlength = 1024 * 4;
|
|
|
|
const int queue_maxlength = 1024 * 28;
|
|
|
|
|
|
|
|
FixedSizeQueue<s16, queue_maxlength> sample_queue;
|
|
|
|
|
2008-08-16 21:58:07 +00:00
|
|
|
} // 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));
|
|
|
|
|
2008-08-31 13:36:52 +00:00
|
|
|
// first get the DTK Music
|
2008-08-16 21:58:07 +00:00
|
|
|
if (g_Config.m_EnableDTKMusic)
|
|
|
|
{
|
|
|
|
g_dspInitialize.pGetAudioStreaming(buffer, numSamples);
|
|
|
|
}
|
|
|
|
|
|
|
|
//if this was called directly from the HLE, and not by timeout
|
2008-11-23 00:01:56 +00:00
|
|
|
if (g_Config.m_EnableHLEAudio && mixer_HLEready)
|
2008-08-16 21:58:07 +00:00
|
|
|
{
|
|
|
|
IUCode* pUCode = CDSPHandler::GetInstance().GetUCode();
|
|
|
|
if (pUCode != NULL)
|
|
|
|
pUCode->MixAdd(buffer, numSamples);
|
|
|
|
}
|
|
|
|
|
|
|
|
push_sync.Enter();
|
|
|
|
int count = 0;
|
2008-09-28 17:34:40 +00:00
|
|
|
while (queue_size > queue_minlength && count < numSamples * 2) {
|
2008-08-16 21:58:07 +00:00
|
|
|
int x = buffer[count];
|
|
|
|
x += sample_queue.front();
|
|
|
|
if (x > 32767) x = 32767;
|
|
|
|
if (x < -32767) x = -32767;
|
2008-09-28 17:34:40 +00:00
|
|
|
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;
|
2008-08-16 21:58:07 +00:00
|
|
|
sample_queue.pop();
|
2008-09-28 17:34:40 +00:00
|
|
|
queue_size-=2;
|
2008-08-16 21:58:07 +00:00
|
|
|
}
|
|
|
|
push_sync.Leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate) {
|
2008-08-23 09:20:36 +00:00
|
|
|
// static FILE *f;
|
2008-08-16 21:58:07 +00:00
|
|
|
// if (!f)
|
|
|
|
// f = fopen("d:\\hello.raw", "wb");
|
|
|
|
// fwrite(buffer, num_stereo_samples * 4, 1, f);
|
2008-09-28 17:34:40 +00:00
|
|
|
if (queue_size == 0)
|
|
|
|
{
|
|
|
|
queue_size = queue_minlength;
|
|
|
|
for (int i = 0; i < queue_minlength; i++)
|
|
|
|
sample_queue.push((s16)0);
|
|
|
|
}
|
2008-08-16 22:28:33 +00:00
|
|
|
|
|
|
|
static int PV1l=0,PV2l=0,PV3l=0,PV4l=0;
|
|
|
|
static int PV1r=0,PV2r=0,PV3r=0,PV4r=0;
|
|
|
|
static int acc=0;
|
2008-08-16 21:58:07 +00:00
|
|
|
|
2008-08-23 11:18:39 +00:00
|
|
|
#ifdef _WIN32
|
2008-11-21 02:53:54 +00:00
|
|
|
if (! (GetAsyncKeyState(VK_TAB)) && g_Config.m_EnableThrottle) {
|
|
|
|
|
|
|
|
/* This is only needed for non-AX sound, currently directly streamed and
|
|
|
|
DTK sound. For AX we call DSound_UpdateSound in AXTask() for example. */
|
2008-09-28 17:34:40 +00:00
|
|
|
while (queue_size > queue_maxlength / 2) {
|
|
|
|
DSound::DSound_UpdateSound();
|
|
|
|
Sleep(0);
|
|
|
|
}
|
2008-08-23 09:20:36 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2008-08-23 11:18:39 +00:00
|
|
|
#else
|
2008-09-28 17:34:40 +00:00
|
|
|
while (queue_size > queue_maxlength) {
|
2008-08-23 11:18:39 +00:00
|
|
|
sleep(0);
|
|
|
|
}
|
|
|
|
#endif
|
2008-08-16 22:28:33 +00:00
|
|
|
//convert into config option?
|
|
|
|
const int mode = 2;
|
|
|
|
|
2008-08-16 21:58:07 +00:00
|
|
|
push_sync.Enter();
|
2008-08-16 22:28:33 +00:00
|
|
|
while (num_stereo_samples)
|
|
|
|
{
|
|
|
|
acc += sample_rate;
|
2008-09-28 17:34:40 +00:00
|
|
|
while (num_stereo_samples && (acc >= 48000))
|
2008-08-16 22:28:33 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2008-08-16 23:53:00 +00:00
|
|
|
if (mode == 1) //linear
|
2008-08-16 22:28:33 +00:00
|
|
|
{
|
|
|
|
DataL = PV1l + ((PV2l - PV1l)*acc)/48000;
|
|
|
|
DataR = PV1r + ((PV2r - PV1r)*acc)/48000;
|
2008-08-16 21:58:07 +00:00
|
|
|
}
|
2008-08-16 23:53:00 +00:00
|
|
|
else if (mode == 2) //cubic
|
2008-08-16 22:28:33 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2008-10-21 20:27:37 +00:00
|
|
|
|
|
|
|
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);
|
2008-08-16 23:53:00 +00:00
|
|
|
queue_size += 2;
|
2008-08-16 22:28:33 +00:00
|
|
|
}
|
2008-08-16 21:58:07 +00:00
|
|
|
push_sync.Leave();
|
|
|
|
}
|