forked from xwax/xwax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jack.c
345 lines (270 loc) · 8.37 KB
/
jack.c
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
/*
* Copyright (C) 2021 Mark Hills <mark@xwax.org>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" 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 this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <jack/jack.h>
#include "device.h"
#include "jack.h"
#define MAX_BLOCK 512 /* samples */
#define SCALE 32768
struct jack {
bool started;
jack_port_t *input_port[DEVICE_CHANNELS],
*output_port[DEVICE_CHANNELS];
};
static jack_client_t *client = NULL;
static unsigned rate,
ndeck = 0,
nstarted = 0;
static struct device *device[4];
/* Interleave samples from a set of JACK buffers into a local buffer */
static void interleave(signed short *buf, jack_default_audio_sample_t *jbuf[],
jack_nframes_t nframes)
{
while (nframes--) {
int n;
for (n = 0; n < DEVICE_CHANNELS; n++) {
*buf = (signed short)(*jbuf[n] * SCALE);
buf++;
jbuf[n]++;
}
}
}
/* Uninterleave samples from a local buffer into a set of JACK buffers */
static void uninterleave(jack_default_audio_sample_t *jbuf[],
signed short *buf, jack_nframes_t nframes)
{
while (nframes--) {
int n;
for (n = 0; n < DEVICE_CHANNELS; n++) {
*jbuf[n] = (jack_default_audio_sample_t)*buf / SCALE;
buf++;
jbuf[n]++;
}
}
}
/* Process the given number of frames of audio on input and output
* of the given JACK device */
static void process_deck(struct device *dv, jack_nframes_t nframes)
{
int n;
jack_default_audio_sample_t *in[DEVICE_CHANNELS], *out[DEVICE_CHANNELS];
jack_nframes_t remain;
struct jack *jack = (struct jack*)dv->local;
assert(dv->timecoder != NULL);
assert(dv->player != NULL);
for (n = 0; n < DEVICE_CHANNELS; n++) {
in[n] = jack_port_get_buffer(jack->input_port[n], nframes);
assert(in[n] != NULL);
out[n] = jack_port_get_buffer(jack->output_port[n], nframes);
assert(out[n] != NULL);
}
/* For large values of nframes, communicate with the timecoder and
* player in smaller blocks */
remain = nframes;
while (remain > 0) {
signed short buf[MAX_BLOCK * DEVICE_CHANNELS];
jack_nframes_t block;
if (remain < MAX_BLOCK)
block = remain;
else
block = MAX_BLOCK;
/* Timecode input */
interleave(buf, in, block);
device_submit(dv, buf, block);
/* Audio output is handle in the inner loop, so that
* we get the timecoder applied in small steps */
device_collect(dv, buf, block);
uninterleave(out, buf, block);
remain -= block;
}
}
/* Process callback, which triggers the processing of audio on all
* decks controlled by this file */
static int process_callback(jack_nframes_t nframes, void *local)
{
size_t n;
for (n = 0; n < ndeck; n++) {
struct jack *jack;
jack = (struct jack*)device[n]->local;
if (jack->started)
process_deck(device[n], nframes);
}
return 0;
}
/* Shutdown callback */
static void shutdown_callback(void *local)
{
}
/* Initialise ourselves as a JACK client, called once per xwax
* session, not per deck */
static int start_jack_client(void)
{
const char *server_name;
jack_status_t status;
client = jack_client_open("xwax", JackNullOption, &status, &server_name);
if (client == NULL) {
if (status & JackServerFailed)
fprintf(stderr, "JACK: Failed to connect\n");
else
fprintf(stderr, "jack_client_open: Failed (0x%x)\n", status);
return -1;
}
if (jack_set_process_callback(client, process_callback, NULL) != 0) {
fprintf(stderr, "JACK: Failed to set process callback\n");
return -1;
}
jack_on_shutdown(client, shutdown_callback, NULL);
rate = jack_get_sample_rate(client);
fprintf(stderr, "JACK: %dHz\n", rate);
return 0;
}
/* Close the JACK client, at which happens when all decks have been
* cleared */
static int stop_jack_client(void)
{
if (jack_client_close(client) != 0) {
fprintf(stderr, "jack_client_close: Failed\n");
return -1;
}
client = NULL;
return 0;
}
/* Register the JACK ports needed for a single deck */
static int register_ports(struct jack *jack, const char *name)
{
size_t n;
assert(DEVICE_CHANNELS == 2);
for (n = 0; n < DEVICE_CHANNELS; n++) {
static const char channel[] = { 'L', 'R' };
char port_name[32];
sprintf(port_name, "%s_timecode_%c", name, channel[n]);
jack->input_port[n] = jack_port_register(client, port_name,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0);
if (jack->input_port[n] == NULL) {
fprintf(stderr, "JACK: Failed to register timecode input port\n");
return -1;
}
sprintf(port_name, "%s_playback_%c", name, channel[n]);
jack->output_port[n] = jack_port_register(client, port_name,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
if (jack->output_port[n] == NULL) {
fprintf(stderr, "JACK: Failed to register audio playback port\n");
return -1;
}
}
return 0;
}
/* Return the sample rate */
static unsigned int sample_rate(struct device *dv)
{
return rate; /* the same rate is used for all decks */
}
/* Start audio rolling on this deck */
static void start(struct device *dv)
{
struct jack *jack = (struct jack*)dv->local;
assert(dv->timecoder != NULL);
assert(dv->player != NULL);
/* On the first call to start, start audio rolling for all decks */
if (nstarted == 0) {
if (jack_activate(client) != 0)
abort();
}
nstarted++;
jack->started = true;
}
/* Stop audio rolling on this deck */
static void stop(struct device *dv)
{
struct jack *jack = (struct jack*)dv->local;
jack->started = false;
nstarted--;
/* On the final stop call, stop JACK rolling */
if (nstarted == 0) {
if (jack_deactivate(client) != 0)
abort();
}
}
/* Close JACK deck and any allocations */
static void clear(struct device *dv)
{
struct jack *jack = (struct jack*)dv->local;
int n;
/* Unregister ports */
for (n = 0; n < DEVICE_CHANNELS; n++) {
if (jack_port_unregister(client, jack->input_port[n]) != 0)
abort();
if (jack_port_unregister(client, jack->output_port[n]) != 0)
abort();
}
free(dv->local);
/* Remove this from the global list, so that potentially xwax could
* continue to run even if a deck is removed */
for (n = 0; n < ndeck; n++) {
if (device[n] == dv)
break;
}
assert(n != ndeck);
if (ndeck == 1) { /* this is the last remaining deck */
stop_jack_client();
ndeck = 0;
} else {
device[n] = device[ndeck - 1]; /* compact the list */
ndeck--;
}
}
static struct device_ops jack_ops = {
.sample_rate = sample_rate,
.start = start,
.stop = stop,
.clear = clear
};
/* Initialise a new JACK deck, creating a new JACK client if required,
* and the approporiate input and output ports */
int jack_init(struct device *dv, const char *name)
{
struct jack *jack;
/* If this is the first JACK deck, initialise the global JACK services */
if (client == NULL) {
if (start_jack_client() == -1)
return -1;
}
jack = malloc(sizeof *jack);
if (jack == NULL) {
perror("malloc");
return -1;
}
jack->started = false;
if (register_ports(jack, name) == -1)
goto fail;
device_init(dv, &jack_ops);
dv->local = jack;
assert(ndeck < sizeof device);
device[ndeck] = dv;
ndeck++;
return 0;
fail:
free(jack);
return -1;
}