Debian Codegraph @

BambooTracker/audio/audio_stream.hpp

raw
1 /*
2 * Copyright (C) 2018-2023 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #pragma once
27
28 #include <QObject>
29 #include <thread>
30 #include <mutex>
31 #include <memory>
32 #include <atomic>
33 #include <cstdint>
34 #include <QSemaphore>
35 #include <QString>
36 #include <QVariant>
37
38 class AudioStream : public QObject
39 {
40 Q_OBJECT
41
42 public:
43 explicit AudioStream(QObject* parent = nullptr);
44 virtual ~AudioStream() override;
45
46 using GenerateCallback = bool (int16_t*, size_t, void*);
47 void setGenerateCallback(GenerateCallback* cb, void* cbPtr);
48
49 using TickUpdateCallback = int (void*);
50 void setTickUpdateCallback(TickUpdateCallback* cb, void* cbPtr);
51
52 // duration: miliseconds
53 virtual bool initialize(uint32_t rate, uint32_t duration, uint32_t intrRate,
54 const QString& backend, const QString& device,
55 QString* errDetail = nullptr);
56 virtual void shutdown() = 0;
57
58 virtual std::vector<QString> getAvailableBackends() const = 0;
59 virtual QString getCurrentBackend() const = 0;
60 virtual std::vector<QString> getAvailableDevices() const = 0;
61 virtual std::vector<QString> getAvailableDevices(const QString& backend) const = 0;
62 virtual QString getDefaultOutputDevice() const = 0;
63 virtual QString getDefaultOutputDevice(const QString& backend) const = 0;
64
65 void setInterruption(uint32_t inrtRate);
66 uint32_t getStreamRate() const noexcept;
67
68 virtual void start();
69 virtual void stop();
70
71 signals:
72 void streamInterrupted(int state);
73 void streamErrorInCallback(const QVariant& data);
74
75 protected:
76 static const std::string AUDIO_OUT_CLIENT_NAME;
77 bool generate(int16_t* container, uint32_t nSamples);
78
79 private:
80 uint32_t rate_;
81 uint32_t intrRate_;
82 uint32_t intrCount_;
83 uint32_t intrCountRest_;
84
85 std::mutex mutex_;
86 GenerateCallback* gcb_;
87 void* gcbPtr_;
88 TickUpdateCallback* tucb_;
89 void* tucbPtr_;
90 std::atomic_int tuState_;
91 bool started_;
92
93 std::atomic_bool quitNotify_;
94 QSemaphore tickNotifierSem_;
95 std::thread tickNotifier_;
96
97 void generateTick();
98
99 void tickNotifierRun();
100 };