transport.c 11.5 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
/*
 *  transport.c -- JACK transport master example client.
 *
 *  Copyright (C) 2003 Jack O'Quin.
5
 *
sletz's avatar
sletz committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
27
#if HAVE_READLINE
sletz's avatar
sletz committed
28
29
#include <readline/readline.h>
#include <readline/history.h>
30
#endif
sletz's avatar
sletz committed
31
32
33
#include <jack/jack.h>
#include <jack/transport.h>

Karl Linden's avatar
Karl Linden committed
34
35
36
37
/* Use a copy of the readline macro whitespace if it does not exist.
 * Not all readline compatible libraries supply the whitespace macro
 * (libedit for example), so pull in the copy in those cases too. */
#if !HAVE_READLINE || !defined(whitespace)
38
39
#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
#endif
sletz's avatar
sletz committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

char *package;				/* program name */
int done = 0;
jack_client_t *client;

/* Time and tempo variables.  These are global to the entire,
 * transport timeline.  There is no attempt to keep a true tempo map.
 * The default time signature is: "march time", 4/4, 120bpm
 */
float time_beats_per_bar = 4.0;
float time_beat_type = 4.0;
double time_ticks_per_beat = 1920.0;
double time_beats_per_minute = 120.0;
volatile int time_reset = 1;		/* true when time values change */

/* JACK timebase callback.
 *
 * Runs in the process thread.  Realtime, must not wait.
 */
59
static void timebase(jack_transport_state_t state, jack_nframes_t nframes,
sletz's avatar
sletz committed
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
	      jack_position_t *pos, int new_pos, void *arg)
{
	double min;			/* minutes since frame 0 */
	long abs_tick;			/* ticks since frame 0 */
	long abs_beat;			/* beats since frame 0 */

	if (new_pos || time_reset) {

		pos->valid = JackPositionBBT;
		pos->beats_per_bar = time_beats_per_bar;
		pos->beat_type = time_beat_type;
		pos->ticks_per_beat = time_ticks_per_beat;
		pos->beats_per_minute = time_beats_per_minute;

		time_reset = 0;		/* time change complete */

		/* Compute BBT info from frame number.  This is relatively
		 * simple here, but would become complex if we supported tempo
		 * or time signature changes at specific locations in the
		 * transport timeline. */

		min = pos->frame / ((double) pos->frame_rate * 60.0);
		abs_tick = min * pos->beats_per_minute * pos->ticks_per_beat;
		abs_beat = abs_tick / pos->ticks_per_beat;

		pos->bar = abs_beat / pos->beats_per_bar;
		pos->beat = abs_beat - (pos->bar * pos->beats_per_bar) + 1;
		pos->tick = abs_tick - (abs_beat * pos->ticks_per_beat);
		pos->bar_start_tick = pos->bar * pos->beats_per_bar *
			pos->ticks_per_beat;
		pos->bar++;		/* adjust start to bar 1 */

#if 0
		/* some debug code... */
		fprintf(stderr, "\nnew position: %" PRIu32 "\tBBT: %3"
			PRIi32 "|%" PRIi32 "|%04" PRIi32 "\n",
			pos->frame, pos->bar, pos->beat, pos->tick);
#endif

	} else {

		/* Compute BBT info based on previous period. */
		pos->tick +=
			nframes * pos->ticks_per_beat * pos->beats_per_minute
			/ (pos->frame_rate * 60);

		while (pos->tick >= pos->ticks_per_beat) {
			pos->tick -= pos->ticks_per_beat;
			if (++pos->beat > pos->beats_per_bar) {
				pos->beat = 1;
				++pos->bar;
				pos->bar_start_tick +=
					pos->beats_per_bar
					* pos->ticks_per_beat;
			}
		}
	}
}

sletz's avatar
Cleanup    
sletz committed
119
static void jack_shutdown(void *arg)
sletz's avatar
sletz committed
120
121
122
123
124
125
126
127
{
#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0400
	rl_cleanup_after_signal();
#endif
	fprintf(stderr, "JACK shut down, exiting ...\n");
	exit(1);
}

sletz's avatar
Cleanup    
sletz committed
128
static void signal_handler(int sig)
sletz's avatar
sletz committed
129
130
131
132
133
134
135
136
{
	jack_client_close(client);
	fprintf(stderr, "signal received, exiting ...\n");
	exit(0);
}

/* Command functions: see commands[] table following. */

sletz's avatar
Cleanup    
sletz committed
137
static void com_activate(char *arg)
sletz's avatar
sletz committed
138
139
140
141
142
143
{
	if (jack_activate(client)) {
		fprintf(stderr, "cannot activate client");
	}
}

sletz's avatar
Cleanup    
sletz committed
144
static void com_deactivate(char *arg)
sletz's avatar
sletz committed
145
146
147
148
149
150
{
	if (jack_deactivate(client)) {
		fprintf(stderr, "cannot deactivate client");
	}
}

sletz's avatar
Cleanup    
sletz committed
151
static void com_exit(char *arg)
sletz's avatar
sletz committed
152
153
154
155
{
	done = 1;
}

sletz's avatar
Cleanup    
sletz committed
156
static void com_help(char *);			/* forward declaration */
sletz's avatar
sletz committed
157

sletz's avatar
Cleanup    
sletz committed
158
static void com_locate(char *arg)
sletz's avatar
sletz committed
159
160
161
162
163
164
165
166
167
{
	jack_nframes_t frame = 0;

	if (*arg != '\0')
		frame = atoi(arg);

	jack_transport_locate(client, frame);
}

sletz's avatar
Cleanup    
sletz committed
168
static void com_master(char *arg)
sletz's avatar
sletz committed
169
170
171
172
173
174
{
	int cond = (*arg != '\0');
	if (jack_set_timebase_callback(client, cond, timebase, NULL) != 0)
		fprintf(stderr, "Unable to take over timebase.\n");
}

sletz's avatar
Cleanup    
sletz committed
175
static void com_play(char *arg)
sletz's avatar
sletz committed
176
177
178
179
{
	jack_transport_start(client);
}

sletz's avatar
Cleanup    
sletz committed
180
static void com_release(char *arg)
sletz's avatar
sletz committed
181
182
183
184
{
	jack_release_timebase(client);
}

sletz's avatar
Cleanup    
sletz committed
185
static void com_stop(char *arg)
sletz's avatar
sletz committed
186
187
188
189
190
191
{
	jack_transport_stop(client);
}

/* Change the tempo for the entire timeline, not just from the current
 * location. */
sletz's avatar
Cleanup    
sletz committed
192
static void com_tempo(char *arg)
sletz's avatar
sletz committed
193
194
195
196
197
198
199
200
201
202
203
{
	float tempo = 120.0;

	if (*arg != '\0')
		tempo = atof(arg);

	time_beats_per_minute = tempo;
	time_reset = 1;
}

/* Set sync timeout in seconds. */
sletz's avatar
Cleanup    
sletz committed
204
static void com_timeout(char *arg)
sletz's avatar
sletz committed
205
206
207
208
209
210
211
212
213
{
	double timeout = 2.0;

	if (*arg != '\0')
		timeout = atof(arg);

	jack_set_sync_timeout(client, (jack_time_t) (timeout*1000000));
}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* Toggle between play and stop state */
static void com_toggle(char *arg)
{       
	jack_position_t current;
	jack_transport_state_t transport_state;

	transport_state = jack_transport_query (client, &current);

	switch (transport_state) {
	case JackTransportStopped:
        	com_play( arg );
		break;
	case JackTransportRolling:
        	com_stop( arg );
		break;
	case JackTransportStarting:
		printf ("state: Starting - no transport toggling");
		break;
	default:
		printf ("state: Starting - no transport toggling");
	} 
}

sletz's avatar
sletz committed
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

/* Command parsing based on GNU readline info examples. */

typedef void cmd_function_t(char *);	/* command function type */

/* Transport command table. */
typedef struct {
	char *name;			/* user printable name */
	cmd_function_t *func;		/* function to call */
	char *doc;			/* documentation  */
} command_t;

/* command table must be in alphabetical order */
command_t commands[] = {
	{"activate",	com_activate,	"Call jack_activate()"},
	{"exit",	com_exit,	"Exit transport program"},
	{"deactivate",	com_deactivate,	"Call jack_deactivate()"},
	{"help",	com_help,	"Display help text [<command>]"},
	{"locate",	com_locate,	"Locate to frame <position>"},
	{"master",	com_master,	"Become timebase master "
					"[<conditionally>]"},
	{"play",	com_play,	"Start transport rolling"},
	{"quit",	com_exit,	"Synonym for `exit'"},
	{"release",	com_release,	"Release timebase"},
	{"stop",	com_stop,	"Stop transport"},
	{"tempo",	com_tempo,	"Set beat tempo <beats_per_min>"},
	{"timeout",	com_timeout,	"Set sync timeout in <seconds>"},
264
	{"toggle",	com_toggle,	"Toggle transport rolling"},
sletz's avatar
sletz committed
265
266
267
	{"?",		com_help,	"Synonym for `help'" },
	{(char *)NULL, (cmd_function_t *)NULL, (char *)NULL }
};
268

sletz's avatar
Cleanup    
sletz committed
269
static command_t *find_command(char *name)
sletz's avatar
sletz committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{
	register int i;
	size_t namelen;

	if ((name == NULL) || (*name == '\0'))
		return ((command_t *)NULL);

	namelen = strlen(name);
	for (i = 0; commands[i].name; i++)
		if (strncmp(name, commands[i].name, namelen) == 0) {

			/* make sure the match is unique */
			if ((commands[i+1].name) &&
			    (strncmp(name, commands[i+1].name, namelen) == 0))
				return ((command_t *)NULL);
			else
				return (&commands[i]);
		}
288

sletz's avatar
sletz committed
289
290
291
	return ((command_t *)NULL);
}

sletz's avatar
Cleanup    
sletz committed
292
static void com_help(char *arg)
sletz's avatar
sletz committed
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
{
	register int i;
	command_t *cmd;

	if (!*arg) {
		/* print help for all commands */
		for (i = 0; commands[i].name; i++) {
			printf("%s\t\t%s.\n", commands[i].name,
			       commands[i].doc);
		}

	} else if ((cmd = find_command(arg))) {
		printf("%s\t\t%s.\n", cmd->name, cmd->doc);

	} else {
		int printed = 0;

		printf("No `%s' command.  Valid command names are:\n", arg);

		for (i = 0; commands[i].name; i++) {
			/* Print in six columns. */
			if (printed == 6) {
				printed = 0;
				printf ("\n");
			}

			printf ("%s\t", commands[i].name);
			printed++;
		}

		printf("\n\nTry `help [command]\' for more information.\n");
	}
}

sletz's avatar
Cleanup    
sletz committed
327
static void execute_command(char *line)
sletz's avatar
sletz committed
328
329
330
331
{
	register int i;
	command_t *command;
	char *word;
332

sletz's avatar
sletz committed
333
334
335
336
337
	/* Isolate the command word. */
	i = 0;
	while (line[i] && whitespace(line[i]))
		i++;
	word = line + i;
338

sletz's avatar
sletz committed
339
340
	while (line[i] && !whitespace(line[i]))
		i++;
341

sletz's avatar
sletz committed
342
343
	if (line[i])
		line[i++] = '\0';
344

sletz's avatar
sletz committed
345
	command = find_command(word);
346

sletz's avatar
sletz committed
347
348
349
350
351
	if (!command) {
		fprintf(stderr, "%s: No such command.  There is `help\'.\n",
			word);
		return;
	}
352

sletz's avatar
sletz committed
353
354
355
	/* Get argument to command, if any. */
	while (whitespace(line[i]))
		i++;
356

sletz's avatar
sletz committed
357
	word = line + i;
358

sletz's avatar
sletz committed
359
360
361
362
363
364
	/* invoke the command function. */
	(*command->func)(word);
}


/* Strip whitespace from the start and end of string. */
sletz's avatar
Cleanup    
sletz committed
365
static char *stripwhite(char *string)
sletz's avatar
sletz committed
366
367
368
369
370
371
372
373
374
{
	register char *s, *t;

	s = string;
	while (whitespace(*s))
		s++;

	if (*s == '\0')
		return s;
375

sletz's avatar
sletz committed
376
377
378
379
	t = s + strlen (s) - 1;
	while (t > s && whitespace(*t))
		t--;
	*++t = '\0';
380

sletz's avatar
sletz committed
381
382
	return s;
}
383

sletz's avatar
Cleanup    
sletz committed
384
static char *dupstr(char *s)
sletz's avatar
sletz committed
385
386
387
388
389
{
	char *r = malloc(strlen(s) + 1);
	strcpy(r, s);
	return r;
}
390

sletz's avatar
sletz committed
391
/* Readline generator function for command completion. */
sletz's avatar
Cleanup    
sletz committed
392
static char *command_generator (const char *text, int state)
sletz's avatar
sletz committed
393
394
395
{
	static int list_index, len;
	char *name;
396

sletz's avatar
sletz committed
397
398
399
400
401
402
403
	/* If this is a new word to complete, initialize now.  This
	   includes saving the length of TEXT for efficiency, and
	   initializing the index variable to 0. */
	if (!state) {
		list_index = 0;
		len = strlen (text);
	}
404

sletz's avatar
sletz committed
405
406
407
408
	/* Return the next name which partially matches from the
	   command list. */
	while ((name = commands[list_index].name)) {
		list_index++;
409

sletz's avatar
sletz committed
410
411
412
		if (strncmp(name, text, len) == 0)
			return dupstr(name);
	}
413

sletz's avatar
sletz committed
414
415
416
	return (char *) NULL;		/* No names matched. */
}

sletz's avatar
Cleanup    
sletz committed
417
static void command_loop()
sletz's avatar
sletz committed
418
{
419
#if HAVE_READLINE
sletz's avatar
sletz committed
420
421
422
423
424
425
426
	char *line, *cmd;
	char prompt[32];

	snprintf(prompt, sizeof(prompt), "%s> ", package);

	/* Allow conditional parsing of the ~/.inputrc file. */
	rl_readline_name = package;
427

sletz's avatar
sletz committed
428
429
	/* Define a custom completion function. */
	rl_completion_entry_function = command_generator;
430
#else
431
432
	char line[64] = {0,};
	char *cmd = NULL;
433
#endif
sletz's avatar
sletz committed
434
435
436
437

	/* Read and execute commands until the user quits. */
	while (!done) {

438
#if HAVE_READLINE
sletz's avatar
sletz committed
439
		line = readline(prompt);
440

sletz's avatar
sletz committed
441
442
443
444
445
		if (line == NULL) {	/* EOF? */
			printf("\n");	/* close out prompt */
			done = 1;
			break;
		}
446
#else
447
448
449
		printf("%s> ", package);
		fgets(line, sizeof(line), stdin);
		line[strlen(line)-1] = '\0';
450
#endif
451

sletz's avatar
sletz committed
452
453
454
455
456
457
		/* Remove leading and trailing whitespace from the line. */
		cmd = stripwhite(line);

		/* If anything left, add to history and execute it. */
		if (*cmd)
		{
458
#if HAVE_READLINE
sletz's avatar
sletz committed
459
			add_history(cmd);
460
#endif
sletz's avatar
sletz committed
461
462
			execute_command(cmd);
		}
463

464
#if HAVE_READLINE
sletz's avatar
sletz committed
465
		free(line);		/* realine() called malloc() */
466
#endif
sletz's avatar
sletz committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
	}
}

int main(int argc, char *argv[])
{
	jack_status_t status;

	/* basename $0 */
	package = strrchr(argv[0], '/');
	if (package == 0)
		package = argv[0];
	else
		package++;

	/* open a connection to the JACK server */
	client = jack_client_open (package, JackNullOption, &status);
	if (client == NULL) {
		fprintf (stderr, "jack_client_open() failed, "
			 "status = 0x%2.0x\n", status);
		return 1;
	}

489
#if !WIN32
sletz's avatar
sletz committed
490
491
	signal(SIGQUIT, signal_handler);
	signal(SIGHUP, signal_handler);
492
493
#endif
	signal(SIGTERM, signal_handler);
sletz's avatar
sletz committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
	signal(SIGINT, signal_handler);

	jack_on_shutdown(client, jack_shutdown, 0);

	if (jack_activate(client)) {
		fprintf(stderr, "cannot activate client");
		return 1;
	}

	/* execute commands until done */
	command_loop();

	jack_client_close(client);
	exit(0);
}