mirror of
https://github.com/mudita/MuditaOS.git
synced 2026-01-16 10:00:03 -05:00
Set of fixes for Bluetooth volume control issues: * split OS volume from A2DP device volume; * added translations for additional popup required to indicate what volume is being set; * fixed issue that on some devices setting the lowest volume level would set the highest in reality; * fixed Bluetooth auto-turnoff functionality; * minor code cleanup.
70 lines
2.8 KiB
C++
70 lines
2.8 KiB
C++
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
|
|
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
|
|
|
|
#include "VolumeScaler.hpp"
|
|
|
|
namespace audio::volume::scaler
|
|
{
|
|
namespace
|
|
{
|
|
/// @brief Takes volume level from bluetooth profile and converts it to according one for the system.
|
|
/// @param profileVolume - Bluetooth profile volume level.
|
|
/// @param profileMaxVolume - Bluetooth profile max volume level.
|
|
/// @return Volume level scaled to satisfy system's range [audio::minVolume, audio::maxVolume].
|
|
Volume btProfileToSystemVolume(std::uint8_t profileVolume, float profileMaxVolume) noexcept
|
|
{
|
|
const auto systemVolume = (profileVolume / profileMaxVolume) * audio::maxVolume;
|
|
// prevents conversion to 0 while in fact sound is not muted
|
|
if (systemVolume > 0.01f && systemVolume < 1.0f) {
|
|
return 1;
|
|
}
|
|
return systemVolume;
|
|
}
|
|
|
|
/// @brief Takes volume level and converts it to according one for the bluetooth profile.
|
|
/// @param systemVolume - system volume level.
|
|
/// @param profileMaxVolume - bluetooth profile max volume level.
|
|
/// @return Volume level scaled to satisfy bluetooth profile range [0, profileMaxVolume].
|
|
std::uint8_t systemToBtProfileVolume(float systemVolume, std::uint8_t profileMaxVolume) noexcept
|
|
{
|
|
return std::round(systemVolume * profileMaxVolume / audio::maxVolume);
|
|
}
|
|
} // namespace
|
|
namespace a2dp
|
|
{
|
|
constexpr auto avrcpMaxVolume = std::uint8_t{0x7F}; // from AVRCP documentation
|
|
|
|
Volume toSystemVolume(std::uint8_t avrcpVolume) noexcept
|
|
{
|
|
return btProfileToSystemVolume(avrcpVolume, static_cast<float>(avrcpMaxVolume));
|
|
}
|
|
} // namespace a2dp
|
|
namespace hsp
|
|
{
|
|
constexpr auto hspMaxVolume = float{0x0F}; // from HSP documentation
|
|
|
|
Volume toSystemVolume(std::uint8_t hspSpeakerGain) noexcept
|
|
{
|
|
return btProfileToSystemVolume(hspSpeakerGain, static_cast<float>(hspMaxVolume));
|
|
}
|
|
std::uint8_t toHSPGain(float systemVolume) noexcept
|
|
{
|
|
return systemToBtProfileVolume(systemVolume, hspMaxVolume);
|
|
}
|
|
} // namespace hsp
|
|
|
|
namespace hfp
|
|
{
|
|
constexpr auto hfpMaxVolume = float{0x0F}; // from HFP documentation
|
|
|
|
Volume toSystemVolume(std::uint8_t hfpSpeakerGain) noexcept
|
|
{
|
|
return btProfileToSystemVolume(hfpSpeakerGain, static_cast<float>(hfpMaxVolume));
|
|
}
|
|
std::uint8_t toHFPGain(float systemVolume) noexcept
|
|
{
|
|
return systemToBtProfileVolume(systemVolume, hfpMaxVolume);
|
|
}
|
|
} // namespace hfp
|
|
} // namespace audio::volume::scaler
|