jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
11KB

  1. /*
  2. * transport.c -- JACK transport master example client.
  3. *
  4. * Copyright (C) 2003 Jack O'Quin.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #if HAVE_READLINE
  27. #include <readline/readline.h>
  28. #include <readline/history.h>
  29. #endif
  30. #include <jack/jack.h>
  31. #include <jack/transport.h>
  32. /* Use a copy of the readline macro whitespace if it does not exist.
  33. * Not all readline compatible libraries supply the whitespace macro
  34. * (libedit for example), so pull in the copy in those cases too. */
  35. #if !HAVE_READLINE || !defined(whitespace)
  36. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  37. #endif
  38. char *package; /* program name */
  39. int done = 0;
  40. jack_client_t *client;
  41. /* Time and tempo variables. These are global to the entire,
  42. * transport timeline. There is no attempt to keep a true tempo map.
  43. * The default time signature is: "march time", 4/4, 120bpm
  44. */
  45. float time_beats_per_bar = 4.0;
  46. float time_beat_type = 4.0;
  47. double time_ticks_per_beat = 1920.0;
  48. double time_beats_per_minute = 120.0;
  49. volatile int time_reset = 1; /* true when time values change */
  50. /* JACK timebase callback.
  51. *
  52. * Runs in the process thread. Realtime, must not wait.
  53. */
  54. static void timebase(jack_transport_state_t state, jack_nframes_t nframes,
  55. jack_position_t *pos, int new_pos, void *arg)
  56. {
  57. double min; /* minutes since frame 0 */
  58. long abs_tick; /* ticks since frame 0 */
  59. long abs_beat; /* beats since frame 0 */
  60. if (new_pos || time_reset) {
  61. pos->valid = JackPositionBBT;
  62. pos->beats_per_bar = time_beats_per_bar;
  63. pos->beat_type = time_beat_type;
  64. pos->ticks_per_beat = time_ticks_per_beat;
  65. pos->beats_per_minute = time_beats_per_minute;
  66. time_reset = 0; /* time change complete */
  67. /* Compute BBT info from frame number. This is relatively
  68. * simple here, but would become complex if we supported tempo
  69. * or time signature changes at specific locations in the
  70. * transport timeline. */
  71. min = pos->frame / ((double) pos->frame_rate * 60.0);
  72. abs_tick = min * pos->beats_per_minute * pos->ticks_per_beat;
  73. abs_beat = abs_tick / pos->ticks_per_beat;
  74. pos->bar = abs_beat / pos->beats_per_bar;
  75. pos->beat = abs_beat - (pos->bar * pos->beats_per_bar) + 1;
  76. pos->tick = abs_tick - (abs_beat * pos->ticks_per_beat);
  77. pos->bar_start_tick = pos->bar * pos->beats_per_bar *
  78. pos->ticks_per_beat;
  79. pos->bar++; /* adjust start to bar 1 */
  80. #if 0
  81. /* some debug code... */
  82. fprintf(stderr, "\nnew position: %" PRIu32 "\tBBT: %3"
  83. PRIi32 "|%" PRIi32 "|%04" PRIi32 "\n",
  84. pos->frame, pos->bar, pos->beat, pos->tick);
  85. #endif
  86. } else {
  87. /* Compute BBT info based on previous period. */
  88. pos->tick +=
  89. nframes * pos->ticks_per_beat * pos->beats_per_minute
  90. / (pos->frame_rate * 60);
  91. while (pos->tick >= pos->ticks_per_beat) {
  92. pos->tick -= pos->ticks_per_beat;
  93. if (++pos->beat > pos->beats_per_bar) {
  94. pos->beat = 1;
  95. ++pos->bar;
  96. pos->bar_start_tick +=
  97. pos->beats_per_bar
  98. * pos->ticks_per_beat;
  99. }
  100. }
  101. }
  102. }
  103. static void jack_shutdown(void *arg)
  104. {
  105. #if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0400
  106. rl_cleanup_after_signal();
  107. #endif
  108. fprintf(stderr, "JACK shut down, exiting ...\n");
  109. exit(1);
  110. }
  111. static void signal_handler(int sig)
  112. {
  113. jack_client_close(client);
  114. fprintf(stderr, "signal received, exiting ...\n");
  115. exit(0);
  116. }
  117. /* Command functions: see commands[] table following. */
  118. static void com_activate(char *arg)
  119. {
  120. if (jack_activate(client)) {
  121. fprintf(stderr, "cannot activate client");
  122. }
  123. }
  124. static void com_deactivate(char *arg)
  125. {
  126. if (jack_deactivate(client)) {
  127. fprintf(stderr, "cannot deactivate client");
  128. }
  129. }
  130. static void com_exit(char *arg)
  131. {
  132. done = 1;
  133. }
  134. static void com_help(char *); /* forward declaration */
  135. static void com_locate(char *arg)
  136. {
  137. jack_nframes_t frame = 0;
  138. if (*arg != '\0')
  139. frame = atoi(arg);
  140. jack_transport_locate(client, frame);
  141. }
  142. static void com_master(char *arg)
  143. {
  144. int cond = (*arg != '\0');
  145. if (jack_set_timebase_callback(client, cond, timebase, NULL) != 0)
  146. fprintf(stderr, "Unable to take over timebase.\n");
  147. }
  148. static void com_play(char *arg)
  149. {
  150. jack_transport_start(client);
  151. }
  152. static void com_release(char *arg)
  153. {
  154. jack_release_timebase(client);
  155. }
  156. static void com_stop(char *arg)
  157. {
  158. jack_transport_stop(client);
  159. }
  160. /* Change the tempo for the entire timeline, not just from the current
  161. * location. */
  162. static void com_tempo(char *arg)
  163. {
  164. float tempo = 120.0;
  165. if (*arg != '\0')
  166. tempo = atof(arg);
  167. time_beats_per_minute = tempo;
  168. time_reset = 1;
  169. }
  170. /* Set sync timeout in seconds. */
  171. static void com_timeout(char *arg)
  172. {
  173. double timeout = 2.0;
  174. if (*arg != '\0')
  175. timeout = atof(arg);
  176. jack_set_sync_timeout(client, (jack_time_t) (timeout*1000000));
  177. }
  178. /* Command parsing based on GNU readline info examples. */
  179. typedef void cmd_function_t(char *); /* command function type */
  180. /* Transport command table. */
  181. typedef struct {
  182. char *name; /* user printable name */
  183. cmd_function_t *func; /* function to call */
  184. char *doc; /* documentation */
  185. } command_t;
  186. /* command table must be in alphabetical order */
  187. command_t commands[] = {
  188. {"activate", com_activate, "Call jack_activate()"},
  189. {"exit", com_exit, "Exit transport program"},
  190. {"deactivate", com_deactivate, "Call jack_deactivate()"},
  191. {"help", com_help, "Display help text [<command>]"},
  192. {"locate", com_locate, "Locate to frame <position>"},
  193. {"master", com_master, "Become timebase master "
  194. "[<conditionally>]"},
  195. {"play", com_play, "Start transport rolling"},
  196. {"quit", com_exit, "Synonym for `exit'"},
  197. {"release", com_release, "Release timebase"},
  198. {"stop", com_stop, "Stop transport"},
  199. {"tempo", com_tempo, "Set beat tempo <beats_per_min>"},
  200. {"timeout", com_timeout, "Set sync timeout in <seconds>"},
  201. {"?", com_help, "Synonym for `help'" },
  202. {(char *)NULL, (cmd_function_t *)NULL, (char *)NULL }
  203. };
  204. static command_t *find_command(char *name)
  205. {
  206. register int i;
  207. size_t namelen;
  208. if ((name == NULL) || (*name == '\0'))
  209. return ((command_t *)NULL);
  210. namelen = strlen(name);
  211. for (i = 0; commands[i].name; i++)
  212. if (strncmp(name, commands[i].name, namelen) == 0) {
  213. /* make sure the match is unique */
  214. if ((commands[i+1].name) &&
  215. (strncmp(name, commands[i+1].name, namelen) == 0))
  216. return ((command_t *)NULL);
  217. else
  218. return (&commands[i]);
  219. }
  220. return ((command_t *)NULL);
  221. }
  222. static void com_help(char *arg)
  223. {
  224. register int i;
  225. command_t *cmd;
  226. if (!*arg) {
  227. /* print help for all commands */
  228. for (i = 0; commands[i].name; i++) {
  229. printf("%s\t\t%s.\n", commands[i].name,
  230. commands[i].doc);
  231. }
  232. } else if ((cmd = find_command(arg))) {
  233. printf("%s\t\t%s.\n", cmd->name, cmd->doc);
  234. } else {
  235. int printed = 0;
  236. printf("No `%s' command. Valid command names are:\n", arg);
  237. for (i = 0; commands[i].name; i++) {
  238. /* Print in six columns. */
  239. if (printed == 6) {
  240. printed = 0;
  241. printf ("\n");
  242. }
  243. printf ("%s\t", commands[i].name);
  244. printed++;
  245. }
  246. printf("\n\nTry `help [command]\' for more information.\n");
  247. }
  248. }
  249. static void execute_command(char *line)
  250. {
  251. register int i;
  252. command_t *command;
  253. char *word;
  254. /* Isolate the command word. */
  255. i = 0;
  256. while (line[i] && whitespace(line[i]))
  257. i++;
  258. word = line + i;
  259. while (line[i] && !whitespace(line[i]))
  260. i++;
  261. if (line[i])
  262. line[i++] = '\0';
  263. command = find_command(word);
  264. if (!command) {
  265. fprintf(stderr, "%s: No such command. There is `help\'.\n",
  266. word);
  267. return;
  268. }
  269. /* Get argument to command, if any. */
  270. while (whitespace(line[i]))
  271. i++;
  272. word = line + i;
  273. /* invoke the command function. */
  274. (*command->func)(word);
  275. }
  276. /* Strip whitespace from the start and end of string. */
  277. static char *stripwhite(char *string)
  278. {
  279. register char *s, *t;
  280. s = string;
  281. while (whitespace(*s))
  282. s++;
  283. if (*s == '\0')
  284. return s;
  285. t = s + strlen (s) - 1;
  286. while (t > s && whitespace(*t))
  287. t--;
  288. *++t = '\0';
  289. return s;
  290. }
  291. static char *dupstr(char *s)
  292. {
  293. char *r = malloc(strlen(s) + 1);
  294. strcpy(r, s);
  295. return r;
  296. }
  297. /* Readline generator function for command completion. */
  298. static char *command_generator (const char *text, int state)
  299. {
  300. static int list_index, len;
  301. char *name;
  302. /* If this is a new word to complete, initialize now. This
  303. includes saving the length of TEXT for efficiency, and
  304. initializing the index variable to 0. */
  305. if (!state) {
  306. list_index = 0;
  307. len = strlen (text);
  308. }
  309. /* Return the next name which partially matches from the
  310. command list. */
  311. while ((name = commands[list_index].name)) {
  312. list_index++;
  313. if (strncmp(name, text, len) == 0)
  314. return dupstr(name);
  315. }
  316. return (char *) NULL; /* No names matched. */
  317. }
  318. static void command_loop()
  319. {
  320. #if HAVE_READLINE
  321. char *line, *cmd;
  322. char prompt[32];
  323. snprintf(prompt, sizeof(prompt), "%s> ", package);
  324. /* Allow conditional parsing of the ~/.inputrc file. */
  325. rl_readline_name = package;
  326. /* Define a custom completion function. */
  327. rl_completion_entry_function = command_generator;
  328. #else
  329. char line[64] = {0,};
  330. char *cmd = NULL;
  331. #endif
  332. /* Read and execute commands until the user quits. */
  333. while (!done) {
  334. #if HAVE_READLINE
  335. line = readline(prompt);
  336. if (line == NULL) { /* EOF? */
  337. printf("\n"); /* close out prompt */
  338. done = 1;
  339. break;
  340. }
  341. #else
  342. printf("%s> ", package);
  343. fgets(line, sizeof(line), stdin);
  344. line[strlen(line)-1] = '\0';
  345. #endif
  346. /* Remove leading and trailing whitespace from the line. */
  347. cmd = stripwhite(line);
  348. /* If anything left, add to history and execute it. */
  349. if (*cmd)
  350. {
  351. #if HAVE_READLINE
  352. add_history(cmd);
  353. #endif
  354. execute_command(cmd);
  355. }
  356. #if HAVE_READLINE
  357. free(line); /* realine() called malloc() */
  358. #endif
  359. }
  360. }
  361. int main(int argc, char *argv[])
  362. {
  363. jack_status_t status;
  364. /* basename $0 */
  365. package = strrchr(argv[0], '/');
  366. if (package == 0)
  367. package = argv[0];
  368. else
  369. package++;
  370. /* open a connection to the JACK server */
  371. client = jack_client_open (package, JackNullOption, &status);
  372. if (client == NULL) {
  373. fprintf (stderr, "jack_client_open() failed, "
  374. "status = 0x%2.0x\n", status);
  375. return 1;
  376. }
  377. #if !WIN32
  378. signal(SIGQUIT, signal_handler);
  379. signal(SIGHUP, signal_handler);
  380. #endif
  381. signal(SIGTERM, signal_handler);
  382. signal(SIGINT, signal_handler);
  383. jack_on_shutdown(client, jack_shutdown, 0);
  384. if (jack_activate(client)) {
  385. fprintf(stderr, "cannot activate client");
  386. return 1;
  387. }
  388. /* execute commands until done */
  389. command_loop();
  390. jack_client_close(client);
  391. exit(0);
  392. }