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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
bqaudioio
Copyright 2007-2021 Particular Programs Ltd.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of Chris Cannam and
Particular Programs Ltd shall not be used in advertising or
otherwise to promote the sale, use or other dealings in this
Software without prior written authorization.
*/
#ifdef HAVE_JACK
#include "JACKAudioIO.h"
#include "DynamicJACK.h"
#include "ApplicationPlaybackSource.h"
#include "ApplicationRecordTarget.h"
#include "Gains.h"
#include "Log.h"
#include <bqvec/Range.h>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <climits>
#include <unistd.h> // getpid
//#define DEBUG_AUDIO_JACK_IO 1
using namespace std;
namespace breakfastquay {
static string defaultConnectionName = "Default Connection";
static string noConnectionName = "No Connection";
static void log(string message) {
Log::log("JACKAudioIO: " + message);
}
vector<string>
JACKAudioIO::getRecordDeviceNames()
{
return { defaultConnectionName, noConnectionName };
}
vector<string>
JACKAudioIO::getPlaybackDeviceNames()
{
return { defaultConnectionName, noConnectionName };
}
JACKAudioIO::JACKAudioIO(Mode mode,
ApplicationRecordTarget *target,
ApplicationPlaybackSource *source,
string recordDevice,
string playbackDevice) :
SystemAudioIO(target, source),
m_mode(mode),
m_client(0),
m_bufferSize(0),
m_sampleRate(0)
{
log("starting");
if (m_mode == Mode::Playback) {
m_target = 0;
}
if (m_mode == Mode::Record) {
m_source = 0;
}
std::string clientName =
(source ? source->getClientName() :
target ? target->getClientName() : "bqaudioio");
JackOptions options = JackNullOption;
#if defined(HAVE_PORTAUDIO) || defined(HAVE_LIBPULSE)
options = JackNoStartServer;
#endif
JackStatus status = JackStatus(0);
m_client = jack_client_open(clientName.c_str(), options, &status);
if (!m_client) {
m_startupError = "Failed to connect to JACK server";
log("ERROR: " + m_startupError);
return;
}
m_bufferSize = jack_get_buffer_size(m_client);
m_sampleRate = jack_get_sample_rate(m_client);
jack_set_xrun_callback(m_client, xrunStatic, this);
jack_set_process_callback(m_client, processStatic, this);
if (jack_activate(m_client)) {
m_startupError = "Failed to activate JACK client";
log("ERROR: " + m_startupError);
return;
}
bool connectRecord = (recordDevice != noConnectionName);
bool connectPlayback = (playbackDevice != noConnectionName);
setup(connectRecord, connectPlayback);
log("started successfully");
}
JACKAudioIO::~JACKAudioIO()
{
if (m_client) {
jack_deactivate(m_client);
jack_client_close(m_client);
log("closed");
}
}
bool
JACKAudioIO::isSourceOK() const
{
if (m_mode == Mode::Playback) {
// record source is irrelevant in playback mode
return true;
} else {
return (m_client != 0);
}
}
bool
JACKAudioIO::isTargetOK() const
{
if (m_mode == Mode::Record) {
// playback target is irrelevant in record mode
return true;
} else {
return (m_client != 0);
}
}
double
JACKAudioIO::getCurrentTime() const
{
if (m_client && m_sampleRate) {
return double(jack_frame_time(m_client)) / double(m_sampleRate);
} else {
return 0.0;
}
}
int
JACKAudioIO::processStatic(jack_nframes_t nframes, void *arg)
{
return ((JACKAudioIO *)arg)->process(nframes);
}
int
JACKAudioIO::xrunStatic(void *arg)
{
return ((JACKAudioIO *)arg)->xrun();
}
void
JACKAudioIO::setup(bool connectRecord, bool connectPlayback)
{
lock_guard<mutex> guard(m_mutex);
int channelsPlay = 2;
int channelsRec = 2;
if (m_source) {
m_source->setSystemPlaybackBlockSize(m_bufferSize);
m_source->setSystemPlaybackSampleRate(m_sampleRate);
if (m_source->getApplicationChannelCount() > 0) {
channelsPlay = m_source->getApplicationChannelCount();
}
}
if (m_target) {
m_target->setSystemRecordBlockSize(m_bufferSize);
m_target->setSystemRecordSampleRate(m_sampleRate);
if (m_target->getApplicationChannelCount() > 0) {
channelsRec = m_target->getApplicationChannelCount();
}
}
if (!m_client) return;
if (channelsPlay == int(m_outputs.size()) &&
channelsRec == int(m_inputs.size())) {
return;
}
const char **playPorts =
jack_get_ports(m_client, NULL, NULL,
JackPortIsPhysical | JackPortIsInput);
const char **capPorts =
jack_get_ports(m_client, NULL, NULL,
JackPortIsPhysical | JackPortIsOutput);
int playPortCount = 0;
while (playPorts && playPorts[playPortCount]) ++playPortCount;
int capPortCount = 0;
while (capPorts && capPorts[capPortCount]) ++capPortCount;
{
ostringstream os;
os << "Setup: have "
<< channelsPlay << " playback channels, "
<< channelsRec << " capture channels, "
<< playPortCount << " playback ports, "
<< capPortCount << " capture ports";
log(os.str());
}
if (m_source) {
while (int(m_outputs.size()) < channelsPlay) {
char name[50];
jack_port_t *port;
sprintf(name, "out %ld", long(m_outputs.size() + 1));
port = jack_port_register(m_client,
name,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput,
0);
if (!port) {
ostringstream os;
os << "ERROR: Failed to create JACK output port " << m_outputs.size();
log(os.str());
return;
} else {
jack_latency_range_t range;
jack_port_get_latency_range(port, JackPlaybackLatency, &range);
m_source->setSystemPlaybackLatency(range.max);
}
if (connectPlayback) {
if (int(m_outputs.size()) < playPortCount) {
jack_connect(m_client,
jack_port_name(port),
playPorts[m_outputs.size()]);
}
}
m_outputs.push_back(port);
}
}
if (m_target) {
while (int(m_inputs.size()) < channelsRec) {
char name[50];
jack_port_t *port;
sprintf(name, "in %ld", long(m_inputs.size() + 1));
port = jack_port_register(m_client,
name,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput,
0);
if (!port) {
ostringstream os;
os << "ERROR: Failed to create JACK input port " << m_inputs.size();
log(os.str());
return;
} else {
jack_latency_range_t range;
jack_port_get_latency_range(port, JackCaptureLatency, &range);
m_target->setSystemRecordLatency(range.max);
}
if (connectRecord) {
if (int(m_inputs.size()) < capPortCount) {
jack_connect(m_client,
capPorts[m_inputs.size()],
jack_port_name(port));
}
}
m_inputs.push_back(port);
}
}
while (int(m_outputs.size()) > channelsPlay) {
vector<jack_port_t *>::iterator itr = m_outputs.end();
--itr;
jack_port_t *port = *itr;
if (port) jack_port_unregister(m_client, port);
m_outputs.erase(itr);
}
while (int(m_inputs.size()) > channelsRec) {
vector<jack_port_t *>::iterator itr = m_inputs.end();
--itr;
jack_port_t *port = *itr;
if (port) jack_port_unregister(m_client, port);
m_inputs.erase(itr);
}
if (m_source) {
m_source->setSystemPlaybackChannelCount(channelsPlay);
}
if (m_target) {
m_target->setSystemRecordChannelCount(channelsRec);
}
if (playPorts) {
jack_free(playPorts);
}
if (capPorts) {
jack_free(capPorts);
}
}
int
JACKAudioIO::process(jack_nframes_t j_nframes)
{
if (!m_mutex.try_lock()) {
return 0;
}
lock_guard<mutex> guard(m_mutex, adopt_lock);
if (j_nframes > INT_MAX) j_nframes = 0;
int nframes = int(j_nframes);
if (m_outputs.empty() && m_inputs.empty()) {
return 0;
}
#ifdef DEBUG_AUDIO_JACK_IO
cout << "JACKAudioIO::process(" << nframes << "): have a purpose in life" << endl;
#endif
#ifdef DEBUG_AUDIO_JACK_IO
if (m_bufferSize != nframes) {
cerr << "WARNING: m_bufferSize != nframes (" << m_bufferSize << " != " << nframes << ")" << endl;
}
#endif
float **inbufs = (float **)alloca(m_inputs.size() * sizeof(float *));
float **outbufs = (float **)alloca(m_outputs.size() * sizeof(float *));
float peakLeft, peakRight;
if (m_target) {
for (int ch = 0; in_range_for(m_inputs, ch); ++ch) {
inbufs[ch] = (float *)jack_port_get_buffer(m_inputs[ch], nframes);
}
peakLeft = 0.0; peakRight = 0.0;
for (int ch = 0; in_range_for(m_inputs, ch); ++ch) {
float peak = 0.0;
for (int i = 0; i < nframes; ++i) {
float sample = fabsf(inbufs[ch][i]);
if (sample > peak) peak = sample;
}
if (ch == 0) peakLeft = peak;
if (ch > 0 || m_inputs.size() == 1) peakRight = peak;
}
m_target->setInputLevels(peakLeft, peakRight);
m_target->putSamples(inbufs, int(m_inputs.size()), nframes);
}
auto gain = Gains::gainsFor(m_outputGain, m_outputBalance, m_outputs.size());
if (m_source) {
for (int ch = 0; in_range_for(m_outputs, ch); ++ch) {
outbufs[ch] = (float *)jack_port_get_buffer(m_outputs[ch], nframes);
}
int received = m_source->getSourceSamples
(outbufs, int(m_outputs.size()), nframes);
peakLeft = 0.0; peakRight = 0.0;
for (int ch = 0; in_range_for(m_outputs, ch); ++ch) {
float peak = 0.0;
for (int i = received; i < nframes; ++i) {
outbufs[ch][i] = 0.0;
}
for (int i = 0; i < nframes; ++i) {
outbufs[ch][i] *= gain[ch];
float sample = fabsf(outbufs[ch][i]);
if (sample > peak) peak = sample;
}
if (ch == 0) peakLeft = peak;
if (ch > 0 || m_outputs.size() == 1) peakRight = peak;
}
m_source->setOutputLevels(peakLeft, peakRight);
} else if (!m_outputs.empty()) {
for (int ch = 0; in_range_for(m_outputs, ch); ++ch) {
for (int i = 0; i < nframes; ++i) {
outbufs[ch][i] = 0.0;
}
}
}
return 0;
}
int
JACKAudioIO::xrun()
{
log("xrun!");
if (m_target) m_target->audioProcessingOverload();
if (m_source) m_source->audioProcessingOverload();
return 0;
}
}
#endif /* HAVE_JACK */
|