| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2009-01-17 14:28:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "AudioCommon/AudioCommon.h"
 | 
					
						
							|  |  |  | #include "AudioCommon/Mixer.h"
 | 
					
						
							|  |  |  | #include "Common/Atomic.h"
 | 
					
						
							|  |  |  | #include "Common/CPUDetect.h"
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | #include "Common/MathUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/ConfigManager.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-16 23:13:01 +01:00
										 |  |  | #include "Core/Core.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/HW/AudioInterface.h"
 | 
					
						
							|  |  |  | #include "Core/HW/VideoInterface.h"
 | 
					
						
							| 
									
										
										
										
											2011-01-28 18:39:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // UGLINESS
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/PowerPC/PowerPC.h"
 | 
					
						
							| 
									
										
										
										
											2011-01-28 18:39:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-12 09:34:53 +00:00
										 |  |  | #if _M_SSE >= 0x301 && !(defined __GNUC__ && !defined __SSSE3__)
 | 
					
						
							|  |  |  | #include <tmmintrin.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | // Executed from sound stream thread
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, bool consider_framelimit) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 	unsigned int currentSample = 0; | 
					
						
							| 
									
										
										
										
											2010-09-28 21:43:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02: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-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-14 16:08:21 -07:00
										 |  |  | 	float numLeft = (float)(((indexW - indexR) & INDEX_MASK) / 2); | 
					
						
							| 
									
										
										
										
											2014-02-06 13:03:40 +01:00
										 |  |  | 	m_numLeftI = (numLeft + m_numLeftI*(CONTROL_AVG-1)) / CONTROL_AVG; | 
					
						
							|  |  |  | 	float offset = (m_numLeftI - LOW_WATERMARK) * CONTROL_FACTOR; | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	if (offset > MAX_FREQ_SHIFT) offset = MAX_FREQ_SHIFT; | 
					
						
							|  |  |  | 	if (offset < -MAX_FREQ_SHIFT) offset = -MAX_FREQ_SHIFT; | 
					
						
							| 
									
										
										
										
											2014-02-06 13:03:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 	//render numleft sample pairs to samples[]
 | 
					
						
							|  |  |  | 	//advance indexR with sample position
 | 
					
						
							|  |  |  | 	//remember fractional offset
 | 
					
						
							| 
									
										
										
										
											2014-02-06 06:38:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-11 14:53:53 +01:00
										 |  |  | 	u32 framelimit = SConfig::GetInstance().m_Framelimit; | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 	float aid_sample_rate = m_input_sample_rate + offset; | 
					
						
							| 
									
										
										
										
											2014-07-24 08:16:17 -04:00
										 |  |  | 	if (consider_framelimit && framelimit > 1) | 
					
						
							| 
									
										
										
										
											2014-02-11 14:53:53 +01:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		aid_sample_rate = aid_sample_rate * (framelimit - 1) * 5 / VideoInterface::TargetRefreshRate; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 	const u32 ratio = (u32)( 65536.0f * aid_sample_rate / (float)m_mixer->m_sampleRate ); | 
					
						
							| 
									
										
										
										
											2014-02-06 06:38:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | 	s32 lvolume = m_LVolume; | 
					
						
							|  |  |  | 	s32 rvolume = m_RVolume; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-12 23:34:53 -07:00
										 |  |  | 	// TODO: consider a higher-quality resampling algorithm.
 | 
					
						
							| 
									
										
										
										
											2014-08-30 16:29:15 -04:00
										 |  |  | 	for (; currentSample < numSamples*2 && ((indexW-indexR) & INDEX_MASK) > 2; currentSample+=2) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 		u32 indexR2 = indexR + 2; //next sample
 | 
					
						
							| 
									
										
										
										
											2014-02-06 06:38:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 		s16 l1 = Common::swap16(m_buffer[indexR & INDEX_MASK]); //current
 | 
					
						
							|  |  |  | 		s16 l2 = Common::swap16(m_buffer[indexR2 & INDEX_MASK]); //next
 | 
					
						
							| 
									
										
										
										
											2014-07-17 18:26:21 +12:00
										 |  |  | 		int sampleL = ((l1 << 16) + (l2 - l1) * (u16)m_frac)  >> 16; | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | 		sampleL = (sampleL * lvolume) >> 8; | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 		sampleL += samples[currentSample + 1]; | 
					
						
							|  |  |  | 		MathUtil::Clamp(&sampleL, -32767, 32767); | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 		samples[currentSample+1] = sampleL; | 
					
						
							| 
									
										
										
										
											2014-02-06 06:38:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 		s16 r1 = Common::swap16(m_buffer[(indexR + 1) & INDEX_MASK]); //current
 | 
					
						
							|  |  |  | 		s16 r2 = Common::swap16(m_buffer[(indexR2 + 1) & INDEX_MASK]); //next
 | 
					
						
							| 
									
										
										
										
											2014-07-17 18:26:21 +12:00
										 |  |  | 		int sampleR = ((r1 << 16) + (r2 - r1) * (u16)m_frac)  >> 16; | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | 		sampleR = (sampleR * rvolume) >> 8; | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 		sampleR += samples[currentSample]; | 
					
						
							|  |  |  | 		MathUtil::Clamp(&sampleR, -32767, 32767); | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 		samples[currentSample] = sampleR; | 
					
						
							| 
									
										
										
										
											2010-09-28 21:43:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-17 18:26:21 +12:00
										 |  |  | 		m_frac += ratio; | 
					
						
							|  |  |  | 		indexR += 2 * (u16)(m_frac >> 16); | 
					
						
							|  |  |  | 		m_frac &= 0xffff; | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-12-22 07:26:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	// Padding
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 	short s[2]; | 
					
						
							| 
									
										
										
										
											2014-02-06 06:55:45 +01:00
										 |  |  | 	s[0] = Common::swap16(m_buffer[(indexR - 1) & INDEX_MASK]); | 
					
						
							|  |  |  | 	s[1] = Common::swap16(m_buffer[(indexR - 2) & INDEX_MASK]); | 
					
						
							| 
									
										
										
										
											2014-07-17 18:41:18 +12:00
										 |  |  | 	s[0] = (s[0] * rvolume) >> 8; | 
					
						
							|  |  |  | 	s[1] = (s[1] * lvolume) >> 8; | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | 	for (; currentSample < numSamples * 2; currentSample += 2) | 
					
						
							| 
									
										
										
										
											2011-03-12 22:02:46 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 		int sampleR = s[0] + samples[currentSample]; | 
					
						
							|  |  |  | 		MathUtil::Clamp(&sampleR, -32767, 32767); | 
					
						
							|  |  |  | 		samples[currentSample] = sampleR; | 
					
						
							|  |  |  | 		int sampleL = s[1] + samples[currentSample + 1]; | 
					
						
							|  |  |  | 		MathUtil::Clamp(&sampleL, -32767, 32767); | 
					
						
							|  |  |  | 		samples[currentSample + 1] = sampleL; | 
					
						
							| 
									
										
										
										
											2011-03-12 22:02:46 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Flush cached variable
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02:00
										 |  |  | 	Common::AtomicStore(m_indexR, indexR); | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return numSamples; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | unsigned int CMixer::Mix(short* samples, unsigned int num_samples, bool consider_framelimit) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!samples) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 	std::lock_guard<std::mutex> lk(m_csMixing); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memset(samples, 0, num_samples * 2 * sizeof(short)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-07 05:30:06 +02:00
										 |  |  | 	if (PowerPC::GetState() != PowerPC::CPU_RUNNING) | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Silence
 | 
					
						
							|  |  |  | 		return num_samples; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_dma_mixer.Mix(samples, num_samples, consider_framelimit); | 
					
						
							|  |  |  | 	m_streaming_mixer.Mix(samples, num_samples, consider_framelimit); | 
					
						
							| 
									
										
										
										
											2014-09-05 22:32:48 +10:00
										 |  |  | 	m_wiimote_speaker_mixer.Mix(samples, num_samples, consider_framelimit); | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | 	if (m_logAudio) | 
					
						
							|  |  |  | 		g_wave_writer.AddStereoSamples(samples, num_samples); | 
					
						
							|  |  |  | 	return num_samples; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CMixer::MixerFifo::PushSamples(const short *samples, unsigned int num_samples) | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02: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-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	// Check if we have enough free space
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02: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 15:34:14 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// AyuanX: Actual re-sampling work has been moved to sound thread
 | 
					
						
							| 
									
										
										
										
											2009-12-25 11:59:04 +00:00
										 |  |  | 	// to alleviate the workload on main thread
 | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	// and we simply store raw data here to make fast mem copy
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02:00
										 |  |  | 	int over_bytes = num_samples * 4 - (MAX_SAMPLES * 2 - (indexW & INDEX_MASK)) * sizeof(short); | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	if (over_bytes > 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02:00
										 |  |  | 		memcpy(&m_buffer[indexW & INDEX_MASK], samples, num_samples * 4 - over_bytes); | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 		memcpy(&m_buffer[0], samples + (num_samples * 4 - over_bytes) / sizeof(short), over_bytes); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02:00
										 |  |  | 		memcpy(&m_buffer[indexW & INDEX_MASK], samples, num_samples * 4); | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-11 21:22:38 +02:00
										 |  |  | 	Common::AtomicAdd(m_indexW, num_samples * 2); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-23 15:34:14 +00:00
										 |  |  | 	return; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-20 02:23:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 18:28:19 -07:00
										 |  |  | void CMixer::PushSamples(const short *samples, unsigned int num_samples) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_dma_mixer.PushSamples(samples, num_samples); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CMixer::PushStreamingSamples(const short *samples, unsigned int num_samples) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_streaming_mixer.PushSamples(samples, num_samples); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 01:23:54 +10:00
										 |  |  | void CMixer::PushWiimoteSpeakerSamples(const short *samples, unsigned int num_samples, unsigned int sample_rate) | 
					
						
							| 
									
										
										
										
											2014-09-05 22:32:48 +10:00
										 |  |  | { | 
					
						
							|  |  |  | 	short samples_stereo[MAX_SAMPLES * 2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (num_samples < MAX_SAMPLES) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_wiimote_speaker_mixer.SetInputSampleRate(sample_rate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (unsigned int i = 0; i < num_samples; ++i) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-09-06 01:23:54 +10:00
										 |  |  | 			samples_stereo[i * 2] = Common::swap16(samples[i]); | 
					
						
							|  |  |  | 			samples_stereo[i * 2 + 1] = Common::swap16(samples[i]); | 
					
						
							| 
									
										
										
										
											2014-09-05 22:32:48 +10:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		m_wiimote_speaker_mixer.PushSamples(samples_stereo, num_samples); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 11:20:19 +08:00
										 |  |  | void CMixer::SetDMAInputSampleRate(unsigned int rate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_dma_mixer.SetInputSampleRate(rate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CMixer::SetStreamInputSampleRate(unsigned int rate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_streaming_mixer.SetInputSampleRate(rate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | void CMixer::SetStreamingVolume(unsigned int lvolume, unsigned int rvolume) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_streaming_mixer.SetVolume(lvolume, rvolume); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 22:32:48 +10:00
										 |  |  | void CMixer::SetWiimoteSpeakerVolume(unsigned int lvolume, unsigned int rvolume) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_wiimote_speaker_mixer.SetVolume(lvolume, rvolume); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 11:20:19 +08:00
										 |  |  | void CMixer::MixerFifo::SetInputSampleRate(unsigned int rate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_input_sample_rate = rate; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-26 16:15:18 -07:00
										 |  |  | void CMixer::MixerFifo::SetVolume(unsigned int lvolume, unsigned int rvolume) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_LVolume = lvolume + (lvolume >> 7); | 
					
						
							|  |  |  | 	m_RVolume = rvolume + (rvolume >> 7); | 
					
						
							|  |  |  | } |