JS8Call-Improved master
Loading...
Searching...
No Matches
AudioDevice.h
Go to the documentation of this file.
1
11
12#ifndef AUDIODEVICE_HPP__
13#define AUDIODEVICE_HPP__
14
15#include <QIODevice>
16
17class QDataStream;
18
27class AudioDevice : public QIODevice {
28 public:
35 enum Channel {
36 Mono,
37 Left,
38 Right,
39 Both
40 }; // these are mapped to combobox index so don't change
41
42 static char const *toString(Channel c) {
43 switch (c) {
44 case Mono:
45 return "Mono";
46 case Left:
47 return "Left";
48 case Right:
49 return "Right";
50 default:
51 return "Both";
52 }
53 }
54
60 static Channel fromString(QString const &str) {
61 QString const s(str.toCaseFolded().trimmed().toLatin1());
62
63 if (s == "both")
64 return Both;
65 else if (s == "right")
66 return Right;
67 else if (s == "left")
68 return Left;
69 else
70 return Mono;
71 }
72
79 bool initialize(OpenMode mode, Channel channel);
80
90 bool isSequential() const override { return true; }
91
97 size_t bytesPerFrame() const {
98 return sizeof(qint16) * (Mono == m_channel ? 1 : 2);
99 }
100
109 Channel channel() const { return m_channel; }
110
111 protected:
112 explicit AudioDevice(QObject *parent = nullptr) : QIODevice(parent) {}
113
120 void store(char const *source, size_t numFrames, qint16 *dest) {
121 qint16 const *begin(reinterpret_cast<qint16 const *>(source));
122 for (qint16 const *i = begin;
123 i != begin + numFrames * (bytesPerFrame() / sizeof(qint16));
124 i += bytesPerFrame() / sizeof(qint16)) {
125 switch (m_channel) {
126 case Mono:
127 *dest++ = *i;
128 break;
129
130 case Right:
131 *dest++ = *(i + 1);
132 break;
133
134 case Both: // should be able to happen but if it
135 // does we'll take left
136 Q_ASSERT(Both == m_channel);
137 [[fallthrough]];
138 case Left:
139 *dest++ = *i;
140 break;
141 }
142 }
143 }
144
151 qint16 *load(qint16 const sample, qint16 *dest) {
152 switch (m_channel) {
153 case Mono:
154 *dest++ = sample;
155 break;
156
157 case Left:
158 *dest++ = sample;
159 *dest++ = 0;
160 break;
161
162 case Right:
163 *dest++ = 0;
164 *dest++ = sample;
165 break;
166
167 case Both:
168 *dest++ = sample;
169 *dest++ = sample;
170 break;
171 }
172 return dest;
173 }
174
175 private:
176 Channel m_channel;
177};
178
179Q_DECLARE_METATYPE(AudioDevice::Channel);
180
181#endif
Abstract base class for audio devices exposed as a QIODevice.
Definition AudioDevice.h:27
void store(char const *source, size_t numFrames, qint16 *dest)
Copy interleaved source samples into the destination buffer.
Definition AudioDevice.h:120
Channel channel() const
Get the current channel selection used by this device.
Definition AudioDevice.h:109
qint16 * load(qint16 const sample, qint16 *dest)
Expand a sample value into the output buffer for the active channel.
Definition AudioDevice.h:151
Channel
Audio channel selection used by the device.
Definition AudioDevice.h:35
size_t bytesPerFrame() const
Number of bytes per audio frame for the current channel setting.
Definition AudioDevice.h:97
static Channel fromString(QString const &str)
Convert a channel label into an AudioDevice channel value.
Definition AudioDevice.h:60
bool isSequential() const override
Whether this device exposes a sequential data stream.
Definition AudioDevice.h:90
bool initialize(OpenMode mode, Channel channel)
Initialize the device for the specified open mode and channel.
Definition AudioDevice.cpp:12