Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[AE/Pulseaudio] add CAEUtil::PercentToPulseVolume to convert hardware…

… volume [0,1] to pulseaudio volume [0,PA_VOLUME_NORM]. This fixes volume range mismatch between AE [-60,0]dB and pulseaudio (-inf,0]dB. This should work with any version of Puleaudio as long as PA_VOLUME_NORM is correctly defined.
  • Loading branch information...
commit e9346f23d4df60648e581d23292d1528e586e1b3 1 parent 275ec26
s7mx1 authored
3  xbmc/cores/AudioEngine/Engines/PulseAE/PulseAESound.cpp
@@ -23,6 +23,7 @@
23 23
24 24 #include "PulseAESound.h"
25 25 #include "AEFactory.h"
  26 +#include "Utils/AEUtil.h"
26 27 #include "utils/log.h"
27 28 #include "MathUtils.h"
28 29 #include "StringUtils.h"
@@ -81,7 +82,7 @@ bool CPulseAESound::Initialize()
81 82
82 83 m_maxVolume = CAEFactory::GetEngine()->GetVolume();
83 84 m_volume = 1.0f;
84   - pa_volume_t paVolume = pa_sw_volume_from_linear((double)(m_volume * m_maxVolume));
  85 + pa_volume_t paVolume = CAEUtil::PercentToPulseVolume((double)(m_volume * m_maxVolume));
85 86 pa_cvolume_set(&m_chVolume, m_sampleSpec.channels, paVolume);
86 87
87 88 pa_threaded_mainloop_lock(m_mainLoop);
5 xbmc/cores/AudioEngine/Engines/PulseAE/PulseAEStream.cpp
@@ -24,6 +24,7 @@
24 24 #include "PulseAEStream.h"
25 25 #include "AEFactory.h"
26 26 #include "Utils/AEUtil.h"
  27 +#include "Utils/AEUtil.h"
27 28 #include "utils/log.h"
28 29 #include "utils/MathUtils.h"
29 30 #include "threads/SingleLock.h"
@@ -138,7 +139,7 @@ CPulseAEStream::CPulseAEStream(pa_context *context, pa_threaded_mainloop *mainLo
138 139
139 140 m_MaxVolume = CAEFactory::GetEngine()->GetVolume();
140 141 m_Volume = 1.0f;
141   - pa_volume_t paVolume = pa_sw_volume_from_linear((double)(m_Volume * m_MaxVolume));
  142 + pa_volume_t paVolume = CAEUtil::PercentToPulseVolume((double)(m_Volume * m_MaxVolume));
142 143 pa_cvolume_set(&m_ChVolume, m_SampleSpec.channels, paVolume);
143 144
144 145 #if PA_CHECK_VERSION(1,0,0)
@@ -419,7 +420,7 @@ void CPulseAEStream::SetVolume(float volume)
419 420 if (volume > 0.f)
420 421 {
421 422 m_Volume = volume;
422   - pa_volume_t paVolume = pa_sw_volume_from_linear((double)(m_Volume * m_MaxVolume));
  423 + pa_volume_t paVolume = CAEUtil::PercentToPulseVolume((double)(m_Volume * m_MaxVolume));
423 424
424 425 pa_cvolume_set(&m_ChVolume, m_SampleSpec.channels, paVolume);
425 426 }
24 xbmc/cores/AudioEngine/Utils/AEUtil.h
@@ -24,6 +24,10 @@
24 24 #include "PlatformDefs.h"
25 25 #include <math.h>
26 26
  27 +#if defined(HAS_PULSEAUDIO)
  28 +#include <pulse/pulseaudio.h>
  29 +#endif
  30 +
27 31 #ifdef TARGET_WINDOWS
28 32 #if _M_IX86_FP>0 && !defined(__SSE__)
29 33 #define __SSE__
@@ -81,6 +85,26 @@ class CAEUtil
81 85 {
82 86 return pow(10.0f, dB/20);
83 87 }
  88 +
  89 + #if defined(HAS_PULSEAUDIO)
  90 + /*
  91 + Hardware volume [0,1] will be converted to dB using PercentToGain() and then converted to scale by
  92 + GainToScale() before reaching PulseAudio. However the underlying math assumes 60dB volume range which
  93 + contradicts (-inf,0] volume range used by pulseaudio 2.0 and above. To fix this we will reverse the
  94 + process of GainToScale() and PercentToGain() to get hardware volume then using our own linear converter to
  95 + get valid pulseaudio volume [0,PA_VOLUME_NORM].
  96 + */
  97 + static inline const int PercentToPulseVolume(const float value)
  98 + {
  99 + float hardware_volume = 0.f;
  100 + // This is to reverse "pow(10.0f, dB/20)" in GainToScale() and "(value - 1)*db_range" in PercentToGain()
  101 + if ( value > 0.f )
  102 + hardware_volume = (double)( (20 * log10(value)) / 60 + 1 );
  103 + // This is to simulate pa_sw_volume_from_linear() which convert a linear hardware volume rage [0,1]
  104 + // to pulseaudio volume rage [0, PA_VOLUME_NORM]
  105 + return (int)( hardware_volume * PA_VOLUME_NORM );
  106 + }
  107 + #endif
84 108
85 109 #ifdef __SSE__
86 110 static void SSEMulArray (float *data, const float mul, uint32_t count);

0 comments on commit e9346f2

Please sign in to comment.
Something went wrong with that request. Please try again.