JACK tools
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.

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