2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "ApplicationVolumeHandling.h"
11 #include "ServiceBroker.h"
12 #include "application/ApplicationComponents.h"
13 #include "application/ApplicationPlayer.h"
14 #include "cores/AudioEngine/Interfaces/AE.h"
15 #include "dialogs/GUIDialogVolumeBar.h"
16 #include "guilib/GUIComponent.h"
17 #include "guilib/GUIWindowManager.h"
18 #include "interfaces/AnnouncementManager.h"
19 #include "peripherals/Peripherals.h"
20 #include "settings/Settings.h"
21 #include "settings/lib/Setting.h"
22 #include "utils/Variant.h"
23 #include "utils/XMLUtils.h"
27 float CApplicationVolumeHandling::GetVolumePercent() const
29 // converts the hardware volume to a percentage
30 return m_volumeLevel
* 100.0f
;
33 float CApplicationVolumeHandling::GetVolumeRatio() const
38 void CApplicationVolumeHandling::SetHardwareVolume(float hardwareVolume
)
40 m_volumeLevel
= std::clamp(hardwareVolume
, VOLUME_MINIMUM
, VOLUME_MAXIMUM
);
42 IAE
* ae
= CServiceBroker::GetActiveAE();
44 ae
->SetVolume(m_volumeLevel
);
47 void CApplicationVolumeHandling::VolumeChanged()
49 CVariant
data(CVariant::VariantTypeObject
);
50 data
["volume"] = static_cast<int>(std::lroundf(GetVolumePercent()));
51 data
["muted"] = m_muted
;
52 const auto announcementMgr
= CServiceBroker::GetAnnouncementManager();
53 announcementMgr
->Announce(ANNOUNCEMENT::Application
, "OnVolumeChanged", data
);
55 auto& components
= CServiceBroker::GetAppComponents();
56 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
57 // if player has volume control, set it.
60 appPlayer
->SetVolume(m_volumeLevel
);
61 appPlayer
->SetMute(m_muted
);
65 void CApplicationVolumeHandling::ShowVolumeBar(const CAction
* action
)
67 const auto& wm
= CServiceBroker::GetGUI()->GetWindowManager();
68 auto* volumeBar
= wm
.GetWindow
<CGUIDialogVolumeBar
>(WINDOW_DIALOG_VOLUME_BAR
);
69 if (volumeBar
!= nullptr && volumeBar
->IsVolumeBarEnabled())
73 volumeBar
->OnAction(*action
);
77 bool CApplicationVolumeHandling::IsMuted() const
79 if (CServiceBroker::GetPeripherals().IsMuted())
81 IAE
* ae
= CServiceBroker::GetActiveAE();
87 void CApplicationVolumeHandling::ToggleMute(void)
95 void CApplicationVolumeHandling::SetMute(bool mute
)
104 void CApplicationVolumeHandling::Mute()
106 if (CServiceBroker::GetPeripherals().Mute())
109 IAE
* ae
= CServiceBroker::GetActiveAE();
116 void CApplicationVolumeHandling::UnMute()
118 if (CServiceBroker::GetPeripherals().UnMute())
121 IAE
* ae
= CServiceBroker::GetActiveAE();
128 void CApplicationVolumeHandling::SetVolume(float iValue
, bool isPercentage
)
130 float hardwareVolume
= iValue
;
133 hardwareVolume
/= 100.0f
;
135 SetHardwareVolume(hardwareVolume
);
139 void CApplicationVolumeHandling::CacheReplayGainSettings(const CSettings
& settings
)
141 // initialize m_replayGainSettings
142 m_replayGainSettings
.iType
= settings
.GetInt(CSettings::SETTING_MUSICPLAYER_REPLAYGAINTYPE
);
143 m_replayGainSettings
.iPreAmp
= settings
.GetInt(CSettings::SETTING_MUSICPLAYER_REPLAYGAINPREAMP
);
144 m_replayGainSettings
.iNoGainPreAmp
=
145 settings
.GetInt(CSettings::SETTING_MUSICPLAYER_REPLAYGAINNOGAINPREAMP
);
146 m_replayGainSettings
.bAvoidClipping
=
147 settings
.GetBool(CSettings::SETTING_MUSICPLAYER_REPLAYGAINAVOIDCLIPPING
);
150 bool CApplicationVolumeHandling::Load(const TiXmlNode
* settings
)
155 const TiXmlElement
* audioElement
= settings
->FirstChildElement("audio");
158 XMLUtils::GetBoolean(audioElement
, "mute", m_muted
);
159 if (!XMLUtils::GetFloat(audioElement
, "fvolumelevel", m_volumeLevel
, VOLUME_MINIMUM
,
161 m_volumeLevel
= VOLUME_MAXIMUM
;
167 bool CApplicationVolumeHandling::Save(TiXmlNode
* settings
) const
172 TiXmlElement
volumeNode("audio");
173 TiXmlNode
* audioNode
= settings
->InsertEndChild(volumeNode
);
177 XMLUtils::SetBoolean(audioNode
, "mute", m_muted
);
178 XMLUtils::SetFloat(audioNode
, "fvolumelevel", m_volumeLevel
);
183 bool CApplicationVolumeHandling::OnSettingChanged(const CSetting
& setting
)
185 const std::string
& settingId
= setting
.GetId();
187 if (StringUtils::EqualsNoCase(settingId
, CSettings::SETTING_MUSICPLAYER_REPLAYGAINTYPE
))
188 m_replayGainSettings
.iType
= static_cast<const CSettingInt
&>(setting
).GetValue();
189 else if (StringUtils::EqualsNoCase(settingId
, CSettings::SETTING_MUSICPLAYER_REPLAYGAINPREAMP
))
190 m_replayGainSettings
.iPreAmp
= static_cast<const CSettingInt
&>(setting
).GetValue();
191 else if (StringUtils::EqualsNoCase(settingId
,
192 CSettings::SETTING_MUSICPLAYER_REPLAYGAINNOGAINPREAMP
))
193 m_replayGainSettings
.iNoGainPreAmp
= static_cast<const CSettingInt
&>(setting
).GetValue();
194 else if (StringUtils::EqualsNoCase(settingId
,
195 CSettings::SETTING_MUSICPLAYER_REPLAYGAINAVOIDCLIPPING
))
196 m_replayGainSettings
.bAvoidClipping
= static_cast<const CSettingBool
&>(setting
).GetValue();