2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 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/
|
|
|
|
|
2009-01-17 14:28:09 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
// This queue solution is temporary. I'll implement something more efficient later.
|
2009-01-17 14:28:09 +00:00
|
|
|
#include <queue> // System
|
|
|
|
|
|
|
|
#include "Thread.h" // Common
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
#include "Mixer.h"
|
|
|
|
#include "FixedSizeQueue.h"
|
2009-03-26 09:29:14 +00:00
|
|
|
#include "AudioCommon.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-06-12 14:40:50 +00:00
|
|
|
int CMixer::Mix(short *samples, int numSamples)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-03-26 10:50:52 +00:00
|
|
|
if (! samples) {
|
|
|
|
Premix(NULL, 0);
|
2009-06-12 14:40:50 +00:00
|
|
|
return 0;
|
2009-03-26 10:50:52 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
// silence
|
2009-03-26 09:29:14 +00:00
|
|
|
memset(samples, 0, numSamples * 2 * sizeof(short));
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-01-20 18:42:01 +00:00
|
|
|
if (g_dspInitialize.pEmulatorState) {
|
|
|
|
if (*g_dspInitialize.pEmulatorState != 0)
|
2009-06-12 14:40:50 +00:00
|
|
|
return 0;
|
2009-01-20 18:42:01 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 09:55:50 +00:00
|
|
|
// first get the DTK Music
|
|
|
|
if (m_EnableDTKMusic) {
|
|
|
|
g_dspInitialize.pGetAudioStreaming(samples, numSamples);
|
|
|
|
}
|
|
|
|
|
2009-03-26 09:29:14 +00:00
|
|
|
Premix(samples, numSamples);
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
int count = 0;
|
2009-06-12 14:40:50 +00:00
|
|
|
|
|
|
|
push_sync.Enter();
|
|
|
|
while (m_queueSize > queue_minlength && count < numSamples * 2)
|
|
|
|
{
|
2009-03-26 09:29:14 +00:00
|
|
|
int x = samples[count];
|
2008-12-08 05:25:12 +00:00
|
|
|
x += sample_queue.front();
|
|
|
|
if (x > 32767) x = 32767;
|
|
|
|
if (x < -32767) x = -32767;
|
2009-03-26 09:29:14 +00:00
|
|
|
samples[count++] = x;
|
2008-12-08 05:25:12 +00:00
|
|
|
sample_queue.pop();
|
2009-03-26 09:29:14 +00:00
|
|
|
x = samples[count];
|
2008-12-08 05:25:12 +00:00
|
|
|
x += sample_queue.front();
|
|
|
|
if (x > 32767) x = 32767;
|
|
|
|
if (x < -32767) x = -32767;
|
2009-03-26 09:29:14 +00:00
|
|
|
samples[count++] = x;
|
2008-12-08 05:25:12 +00:00
|
|
|
sample_queue.pop();
|
2009-03-26 09:29:14 +00:00
|
|
|
m_queueSize-=2;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
push_sync.Leave();
|
2009-06-12 14:40:50 +00:00
|
|
|
|
|
|
|
return count;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 19:50:06 +00:00
|
|
|
|
2009-03-28 14:08:06 +00:00
|
|
|
void CMixer::PushSamples(short *samples, int num_stereo_samples, int core_sample_rate)
|
2009-02-14 01:07:20 +00:00
|
|
|
{
|
2009-03-27 11:06:52 +00:00
|
|
|
push_sync.Enter();
|
2009-03-26 09:29:14 +00:00
|
|
|
if (m_queueSize == 0)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2009-03-26 09:29:14 +00:00
|
|
|
m_queueSize = queue_minlength;
|
2008-12-08 05:25:12 +00:00
|
|
|
for (int i = 0; i < queue_minlength; i++)
|
|
|
|
sample_queue.push((s16)0);
|
|
|
|
}
|
2009-03-27 11:06:52 +00:00
|
|
|
push_sync.Leave();
|
2008-12-08 05:25:12 +00:00
|
|
|
static int PV1l=0,PV2l=0,PV3l=0,PV4l=0;
|
|
|
|
static int PV1r=0,PV2r=0,PV3r=0,PV4r=0;
|
2009-01-29 00:57:55 +00:00
|
|
|
static int acc=0;
|
2009-02-17 22:48:16 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (GetAsyncKeyState(VK_TAB))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2009-02-14 01:07:20 +00:00
|
|
|
// Write Other Audio
|
2009-03-28 14:08:06 +00:00
|
|
|
if (!m_throttle)
|
2009-06-15 04:30:02 +00:00
|
|
|
return;
|
2009-03-27 11:06:52 +00:00
|
|
|
|
2009-06-15 04:30:02 +00:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// The auto throttle function. This loop will put a ceiling on the CPU MHz.
|
2009-09-08 16:07:13 +00:00
|
|
|
// ----------------------------
|
2009-03-27 11:06:52 +00:00
|
|
|
/* This is only needed for non-AX sound, currently directly
|
|
|
|
streamed and DTK sound. For AX we call SoundStream::Update in
|
|
|
|
AXTask() for example. */
|
2009-06-15 04:30:02 +00:00
|
|
|
while (m_queueSize > queue_maxlength / 2)
|
|
|
|
{
|
2009-03-27 11:06:52 +00:00
|
|
|
// Urgh.
|
|
|
|
if (g_dspInitialize.pEmulatorState) {
|
|
|
|
if (*g_dspInitialize.pEmulatorState != 0)
|
|
|
|
return;
|
2009-03-26 09:29:14 +00:00
|
|
|
}
|
2009-03-27 11:06:52 +00:00
|
|
|
soundStream->Update();
|
2009-07-17 22:57:02 +00:00
|
|
|
Common::YieldCPU();
|
2009-03-27 11:06:52 +00:00
|
|
|
}
|
2009-06-15 04:30:02 +00:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2009-03-27 11:06:52 +00:00
|
|
|
push_sync.Enter();
|
|
|
|
while (num_stereo_samples) {
|
2009-03-28 14:08:06 +00:00
|
|
|
acc += core_sample_rate;
|
2009-03-27 11:06:52 +00:00
|
|
|
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();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|