Debian Codegraph @

BambooTracker/audio/audio_stream_rtaudio.cpp

raw
1 /*
2 * Copyright (C) 2019-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 #include "audio_stream_rtaudio.hpp"
27 #include <string>
28 #include <vector>
29 #include <QVariant>
30 #include "RtAudio.h"
31
32 AudioStreamRtAudio::AudioStreamRtAudio(QObject* parent)
33 : AudioStream(parent)
34 {
35 audio_.reset(new RtAudio);
36 }
37
38 AudioStreamRtAudio::~AudioStreamRtAudio() = default;
39
40 bool AudioStreamRtAudio::initialize(uint32_t rate, uint32_t duration, uint32_t intrRate,
41 const QString& backend, const QString& device,
42 QString* errDetail)
43 {
44 shutdown();
45
46 setBackend(backend);
47
48 RtAudio* audio = audio_.get();
49 const std::string deviceUtf8 = device.toStdString();
50
51 RtAudio::StreamParameters param;
52 param.nChannels = 2;
53 param.deviceId = ~0u;
54 for (unsigned int id : audio->getDeviceIds()) {
55 RtAudio::DeviceInfo info = audio->getDeviceInfo(id);
56 if (info.outputChannels >= 2 && info.name == deviceUtf8)
57 param.deviceId = id;
58 }
59 if (param.deviceId == ~0u)
60 param.deviceId = audio->getDefaultOutputDevice();
61
62 RtAudio::StreamOptions opts;
63 opts.flags = RTAUDIO_SCHEDULE_REALTIME;
64 opts.streamName = AUDIO_OUT_CLIENT_NAME;
65 opts.numberOfBuffers = 2;
66
67 auto callback =
68 +[](void* outputBuffer, void*, unsigned int nFrames,
69 double, RtAudioStreamStatus, void* userData) -> int {
70 auto stream = reinterpret_cast<AudioStreamRtAudio*>(userData);
71 bool result = stream->generate(static_cast<int16_t*>(outputBuffer), nFrames);
72 return result ? 0 : 1;
73 };
74
75 unsigned int bufferSize = rate * duration / 1000;
76 bool isSuccessed = false;
77 const RtAudioErrorType errorType = audio->openStream(&param, nullptr, RTAUDIO_SINT16, rate, &bufferSize, callback, this, &opts);
78 if (errorType == RtAudioErrorType::RTAUDIO_NO_ERROR) {
79 if (errDetail) *errDetail = "";
80 isSuccessed = true;
81 rate = audio->getStreamSampleRate(); // Match to real rate (for ALSA)
82 }
83 else {
84 if (errDetail) *errDetail = QString::fromStdString(audio->getErrorText());
85 }
86
87 AudioStream::initialize(rate, duration, intrRate, backend, device);
88 return isSuccessed;
89 }
90
91 void AudioStreamRtAudio::shutdown()
92 {
93 if (audio_->isStreamOpen()) audio_->closeStream();
94 }
95
96 void AudioStreamRtAudio::setBackend(const QString& backend)
97 {
98 std::vector<RtAudio::Api> apis;
99 RtAudio::getCompiledApi(apis);
100 size_t i = 0;
101 for (const auto& api : apis) {
102 if (backend == QString::fromStdString(RtAudio::getApiDisplayName(api))) {
103 audio_.reset(new RtAudio(apis[i]));
104 return;
105 }
106 ++i;
107 }
108 audio_.reset(new RtAudio);
109 }
110
111 std::vector<QString> AudioStreamRtAudio::getAvailableBackends() const
112 {
113 std::vector<RtAudio::Api> apis;
114 std::vector<QString> names;
115 RtAudio::getCompiledApi(apis);
116 for (const auto& api : apis)
117 names.push_back(QString::fromStdString(RtAudio::getApiDisplayName(api)));
118 return names;
119 }
120
121 QString AudioStreamRtAudio::getCurrentBackend() const
122 {
123 return QString::fromStdString(RtAudio::getApiDisplayName(audio_->getCurrentApi()));
124 }
125
126 std::vector<QString> AudioStreamRtAudio::getAvailableDevices() const
127 {
128 RtAudio* audio = audio_.get();
129 std::vector<QString> devices;
130
131 for (unsigned int id : audio->getDeviceIds()) {
132 RtAudio::DeviceInfo info = audio->getDeviceInfo(id);
133 if (info.outputChannels >= 2)
134 devices.push_back(QString::fromStdString(info.name));
135 }
136
137 return devices;
138 }
139
140 std::vector<QString> AudioStreamRtAudio::getAvailableDevices(const QString& backend) const
141 {
142 std::vector<RtAudio::Api> apis;
143 RtAudio::getCompiledApi(apis);
144 RtAudio::Api api = RtAudio::RTAUDIO_DUMMY;
145 for (const auto& apiAvailable : apis) {
146 if (backend == QString::fromStdString(RtAudio::getApiDisplayName(apiAvailable))) {
147 api = apiAvailable;
148 break;
149 }
150 }
151
152 std::vector<QString> list;
153 auto a = std::make_unique<RtAudio>(api);
154 list.reserve(a->getDeviceCount());
155 for (unsigned int id : a->getDeviceIds()) {
156 RtAudio::DeviceInfo info = a->getDeviceInfo(id);
157 if (info.outputChannels >= 2)
158 list.push_back(QString::fromStdString(info.name));
159 }
160 return list;
161 }
162
163 QString AudioStreamRtAudio::getDefaultOutputDevice() const
164 {
165 return QString::fromStdString(audio_->getDeviceInfo(audio_->getDefaultOutputDevice()).name);
166 }
167
168 QString AudioStreamRtAudio::getDefaultOutputDevice(const QString& backend) const
169 {
170 std::vector<RtAudio::Api> apis;
171 RtAudio::getCompiledApi(apis);
172 RtAudio::Api api = RtAudio::RTAUDIO_DUMMY;
173 for (const auto& apiAvailable : apis) {
174 if (backend == QString::fromStdString(RtAudio::getApiDisplayName(apiAvailable))) {
175 api = apiAvailable;
176 break;
177 }
178 }
179
180 std::vector<QString> list;
181 auto a = std::make_unique<RtAudio>(api);
182 return QString::fromStdString(a->getDeviceInfo(a->getDefaultOutputDevice()).name);
183 }
184
185 void AudioStreamRtAudio::start()
186 {
187 AudioStream::start();
188 if (audio_->isStreamOpen() && !audio_->isStreamRunning()) audio_->startStream();
189 }
190
191 void AudioStreamRtAudio::stop()
192 {
193 AudioStream::stop();
194 if (audio_->isStreamOpen() && audio_->isStreamRunning()) audio_->stopStream();
195 }