SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
CLIAudio.hxx
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright (c) 2026 Matthew H. Reilly (kb1vc)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9
10 Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
43
44#include <QThread>
45#include <QAudioDevice>
46#include <atomic>
47#include <string>
48
49namespace SoDaCLI {
50
60 QAudioDevice findOutputDevice(const std::string & name);
61
70 QAudioDevice findInputDevice(const std::string & name);
71
79
80 // -------------------------------------------------------------------
81
92 class AudioOutThread : public QThread {
93 Q_OBJECT
94 public:
101 explicit AudioOutThread(const std::string & sockPath,
102 const QAudioDevice & dev,
103 float initVolume = 0.0f,
104 QObject * parent = nullptr);
106
111 void setVolume(float v) { pending_volume.store(v); }
112
117 double getRMS() const { return last_rms.load(); }
118
122 void stopAudio();
123
124 protected:
125 void run() override;
126
127 private:
128 std::string sock_path;
129 QAudioDevice device;
130 std::atomic<float> pending_volume;
131 std::atomic<bool> stop_flag;
132 std::atomic<double> last_rms{-1.0};
133 };
134
135 // -------------------------------------------------------------------
136
147 class AudioInThread : public QThread {
148 Q_OBJECT
149 public:
156 explicit AudioInThread(const std::string & sockPath,
157 const QAudioDevice & dev,
158 float initGain = 0.0f,
159 QObject * parent = nullptr);
161
166 void setGain(float g) { pending_gain.store(g); }
167
171 void stopAudio();
172
173 protected:
174 void run() override;
175
176 private:
177 std::string sock_path;
178 QAudioDevice device;
179 std::atomic<float> pending_gain;
180 std::atomic<bool> stop_flag;
181 };
182
183 // -------------------------------------------------------------------
184
193 class AudioToneThread : public QThread {
194 Q_OBJECT
195 public:
200 explicit AudioToneThread(const QAudioDevice & dev,
201 QObject * parent = nullptr);
203
205 void stopAudio();
206
207 protected:
208 void run() override;
209
210 private:
211 QAudioDevice device;
212 std::atomic<bool> stop_flag;
213 };
214
215} // namespace SoDaCLI
void setGain(float g)
Set capture gain (thread-safe).
Definition CLIAudio.hxx:166
std::atomic< bool > stop_flag
Definition CLIAudio.hxx:180
AudioInThread(const std::string &sockPath, const QAudioDevice &dev, float initGain=0.0f, QObject *parent=nullptr)
void stopAudio()
Stop the thread cleanly.
void run() override
std::atomic< float > pending_gain
Definition CLIAudio.hxx:179
AudioOutThread(const std::string &sockPath, const QAudioDevice &dev, float initVolume=0.0f, QObject *parent=nullptr)
std::atomic< double > last_rms
Definition CLIAudio.hxx:132
std::atomic< bool > stop_flag
Definition CLIAudio.hxx:131
double getRMS() const
Return the RMS of samples received in the last completed second.
Definition CLIAudio.hxx:117
void setVolume(float v)
Set playback volume (thread-safe).
Definition CLIAudio.hxx:111
void stopAudio()
Stop the thread cleanly.
void run() override
std::atomic< float > pending_volume
Definition CLIAudio.hxx:130
std::atomic< bool > stop_flag
Definition CLIAudio.hxx:212
AudioToneThread(const QAudioDevice &dev, QObject *parent=nullptr)
void stopAudio()
Stop the thread cleanly.
QAudioDevice findOutputDevice(const std::string &name)
Find an audio output device by name.
QAudioDevice findInputDevice(const std::string &name)
Find an audio input device by name.
void listAudioDevices()
Print all available audio input and output devices to stdout.