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
|
/*
* Copyright (C) 2011 Mark Hills <mark@pogo.org.uk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <jack/jack.h>
#include "device.h"
#include "jack.h"
#include "player.h"
#include "timecoder.h"
#define MAX_DECKS 4
#define MAX_BLOCK 512 /* samples */
#define SCALE 32768
struct jack_t {
int started;
jack_port_t *input_port[DEVICE_CHANNELS],
*output_port[DEVICE_CHANNELS];
};
static jack_client_t *client = NULL;
static int rate,
decks = 0,
started = 0;
static struct device_t *device[MAX_DECKS];
/* 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)
{
int n;
while (nframes--) {
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)
{
int n;
while (nframes--) {
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_t *dv, jack_nframes_t nframes)
{
int n;
jack_default_audio_sample_t *in[DEVICE_CHANNELS], *out[DEVICE_CHANNELS];
jack_nframes_t remain;
struct jack_t *jack = (struct jack_t*)dv->local;
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);
if (dv->timecoder)
timecoder_submit(dv->timecoder, buf, block);
/* Audio output -- handle in the same loop for finer granularity */
player_collect(dv->player, buf, block, rate);
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)
{
int n;
struct jack_t *jack;
for (n = 0; n < decks; n++) {
jack = (struct jack_t*)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_t *jack, const char *name)
{
int n;
const char channel[] = { 'L', 'R' };
char port_name[32];
assert(DEVICE_CHANNELS == 2);
for (n = 0; n < DEVICE_CHANNELS; n++) {
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_t *dv)
{
return rate; /* the same rate is used for all decks */
}
/* Start audio rolling on this deck */
static void start(struct device_t *dv)
{
struct jack_t *jack = (struct jack_t*)dv->local;
/* On the first call to start, start audio rolling for all decks */
if (started == 0) {
if (jack_activate(client) != 0)
abort();
}
started++;
jack->started = 1;
}
/* Stop audio rolling on this deck */
static void stop(struct device_t *dv)
{
struct jack_t *jack = (struct jack_t*)dv->local;
jack->started = 0;
started--;
/* On the final stop call, stop JACK rolling */
if (started == 0) {
if (jack_deactivate(client) != 0)
abort();
}
}
/* Close JACK deck and any allocations */
static void clear(struct device_t *dv)
{
struct jack_t *jack = (struct jack_t*)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 < decks; n++) {
if (device[n] == dv)
break;
}
assert(n != decks);
if (decks == 1) { /* this is the last remaining deck */
stop_jack_client();
decks = 0;
} else {
device[n] = device[decks - 1]; /* compact the list */
decks--;
}
}
static struct device_type_t jack_type = {
.pollfds = NULL,
.handle = NULL, /* done via JACK's own callbacks */
.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_t *dv, const char *name)
{
struct jack_t *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(struct jack_t));
if (!jack) {
perror("malloc");
return -1;
}
jack->started = 0;
if (register_ports(jack, name) == -1)
goto fail;
dv->local = jack;
dv->type = &jack_type;
assert(decks < MAX_DECKS);
device[decks] = dv;
decks++;
return 0;
fail:
free(jack);
return -1;
}
|