jack1 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.

136 lines
2.8KB

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <stdlib.h>
  6. #include <jack/jack.h>
  7. #include <jack/transport.h>
  8. typedef struct {
  9. volatile jack_nframes_t guard1;
  10. volatile jack_transport_info_t info;
  11. volatile jack_nframes_t guard2;
  12. } guarded_transport_info_t;
  13. guarded_transport_info_t now;
  14. jack_client_t *client;
  15. void
  16. showtime ()
  17. {
  18. guarded_transport_info_t current;
  19. int tries = 0;
  20. /* Since "now" is updated from the process() thread every
  21. * buffer period, we must copy it carefully to avoid getting
  22. * an incoherent hash of multiple versions. */
  23. do {
  24. /* Throttle the busy wait if we don't get the a clean
  25. * copy very quickly. */
  26. if (tries > 10) {
  27. usleep (20);
  28. tries = 0;
  29. }
  30. current = now;
  31. tries++;
  32. } while (current.guard1 != current.guard2);
  33. if (current.info.valid & JackTransportPosition)
  34. printf ("frame: %lu ", current.info.frame);
  35. else
  36. printf ("frame: [-] ");
  37. if (current.info.valid & JackTransportState)
  38. printf ("state: %d ", current.info.transport_state);
  39. else
  40. printf ("state: [-] ");
  41. if (current.info.valid & JackTransportLoop)
  42. printf ("loop: %lu-%lu ", current.info.loop_start,
  43. current.info.loop_end);
  44. else
  45. printf ("loop: [-] ");
  46. if (current.info.valid & JackTransportBBT)
  47. printf ("BBT: %d|%d|%d\n", current.info.bar,
  48. current.info.beat, current.info.tick);
  49. else
  50. printf ("BBT: [-]\n");
  51. }
  52. int
  53. process (jack_nframes_t nframes, void *arg)
  54. {
  55. /* The guard flags contain a running counter of sufficiently
  56. * high resolution, that showtime() can detect whether the
  57. * last update is complete. */
  58. now.guard1 = jack_frame_time(client);
  59. jack_get_transport_info (client, &now.info);
  60. now.guard2 = now.guard1;
  61. return 0;
  62. }
  63. void
  64. jack_shutdown (void *arg)
  65. {
  66. exit (1);
  67. }
  68. void
  69. signal_handler (int sig)
  70. {
  71. jack_client_close (client);
  72. fprintf (stderr, "signal received, exiting ...\n");
  73. exit (0);
  74. }
  75. int
  76. main (int argc, char *argv[])
  77. {
  78. /* try to become a client of the JACK server */
  79. if ((client = jack_client_new ("showtime")) == 0) {
  80. fprintf (stderr, "jack server not running?\n");
  81. return 1;
  82. }
  83. signal (SIGQUIT, signal_handler);
  84. signal (SIGTERM, signal_handler);
  85. signal (SIGHUP, signal_handler);
  86. signal (SIGINT, signal_handler);
  87. /* tell the JACK server to call `process()' whenever
  88. there is work to be done.
  89. */
  90. jack_set_process_callback (client, process, 0);
  91. /* tell the JACK server to call `jack_shutdown()' if
  92. it ever shuts down, either entirely, or if it
  93. just decides to stop calling us.
  94. */
  95. jack_on_shutdown (client, jack_shutdown, 0);
  96. /* tell the JACK server that we are ready to roll */
  97. if (jack_activate (client)) {
  98. fprintf (stderr, "cannot activate client");
  99. return 1;
  100. }
  101. while (1) {
  102. usleep (100000);
  103. showtime ();
  104. }
  105. jack_client_close (client);
  106. exit (0);
  107. }