~aleteoryx/muditaos

ref: 49de2d2c29c6487454bfc2842892a635979a0e91 muditaos/module-audio/Audio/Audio.hpp -rw-r--r-- 3.1 KiB
49de2d2c — tomaszkrosnowski [EGD-6314] Phone mode widget visibility 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
// 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
    {
      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;
        }

        static std::optional<Tags> GetFileTags(const char *filename);

        // 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();
        }

        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::BlutoothA2DPDeviceState)) {
                return Profile::Type::PlaybackBluetoothA2DP;
            }
            if (audioSinkState.isConnected(EventType::JackState)) {
                return Profile::Type::PlaybackHeadphones;
            }
            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();

      private:
        void UpdateProfiles();

        AudioSinkState audioSinkState;

        std::shared_ptr<BluetoothStreamData> btData;

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

        AudioServiceMessage::Callback serviceCallback;
    };

} // namespace audio