2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-01-17 07:28:09 -07:00
|
|
|
|
2009-12-23 08:34:14 -07:00
|
|
|
#include "Atomic.h"
|
2008-12-07 22:25:12 -07:00
|
|
|
#include "Mixer.h"
|
2009-03-26 03:29:14 -06:00
|
|
|
#include "AudioCommon.h"
|
2011-01-12 00:08:35 -07:00
|
|
|
#include "CPUDetect.h"
|
2013-12-07 13:14:29 -07:00
|
|
|
#include "../Core/Host.h"
|
2011-01-12 00:08:35 -07:00
|
|
|
|
2013-12-07 13:14:29 -07:00
|
|
|
#include "../Core/HW/AudioInterface.h"
|
2011-01-28 11:39:30 -07:00
|
|
|
|
|
|
|
// UGLINESS
|
2013-12-07 13:14:29 -07:00
|
|
|
#include "../Core/PowerPC/PowerPC.h"
|
2011-01-28 11:39:30 -07:00
|
|
|
|
2011-01-12 02:34:53 -07:00
|
|
|
#if _M_SSE >= 0x301 && !(defined __GNUC__ && !defined __SSSE3__)
|
|
|
|
#include <tmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2009-12-23 08:34:14 -07:00
|
|
|
// Executed from sound stream thread
|
|
|
|
unsigned int CMixer::Mix(short* samples, unsigned int numSamples)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2009-12-23 08:34:14 -07:00
|
|
|
if (!samples)
|
2009-06-12 08:40:50 -06:00
|
|
|
return 0;
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2011-12-30 21:16:12 -07:00
|
|
|
std::lock_guard<std::mutex> lk(m_csMixing);
|
|
|
|
|
|
|
|
if (PowerPC::GetState() != PowerPC::CPU_RUNNING)
|
2009-12-23 08:34:14 -07:00
|
|
|
{
|
2011-01-28 11:39:30 -07:00
|
|
|
// Silence
|
|
|
|
memset(samples, 0, numSamples * 4);
|
|
|
|
return numSamples;
|
2009-03-30 03:55:50 -06:00
|
|
|
}
|
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
unsigned int currentSample = 0;
|
2010-09-28 15:43:38 -06:00
|
|
|
|
2013-07-11 13:22:38 -06:00
|
|
|
// Cache access in non-volatile variable
|
|
|
|
// This is the only function changing the read value, so it's safe to
|
|
|
|
// cache it locally although it's written here.
|
|
|
|
// The writing pointer will be modified outside, but it will only increase,
|
|
|
|
// so we will just ignore new written data while interpolating.
|
|
|
|
// Without this cache, the compiler wouldn't be allowed to optimize the
|
|
|
|
// interpolation loop.
|
|
|
|
u32 indexR = Common::AtomicLoad(m_indexR);
|
|
|
|
u32 indexW = Common::AtomicLoad(m_indexW);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-02-06 05:03:40 -07:00
|
|
|
float numLeft = ((indexW - indexR) & INDEX_MASK) / 2;
|
|
|
|
m_numLeftI = (numLeft + m_numLeftI*(CONTROL_AVG-1)) / CONTROL_AVG;
|
|
|
|
float offset = (m_numLeftI - LOW_WATERMARK) * CONTROL_FACTOR;
|
|
|
|
if(offset > MAX_FREQ_SHIFT) offset = MAX_FREQ_SHIFT;
|
|
|
|
if(offset < -MAX_FREQ_SHIFT) offset = -MAX_FREQ_SHIFT;
|
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
//render numleft sample pairs to samples[]
|
|
|
|
//advance indexR with sample position
|
|
|
|
//remember fractional offset
|
2014-02-05 22:38:04 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
static u32 frac = 0;
|
2014-02-06 05:03:40 -07:00
|
|
|
const u32 ratio = (u32)( 65536.0f * (float)AudioInterface::GetAIDSampleRate() / (float)m_sampleRate + offset );
|
2014-02-05 22:38:04 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
if(ratio > 0x10000)
|
|
|
|
ERROR_LOG(AUDIO, "ratio out of range");
|
2014-02-05 22:38:04 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
for (; currentSample < numSamples*2 && ((indexW-indexR) & INDEX_MASK) > 2; currentSample+=2) {
|
|
|
|
u32 indexR2 = indexR + 2; //next sample
|
2014-02-05 22:38:04 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
s16 l1 = Common::swap16(m_buffer[indexR & INDEX_MASK]); //current
|
|
|
|
s16 l2 = Common::swap16(m_buffer[indexR2 & INDEX_MASK]); //next
|
|
|
|
int sampleL = ((l1 << 16) + (l2 - l1) * (u16)frac) >> 16;
|
|
|
|
samples[currentSample+1] = sampleL;
|
2014-02-05 22:38:04 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
s16 r1 = Common::swap16(m_buffer[(indexR + 1) & INDEX_MASK]); //current
|
|
|
|
s16 r2 = Common::swap16(m_buffer[(indexR2 + 1) & INDEX_MASK]); //next
|
|
|
|
int sampleR = ((r1 << 16) + (r2 - r1) * (u16)frac) >> 16;
|
|
|
|
samples[currentSample] = sampleR;
|
2010-09-28 15:43:38 -06:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
frac += ratio;
|
|
|
|
indexR += 2 * (u16)(frac >> 16);
|
|
|
|
frac &= 0xffff;
|
2009-12-23 08:34:14 -07:00
|
|
|
}
|
2009-12-22 00:26:30 -07:00
|
|
|
|
2009-12-23 08:34:14 -07:00
|
|
|
// Padding
|
2014-02-05 22:55:45 -07:00
|
|
|
unsigned short s[2];
|
|
|
|
s[0] = Common::swap16(m_buffer[(indexR - 1) & INDEX_MASK]);
|
|
|
|
s[1] = Common::swap16(m_buffer[(indexR - 2) & INDEX_MASK]);
|
|
|
|
for (; currentSample < numSamples*2; currentSample+=2)
|
2011-03-12 15:02:46 -07:00
|
|
|
{
|
2014-02-05 22:55:45 -07:00
|
|
|
samples[currentSample] = s[0];
|
|
|
|
samples[currentSample+1] = s[1];
|
2011-03-12 15:02:46 -07:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
|
|
|
// Flush cached variable
|
2013-07-11 13:22:38 -06:00
|
|
|
Common::AtomicStore(m_indexR, indexR);
|
2009-12-23 08:34:14 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
// Add the DSPHLE sound, re-sampling is done inside
|
|
|
|
Premix(samples, numSamples);
|
2011-02-11 11:59:42 -07:00
|
|
|
|
2014-02-05 22:55:45 -07:00
|
|
|
// Add the DTK Music
|
|
|
|
// Re-sampling is done inside
|
|
|
|
AudioInterface::Callback_GetStreaming(samples, numSamples, m_sampleRate);
|
|
|
|
if (m_logAudio)
|
|
|
|
g_wave_writer.AddStereoSamples(samples, numSamples);
|
2009-12-23 08:34:14 -07:00
|
|
|
|
|
|
|
return numSamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-28 11:39:30 -07:00
|
|
|
void CMixer::PushSamples(const short *samples, unsigned int num_samples)
|
2009-12-23 08:34:14 -07:00
|
|
|
{
|
2013-07-11 13:22:38 -06:00
|
|
|
// Cache access in non-volatile variable
|
|
|
|
// indexR isn't allowed to cache in the audio throttling loop as it
|
|
|
|
// needs to get updates to not deadlock.
|
|
|
|
u32 indexW = Common::AtomicLoad(m_indexW);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2010-08-17 09:45:12 -06:00
|
|
|
if (m_throttle)
|
Hy, this is my first commit, and i hope it is not bad xD.
- First change is for Mixer.cpp, I've just re-added the functionality lost in r4724, so, if you disable audio throttle, games like donkey kong jungle beat, will work properly.
- Second change points to a doubt comment on UCode_Zelda_Voice.cpp, where it did not know here PB.NeedsReset came from. Well, the answer is it came from line 03b2 of the dumped Ucode, so when PB.IsBlanck equals to zero, PB.NeedsReset is zero too.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6100 8ced0084-cf51-0410-be5f-012b33b47a6e
2010-08-16 09:22:53 -06:00
|
|
|
{
|
|
|
|
// The auto throttle function. This loop will put a ceiling on the CPU MHz.
|
2013-07-11 13:22:38 -06:00
|
|
|
while (num_samples * 2 + ((indexW - Common::AtomicLoad(m_indexR)) & INDEX_MASK) >= MAX_SAMPLES * 2)
|
2009-12-23 08:34:14 -07:00
|
|
|
{
|
2013-10-28 23:23:17 -06:00
|
|
|
if (*PowerPC::GetStatePtr() != PowerPC::CPU_RUNNING || soundStream->IsMuted())
|
2011-01-28 11:39:30 -07:00
|
|
|
break;
|
2009-12-25 04:59:04 -07:00
|
|
|
// Shortcut key for Throttle Skipping
|
2011-01-30 09:40:38 -07:00
|
|
|
if (Host_GetKeyState('\t'))
|
|
|
|
break;
|
2009-12-23 08:34:14 -07:00
|
|
|
SLEEP(1);
|
2009-12-25 04:59:04 -07:00
|
|
|
soundStream->Update();
|
2009-12-23 08:34:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have enough free space
|
2013-07-11 13:22:38 -06:00
|
|
|
// indexW == m_indexR results in empty buffer, so indexR must always be smaller than indexW
|
|
|
|
if (num_samples * 2 + ((indexW - Common::AtomicLoad(m_indexR)) & INDEX_MASK) >= MAX_SAMPLES * 2)
|
2009-12-23 08:34:14 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// AyuanX: Actual re-sampling work has been moved to sound thread
|
2009-12-25 04:59:04 -07:00
|
|
|
// to alleviate the workload on main thread
|
2009-12-23 08:34:14 -07:00
|
|
|
// and we simply store raw data here to make fast mem copy
|
2013-07-11 13:22:38 -06:00
|
|
|
int over_bytes = num_samples * 4 - (MAX_SAMPLES * 2 - (indexW & INDEX_MASK)) * sizeof(short);
|
2009-12-23 08:34:14 -07:00
|
|
|
if (over_bytes > 0)
|
|
|
|
{
|
2013-07-11 13:22:38 -06:00
|
|
|
memcpy(&m_buffer[indexW & INDEX_MASK], samples, num_samples * 4 - over_bytes);
|
2009-12-23 08:34:14 -07:00
|
|
|
memcpy(&m_buffer[0], samples + (num_samples * 4 - over_bytes) / sizeof(short), over_bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-11 13:22:38 -06:00
|
|
|
memcpy(&m_buffer[indexW & INDEX_MASK], samples, num_samples * 4);
|
2009-12-23 08:34:14 -07:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-07-11 13:22:38 -06:00
|
|
|
Common::AtomicAdd(m_indexW, num_samples * 2);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-12-23 08:34:14 -07:00
|
|
|
return;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
2009-12-19 19:23:26 -07:00
|
|
|
|