[ExecString] combine SplitParameters with identical function of CUtil
[xbmc.git] / xbmc / application / ApplicationVolumeHandling.cpp
blob25229be7da42dd0126fe5a8f6768a0270947ea62
1 /*
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.
7 */
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"
25 #include <tinyxml.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
35 return m_volumeLevel;
38 void CApplicationVolumeHandling::SetHardwareVolume(float hardwareVolume)
40 m_volumeLevel = std::clamp(hardwareVolume, VOLUME_MINIMUM, VOLUME_MAXIMUM);
42 IAE* ae = CServiceBroker::GetActiveAE();
43 if (ae)
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.
58 if (appPlayer)
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())
71 volumeBar->Open();
72 if (action)
73 volumeBar->OnAction(*action);
77 bool CApplicationVolumeHandling::IsMuted() const
79 if (CServiceBroker::GetPeripherals().IsMuted())
80 return true;
81 IAE* ae = CServiceBroker::GetActiveAE();
82 if (ae)
83 return ae->IsMuted();
84 return true;
87 void CApplicationVolumeHandling::ToggleMute(void)
89 if (m_muted)
90 UnMute();
91 else
92 Mute();
95 void CApplicationVolumeHandling::SetMute(bool mute)
97 if (m_muted != mute)
99 ToggleMute();
100 m_muted = mute;
104 void CApplicationVolumeHandling::Mute()
106 if (CServiceBroker::GetPeripherals().Mute())
107 return;
109 IAE* ae = CServiceBroker::GetActiveAE();
110 if (ae)
111 ae->SetMute(true);
112 m_muted = true;
113 VolumeChanged();
116 void CApplicationVolumeHandling::UnMute()
118 if (CServiceBroker::GetPeripherals().UnMute())
119 return;
121 IAE* ae = CServiceBroker::GetActiveAE();
122 if (ae)
123 ae->SetMute(false);
124 m_muted = false;
125 VolumeChanged();
128 void CApplicationVolumeHandling::SetVolume(float iValue, bool isPercentage)
130 float hardwareVolume = iValue;
132 if (isPercentage)
133 hardwareVolume /= 100.0f;
135 SetHardwareVolume(hardwareVolume);
136 VolumeChanged();
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)
152 if (!settings)
153 return false;
155 const TiXmlElement* audioElement = settings->FirstChildElement("audio");
156 if (audioElement)
158 XMLUtils::GetBoolean(audioElement, "mute", m_muted);
159 if (!XMLUtils::GetFloat(audioElement, "fvolumelevel", m_volumeLevel, VOLUME_MINIMUM,
160 VOLUME_MAXIMUM))
161 m_volumeLevel = VOLUME_MAXIMUM;
164 return true;
167 bool CApplicationVolumeHandling::Save(TiXmlNode* settings) const
169 if (!settings)
170 return false;
172 TiXmlElement volumeNode("audio");
173 TiXmlNode* audioNode = settings->InsertEndChild(volumeNode);
174 if (!audioNode)
175 return false;
177 XMLUtils::SetBoolean(audioNode, "mute", m_muted);
178 XMLUtils::SetFloat(audioNode, "fvolumelevel", m_volumeLevel);
180 return true;
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();
197 else
198 return false;
200 return true;