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"
|
2011-01-30 12:27:46 -07:00
|
|
|
#include "../../Core/Src/Host.h"
|
2011-01-12 00:08:35 -07:00
|
|
|
|
2011-01-28 11:39:30 -07:00
|
|
|
#include "../../Core/Src/HW/AudioInterface.h"
|
|
|
|
|
|
|
|
// UGLINESS
|
|
|
|
#include "../../Core/Src/PowerPC/PowerPC.h"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-12-23 08:34:14 -07:00
|
|
|
unsigned int numLeft = Common::AtomicLoad(m_numSamples);
|
2010-10-01 17:23:13 -06:00
|
|
|
if (m_AIplaying) {
|
2010-09-28 15:43:38 -06:00
|
|
|
if (numLeft < numSamples)//cannot do much about this
|
2010-10-01 17:23:13 -06:00
|
|
|
m_AIplaying = false;
|
2010-09-28 15:43:38 -06:00
|
|
|
if (numLeft < MAX_SAMPLES/4)//low watermark
|
2010-10-01 17:23:13 -06:00
|
|
|
m_AIplaying = false;
|
2010-09-28 15:43:38 -06:00
|
|
|
} else {
|
|
|
|
if (numLeft > MAX_SAMPLES/2)//high watermark
|
2010-10-01 17:23:13 -06:00
|
|
|
m_AIplaying = true;
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
2010-09-28 15:43:38 -06:00
|
|
|
|
2010-10-01 17:23:13 -06:00
|
|
|
if (m_AIplaying) {
|
2010-09-28 15:43:38 -06:00
|
|
|
numLeft = (numLeft > numSamples) ? numSamples : numLeft;
|
|
|
|
|
2011-05-05 08:53:00 -06:00
|
|
|
if (AudioInterface::GetAIDSampleRate() == m_sampleRate) // (1:1)
|
2010-09-28 15:43:38 -06:00
|
|
|
{
|
2011-01-12 00:08:35 -07:00
|
|
|
#if _M_SSE >= 0x301
|
|
|
|
if (cpu_info.bSSSE3 && !((numLeft * 2) % 8))
|
|
|
|
{
|
2011-03-12 15:02:46 -07:00
|
|
|
static const __m128i sr_mask =
|
|
|
|
_mm_set_epi32(0x0C0D0E0FL, 0x08090A0BL,
|
2013-03-19 19:51:12 -06:00
|
|
|
0x04050607L, 0x00010203L);
|
2011-03-12 15:02:46 -07:00
|
|
|
|
2011-01-12 00:08:35 -07:00
|
|
|
for (unsigned int i = 0; i < numLeft * 2; i += 8)
|
|
|
|
{
|
|
|
|
_mm_storeu_si128((__m128i *)&samples[i], _mm_shuffle_epi8(_mm_loadu_si128((__m128i *)&m_buffer[(m_indexR + i) & INDEX_MASK]), sr_mask));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2011-01-12 14:21:15 -07:00
|
|
|
for (unsigned int i = 0; i < numLeft * 2; i+=2)
|
2011-01-12 00:08:35 -07:00
|
|
|
{
|
2011-01-12 14:21:15 -07:00
|
|
|
samples[i] = Common::swap16(m_buffer[(m_indexR + i + 1) & INDEX_MASK]);
|
|
|
|
samples[i+1] = Common::swap16(m_buffer[(m_indexR + i) & INDEX_MASK]);
|
2011-01-12 00:08:35 -07:00
|
|
|
}
|
|
|
|
}
|
2010-09-28 15:43:38 -06:00
|
|
|
m_indexR += numLeft * 2;
|
|
|
|
}
|
2010-11-11 20:39:07 -07:00
|
|
|
else //linear interpolation
|
2010-09-28 15:43:38 -06:00
|
|
|
{
|
2010-11-11 20:39:07 -07:00
|
|
|
//render numleft sample pairs to samples[]
|
|
|
|
//advance m_indexR with sample position
|
|
|
|
//remember fractional offset
|
|
|
|
|
|
|
|
static u32 frac = 0;
|
2011-05-05 08:53:00 -06:00
|
|
|
const u32 ratio = (u32)( 65536.0f * (float)AudioInterface::GetAIDSampleRate() / (float)m_sampleRate );
|
2010-11-11 20:39:07 -07:00
|
|
|
|
|
|
|
for (u32 i = 0; i < numLeft * 2; i+=2) {
|
|
|
|
u32 m_indexR2 = m_indexR + 2; //next sample
|
|
|
|
if ((m_indexR2 & INDEX_MASK) == (m_indexW & INDEX_MASK)) //..if it exists
|
|
|
|
m_indexR2 = m_indexR;
|
|
|
|
|
|
|
|
s16 l1 = Common::swap16(m_buffer[m_indexR & INDEX_MASK]); //current
|
|
|
|
s16 l2 = Common::swap16(m_buffer[m_indexR2 & INDEX_MASK]); //next
|
2013-03-19 19:51:12 -06:00
|
|
|
int sampleL = ((l1 << 16) + (l2 - l1) * (u16)frac) >> 16;
|
|
|
|
samples[i+1] = sampleL;
|
|
|
|
|
2010-11-11 20:39:07 -07:00
|
|
|
s16 r1 = Common::swap16(m_buffer[(m_indexR + 1) & INDEX_MASK]); //current
|
|
|
|
s16 r2 = Common::swap16(m_buffer[(m_indexR2 + 1) & INDEX_MASK]); //next
|
2010-11-11 22:05:27 -07:00
|
|
|
int sampleR = ((r1 << 16) + (r2 - r1) * (u16)frac) >> 16;
|
2011-01-12 14:21:15 -07:00
|
|
|
samples[i] = sampleR;
|
2010-11-11 20:39:07 -07:00
|
|
|
|
|
|
|
frac += ratio;
|
|
|
|
m_indexR += 2 * (u16)(frac >> 16);
|
|
|
|
frac &= 0xffff;
|
|
|
|
}
|
2010-09-28 15:43:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
numLeft = 0;
|
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
|
|
|
|
if (numSamples > numLeft)
|
2011-03-12 15:02:46 -07:00
|
|
|
{
|
|
|
|
unsigned short s[2];
|
|
|
|
s[0] = Common::swap16(m_buffer[(m_indexR - 1) & INDEX_MASK]);
|
|
|
|
s[1] = Common::swap16(m_buffer[(m_indexR - 2) & INDEX_MASK]);
|
|
|
|
for (unsigned int i = numLeft*2; i < numSamples*2; i+=2)
|
|
|
|
*(u32*)(samples+i) = *(u32*)(s);
|
|
|
|
// memset(&samples[numLeft * 2], 0, (numSamples - numLeft) * 4);
|
|
|
|
}
|
2009-12-23 08:34:14 -07:00
|
|
|
|
2011-02-11 11:59:42 -07:00
|
|
|
//when logging, also throttle HLE audio
|
|
|
|
if (m_logAudio) {
|
|
|
|
if (m_AIplaying) {
|
|
|
|
Premix(samples, numLeft);
|
2009-12-23 08:34:14 -07:00
|
|
|
|
2012-04-22 13:02:43 -06:00
|
|
|
AudioInterface::Callback_GetStreaming(samples, numLeft, m_sampleRate);
|
2011-02-11 11:59:42 -07:00
|
|
|
|
|
|
|
g_wave_writer.AddStereoSamples(samples, numLeft);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { //or mix as usual
|
|
|
|
// Add the DSPHLE sound, re-sampling is done inside
|
|
|
|
Premix(samples, numSamples);
|
|
|
|
|
|
|
|
// Add the DTK Music
|
2012-04-22 13:02:43 -06:00
|
|
|
// Re-sampling is done inside
|
|
|
|
AudioInterface::Callback_GetStreaming(samples, numSamples, m_sampleRate);
|
2009-12-23 08:34:14 -07:00
|
|
|
}
|
|
|
|
|
2011-02-11 11:59:42 -07:00
|
|
|
|
2009-12-25 04:59:04 -07:00
|
|
|
Common::AtomicAdd(m_numSamples, -(s32)numLeft);
|
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
|
|
|
{
|
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.
|
2011-01-01 12:55:03 -07:00
|
|
|
while (num_samples + Common::AtomicLoad(m_numSamples) > MAX_SAMPLES)
|
2009-12-23 08:34:14 -07:00
|
|
|
{
|
2011-12-30 21:16:12 -07: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
|
2010-11-11 20:39:07 -07:00
|
|
|
if (num_samples + Common::AtomicLoad(m_numSamples) > MAX_SAMPLES)
|
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
|
|
|
|
int over_bytes = num_samples * 4 - (MAX_SAMPLES * 2 - (m_indexW & INDEX_MASK)) * sizeof(short);
|
|
|
|
if (over_bytes > 0)
|
|
|
|
{
|
|
|
|
memcpy(&m_buffer[m_indexW & INDEX_MASK], samples, num_samples * 4 - over_bytes);
|
|
|
|
memcpy(&m_buffer[0], samples + (num_samples * 4 - over_bytes) / sizeof(short), over_bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&m_buffer[m_indexW & INDEX_MASK], samples, num_samples * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_indexW += num_samples * 2;
|
|
|
|
|
2011-05-05 08:53:00 -06:00
|
|
|
if (AudioInterface::GetAIDSampleRate() == m_sampleRate)
|
2010-11-11 20:39:07 -07:00
|
|
|
Common::AtomicAdd(m_numSamples, num_samples);
|
2011-05-05 08:53:00 -06:00
|
|
|
else if ((AudioInterface::GetAIDSampleRate() == 32000) && (m_sampleRate == 48000))
|
2010-12-29 06:07:00 -07:00
|
|
|
Common::AtomicAdd(m_numSamples, num_samples * 3 / 2);
|
2011-05-05 08:53:00 -06:00
|
|
|
else
|
|
|
|
Common::AtomicAdd(m_numSamples, num_samples * 2 / 3);
|
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
|
|
|
|
2009-12-23 08:34:14 -07:00
|
|
|
unsigned int CMixer::GetNumSamples()
|
2009-12-19 19:23:26 -07:00
|
|
|
{
|
2009-12-23 08:34:14 -07:00
|
|
|
return Common::AtomicLoad(m_numSamples);
|
2009-12-19 19:23:26 -07:00
|
|
|
}
|
|
|
|
|