Skip to content

Commit

Permalink
AESinkAudioTrack: Reconsider samplerate matching
Browse files Browse the repository at this point in the history
  • Loading branch information
fritsch committed Oct 30, 2016
1 parent e75604c commit 7bada62
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions xbmc/cores/AudioEngine/Sinks/AESinkAUDIOTRACK.cpp
Expand Up @@ -245,12 +245,19 @@ bool CAESinkAUDIOTRACK::Initialize(AEAudioFormat &format, std::string &device)
int stream = CJNIAudioManager::STREAM_MUSIC;
m_encoding = CJNIAudioFormat::ENCODING_PCM_16BIT;

// Get equal or lower supported sample rate
std::set<unsigned int>::iterator s = m_sink_sampleRates.upper_bound(m_format.m_sampleRate);
if (--s != m_sink_sampleRates.begin())
m_sink_sampleRate = *s;
else
m_sink_sampleRate = CJNIAudioTrack::getNativeOutputSampleRate(CJNIAudioManager::STREAM_MUSIC);
uint32_t distance = 192000; // max upper distance
for (auto& s : m_sink_sampleRates)
{
// prefer best match or alternatively something that divides nicely and
// is not too far away
uint32_t d = std::abs((int)m_format.m_sampleRate - (int)s) + 8 * (s > m_format.m_sampleRate ? (s % m_format.m_sampleRate) : (m_format.m_sampleRate % s));
if (d < distance)
{
m_sink_sampleRate = s;
distance = d;
CLog::Log(LOGDEBUG, "Updated SampleRate: %u Distance: %u", m_sink_sampleRate, d);
}
}

if (m_format.m_dataFormat == AE_FMT_RAW && !CXBMCApp::IsHeadsetPlugged())
{
Expand Down

0 comments on commit 7bada62

Please sign in to comment.