~aleteoryx/muditaos

ref: 8fafd788a5ef70d89e4d20cb80052261d13d09ff muditaos/module-audio/board/linux/LinuxAudioDevice.hpp -rw-r--r-- 2.5 KiB
8fafd788 — Paweł Joński [BH-1346] Fix minute singular copy in Spanish 3 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
// 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 <Audio/AudioDevice.hpp>
#include <Audio/AudioFormat.hpp>
#include <Audio/codec.hpp>

#include <portaudio.h>

#include <deque>

namespace audio
{
    class LinuxAudioDevice : public audio::AudioDevice
    {
      public:
        LinuxAudioDevice(const float initialVolume);
        virtual ~LinuxAudioDevice();

        auto Start() -> RetCode override;
        auto Stop() -> RetCode override;

        /// Set device output volume
        /// @param vol desired volume from 0 to 10
        /// @return RetCode::Success if OK, or RetCode::Failure otherwise
        auto setOutputVolume(float vol) -> RetCode override;

        /// Set device input gain
        /// @param gain desired input gain from 0 to 100
        /// @return RetCode::Success if OK, or RetCode::Failure otherwise
        auto setInputGain(float gain) -> RetCode override;

        auto getTraits() const -> Traits override;
        auto getSupportedFormats() -> std::vector<audio::AudioFormat> override;
        auto getSourceFormat() -> audio::AudioFormat override;

        // Endpoint control methods
        void onDataSend() override;
        void onDataReceive() override;
        void enableInput() override;
        void enableOutput() override;
        void disableInput() override;
        void disableOutput() override;

      private:
        int streamCallback(const void *input,
                           void *output,
                           unsigned long frameCount,
                           const PaStreamCallbackTimeInfo *timeInfo,
                           PaStreamCallbackFlags statusFlags);
        bool isCacheReady(std::size_t expectedSize) const noexcept;
        void cacheToOutputBuffer(std::int16_t *buffer, std::size_t size);

        void closeStream();

        constexpr static std::initializer_list<unsigned int> supportedSampleRates  = {44100, 48000};
        constexpr static std::initializer_list<unsigned int> supportedBitWidths    = {16};
        constexpr static std::initializer_list<unsigned int> supportedChannelModes = {1, 2};

        std::vector<audio::AudioFormat> supportedFormats;
        audio::AudioFormat currentFormat;

        /// pointer to portaudio stream
        PaStream *stream = nullptr;

        /// Local cache to store data read from Pure stream
        std::deque<std::int16_t> cache;

        float volumeFactor = 1.0f;
    };

} // namespace audio