~aleteoryx/muditaos

ref: sign_test muditaos/module-audio/Audio/VolumeScaler.cpp -rw-r--r-- 2.8 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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