-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjack_play_record.c
More file actions
518 lines (447 loc) · 17.4 KB
/
jack_play_record.c
File metadata and controls
518 lines (447 loc) · 17.4 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
/** @file jack_play_record.c
*
* @brief This client will play or record a multichannel
* audio file within JACK
*/
// "standard" libraries
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// libraries/code that require building/linking
#include <pthread.h>
#include <sndfile.h>
#include <jack/jack.h>
#include <pa_ringbuffer.h>
#define JACK_PLAY_RECORD_MAX_PORTS (64)
#define JACK_PLAY_RECORD_MAX_FRAMES (16384)
jack_port_t *jackin_ports[JACK_PLAY_RECORD_MAX_PORTS];
jack_port_t *jackout_ports[JACK_PLAY_RECORD_MAX_PORTS];
jack_client_t *client;
const char *PLAY_NAME = "jack_play";
const char *REC_NAME = "jack_record";
// FIXME w/ enum?
#define PLAY_MODE (SFM_READ)
#define REC_MODE (SFM_WRITE)
#define SND_FNAME_SIZE (2048)
#define JACK_CLIENT_NAME_SIZE (2048)
#define JACK_PORT_NAME_SIZE (2048)
char sndfname[SND_FNAME_SIZE] = {0};
SNDFILE *sndf;
SF_INFO sndfinfo;
int sndmode = PLAY_MODE;
int sndchans = 0;
int waitchans = 0;
int keep_waiting = 0;
int repetitions = 0;
int repetitions_finished = 0;
char jackname[JACK_CLIENT_NAME_SIZE] = {0};
// Interleaved buffers for dumping in/out of the the PaUtilRingBuffer
// There is one for each thread, the fileio thread, and the jack thread
jack_default_audio_sample_t linbufFILE[JACK_PLAY_RECORD_MAX_PORTS * JACK_PLAY_RECORD_MAX_FRAMES];
jack_default_audio_sample_t linbufJACK[JACK_PLAY_RECORD_MAX_PORTS * JACK_PLAY_RECORD_MAX_FRAMES];
PaUtilRingBuffer pa_ringbuf_; // ringbuffer for communicating between threads
PaUtilRingBuffer *pa_ringbuf = &(pa_ringbuf_);
void * ringbuf_memory; // ringbuffer pointer for use with malloc/free
int ringbuf_nframes = JACK_PLAY_RECORD_MAX_FRAMES;
#define ISPOW2(x) ((x) > 0 && !((x) & (x-1)))
int nextpow2(int x) {
if(ISPOW2(x)) {
return x;
}
int power = 2;
while (x >>= 1) power <<= 1;
return (int)(1 << power);
}
void *fileio_function(void *ptr) {
// int type = (int) ptr;
// fprintf(stderr,"Thread - %d\n",type);
// return ptr;
int nframes_write_available, nframes_read_available;
int nframes_read, nframes_written;
ptr = ptr; // mollify compiler
while(1) {
if(sndmode == PLAY_MODE) {
nframes_write_available =
PaUtil_GetRingBufferWriteAvailable(pa_ringbuf);
if( nframes_write_available > 0 && (repetitions==0 || repetitions_finished < repetitions) ) {
// read data from sndf in to interleaved buffer
nframes_read = sf_readf_float(sndf, &(linbufFILE[0]), nframes_write_available);
if(nframes_read < nframes_write_available ) {
sf_seek(sndf, 0, SEEK_SET); // rewind to beginning of file
repetitions_finished += 1;
}
nframes_written = PaUtil_WriteRingBuffer(pa_ringbuf, &(linbufFILE[0]), nframes_read);
}
else {
memset((void *)(&(linbufFILE[0])), 0, sizeof(linbufFILE));
nframes_written = PaUtil_WriteRingBuffer(pa_ringbuf, &(linbufFILE[0]), nframes_write_available);
}
}
else if(sndmode == REC_MODE) {
nframes_read_available = PaUtil_GetRingBufferReadAvailable(pa_ringbuf);
if( nframes_read_available > 0) {
nframes_read = PaUtil_ReadRingBuffer(
pa_ringbuf, &(linbufFILE[0]), nframes_read_available);
nframes_written = sf_writef_float(sndf, &(linbufFILE[0]), nframes_read);
if(nframes_read != nframes_written) {
printf("\nWRN: in fileio_function / REC_MODE\n nframes_read(from ring buffer)=%d\n nframes_written(to file)=%d\n",
nframes_read, nframes_written);
}
}
}
else{
/* FIXME, catch this error */
}
usleep(85000); // 85k ~ 16k (buf) / 192k (max rate) * 1e6 (usecs) // sched_yield();
} // end while(1)
}
int waiting_check(void) {
int cidx, connectedchans = 0;
// if waitchans > 0, let's wait until waitchans channels have been connected
if(waitchans > 0) {
/* count number of connected channels */
for(cidx=0; cidx<sndchans; cidx++) {
if(sndmode == PLAY_MODE) {
connectedchans += jack_port_connected(jackout_ports[cidx]) ? 1 : 0;
}
else if(sndmode == REC_MODE) {
connectedchans += jack_port_connected(jackin_ports[cidx]) ? 1 : 0;
}
else {
/* FIXME catch error */
}
}
/* if connected channels is large enough,
break out of this loop and start playing/recording */
if(connectedchans >= waitchans) {
return 0;
}
else {
return 1;
}
}
return 0;
}
/**
* The process callback for this JACK application is called in a
* special realtime thread once for each audio cycle.
*
* This client uses a PortAudio ringbuffer as buffer "in RAM"
* Reading from the disk directly in the jack process callback
* will likely yield dropouts, and we want to avoid that.
*/
int jack_process (jack_nframes_t nframes, void *arg)
{
int cidx, sidx;
jack_nframes_t fidx, nframes_read_available, nframes_write_available;
jack_nframes_t nframes_read, nframes_written;
if(keep_waiting) {
// don't touch ringbuffer, and nothing will happen re: the file
if(sndmode == PLAY_MODE) {
for(cidx=0; cidx<sndchans; cidx++) {
jack_default_audio_sample_t *jackbuf = jack_port_get_buffer(jackout_ports[cidx], nframes);
for(fidx=0; fidx<nframes; fidx++) {
*(jackbuf++) = 0.0;
}
}
}
else if(sndmode == REC_MODE) {
for(cidx=0; cidx<sndchans; cidx++) {
// is it necessary to do anything here?
// jack_default_audio_sample_t *jackbuf = jack_port_get_buffer(jackin_ports[cidx], nframes);
}
}
keep_waiting = waiting_check();
return 0;
}
// silence compiler
arg = arg;
// jack_default_audio_sample_t *in, *out;
if(sndmode == PLAY_MODE) {
// read from pa_ringbuf
nframes_read_available = PaUtil_GetRingBufferReadAvailable(pa_ringbuf);
if(nframes_read_available < nframes) {
/* FIXME, report underflow */
/* FIXME, zero out (nframes - nframes_to_read) number of frames
in linbufJACK */
}
nframes_read = PaUtil_ReadRingBuffer(
pa_ringbuf, &(linbufJACK[0]), nframes);
if(nframes_read != nframes) {
printf("Underflow reading from pa_ringbuf\n");
}
// get jack buffers as needed, and write directly in to those buffers
for(cidx=0; cidx<sndchans; cidx++) {
jack_default_audio_sample_t *jackbuf = jack_port_get_buffer(jackout_ports[cidx], nframes);
for(fidx=0; fidx<nframes; fidx++) {
*(jackbuf++) = linbufJACK[(fidx*sndchans) + cidx];
}
}
} // end PLAY_MODE
else if(sndmode == REC_MODE) {
// get pointers for all jack port buffers
jack_default_audio_sample_t *jackbufs[JACK_PLAY_RECORD_MAX_PORTS];
for(cidx=0; cidx<sndchans; cidx++) {
jackbufs[cidx] = jack_port_get_buffer(jackin_ports[cidx], nframes);
}
// write to linbufJACK one sample at a time
// set outer loop over frames/samples
sidx = 0; // use sample index to book-keep current index in to linbufJACK
for(fidx=0; fidx<nframes; fidx++) {
// set inner loop over channels/jackbufs
for(cidx=0; cidx<sndchans; cidx++) {
// this is naive, but might be fast enough
linbufJACK[sidx++] = jackbufs[cidx][fidx];
}
}
nframes_write_available = PaUtil_GetRingBufferWriteAvailable(pa_ringbuf);
if( nframes_write_available < nframes) {
/* FIXME, report overflow problem */
}
nframes_written = PaUtil_WriteRingBuffer(
pa_ringbuf, &(linbufJACK[0]), nframes);
if( nframes_written != nframes) {
/* FIXME, report overflow */
}
} // end REC_MODE
else {
/* FIXME, catch this error */
}
return 0;
}
/**
* JACK calls this shutdown_callback if the server ever shuts down or
* decides to disconnect the client.
*/
void jack_shutdown (void *arg)
{
free(ringbuf_memory);
arg=arg; /* silence compiler */
exit (1);
}
void usage(void) {
printf("\n\n");
printf("Usage: jack_play_record [OPTION...] [-p play.wav | -c chans -r rec.wav]\n");
printf(" -h, print this help text\n");
printf(" -c, specify the number of channels (required for recording)\n");
printf(" -n, specify the name of the jack client\n");
printf(" -f, specify the intended nframes for use with jack server\n");
printf(" note, that this will save on memory, but is unsafe if the\n");
printf(" jack server nframes value is ever increased\n");
printf(" -e, specify number of repetitions, default=0 (infinite)\n");
printf(" -w, wait until W ports have been connected before playing or recording\n");
printf("\n\n");
}
void fyi(void) {
char *play_record = sndmode==PLAY_MODE ? "play from" : "record to";
printf("\nINFO: Attempting to\n %s %s, where\n channels=%d, and \n client-name='%s'\n\n",
play_record, sndfname, sndchans, jackname);
}
int main (int argc, char *argv[])
{
// const char **ports;
pthread_t fileio_thread;
int thr = 1;
const char *server_name = NULL;
jack_options_t options = JackNullOption;
jack_status_t status;
int cidx, c, err;
char portname[JACK_PORT_NAME_SIZE] = {0};
while ((c = getopt (argc, argv, "p:r:c:n:f:w:e:h")) != -1)
switch (c)
{
case 'p':
sndmode = PLAY_MODE;
snprintf(sndfname, SND_FNAME_SIZE, "%s", optarg);
break;
case 'r':
sndmode = REC_MODE;
snprintf(sndfname, SND_FNAME_SIZE, "%s", optarg);
break;
case 'c':
sndchans = atoi(optarg);
break;
case 'n':
snprintf(jackname, JACK_CLIENT_NAME_SIZE, "%s", optarg);
break;
case 'f':
ringbuf_nframes = atoi(optarg);
break;
case 'w':
waitchans = atoi(optarg);
break;
case 'e':
repetitions = atoi(optarg);
break;
case 'h':
usage();
return 0;
default:
abort ();
}
/* after parsing args, if sndfname is empty, then just print usage */
if(0 == strlen((const char *)sndfname)) {
usage();
return 0;
}
/* ensure there's a reasonable jack client name if not already set */
if( jackname[0] == 0 ) {
snprintf(jackname, JACK_CLIENT_NAME_SIZE, "%s", \
(sndmode==PLAY_MODE) ? (PLAY_NAME) : (REC_NAME)) ;
}
/* let user know what settings have been parsed */
fyi();
/* force 0 <= waitchans <= sndchans */
waitchans = waitchans < 0 ? 0 : waitchans;
waitchans = waitchans > sndchans ? sndchans : waitchans;
if(waitchans > 0) {
keep_waiting = 1;
}
/* open a client connection to the JACK server */
client = jack_client_open(jackname, options, &status, server_name);
if (client == NULL) {
fprintf (stderr, "jack_client_open() failed, "
"status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
exit (1);
}
if (status & JackServerStarted) {
fprintf (stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
snprintf(jackname, JACK_CLIENT_NAME_SIZE, "%s", jack_get_client_name(client));
fprintf(stderr, "unique name `%s' assigned\n", &(jackname[0]));
}
/* with an unconfigured jack client, we can do some sndfile prep, like get the sample rate*/
if( sndmode == PLAY_MODE ){
sndfinfo.format = 0;
sndf = sf_open((const char *)sndfname, sndmode, &sndfinfo);
sndchans = sndfinfo.channels;
}
else if(sndmode == REC_MODE ){
/* if recording, error out if channels is not specified */
if( sndchans <= 0 || sndchans > 1024 ) {
printf("\nFor recording, number of channels must be specified with the -c option. Here is an example:\n");
printf(" jack_play_record -r file_to_write_to.wav -c 4\n");
exit(1);
}
sndfinfo.samplerate = jack_get_sample_rate(client);
sndfinfo.channels = sndchans;
sndfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
sndf = sf_open((const char *)sndfname, sndmode, &sndfinfo);
}
int sferr = sf_error(sndf);
if(sferr) {
printf("Tried to open %s and obtained this error code from sf_error: %d\n",
sndfname, sferr);
}
/* tell the JACK server to call `process()' whenever
there is work to be done.
*/
jack_set_process_callback (client, jack_process, 0);
/* tell the JACK server to call `jack_shutdown()' if
it ever shuts down, either entirely, or if it
just decides to stop calling us.
*/
jack_on_shutdown (client, jack_shutdown, 0);
/* FIXME, throw error if file sample rate and jack sample rate are different */
/* create jack ports */
for(cidx=0; cidx<sndchans; cidx++) {
if(sndmode == PLAY_MODE){
snprintf(portname, JACK_PORT_NAME_SIZE, "out_%02d", cidx+1);
jackout_ports[cidx] = jack_port_register(client, portname,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
/* printf("jackout_ports[%d] = %p\n", cidx, jackout_ports[cidx]); */
}
else if(sndmode == REC_MODE) {
snprintf(portname, JACK_PORT_NAME_SIZE, "in_%02d", cidx+1);
jackin_ports[cidx] = jack_port_register (client, portname,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0);
/* printf("jackin_ports[%d] = %p\n", cidx, jackin_ports[cidx]); */
}
// else {} , FIXME
}
/* Let's set up a pa_ringbuffer, for single producer, single consumer */
/* ensure ringbuf_nframes is a power of 2 */
ringbuf_nframes = nextpow2(ringbuf_nframes);
/* malloc space for pa_ringbuffer */
ringbuf_memory = malloc(
sizeof(jack_default_audio_sample_t) * sndchans * 4 * ringbuf_nframes);
// ring_buffer_size_t PaUtil_InitializeRingBuffer ( PaUtilRingBuffer * rbuf,
// ring_buffer_size_t elementSizeBytes,
// ring_buffer_size_t elementCount,
// void * dataPtr )
err = PaUtil_InitializeRingBuffer(pa_ringbuf,
sizeof(jack_default_audio_sample_t) * sndchans,
4 * ringbuf_nframes,
ringbuf_memory);
if(err) {
printf("encountered error code (%d) trying to call PaUtil_InitializeRingBuffer\n",err);
}
// if we're playing a file, let's pre-load the ring buffer with some data
if(sndmode == PLAY_MODE){
int nframes_write_available = PaUtil_GetRingBufferWriteAvailable(pa_ringbuf);
int nframes_read = sf_readf_float(sndf, &(linbufJACK[0]), nframes_write_available);
int nframes_written = PaUtil_WriteRingBuffer(pa_ringbuf, &(linbufJACK[0]), nframes_read);
if(nframes_write_available != nframes_read) {
printf("WRN: in pre-loading pa_ringbuf, nframes_write_available = %d, nframes_read = %d\n",
nframes_write_available, nframes_read);
}
if(nframes_read != nframes_written) {
printf("WRN: in pre-loading pa_ringbuf, nframes_read = %d, nframes_read = %d\n",
nframes_read, nframes_read);
}
}
// start the fileio_thread
pthread_create(&fileio_thread, NULL, *fileio_function, (void *) &(thr));
/* Tell the JACK server that we are ready to roll. Our
* process() callback will start running now. */
if (jack_activate (client)) {
fprintf (stderr, "cannot activate client");
exit (1);
}
/* Connect the ports. You can't do this before the client is
* activated, because we can't make connections to clients
* that aren't running. Note the confusing (but necessary)
* orientation of the driver backend ports: playback ports are
* "input" to the backend, and capture ports are "output" from
* it.
*/
/* FIXME add command line arg to auto-connect to a block... */
// ports = jack_get_ports (client, NULL, NULL,
// JackPortIsPhysical|JackPortIsOutput);
// if (ports == NULL) {
// fprintf(stderr, "no physical capture ports\n");
// exit (1);
// }
// if (jack_connect (client, ports[0], jack_port_name (input_port))) {
// fprintf (stderr, "cannot connect input ports\n");
// }
// free (ports);
// ports = jack_get_ports (client, NULL, NULL,
// JackPortIsPhysical|JackPortIsInput);
// if (ports == NULL) {
// fprintf(stderr, "no physical playback ports\n");
// exit (1);
// }
// if (jack_connect (client, jack_port_name (output_port), ports[0])) {
// fprintf (stderr, "cannot connect output ports\n");
// }
// free (ports);
/* keep running until stopped by the user */
sleep (-1);
/* this is never reached but if the program
had some other way to exit besides being killed,
they would be important to call.
*/
jack_client_close (client);
exit (0);
}