~aleteoryx/muditaos

ref: ad058041a5c206d05d957c6334acb5a62af6b68c muditaos/module-audio/Audio/Audio.hpp -rw-r--r-- 3.8 KiB
ad058041 — Lucjan Bryndza [EGD-7770] Fix hardfault with FileIndexer 4 years 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <service-bluetooth/ServiceBluetoothCommon.hpp>

#include "AudioCommon.hpp"
#include "decoder/Decoder.hpp"
#include "Operation/Operation.hpp"

#include <bitset>
#include <functional>
#include <memory>
#include <optional>

namespace audio
{

    class Audio
    {
        enum class Muted : bool
        {
            True,
            False
        };

      public:
        enum class State
        {
            Idle,
            Playback,
            Recording,
            Routing,
        };

        Audio(AudioServiceMessage::Callback callback);

        virtual ~Audio() = default;

        // Events
        audio::RetCode SendEvent(std::shared_ptr<Event> evt);

        // utilities
        Position GetPosition();

        virtual State GetCurrentState() const
        {
            return currentState;
        }

        // Range 0-1
        audio::RetCode SetOutputVolume(Volume vol);

        // Range 0-10
        audio::RetCode SetInputGain(Gain gain);

        Volume GetOutputVolume()
        {
            return currentOperation->GetOutputVolume();
        }

        Gain GetInputGain()
        {
            return currentOperation->GetInputGain();
        }

        [[nodiscard]] auto IsMuted() const noexcept
        {
            return muted == Muted::True;
        }

        const Operation &GetCurrentOperation() const
        {
            // currentOperation always exists - null pattern design
            return *(currentOperation.get());
        }

        virtual audio::PlaybackType GetCurrentOperationPlaybackType() const
        {
            return GetCurrentOperation().GetPlaybackType();
        }

        virtual Operation::State GetCurrentOperationState() const
        {
            return GetCurrentOperation().GetState();
        }

        audio::Profile::Type GetPriorityPlaybackProfile() const
        {
            if (audioSinkState.isConnected(EventType::JackState)) {
                return Profile::Type::PlaybackHeadphones;
            }
            if (audioSinkState.isConnected(EventType::BlutoothA2DPDeviceState)) {
                return Profile::Type::PlaybackBluetoothA2DP;
            }
            return Profile::Type::PlaybackLoudspeaker;
        }

        // Operations
        virtual audio::RetCode Start(Operation::Type op,
                                     audio::Token token                      = audio::Token::MakeBadToken(),
                                     const char *fileName                    = "",
                                     const audio::PlaybackType &playbackType = audio::PlaybackType::None);

        virtual audio::RetCode Start();
        virtual audio::RetCode Stop();
        virtual audio::RetCode Pause();
        virtual audio::RetCode Resume();
        virtual audio::RetCode Mute();

      protected:
        AudioSinkState audioSinkState;

      private:
        void SendUpdateEventsToCurrentOperation();
        /**
         * @brief Sends update to the current operation and switches to priority profile.
         */
        void UpdateProfiles();
        /**
         * @brief Sends update to the current operation and switches to priority profile.
         *
         * @param playbackType if it's callringtone and bluetooth a2dp is used then
         * ignore priorities and change profile to the earpeaker. Not needed otherwise.
         */
        void UpdateProfiles(audio::PlaybackType playbackType);

        Muted muted = Muted::False;

        std::shared_ptr<BluetoothStreamData> btData;

        State currentState = State::Idle;
        std::unique_ptr<Operation> currentOperation;

        AudioServiceMessage::Callback serviceCallback;
    };

} // namespace audio