Skip to content

Commit

Permalink
AESinkPULSE: Revisit volume
Browse files Browse the repository at this point in the history
  • Loading branch information
fritsch committed Apr 3, 2014
1 parent 1790b03 commit 7fb9594
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions xbmc/cores/AudioEngine/Sinks/AESinkPULSE.cpp
Expand Up @@ -596,7 +596,7 @@ bool CAESinkPULSE::Initialize(AEAudioFormat &format, std::string &device)
m_BytesPerSecond = pa_bytes_per_second(&spec);
unsigned int frameSize = pa_frame_size(&spec);

m_Stream = pa_stream_new_extended(m_Context, "audio stream", info, 1, NULL);
m_Stream = pa_stream_new_extended(m_Context, "xbmc audio stream", info, 1, NULL);
pa_format_info_free(info[0]);

if (m_Stream == NULL)
Expand Down Expand Up @@ -654,26 +654,24 @@ bool CAESinkPULSE::Initialize(AEAudioFormat &format, std::string &device)
//update local volume if we are in non passthrough mode
if (!m_passthrough)
{
unsigned int devidx = pa_stream_get_index(m_Stream);
unsigned int sink_input_idx = pa_stream_get_index(m_Stream);
SinkInputInfoStruct sii;
sii.mainloop = m_MainLoop;
bool success = WaitForOperation(pa_context_get_sink_input_info(m_Context, devidx, SinkInputInfoCallback, &sii), m_MainLoop, "Get Sink Input Info");
bool success = WaitForOperation(pa_context_get_sink_input_info(m_Context, sink_input_idx, SinkInputInfoCallback, &sii), m_MainLoop, "Get Sink Input Info");
if(success && sii.is_valid)
{
// we don't have per channel values so average them
// we don't have per channel values so use avg of them
pa_volume_t p_vol = pa_cvolume_avg(&sii.volume);
// store it internally
m_Volume = sii.volume;
float sValue = std::min(p_vol / (float) PA_VOLUME_NORM, 1.0f);
float sValue = (float) pa_sw_volume_to_linear(p_vol);
CLog::Log(LOGDEBUG, "Restored Stream value to %f", sValue);
sValue = std::min(sValue, 1.0f);
g_application.SetVolume(sValue, false);
if (sii.mute && sValue > 0)
{
CLog::Log(LOGDEBUG, "PulseAudio: Stream is muted - perhaps was a user wish - if volume is changed we unmute");
}
}

}
}

const pa_buffer_attr *a;
Expand Down Expand Up @@ -814,20 +812,23 @@ void CAESinkPULSE::SetVolume(float volume)
{
if (m_IsAllocated && !m_passthrough)
{
// undo internal Engine pseudo gain calculation
float per_cent_volume = CAEUtil::GainToPercent(CAEUtil::ScaleToGain(volume));

pa_threaded_mainloop_lock(m_MainLoop);
bool external_change = false;
//check if internal volume does not match sink volume
unsigned int devidx = pa_stream_get_index(m_Stream);
//check if internal volume and sink input volume do not match
unsigned int sink_input_idx = pa_stream_get_index(m_Stream);
SinkInputInfoStruct sii;
sii.mainloop = m_MainLoop;
bool success = WaitForOperation(pa_context_get_sink_input_info(m_Context, devidx, SinkInputInfoCallback, &sii), m_MainLoop, "Get Sink Input Info");
bool success = WaitForOperation(pa_context_get_sink_input_info(m_Context, sink_input_idx, SinkInputInfoCallback, &sii), m_MainLoop, "Get Sink Input Info");
float sValue = 0.0f;
if(success && sii.is_valid)
{
// we don't have per channel values so average them
// we don't have per channel values so use avg of them
pa_volume_t n_vol = pa_cvolume_avg(&sii.volume);
pa_volume_t o_vol = pa_cvolume_avg(&m_Volume);
sValue = std::min(n_vol / (float) PA_VOLUME_NORM, 1.0f);
sValue = (float) pa_sw_volume_to_linear(n_vol);
if (n_vol != o_vol)
{
external_change = true;
Expand All @@ -836,24 +837,28 @@ void CAESinkPULSE::SetVolume(float volume)
CLog::Log(LOGDEBUG, "Restored Volume cause of external change to value to %f", sValue);
g_application.SetVolume(sValue, false);
}
// unmute if we should not be muted
if (sii.mute && sValue > 0)
{
pa_operation *op = pa_context_set_sink_input_mute(m_Context, sii.index, 0, NULL, NULL);
if (op == NULL)
CLog::Log(LOGERROR, "PulseAudio: Failed to unmute the stream");
else
pa_operation_unref(op);
}
}
// unmute if we should not be muted
if (sii.mute && sValue > 0)
else // we don't know stream volume so don't change anything
{
pa_operation *op = pa_context_set_sink_input_mute(m_Context, sii.index, 0, NULL, NULL);
if (op == NULL)
CLog::Log(LOGERROR, "PulseAudio: Failed to unmute the stream");
else
pa_operation_unref(op);
external_change = true;
}
if (!external_change)
{
pa_volume_t pavolume = pa_sw_volume_from_linear(volume);
pa_volume_t pavolume = pa_sw_volume_from_linear(per_cent_volume);
if ( pavolume <= 0 )
pa_cvolume_mute(&m_Volume, m_Channels);
else
pa_cvolume_set(&m_Volume, m_Channels, pavolume);
pa_operation *op = pa_context_set_sink_input_volume(m_Context, devidx, &m_Volume, NULL, NULL);
pa_operation *op = pa_context_set_sink_input_volume(m_Context, sink_input_idx, &m_Volume, NULL, NULL);
if (op == NULL)
CLog::Log(LOGERROR, "PulseAudio: Failed to set volume");
else
Expand Down

0 comments on commit 7fb9594

Please sign in to comment.