~aleteoryx/muditaos

ref: eda92600a7df852e18bdb44388966248a4af3c77 muditaos/module-audio/Audio/Audio.cpp -rw-r--r-- 5.0 KiB
eda92600 — Maciej Gibowicz [BH-2095] Add quote interval setting 10 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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 "Audio.hpp"
#include "Operation/Operation.hpp"

#include <log/log.hpp>
#include <bsp/headset/headset.hpp>

namespace audio
{
    Audio::Audio(AudioServiceMessage::Callback callback) : currentOperation(), serviceCallback(std::move(callback))
    {
        auto ret = Operation::Create(
            Operation::Type::Idle, "", audio::PlaybackType::None, audio::PlaybackMode::Single, serviceCallback);
        if (ret) {
            currentOperation = std::move(ret);
        }
        audioSinkState.setConnected(EventType::JackState, bsp::headset::IsInserted());
    }

    Position Audio::GetPosition()
    {
        return currentOperation->GetPosition();
    }

    audio::RetCode Audio::SendEvent(std::shared_ptr<Event> evt)
    {
        audioSinkState.UpdateState(evt);
        UpdateProfiles();
        return currentOperation->SendEvent(std::move(evt));
    }

    audio::RetCode Audio::SetOutputVolume(float vol)
    {
        return currentOperation->SetOutputVolume(static_cast<float>(vol));
    }

    audio::RetCode Audio::SetInputGain(Gain gain)
    {
        auto gainToSet = gain;
        if (gain > maxGain) {
            gainToSet = maxGain;
        }
        if (gain < minGain) {
            gainToSet = minGain;
        }
        return currentOperation->SetInputGain(gainToSet);
    }

    audio::RetCode Audio::Start(Operation::Type op,
                                audio::Token token,
                                const std::string &filePath,
                                const audio::PlaybackType &playbackType,
                                const audio::PlaybackMode &playbackMode)
    {

        try {
            auto ret = Operation::Create(op, filePath, playbackType, playbackMode, serviceCallback);
            switch (op) {
            case Operation::Type::Playback:
                currentState = State::Playback;
                break;
            case Operation::Type::Recorder:
                currentState = State::Recording;
                break;
            case Operation::Type::Router:
                currentState = State::Routing;
                break;
            case Operation::Type::Idle:
                break;
            }
            currentOperation = std::move(ret);
            UpdateProfiles(playbackType);
        }
        catch (const AudioInitException &audioException) {
            // If creating operation failed fallback to IdleOperation which is guaranteed to work
            LOG_ERROR(
                "Failed to create operation type %s, error message: %s", Operation::c_str(op), audioException.what());
            currentOperation = Operation::Create(Operation::Type::Idle);
            currentState     = State::Idle;
            return audioException.getErrorCode();
        }

        return currentOperation->Start(token);
    }

    audio::RetCode Audio::Start()
    {
        if (currentState != State::Idle) {
            currentOperation->Stop();
        }
        return Start(currentOperation->GetOperationType(),
                     currentOperation->GetToken(),
                     currentOperation->GetFilePath(),
                     currentOperation->GetPlaybackType());
    }

    audio::RetCode Audio::Stop()
    {
        if (currentState == State::Idle) {
            return RetCode::Success;
        }

        auto retStop = currentOperation->Stop();
        if (retStop != RetCode::Success) {
            LOG_ERROR("Operation STOP failure: %s", audio::str(retStop).c_str());
        }

        muted    = Muted::False;
        auto ret = Operation::Create(Operation::Type::Idle);
        if (ret) {
            currentState     = State::Idle;
            currentOperation = std::move(ret);
            return RetCode::Success;
        }
        else {
            return RetCode::OperationCreateFailed;
        }
    }

    audio::RetCode Audio::Pause()
    {
        if (currentState == State::Idle) {
            return RetCode::InvokedInIncorrectState;
        }

        return currentOperation->Pause();
    }

    audio::RetCode Audio::Resume()
    {
        if (currentState == State::Idle) {
            return RetCode::InvokedInIncorrectState;
        }
        return currentOperation->Resume();
    }

    audio::RetCode Audio::Mute()
    {
        muted = Muted::True;
        return SetOutputVolume(0.0f);
    }

    void Audio::SendUpdateEventsToCurrentOperation()
    {
        auto updateEvents = audioSinkState.getUpdateEvents();
        for (auto &event : updateEvents) {
            currentOperation->SendEvent(event);
        }
    }

    void Audio::UpdateProfiles()
    {
        SendUpdateEventsToCurrentOperation();
        currentOperation->SwitchToPriorityProfile();
    }

    void Audio::UpdateProfiles(audio::PlaybackType playbackType)
    {
        SendUpdateEventsToCurrentOperation();
        currentOperation->SwitchToPriorityProfile(playbackType);
    }

} // namespace audio