~aleteoryx/muditaos

ref: a405cad694b867fcd2498984830bd97d4b9bde2f muditaos/module-audio/Audio/Profiles/Profile.cpp -rw-r--r-- 4.9 KiB
a405cad6Aleteoryx trim readme 7 days 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "Profile.hpp"

#include "ProfileIdle.hpp"

#include "ProfileRecordingOnBoardMic.hpp"
#include "ProfileRecordingHeadphones.hpp"
#include "ProfileRecordingBluetoothHSP.hpp"

#include "ProfilePlaybackHeadphones.hpp"
#include "ProfilePlaybackLoudspeaker.hpp"

#include "ProfileRoutingEarspeaker.hpp"
#include "ProfileRoutingHeadphones.hpp"
#include "ProfileRoutingLoudspeaker.hpp"
#include "ProfileRoutingBluetoothHSP.hpp"

#include "ProfilePlaybackBluetoothA2DP.hpp"
#include "ProfileRoutingBluetoothHFP.hpp"
#include "ProfileRecordingBluetoothHFP.hpp"

#include "ProfileConfigUtils.hpp"
#include <Utils.hpp>

namespace audio
{

    std::unique_ptr<Profile> Profile::Create(const Type t, std::optional<Volume> vol, std::optional<Gain> gain)
    {
        std::unique_ptr<Profile> inst;

        switch (t) {
        case Type::PlaybackHeadphones:
            assert(vol);
            inst = std::make_unique<ProfilePlaybackHeadphones>(vol.value());
            break;
        case Type::PlaybackLoudspeaker:
            assert(vol);
            inst = std::make_unique<ProfilePlaybackLoudspeaker>(vol.value());
            break;
        case Type::PlaybackBluetoothA2DP:
            assert(vol);
            inst = std::make_unique<ProfilePlaybackBluetoothA2DP>(vol.value());
            break;
        case Type::RecordingBuiltInMic:
            assert(gain);
            inst = std::make_unique<ProfileRecordingOnBoardMic>(gain.value());
            break;
        case Type::RecordingHeadphones:
            assert(gain);
            inst = std::make_unique<ProfileRecordingHeadphones>(gain.value());
            break;
        case Type::RecordingBluetoothHSP:
            assert(gain);
            inst = std::make_unique<ProfileRecordingBluetoothHSP>(gain.value());
            break;
        case Type::RecordingBluetoothHFP:
            assert(gain);
            inst = std::make_unique<ProfileRecordingBluetoothHFP>(gain.value());
            break;
        case Type::RoutingHeadphones:
            assert(gain && vol);
            inst = std::make_unique<ProfileRoutingHeadphones>(vol.value(), gain.value());
            break;
        case Type::RoutingLoudspeaker:
            assert(gain && vol);
            inst = std::make_unique<ProfileRoutingLoudspeaker>(vol.value(), gain.value());
            break;
        case Type::RoutingEarspeaker:
            assert(gain && vol);
            inst = std::make_unique<ProfileRoutingEarspeaker>(vol.value(), gain.value());
            break;
        case Type::RoutingBluetoothHSP:
            assert(gain && vol);
            inst = std::make_unique<ProfileRoutingBluetoothHSP>(vol.value(), gain.value());
            break;
        case Type::RoutingBluetoothHFP:
            assert(gain && vol);
            inst = std::make_unique<ProfileRoutingBluetoothHFP>(vol.value(), gain.value());
            break;
        case Type::Idle:
            inst = std::make_unique<ProfileIdle>();
            break;
        }

        return inst;
    }

    Profile::Profile(const std::string &name,
                     const Type type,
                     const audio::codec::Configuration &fmt,
                     AudioDevice::Type devType)
        : audioConfiguration(fmt), audioDeviceType(devType), name(name), type(type)
    {}

    Profile::Profile(const std::string &name,
                     const Type type,
                     const std::filesystem::path &configurationPath,
                     const audio::codec::Configuration &fallbackConfig,
                     AudioDevice::Type devType)
        : audioDeviceType(devType), name(name), type(type)
    {
        try {
            audioConfiguration = loadConfigurationFromFile(configurationPath);
        }
        catch (std::invalid_argument &e) {
            LOG_ERROR("Failed loading the profile configuration from file, using fallback! Cause: %s", e.what());
            audioConfiguration = fallbackConfig;
        }
    }

    void Profile::SetInputGain(Gain gain)
    {
        audioConfiguration.inputGain = gain;
    }

    void Profile::SetOutputVolume(Volume vol)
    {
        audioConfiguration.outputVolume = vol;
    }

    void Profile::SetInputPath(audio::codec::InputPath path)
    {
        audioConfiguration.inputPath = path;
    }

    void Profile::SetOutputPath(audio::codec::OutputPath path)
    {
        audioConfiguration.outputPath = path;
    }

    void Profile::SetInOutFlags(uint32_t flags)
    {
        audioConfiguration.flags = flags;
    }

    void Profile::SetSampleRate(uint32_t samplerate)
    {
        audioConfiguration.sampleRate_Hz = samplerate;
    }

    const std::string str(const Profile::Type &profileType)
    {
        if (profileType == Profile::Type::Idle) {
            return "";
        }
        return utils::enumToString(profileType);
    }

} // namespace audio