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.

460 lines
10KB

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