~aleteoryx/muditaos

muditaos/module-apps/apps-common/audio/SoundsPlayer.cpp -rw-r--r-- 2.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
// 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 "SoundsPlayer.hpp"

SoundsPlayer::SoundsPlayer(app::ApplicationCommon *app)
    : audioOperations{std::make_unique<app::AsyncAudioOperations>(app)}
{}

bool SoundsPlayer::play(const std::string &path, AudioStoppedCallback cb)
{
    audioStoppedCallback = nullptr;
    return audioOperations->play(path, [this, path, cb](audio::RetCode retCode, audio::Token token) {
        if (retCode != audio::RetCode::Success || !token.IsValid()) {
            LOG_ERROR("Audio preview callback failed with retcode = %s. Token validity: %d",
                      str(retCode).c_str(),
                      token.IsValid());
            return;
        }
        currentState         = State::Playing;
        currentToken         = token;
        currentPath          = path;
        audioStoppedCallback = cb;
    });
}

bool SoundsPlayer::pause()
{
    return audioOperations->pause(currentToken, [this](audio::RetCode retCode, audio::Token token) {
        if (token != currentToken) {
            LOG_ERROR("Audio preview pause failed: wrong token");
            return;
        }
        if (retCode != audio::RetCode::Success || !token.IsValid()) {
            LOG_ERROR("Audio preview pause failed with retcode = %s. Token validity: %d",
                      str(retCode).c_str(),
                      token.IsValid());
            return;
        }
        currentState = State::Paused;
    });
}

bool SoundsPlayer::resume()
{
    return audioOperations->resume(currentToken, [this](audio::RetCode retCode, audio::Token token) {
        if (token != currentToken) {
            LOG_ERROR("Audio preview resume failed: wrong token");
            return;
        }

        if (retCode != audio::RetCode::Success || !token.IsValid()) {
            LOG_ERROR("Audio preview pause failed with retcode = %s. Token validity: %d",
                      str(retCode).c_str(),
                      token.IsValid());
            return;
        }

        currentState = State::Playing;
    });
}

bool SoundsPlayer::stop()
{
    audioStoppedCallback = nullptr;

    if (currentToken.IsValid()) {
        return audioOperations->stop(currentToken,
                                     [this](audio::RetCode, audio::Token) { currentState = State::Stopped; });
    }
    return false;
}

bool SoundsPlayer::stop(audio::Token token)
{
    if (currentToken.IsValid() && currentToken == token) {
        currentState = State::Stopped;
        if (audioStoppedCallback != nullptr) {
            audioStoppedCallback();
        }
        return true;
    }
    return false;
}

bool SoundsPlayer::isInState(State state) const noexcept
{
    return currentState == state;
}

bool SoundsPlayer::previouslyPlayed(const std::string &filePath) const
{
    return currentToken.IsValid() && currentPath == filePath;
}