forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.cpp
370 lines (316 loc) · 9 KB
/
audio.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "audio.hpp"
#include "util/common.hpp"
#include "bridge.hpp"
namespace rack {
AudioIO::AudioIO() {
setDriver(RtAudio::UNSPECIFIED);
}
AudioIO::~AudioIO() {
closeStream();
}
std::vector<int> AudioIO::getDrivers() {
std::vector<RtAudio::Api> apis;
RtAudio::getCompiledApi(apis);
std::vector<int> drivers;
for (RtAudio::Api api : apis)
drivers.push_back((int) api);
// Add fake Bridge driver
drivers.push_back(BRIDGE_DRIVER);
return drivers;
}
std::string AudioIO::getDriverName(int driver) {
switch (driver) {
case RtAudio::UNSPECIFIED: return "Unspecified";
case RtAudio::LINUX_ALSA: return "ALSA";
case RtAudio::LINUX_PULSE: return "PulseAudio";
case RtAudio::LINUX_OSS: return "OSS";
case RtAudio::UNIX_JACK: return "JACK";
case RtAudio::MACOSX_CORE: return "Core Audio";
case RtAudio::WINDOWS_WASAPI: return "WASAPI";
case RtAudio::WINDOWS_ASIO: return "ASIO";
case RtAudio::WINDOWS_DS: return "DirectSound";
case RtAudio::RTAUDIO_DUMMY: return "Dummy Audio";
case BRIDGE_DRIVER: return "Bridge";
default: return "Unknown";
}
}
void AudioIO::setDriver(int driver) {
// Close device
setDevice(-1, 0);
// Close driver
if (rtAudio) {
delete rtAudio;
rtAudio = NULL;
}
this->driver = 0;
// Open driver
if (driver >= 0) {
rtAudio = new RtAudio((RtAudio::Api) driver);
this->driver = (int) rtAudio->getCurrentApi();
}
else if (driver == BRIDGE_DRIVER) {
this->driver = BRIDGE_DRIVER;
}
}
int AudioIO::getDeviceCount() {
if (rtAudio) {
return rtAudio->getDeviceCount();
}
else if (driver == BRIDGE_DRIVER) {
return BRIDGE_NUM_PORTS;
}
return 0;
}
bool AudioIO::getDeviceInfo(int device, RtAudio::DeviceInfo *deviceInfo) {
if (!deviceInfo)
return false;
if (rtAudio) {
if (device == this->device) {
*deviceInfo = this->deviceInfo;
return true;
}
else {
try {
*deviceInfo = rtAudio->getDeviceInfo(device);
return true;
}
catch (RtAudioError &e) {
warn("Failed to query RtAudio device: %s", e.what());
}
}
}
return false;
}
int AudioIO::getDeviceChannels(int device) {
if (device < 0)
return 0;
if (rtAudio) {
RtAudio::DeviceInfo deviceInfo;
if (getDeviceInfo(device, &deviceInfo))
return max((int) deviceInfo.inputChannels, (int) deviceInfo.outputChannels);
}
else if (driver == BRIDGE_DRIVER) {
return max(BRIDGE_OUTPUTS, BRIDGE_INPUTS);
}
return 0;
}
std::string AudioIO::getDeviceName(int device) {
if (device < 0)
return "";
if (rtAudio) {
RtAudio::DeviceInfo deviceInfo;
if (getDeviceInfo(device, &deviceInfo))
return deviceInfo.name;
}
else if (driver == BRIDGE_DRIVER) {
return stringf("%d", device + 1);
}
return "";
}
std::string AudioIO::getDeviceDetail(int device, int offset) {
if (device < 0)
return "";
if (rtAudio) {
RtAudio::DeviceInfo deviceInfo;
if (getDeviceInfo(device, &deviceInfo)) {
std::string deviceDetail = stringf("%s (", deviceInfo.name.c_str());
if (offset < (int) deviceInfo.inputChannels)
deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, (int) deviceInfo.inputChannels));
if (offset < (int) deviceInfo.inputChannels && offset < (int) deviceInfo.outputChannels)
deviceDetail += ", ";
if (offset < (int) deviceInfo.outputChannels)
deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, (int) deviceInfo.outputChannels));
deviceDetail += ")";
return deviceDetail;
}
}
else if (driver == BRIDGE_DRIVER) {
return stringf("Port %d", device + 1);
}
return "";
}
void AudioIO::setDevice(int device, int offset) {
closeStream();
this->device = device;
this->offset = offset;
openStream();
}
std::vector<int> AudioIO::getSampleRates() {
if (rtAudio) {
try {
RtAudio::DeviceInfo deviceInfo = rtAudio->getDeviceInfo(device);
std::vector<int> sampleRates(deviceInfo.sampleRates.begin(), deviceInfo.sampleRates.end());
return sampleRates;
}
catch (RtAudioError &e) {
warn("Failed to query RtAudio device: %s", e.what());
}
}
return {};
}
void AudioIO::setSampleRate(int sampleRate) {
if (sampleRate == this->sampleRate)
return;
closeStream();
this->sampleRate = sampleRate;
openStream();
}
std::vector<int> AudioIO::getBlockSizes() {
if (rtAudio) {
return {64, 128, 256, 512, 1024, 2048, 4096};
}
return {};
}
void AudioIO::setBlockSize(int blockSize) {
if (blockSize == this->blockSize)
return;
closeStream();
this->blockSize = blockSize;
openStream();
}
void AudioIO::setChannels(int numOutputs, int numInputs) {
this->numOutputs = numOutputs;
this->numInputs = numInputs;
onChannelsChange();
}
static int rtCallback(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData) {
AudioIO *audioIO = (AudioIO*) userData;
assert(audioIO);
audioIO->processStream((const float *) inputBuffer, (float *) outputBuffer, nFrames);
return 0;
}
void AudioIO::openStream() {
if (device < 0)
return;
if (rtAudio) {
// Open new device
try {
deviceInfo = rtAudio->getDeviceInfo(device);
}
catch (RtAudioError &e) {
warn("Failed to query RtAudio device: %s", e.what());
return;
}
if (rtAudio->isStreamOpen())
return;
setChannels(clamp((int) deviceInfo.outputChannels - offset, 0, maxChannels), clamp((int) deviceInfo.inputChannels - offset, 0, maxChannels));
if (numOutputs == 0 && numInputs == 0) {
warn("RtAudio device %d has 0 inputs and 0 outputs", device);
return;
}
RtAudio::StreamParameters outParameters;
outParameters.deviceId = device;
outParameters.nChannels = numOutputs;
outParameters.firstChannel = offset;
RtAudio::StreamParameters inParameters;
inParameters.deviceId = device;
inParameters.nChannels = numInputs;
inParameters.firstChannel = offset;
RtAudio::StreamOptions options;
options.flags |= RTAUDIO_JACK_DONT_CONNECT;
options.streamName = "VCV Rack";
int closestSampleRate = deviceInfo.preferredSampleRate;
for (int sr : deviceInfo.sampleRates) {
if (abs(sr - sampleRate) < abs(closestSampleRate - sampleRate)) {
closestSampleRate = sr;
}
}
try {
info("Opening audio RtAudio device %d with %d in %d out", device, numInputs, numOutputs);
rtAudio->openStream(
numOutputs == 0 ? NULL : &outParameters,
numInputs == 0 ? NULL : &inParameters,
RTAUDIO_FLOAT32, closestSampleRate, (unsigned int*) &blockSize,
&rtCallback, this, &options, NULL);
}
catch (RtAudioError &e) {
warn("Failed to open RtAudio stream: %s", e.what());
return;
}
try {
info("Starting RtAudio stream %d", device);
rtAudio->startStream();
}
catch (RtAudioError &e) {
warn("Failed to start RtAudio stream: %s", e.what());
return;
}
// Update sample rate because this may have changed
this->sampleRate = rtAudio->getStreamSampleRate();
onOpenStream();
}
else if (driver == BRIDGE_DRIVER) {
setChannels(BRIDGE_OUTPUTS, BRIDGE_INPUTS);
bridgeAudioSubscribe(device, this);
}
}
void AudioIO::closeStream() {
setChannels(0, 0);
if (rtAudio) {
if (rtAudio->isStreamRunning()) {
info("Stopping RtAudio stream %d", device);
try {
rtAudio->stopStream();
}
catch (RtAudioError &e) {
warn("Failed to stop RtAudio stream %s", e.what());
}
}
if (rtAudio->isStreamOpen()) {
info("Closing RtAudio stream %d", device);
try {
rtAudio->closeStream();
}
catch (RtAudioError &e) {
warn("Failed to close RtAudio stream %s", e.what());
}
}
deviceInfo = RtAudio::DeviceInfo();
}
else if (driver == BRIDGE_DRIVER) {
bridgeAudioUnsubscribe(device, this);
}
onCloseStream();
}
json_t *AudioIO::toJson() {
json_t *rootJ = json_object();
json_object_set_new(rootJ, "driver", json_integer(driver));
std::string deviceName = getDeviceName(device);
json_object_set_new(rootJ, "deviceName", json_string(deviceName.c_str()));
json_object_set_new(rootJ, "offset", json_integer(offset));
json_object_set_new(rootJ, "maxChannels", json_integer(maxChannels));
json_object_set_new(rootJ, "sampleRate", json_integer(sampleRate));
json_object_set_new(rootJ, "blockSize", json_integer(blockSize));
return rootJ;
}
void AudioIO::fromJson(json_t *rootJ) {
closeStream();
json_t *driverJ = json_object_get(rootJ, "driver");
if (driverJ)
setDriver(json_number_value(driverJ));
json_t *deviceNameJ = json_object_get(rootJ, "deviceName");
if (deviceNameJ) {
std::string deviceName = json_string_value(deviceNameJ);
// Search for device ID with equal name
for (int device = 0; device < getDeviceCount(); device++) {
if (getDeviceName(device) == deviceName) {
this->device = device;
break;
}
}
}
json_t *offsetJ = json_object_get(rootJ, "offset");
if (offsetJ)
offset = json_integer_value(offsetJ);
json_t *maxChannelsJ = json_object_get(rootJ, "maxChannels");
if (maxChannelsJ)
maxChannels = json_integer_value(maxChannelsJ);
json_t *sampleRateJ = json_object_get(rootJ, "sampleRate");
if (sampleRateJ)
sampleRate = json_integer_value(sampleRateJ);
json_t *blockSizeJ = json_object_get(rootJ, "blockSize");
if (blockSizeJ)
blockSize = json_integer_value(blockSizeJ);
openStream();
}
} // namespace rack