-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiosystem.cpp
287 lines (277 loc) · 8.33 KB
/
audiosystem.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "audiosystem.h"
#include "dsp.h"
#include "logger.h"
#include <QSettings>
#include <math.h>
using namespace AudioSystem;
std::unique_ptr<Manager> Manager::create()
{
return std::unique_ptr<Manager>(new Manager);
}
Manager::Manager()
: _state(INVALID),
_stream(0),
_dsp(0)
{
connect(this, SIGNAL(message(QString)), Logger::getInstance(), SLOT(message(QString)));
connect(this, SIGNAL(warn(QString)), Logger::getInstance(), SLOT(warn(QString)));
connect(this, SIGNAL(error(QString)), Logger::getInstance(), SLOT(error(QString)));
}
Manager::~Manager()
{
if (_state == STREAMING)
stopDeviceStream();
if (_state == READY)
closeDeviceStream();
Pa_Terminate();
disconnect(this, SIGNAL(message(QString)), Logger::getInstance(), SLOT(message(QString)));
disconnect(this, SIGNAL(warn(QString)), Logger::getInstance(), SLOT(warn(QString)));
disconnect(this, SIGNAL(error(QString)), Logger::getInstance(), SLOT(error(QString)));
}
bool Manager::init()
{
PaError e;
e = Pa_Initialize();
if (e != paNoError) {
emit message(QString("PortAudio init failed"));
emit message(QString(Pa_GetErrorText(e)));
return false;
}
emit message(QString("Audio initialized"));
emit message(QString(Pa_GetVersionText()));
_state = INITIALIZED;
return true;
}
DeviceList Manager::getDeviceList() const
{
DeviceList l;
if (_state < INITIALIZED) {
emit warn("not init");
return l;
}
int devcount = Pa_GetDeviceCount();
if (devcount < 0) {
QString msg("getting device list");
msg.append(Pa_GetErrorText(devcount));
emit error(msg);
}
for (int i = 0; i < devcount; ++i) {
const PaDeviceInfo* pdi = Pa_GetDeviceInfo(i);
if (pdi == 0) {
QString msg("Error getting device info: " + i);
emit error(msg);
continue;
}
if (pdi->maxInputChannels <= 0)
continue;
Device d;
d.first = QString(pdi->name);
d.second = i;
l.append(d);
}
return l;
}
Device Manager::getDeviceByName(const QString& name)
{
DeviceList l = getDeviceList();
AudioSystem::DeviceList::Iterator it = l.begin();
while (it != l.end()) {
if (it->first == name)
return *it;
++it;
}
Device d("INVALID", -1);
return d;
}
bool Manager::checkModeSupported(const Device& d, const Mode& m) const
{
if (_state < INITIALIZED)
return false;
const PaDeviceInfo* pdi = Pa_GetDeviceInfo(d.second);
PaStreamParameters params;
params.device = d.second;
params.channelCount = m.numChannels;
params.suggestedLatency = pdi->defaultHighInputLatency;
params.sampleFormat = getSampleFormat(m.sampleFormat);
params.hostApiSpecificStreamInfo = NULL;
if (Pa_IsFormatSupported(¶ms, 0, m.sampleRate) == paNoError)
return true;
return false;
}
PaSampleFormat Manager::getSampleFormat(SAMPLEFORMAT t) const
{
switch (t) {
case INT8:
return paInt8;
case INT16:
return paInt16;
case INT24:
return paInt24;
case INT32:
return paInt32;
case FLOAT:
return paFloat32;
default:
return paFloat32;
}
}
bool Manager::openDeviceStream()
{
if (_state != INITIALIZED)
return false;
PaError err;
PaStreamParameters params;
QSettings s;
s.beginGroup("audio");
if (!s.contains("deviceName") || !s.contains("sampleRate") || !s.contains("sampleFormat") || !s.contains("numChannels")) {
emit error("invalid device config found. Please reconfigure.");
return false;
}
Device d = getDeviceByName(s.value("deviceName").toString());
if (d.second == -1) {
emit error("configured device not found");
return false;
}
Mode m;
m.sampleRate = s.value("sampleRate").toInt();
m.numChannels = s.value("numChannels").toInt();
m.sampleFormat = static_cast<SAMPLEFORMAT>(s.value("sampleFormat").toInt());
params.sampleFormat = getSampleFormat(m.sampleFormat);
// FIXME we'll set that for now to
//params.sampleFormat = paInt16;
//m.sampleFormat = 16;
//m.numChannels = 1;
// END FIXME
params.device = d.second;
params.channelCount = m.numChannels;
params.suggestedLatency = Pa_GetDeviceInfo(params.device)->defaultLowInputLatency;
params.hostApiSpecificStreamInfo = 0;
emit message("Opening device...");
err = Pa_OpenStream(&_stream,
¶ms,
0,
m.sampleRate,
PA_FRAMES,
paNoFlag,
Manager::_PAcallback,
this);
if (err != paNoError) {
emit error("Unable to open device");
_state = INVALID;
emit message(QString(Pa_GetErrorText(err)));
return false;
}
_streamingMode = m;
_state = READY;
return true;
}
void Manager::startDeviceStream()
{
if (_state != READY)
return;
emit message("Starting device stream...");
_state = STREAMING;
Pa_StartStream(_stream);
}
void Manager::stopDeviceStream()
{
if (_state != STREAMING)
return;
emit message("Stopping device stream...");
Pa_StopStream(_stream);
_state = READY;
}
bool Manager::closeDeviceStream()
{
if (_state != READY)
return false;
emit message("Closing device...");
Pa_CloseStream(_stream);
_state = INITIALIZED;
return true;
}
int Manager::_PAcallback(const void* input,
void* output,
unsigned long frames,
const PaStreamCallbackTimeInfo* ti,
PaStreamCallbackFlags statusFlags,
void* user)
{
// get "this" pointer
Manager* self = static_cast<Manager*>(user);
const sample_t* in = static_cast<const sample_t*>(input);
static sample_t* scratch = 0;
static uint32_t length = 0;
if (self->_dsp) {
uint8_t channels = self->_streamingMode.numChannels;
if (length < (frames * channels)) {
int32_t size;
switch (self->_streamingMode.sampleFormat) {
case INT8:
size = 8;
break;
case INT16:
size = 16;
break;
case INT24:
size = 32;
break;
case INT32:
size = 32;
break;
case FLOAT:
size = 32;
break;
default:
assert(0);
}
scratch = ( sample_t* )realloc(scratch, frames * channels * size);
length = frames * channels;
}
switch (self->_streamingMode.sampleFormat) {
case INT8:
self->convert(( int8_t* )input, frames * channels, scratch);
break;
case INT16:
self->convert(( int16_t* )input, frames * channels, scratch);
break;
case INT24:
self->convert24(( int32_t* )input, frames * channels, scratch);
break;
case INT32:
self->convert(( int32_t* )input, frames * channels, scratch);
break;
case FLOAT:
self->convert(( float* )input, frames * channels, scratch);
break;
default:
return paAbort;
}
short* v = ( short* )input;
self->_dsp->feed(scratch, frames * channels);
}
return paContinue;
}
void Manager::convert(int8_t* in, uint32_t len, sample_t* out)
{
for (uint32_t i = 0; i < len; ++i)
out[i] = in[i] / 255.0f;
}
void Manager::convert(int16_t* in, uint32_t len, sample_t* out)
{
for (uint32_t i = 0; i < len; ++i)
out[i] = in[i] / 32768.0f;
}
void Manager::convert24(int32_t* in, uint32_t len, sample_t* out)
{
}
void Manager::convert(int32_t* in, uint32_t len, sample_t* out)
{
for (uint32_t i = 0; i < len; ++i)
out[i] = in[i] / ( double )INT_MAX;
}
void Manager::convert(float* in, uint32_t len, sample_t* out)
{
for (uint32_t i = 0; i < len; ++i)
out[i] = in[i];
}