[go: up one dir, main page]

File: jack_a.c

package info (click to toggle)
timidity 2.13.2-40.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 13,132 kB
  • sloc: ansic: 158,296; sh: 3,778; makefile: 1,156; tcl: 1,048; lisp: 499; perl: 285; ruby: 126
file content (519 lines) | stat: -rw-r--r-- 12,173 bytes parent folder | download | duplicates (14)
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
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    jack_a.c - Copyright (C) 2003 Takashi Iwai <tiwai@suse.de>

    This program 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 2 of the License, or
    (at your option) any later version.

    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 for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    jack_a.c

    Functions to play sound on the JACK system


    Since the model of JACK (so-called "pull" model) doesn't match
    with the current model of TiMidity ("push" model), this audio
    driver is implemented with another intermediate ring buffer.

    The audio data from timidity (S16 or U8 PCM) is converted to
    JACK audio stream(s) in float and stored once on the ring buffer,
    which is read eventually in the client's process callback and
    copied to the JACK buffers.  Note that this buffer is independent
    from the audio-queue.  (Yes, it's re-invention of wheels, but we
    cannot handle dynamic memory allocation in JACK's process thread.)

    The buffer size options (-B) specify the size of the ring buffer.
    To get the best perfomance, you'll need to set up them to match
    with the parameter of JACK server.

*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef NO_STRING_H /* for memmove */
#include <string.h>
#else
#include <strings.h>
#endif

#include <jack/jack.h>

#include "timidity.h"
#include "common.h"
#include "output.h"
#include "controls.h"
#include "timer.h"
#include "instrum.h"
#include "playmidi.h"
#include "miditrace.h"

/*
 */
static int open_jack(void);
static void close_jack(void);
static int write_jack(char *buf, int32 nbytes);
static int actl_jack(int request, void *arg);
static int detect(void);


/*
 */

#define dpm jack_play_mode

PlayMode dpm = {
	DEFAULT_RATE,
	PE_16BIT|PE_SIGNED,
	PF_PCM_STREAM|PF_CAN_TRACE|PF_BUFF_FRAGM_OPT,
	-1,
	{0},
	"JACK device", 'j',
	NULL,
	open_jack,
	close_jack,
	write_jack,
	actl_jack,
	detect
};

/*
 * simple ring-buffer
 */

struct tm_ringbuf {
	long rdptr, wrptr;	/* read, write pointers (not bound in ringbuffer size!) */
	int size;		/* ring buffer size */
	jack_default_audio_sample_t *buf[2];	/* left, right buffers */
};

static void ringbuf_init(struct tm_ringbuf *rbuf, int size, int channels)
{
	int i;

	memset(rbuf, 0, sizeof(*rbuf));
	rbuf->size = size;
	for (i = 0; i < channels; i++)
		rbuf->buf[i] = (jack_default_audio_sample_t *)safe_malloc(sizeof(jack_default_audio_sample_t) * size);
	rbuf->rdptr = rbuf->wrptr = 0;
}

static void ringbuf_destroy(struct tm_ringbuf *rbuf)
{
	int i;
	for (i = 0; i < 2; i++) {
		free(rbuf->buf[i]);
		rbuf->buf[i] = NULL;
	}
	rbuf->rdptr = rbuf->wrptr = 0;
}

/* get avaialble samples for read */
inline static int ringbuf_get_available(struct tm_ringbuf *rbuf)
{
	return rbuf->wrptr - rbuf->rdptr;
}

/* get empty sample spaces for write */
inline static int ringbuf_get_empty(struct tm_ringbuf *rbuf)
{
	return rbuf->size - ringbuf_get_available(rbuf);
}

static int ringbuf_bound_readable(struct tm_ringbuf *rbuf, int size)
{
	int rdptr = rbuf->rdptr % rbuf->size;
	if (rdptr + size >= rbuf->size)
		size = rbuf->size - rdptr;
	return size;
}

inline static jack_default_audio_sample_t *
ringbuf_get_readbuf(struct tm_ringbuf *rbuf, int c)
{
	return rbuf->buf[c] + (rbuf->rdptr % rbuf->size);
}

inline static void ringbuf_read_advance(struct tm_ringbuf *rbuf, int size)
{
	rbuf->rdptr += size;
}

static int ringbuf_bound_writable(struct tm_ringbuf *rbuf, int size)
{
	int wrptr = rbuf->wrptr % rbuf->size;
	if (wrptr + size >= rbuf->size)
		size = rbuf->size - wrptr;
	return size;
}

inline static jack_default_audio_sample_t *
ringbuf_get_writebuf(struct tm_ringbuf *rbuf, int c)
{
	return rbuf->buf[c] + (rbuf->wrptr % rbuf->size);
}

inline static void ringbuf_write_advance(struct tm_ringbuf *rbuf, int size)
{
	rbuf->wrptr += size;
}

inline static void ringbuf_clear(struct tm_ringbuf *rbuf)
{
	rbuf->wrptr = rbuf->rdptr = 0;
}


/*
 * jack control context
 */
struct tm_jack {
	jack_client_t *client;
	jack_port_t *ports[2];

	int channels;		/* number of channels */
	int sample_16bit;	/* 16bit sample */
	int shift;		/* sample bit shift */
	int frag_size;		/* buffer fragment size (in samples) */
	int frags;		/* buffer fragments */

	pthread_cond_t cond;
	pthread_mutex_t lock;
	int running;
	int shutdown;

	struct tm_ringbuf rbuf;
};


/*
 * jack process callback.
 * here we only copy the stream data from the ring buffer.
 */

static int transfer_callback(jack_nframes_t nframes, void *arg)
{
	struct tm_jack *ctx = (struct tm_jack *)arg;
	int i;
	int size;
	jack_default_audio_sample_t *outbuf[2];

	for (i = 0; i < ctx->channels; i++)
		outbuf[i] = (jack_default_audio_sample_t *)jack_port_get_buffer(ctx->ports[i], nframes);

	if (! ctx->running) {
		/* not running yet, set silence and quit */
		for (i = 0; i < ctx->channels; i++)
			memset(outbuf[i], 0, sizeof(jack_default_audio_sample_t) * nframes);
		return 0;
	}

	size = ringbuf_get_available(&ctx->rbuf);
	if (size > nframes)
		size = nframes;

	while (size > 0) {
		int size1 = ringbuf_bound_readable(&ctx->rbuf, size);
		for (i = 0; i < ctx->channels; i++) {
			memcpy(outbuf[i], ringbuf_get_readbuf(&ctx->rbuf, i),
			       size1 * sizeof(jack_default_audio_sample_t));
			outbuf[i] += size1;
		}
		ringbuf_read_advance(&ctx->rbuf, size1);
		size -= size1;
	}
	/* wake up the main thread */
	pthread_cond_signal(&ctx->cond);
	return 0;
}

static void shutdown_callback(void *arg)
{
	struct tm_jack *ctx = (struct tm_jack *)arg;
	if (! ctx->shutdown)
		safe_exit(1);
}


/*
 */

static struct tm_jack jack_ctx;

#define TIMIDITY_JACK_CLIENT_NAME	"TiMidity"
#define TIMIDITY_JACK_PORT_NAME		"port_%d"

static int detect(void)
{
	jack_client_t *client;
	client = jack_client_new(TIMIDITY_JACK_CLIENT_NAME);
	if (! client)
		return 0;
	jack_client_close(client);
	return 1; /* found */
}

static int open_jack(void)
{
	int i, rate;
	int ret_val = 0;
	struct tm_jack *ctx = &jack_ctx;
	const char **dst_ports;

	memset(ctx, 0, sizeof(*ctx));

	ctx->client = jack_client_new(TIMIDITY_JACK_CLIENT_NAME);
	if (! ctx->client)
		return -1;

	jack_set_process_callback(ctx->client, transfer_callback, ctx);
	jack_on_shutdown(ctx->client, shutdown_callback, ctx);

	dpm.encoding &= ~(PE_ULAW|PE_ALAW|PE_BYTESWAP);
	/* check channels */
	if (dpm.encoding & PE_MONO)
		ctx->channels = 1;
	else
		ctx->channels = 2;

	/* sample bit check */
	ctx->sample_16bit = (dpm.encoding & PE_16BIT) ? 1 : 0;
	if (ctx->sample_16bit)
		dpm.encoding |= PE_SIGNED; /* S16 only */
	else
		dpm.encoding &= ~PE_SIGNED; /* U8 only */

	/* check sample bit shift */
	ctx->shift = 0;
	if (ctx->channels > 1)
		ctx->shift++;
	if (ctx->sample_16bit)
		ctx->shift++;

	for (i = 0; i < ctx->channels; i++) {
		char name[32];
		sprintf(name, TIMIDITY_JACK_PORT_NAME, i + 1);
		ctx->ports[i] = jack_port_register(ctx->client, name,
						   JACK_DEFAULT_AUDIO_TYPE,
						   JackPortIsOutput, 0);
		if (! ctx->ports[i]) {
			ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
				  "Cannot register a JACK port '%s'", name);
			jack_client_close(ctx->client);
			return -1;
		}
	}

	/* check the sample rate.
	 * if not match, correct the rate
	 */
	rate = jack_get_sample_rate(ctx->client);
	if (rate != dpm.rate) {
		dpm.rate = rate;
		ret_val = 1;
	}

	/* get the buffer sizes */
	if (dpm.extra_param[1] != 0)
		ctx->frag_size = dpm.extra_param[1];
	else
		ctx->frag_size = audio_buffer_size;
	if (dpm.extra_param[0] == 0)
		ctx->frags = 2;
	else
		ctx->frags = dpm.extra_param[0];

	/* initialize rest stuffs */
	pthread_cond_init(&ctx->cond, NULL);
	pthread_mutex_init(&ctx->lock, NULL);
	ringbuf_init(&ctx->rbuf, ctx->frag_size * ctx->frags, ctx->channels);
	ctx->running = 0;

	/* rock it baby */
	if (jack_activate(ctx->client) < 0) {
		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Cannot activate JACK engine");
		jack_client_close(ctx->client);
		ctx->client = NULL;
		return -1;
	}

	/* 
	 * it seems the JACK port connection must be done after
	 * activating jack client...
	 */
	/* detect destination ports */
	dst_ports = jack_get_ports(ctx->client, NULL, NULL,
				   JackPortIsInput|JackPortIsPhysical);
	if (dst_ports) {
		/* connect them */
		for (i = 0; dst_ports[i] && i < ctx->channels; i++) {
			if (jack_connect(ctx->client, jack_port_name(ctx->ports[i]), dst_ports[i]))
				break;
		}
		free(dst_ports);
	}

	return ret_val;
}

/*
 * close callback
 */
static void close_jack(void)
{
	struct tm_jack *ctx = &jack_ctx;
	if (ctx->client) {
		ctx->shutdown = 1;
		ctx->running = 0;
		jack_deactivate(ctx->client);
		sleep(2);
		jack_client_close(ctx->client);
		sleep(2);
		ringbuf_destroy(&ctx->rbuf);
		pthread_cond_destroy(&ctx->cond);
	}
}

/*
 * convert 16bit PCM to JACK float [-1,1]
 */
static void convert_stream_16(struct tm_jack *ctx, int c, int size, short *buf)
{
	int i;
	jack_default_audio_sample_t *inbuf = ringbuf_get_writebuf(&ctx->rbuf, c);
	for (i = 0; i < size; i++, buf += ctx->channels, inbuf++) {
		/* well, we can use ftol() in C99 but here let's leave
		 * the optimization for the compiler...
		 */
		jack_default_audio_sample_t val;
		val = (jack_default_audio_sample_t)*buf / 32768.0;
		*inbuf = val;
	}
}

/*
 * convert 8bit PCM to JACK float [-1,1]
 */
static void convert_stream_8(struct tm_jack *ctx, int c, int size, char *buf)
{
	int i;
	jack_default_audio_sample_t *inbuf = ringbuf_get_writebuf(&ctx->rbuf, c);
	for (i = 0; i < size; i++, buf += ctx->channels, inbuf++) {
		signed char cval = *buf ^ 0x80; /* to signed char */
		*inbuf = (jack_default_audio_sample_t)cval / 128.0;
	}
}

/*
 * write callback
 */
static int write_jack(char *buf, int32 nbytes)
{
	struct tm_jack *ctx = &jack_ctx;
	int nframes;
	int i;

	nframes = nbytes >> ctx->shift;
	if (nframes <= 0)
		return 0;

	for (;;) {
		int size = ringbuf_get_empty(&ctx->rbuf);
		if (size > nframes)
			size = nframes;
		while (size > 0) {
			int size1 = ringbuf_bound_writable(&ctx->rbuf, size);
			if (ctx->sample_16bit) {
				short *sbuf = (short *)buf;
				for (i = 0; i < ctx->channels; i++, sbuf++)
					convert_stream_16(ctx, i, size1, sbuf);
			} else {
				char *sbuf = (char *)buf;
				for (i = 0; i < ctx->channels; i++, sbuf++)
					convert_stream_8(ctx, i, size1, sbuf);
			}
			ringbuf_write_advance(&ctx->rbuf, size1);
			/* set running flag */
			ctx->running = 1;
			nframes -= size1;
			if (! nframes)
				return 0;
			buf += size1 << ctx->shift;
			size -= size1;
		}

		/* blocking behavior: sleep until the process thread
		 * wakes up...
		 */
		pthread_cond_wait(&ctx->cond, &ctx->lock);
	}
}


/*
 * audio control callback
 */
static int actl_jack(int request, void *arg)
{
	struct tm_jack *ctx = &jack_ctx;

	switch (request) {
	case PM_REQ_GETFRAGSIZ:
		if (ctx->frag_size == 0)
			return -1;
		*((int *)arg) = ctx->frag_size;
		return 0;

	case PM_REQ_GETQSIZ:
		if (ctx->frag_size == -1)
			return -1;
		*((int *)arg) = ctx->frag_size * ctx->frags;
		return 0;

	case PM_REQ_GETFILLABLE:
		*((int *)arg) = ringbuf_get_empty(&ctx->rbuf);
		return 0;
    
	case PM_REQ_GETFILLED:
		*((int *)arg) = ringbuf_get_available(&ctx->rbuf);
		return 0;

	case PM_REQ_GETSAMPLES:
		*((int *)arg) = ctx->rbuf.rdptr;
		return 0;

	case PM_REQ_FLUSH:
		if (ctx->running) {
			while (ringbuf_get_available(&ctx->rbuf) > 0)
				pthread_cond_wait(&ctx->cond, &ctx->lock);
		}
		/* fallthrough */
	case PM_REQ_DISCARD:
		ctx->running = 0;
		ringbuf_clear(&ctx->rbuf);
		return 0;

	}
	return -1;
}