-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjackaudioio.cpp
More file actions
553 lines (485 loc) · 16.7 KB
/
jackaudioio.cpp
File metadata and controls
553 lines (485 loc) · 16.7 KB
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//C++ Classes that wrap JACK
//Copyright 2007 Alex Norman
//
//This file is part of JACKC++.
//
//JACKC++ is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//JACKC++ is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with JACKC++. If not, see <http://www.gnu.org/licenses/>.
#include "jackaudioio.hpp"
#include <iostream>
#include <errno.h>
#include <sstream>
#include <algorithm>
#include <unistd.h>
template <typename T>
std::string ToString(T aValue){
std::stringstream ss;
ss << aValue;
return ss.str();
}
/* callback for jack's error messages */
static void error_callback (const char *msg) {
std::cerr << "Jack:" << msg << std::endl;
std::cerr.flush();
}
static void shutdown_callback (void *arg) {
return ((JackCpp::AudioIO *)arg)->jackShutdownCallback();
}
void JackCpp::AudioIO::jackShutdownCallback(){
std::cerr << std::endl << "jack has shutdown" << std::endl;
}
int JackCpp::AudioIO::jackProcessCallback(jack_nframes_t nframes, void *arg){
JackCpp::AudioIO* callbackjackobject = (AudioIO * )arg;
return callbackjackobject->jackToClassAudioCallback(nframes);
}
int JackCpp::AudioIO::jackToClassAudioCallback(jack_nframes_t nframes){
//read in commands
while(mCmdBuffer.getReadSpace() > 0){
cmd_t cmd;
mCmdBuffer.read(cmd);
switch(cmd){
case add_in_port:
//we will have tested that we have this capacity, so we resize the buffer
//to include the new port
mJackInBuf.resize(mJackInBuf.size() + 1);
mNumInputPorts++;
break;
case add_out_port:
//we will have tested that we have this capacity, so we resize the buffer
//to include the new port
mJackOutBuf.resize(mJackOutBuf.size() + 1);
mNumOutputPorts++;
break;
}
}
//get the input and output buffers
for(unsigned int i = 0; i < mNumInputPorts; i++)
mJackInBuf[i] = (jack_default_audio_sample_t *) jack_port_get_buffer ( mInputPorts[i], nframes);
for(unsigned int i = 0; i < mNumOutputPorts; i++)
mJackOutBuf[i] = (jack_default_audio_sample_t *) jack_port_get_buffer ( mOutputPorts[i], nframes);
return audioCallback(nframes, mJackInBuf, mJackOutBuf);
}
jack_client_t * JackCpp::AudioIO::client(){
return mJackClient;
}
JackCpp::AudioIO::AudioIO(std::string name, unsigned int inPorts, unsigned int outPorts, bool startServer)
throw(std::runtime_error) : mCmdBuffer(256,true)
{
createClient(name, inPorts, outPorts, startServer);
}
JackCpp::AudioIO::AudioIO() : mCmdBuffer(256,true), mJackClient(NULL)
{
}
void JackCpp::AudioIO::createClient(std::string name, unsigned int inPorts, unsigned int outPorts, bool startServer)
throw(std::runtime_error)
{
if (mJackClient) {
//XXX close it
}
jack_options_t jack_open_options = JackNullOption;
if (startServer == false)
jack_open_options = JackNoStartServer;
mJackState = notActive;
//set the error callback
jack_set_error_function (error_callback);
/* try to become a client of the JACK server */
if ((mJackClient = jack_client_open (name.c_str(), jack_open_options, NULL)) == 0) {
throw std::runtime_error("cannot create client jack server not running?");
}
#ifdef __APPLE__
else {
// because the mac version of jack is being totally LAME
sleep(2);
}
#endif
//set the shutdown callback
jack_on_shutdown (mJackClient, shutdown_callback, this);
//allocate ports
if (inPorts > 0){
for(unsigned int i = 0; i < inPorts; i++){
std::string portname = "input";
portname.append(ToString(i));
mInputPorts.push_back(
jack_port_register (mJackClient, portname.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0));
mPortNames.push_back(portname);
}
//reserve the data for the jack callback buffers
for(unsigned int i = 0; i < mInputPorts.size(); i++)
mJackInBuf.push_back(NULL);
}
if (outPorts > 0){
for(unsigned int i = 0; i < outPorts; i++){
std::string portname = "output";
portname.append(ToString(i));
mOutputPorts.push_back(
jack_port_register (mJackClient, portname.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0));
mPortNames.push_back(portname);
}
//reserve the data for the jack callback buffers
for(unsigned int i = 0; i < mOutputPorts.size(); i++)
mJackOutBuf.push_back(NULL);
}
//set up the callback
if(0 != jack_set_process_callback (mJackClient, JackCpp::AudioIO::jackProcessCallback, this))
throw std::runtime_error("cannot register process callback");
}
JackCpp::AudioIO::~AudioIO(){
//make sure to deactiveate the client if we need to
switch(mJackState){
case active:
stop();
close();
break;
case notActive:
close();
break;
default:
break;
//do nothing
}
}
bool JackCpp::AudioIO::portExists(std::string name){
//see if the port name exists
std::vector<std::string>::iterator it;
it = std::find(mPortNames.begin(),mPortNames.end(), name);
if (it != mPortNames.end())
return true;
else
return false;
}
void JackCpp::AudioIO::reserveOutPorts(unsigned int num)
throw(std::runtime_error)
{
if(getState() == active)
throw std::runtime_error("reserving ports while the client is running is not supported yet.");
mOutputPorts.reserve(num);
mJackOutBuf.reserve(num);
}
void JackCpp::AudioIO::reserveInPorts(unsigned int num)
throw(std::runtime_error)
{
if(getState() == active)
throw std::runtime_error("reserving ports while the client is running is not supported yet.");
mInputPorts.reserve(num);
mJackInBuf.reserve(num);
}
unsigned int JackCpp::AudioIO::inPorts(){
return mInputPorts.size();
}
unsigned int JackCpp::AudioIO::outPorts(){
return mOutputPorts.size();
}
unsigned int JackCpp::AudioIO::addInPort(std::string name)
throw(std::runtime_error)
{
if (mJackState == active && mInputPorts.size() == mInputPorts.capacity())
throw std::runtime_error("trying to add input ports while the client is running and there are not reserved ports");
if(portExists(name)){
std::string ret_string("cannot register new inport: ");
ret_string.append(name);
ret_string.append(" port already exists with that name");
throw std::runtime_error(ret_string);
}
//allocate the item in the vector
jack_port_t * newPort = jack_port_register (mJackClient, name.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
if(newPort == NULL){
std::string ret_string("cannot register new inport: ");
ret_string.append(name);
throw std::runtime_error(ret_string);
}
mInputPorts.push_back(newPort);
mPortNames.push_back(name);
//if we're active then send a command indicating this change
if (mJackState == active) {
//loop while there isn't space to write
while(mCmdBuffer.getWriteSpace() == 0);
mCmdBuffer.write(add_in_port);
} else
mJackInBuf.push_back(NULL);
return mInputPorts.size() - 1;
}
//add an output port, if we are active then deactivate and reactivate after
//maybe we can do this more intelligently in the future?
unsigned int JackCpp::AudioIO::addOutPort(std::string name)
throw(std::runtime_error)
{
if (mJackState == active && mOutputPorts.size() == mOutputPorts.capacity())
throw std::runtime_error("trying to add output ports while the client is running and there are not reserved ports");
if(portExists(name)){
std::string ret_string("cannot register new outport: ");
ret_string.append(name);
ret_string.append(" port already exists with that name");
throw std::runtime_error(ret_string);
}
//allocate the item in the vector
jack_port_t * newPort = jack_port_register (mJackClient, name.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if(newPort == NULL){
std::string ret_string("cannot register new outport: ");
ret_string.append(name);
throw std::runtime_error(ret_string);
}
mOutputPorts.push_back(newPort);
mPortNames.push_back(name);
//if we're active then send a command indicating this change
if (mJackState == active) {
//loop while there isn't space to write
while(mCmdBuffer.getWriteSpace() == 0);
mCmdBuffer.write(add_out_port);
} else
mJackOutBuf.push_back(NULL);
return mOutputPorts.size() - 1;
}
unsigned int JackCpp::AudioIO::removeInPort(std::string name)
throw(std::runtime_error)
{
// if the client is active this presents all sorts of issues
if (mJackState == active)
throw std::runtime_error("removing ports while the client is active is not supported");
std::vector<std::string>::iterator name_iter;
name_iter = std::find(mPortNames.begin(), mPortNames.end(), name);
if (name_iter == mPortNames.end())
throw std::runtime_error("cannot remove non-existent port: " + name);
std::vector<jack_port_t *>::iterator port_iter = mInputPorts.begin();
for (; port_iter != mInputPorts.end(); ++port_iter) {
if (std::string(jack_port_short_name(*port_iter)) == name)
break;
}
if (port_iter == mInputPorts.end())
throw std::runtime_error("could not find port in port list!");
if (jack_port_unregister(mJackClient, *port_iter))
throw std::runtime_error("could not unregister port!");
mInputPorts.erase(port_iter);
mPortNames.erase(name_iter);
mJackInBuf.pop_back();
return mInputPorts.size();
}
unsigned int JackCpp::AudioIO::removeOutPort(std::string name)
throw(std::runtime_error)
{
// if the client is active this presents all sorts of issues
if (mJackState == active)
throw std::runtime_error("removing ports while the client is active is not supported");
std::vector<std::string>::iterator name_iter;
name_iter = std::find(mPortNames.begin(), mPortNames.end(), name);
if (name_iter == mPortNames.end())
throw std::runtime_error("cannot remove non-existent port: " + name);
std::vector<jack_port_t *>::iterator port_iter = mOutputPorts.begin();
for (; port_iter != mOutputPorts.end(); ++port_iter) {
if (std::string(jack_port_short_name(*port_iter)) == name)
break;
}
if (port_iter == mOutputPorts.end())
throw std::runtime_error("could not find port in port list!");
if (jack_port_unregister(mJackClient, *port_iter))
throw std::runtime_error("could not unregister port!");
mOutputPorts.erase(port_iter);
mPortNames.erase(name_iter);
mJackOutBuf.pop_back();
return mOutputPorts.size();
}
void JackCpp::AudioIO::connectTo(unsigned int index, std::string destPortName)
throw(std::range_error, std::runtime_error)
{
int connect_ret;
if (mJackState != active)
throw std::runtime_error("client must be active before connecting ports");
if(index < mOutputPorts.size()){
connect_ret = jack_connect(mJackClient, jack_port_name(mOutputPorts[index]), destPortName.c_str());
if(connect_ret != 0 && connect_ret != EEXIST){
std::string ret_string("cannot connect source: ");
ret_string.append(jack_port_name(mOutputPorts[index]));
ret_string.append(" to dest: ");
ret_string.append(destPortName);
ret_string.append(" does dest exist?");
throw std::range_error(ret_string);
}
} else
throw std::range_error("outport index out of range");
}
void JackCpp::AudioIO::connectFrom(unsigned int index, std::string sourcePortName)
throw(std::range_error, std::runtime_error)
{
int connect_ret;
if (mJackState != active)
throw std::runtime_error("client must be active before connecting ports");
if(index < mInputPorts.size()){
connect_ret = jack_connect(mJackClient, sourcePortName.c_str(), jack_port_name(mInputPorts[index]));
if(connect_ret != 0 && connect_ret != EEXIST){
std::string ret_string("cannot connect source: ");
ret_string.append(sourcePortName);
ret_string.append(" to dest: ");
ret_string.append(jack_port_name(mInputPorts[index]));
ret_string.append(" does source exist?");
throw std::range_error(ret_string);
}
} else
throw std::range_error("inport index out of range");
}
//XXX should the "free" free the names that these ports point too as well?
void JackCpp::AudioIO::connectToPhysical(unsigned int index, unsigned physical_index)
throw(std::range_error, std::runtime_error)
{
const char **ports;
if (mJackState != active)
throw std::runtime_error("client must be active before connecting ports");
if (index > mOutputPorts.size())
throw std::range_error("outport index out of range");
ports = jack_get_ports (mJackClient, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
if(ports == NULL){
throw std::range_error("no physical inports to connect to");
}
//make sure the port exists
for(unsigned int i = 0; i <= physical_index; i++){
if(ports[i] == NULL){
free(ports);
throw std::range_error("physical inport index out of range");
}
}
connectTo(index, ports[physical_index]);
free(ports);
}
//XXX should the "free" free the names that these ports point too as well?
void JackCpp::AudioIO::connectFromPhysical(unsigned int index, unsigned physical_index)
throw(std::range_error, std::runtime_error)
{
const char **ports;
if (mJackState != active)
throw std::runtime_error("client must be active before connecting ports");
if (index > mInputPorts.size())
throw std::range_error("inport index out of range");
ports = jack_get_ports (mJackClient, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
if(ports == NULL){
throw std::range_error("no physical outports to connect to");
}
//make sure the port exists
for(unsigned int i = 0; i <= physical_index; i++){
if(ports[i] == NULL){
free(ports);
throw std::range_error("physical outport index out of range");
}
}
connectFrom(index, ports[physical_index]);
free(ports);
}
void JackCpp::AudioIO::disconnectInPort(unsigned int index)
throw(std::range_error, std::runtime_error)
{
if (mJackState != active)
throw std::runtime_error("client must be active before disconnecting ports");
if(index < mInputPorts.size()){
jack_port_disconnect(mJackClient, mInputPorts[index]);
} else
throw std::range_error("inport index out of range");
}
void JackCpp::AudioIO::disconnectOutPort(unsigned int index)
throw(std::range_error, std::runtime_error)
{
if (mJackState != active)
throw std::runtime_error("client must be active before disconnecting ports");
if(index < mOutputPorts.size()){
jack_port_disconnect(mJackClient, mOutputPorts[index]);
} else
throw std::range_error("outport index out of range");
}
unsigned int JackCpp::AudioIO::numConnectionsInPort(unsigned int index)
throw(std::range_error)
{
if(index < mInputPorts.size())
return jack_port_connected(mInputPorts[index]);
else
throw std::range_error("inport index out of range");
}
unsigned int JackCpp::AudioIO::numConnectionsOutPort(unsigned int index)
throw(std::range_error)
{
if(index < mOutputPorts.size())
return jack_port_connected(mOutputPorts[index]);
else
throw std::range_error("outport index out of range");
}
unsigned int JackCpp::AudioIO::numPhysicalDestinationPorts(){
const char **ports;
unsigned int cnt = 0;
ports = jack_get_ports (mJackClient, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
if (ports != NULL){
while(ports[cnt] != NULL)
cnt++;
free(ports);
return cnt;
} else
return 0;
}
unsigned int JackCpp::AudioIO::numPhysicalSourcePorts(){
const char **ports;
unsigned int cnt = 0;
//XXX is this really correct? we should get the naming right...
ports = jack_get_ports (mJackClient, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
if (ports != NULL){
while(ports[cnt] != NULL)
cnt++;
free(ports);
return cnt;
} else
return 0;
}
std::string JackCpp::AudioIO::getInputPortName(unsigned int index)
throw(std::range_error)
{
if(index < mInputPorts.size())
return std::string(jack_port_name(mInputPorts[index]));
else
throw std::range_error("inport index out of range");
}
std::string JackCpp::AudioIO::getOutputPortName(unsigned int index)
throw(std::range_error)
{
if(index < mOutputPorts.size())
return std::string(jack_port_name(mOutputPorts[index]));
else
throw std::range_error("outport index out of range");
}
void JackCpp::AudioIO::start()
throw(std::runtime_error)
{
//update these so that the callback can use them
if(mJackState != active){
mNumOutputPorts = mOutputPorts.size();
mNumInputPorts = mInputPorts.size();
}
if (jack_activate(mJackClient) != 0)
throw std::runtime_error("cannot activate the client");
mJackState = active;
}
void JackCpp::AudioIO::stop()
throw(std::runtime_error)
{
if (jack_deactivate(mJackClient) != 0)
throw std::runtime_error("cannot deactivate the client");
mJackState = notActive;
}
void JackCpp::AudioIO::close()
throw(std::runtime_error)
{
if (jack_client_close(mJackClient) != 0)
throw std::runtime_error("cannot close the client");
mJackState = closed;
}
float JackCpp::AudioIO::getCpuLoad(){
return jack_cpu_load(mJackClient);
}
jack_nframes_t JackCpp::AudioIO::getSampleRate(){
return jack_get_sample_rate(mJackClient);
}
jack_nframes_t JackCpp::AudioIO::getBufferSize(){
return jack_get_buffer_size(mJackClient);
}