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.

265 lines
6.1KB

  1. /** @file tw.c
  2. *
  3. * @brief This simple client demonstrates the basic features of JACK
  4. * as they would be used by many applications.
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <jack/jack.h>
  13. jack_port_t *input_port;
  14. jack_port_t *output_port;
  15. jack_client_t *client;
  16. /* a simple state machine for this client */
  17. volatile enum {
  18. Init,
  19. Run,
  20. Exit
  21. } client_state = Init;
  22. static void signal_handler(int sig)
  23. {
  24. jack_client_close(client);
  25. fprintf(stderr, "signal received, exiting ...\n");
  26. exit(0);
  27. }
  28. /**
  29. * The process callback for this JACK application is called in a
  30. * special realtime thread once for each audio cycle.
  31. *
  32. * This client follows a simple rule: when the JACK transport is
  33. * running, copy the input port to the output. When it stops, exit.
  34. */
  35. static int
  36. _process (jack_nframes_t nframes)
  37. {
  38. jack_default_audio_sample_t *in, *out;
  39. jack_transport_state_t ts = jack_transport_query(client, NULL);
  40. if (ts == JackTransportRolling) {
  41. if (client_state == Init)
  42. client_state = Run;
  43. in = jack_port_get_buffer (input_port, nframes);
  44. out = jack_port_get_buffer (output_port, nframes);
  45. memcpy (out, in,
  46. sizeof (jack_default_audio_sample_t) * nframes);
  47. } else if (ts == JackTransportStopped) {
  48. if (client_state == Run) {
  49. client_state = Exit;
  50. return -1; // to stop the thread
  51. }
  52. }
  53. return 0;
  54. }
  55. static void* jack_thread(void *arg)
  56. {
  57. jack_client_t* client = (jack_client_t*) arg;
  58. while (1) {
  59. jack_nframes_t frames = jack_cycle_wait (client);
  60. int status = _process(frames);
  61. jack_cycle_signal (client, status);
  62. /*
  63. Possibly do something else after signaling next clients in the graph
  64. */
  65. /* End condition */
  66. if (status != 0)
  67. return 0;
  68. }
  69. /* not reached*/
  70. return 0;
  71. }
  72. /*
  73. static void* jack_thread(void *arg)
  74. {
  75. jack_client_t* client = (jack_client_t*) arg;
  76. while (1) {
  77. jack_nframes_t frames;
  78. int status;
  79. // cycle 1
  80. frames = jack_cycle_wait (client);
  81. status = _process(frames);
  82. jack_cycle_signal (client, status);
  83. // cycle 2
  84. frames = jack_cycle_wait (client);
  85. status = _process(frames);
  86. jack_cycle_signal (client, status);
  87. // cycle 3
  88. frames = jack_cycle_wait (client);
  89. status = _process(frames);
  90. jack_cycle_signal (client, status);
  91. // cycle 4
  92. frames = jack_cycle_wait (client);
  93. status = _process(frames);
  94. jack_cycle_signal (client, status);
  95. }
  96. return 0;
  97. }
  98. */
  99. /**
  100. * JACK calls this shutdown_callback if the server ever shuts down or
  101. * decides to disconnect the client.
  102. */
  103. static void
  104. jack_shutdown (void *arg)
  105. {
  106. fprintf(stderr, "JACK shut down, exiting ...\n");
  107. exit (1);
  108. }
  109. int
  110. main (int argc, char *argv[])
  111. {
  112. const char **ports;
  113. const char *client_name;
  114. const char *server_name = NULL;
  115. jack_options_t options = JackNullOption;
  116. jack_status_t status;
  117. if (argc >= 2) { /* client name specified? */
  118. client_name = argv[1];
  119. if (argc >= 3) { /* server name specified? */
  120. server_name = argv[2];
  121. options |= JackServerName;
  122. }
  123. } else { /* use basename of argv[0] */
  124. client_name = strrchr(argv[0], '/');
  125. if (client_name == 0) {
  126. client_name = argv[0];
  127. } else {
  128. client_name++;
  129. }
  130. }
  131. /* open a client connection to the JACK server */
  132. client = jack_client_open (client_name, options, &status, server_name);
  133. if (client == NULL) {
  134. fprintf (stderr, "jack_client_open() failed, "
  135. "status = 0x%2.0x\n", status);
  136. if (status & JackServerFailed) {
  137. fprintf (stderr, "Unable to connect to JACK server\n");
  138. }
  139. exit (1);
  140. }
  141. if (status & JackServerStarted) {
  142. fprintf (stderr, "JACK server started\n");
  143. }
  144. if (status & JackNameNotUnique) {
  145. client_name = jack_get_client_name(client);
  146. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  147. }
  148. /* tell the JACK server to call `process()' whenever
  149. there is work to be done.
  150. */
  151. if (jack_set_process_thread(client, jack_thread, client) < 0)
  152. exit(1);
  153. /* tell the JACK server to call `jack_shutdown()' if
  154. it ever shuts down, either entirely, or if it
  155. just decides to stop calling us.
  156. */
  157. jack_on_shutdown (client, jack_shutdown, 0);
  158. /* display the current sample rate.
  159. */
  160. printf ("engine sample rate: %" PRIu32 "\n",
  161. jack_get_sample_rate (client));
  162. /* create two ports */
  163. input_port = jack_port_register (client, "input",
  164. JACK_DEFAULT_AUDIO_TYPE,
  165. JackPortIsInput, 0);
  166. output_port = jack_port_register (client, "output",
  167. JACK_DEFAULT_AUDIO_TYPE,
  168. JackPortIsOutput, 0);
  169. if ((input_port == NULL) || (output_port == NULL)) {
  170. fprintf(stderr, "no more JACK ports available\n");
  171. exit (1);
  172. }
  173. /* Tell the JACK server that we are ready to roll. Our
  174. * process() callback will start running now. */
  175. if (jack_activate (client)) {
  176. fprintf (stderr, "cannot activate client");
  177. exit (1);
  178. }
  179. /* Connect the ports. You can't do this before the client is
  180. * activated, because we can't make connections to clients
  181. * that aren't running. Note the confusing (but necessary)
  182. * orientation of the driver backend ports: playback ports are
  183. * "input" to the backend, and capture ports are "output" from
  184. * it.
  185. */
  186. ports = jack_get_ports (client, NULL, NULL,
  187. JackPortIsPhysical|JackPortIsOutput);
  188. if (ports == NULL) {
  189. fprintf(stderr, "no physical capture ports\n");
  190. exit (1);
  191. }
  192. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  193. fprintf (stderr, "cannot connect input ports\n");
  194. }
  195. jack_free (ports);
  196. ports = jack_get_ports (client, NULL, NULL,
  197. JackPortIsPhysical|JackPortIsInput);
  198. if (ports == NULL) {
  199. fprintf(stderr, "no physical playback ports\n");
  200. exit (1);
  201. }
  202. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  203. fprintf (stderr, "cannot connect output ports\n");
  204. }
  205. jack_free (ports);
  206. /* install a signal handler to properly quits jack client */
  207. signal(SIGQUIT, signal_handler);
  208. signal(SIGTERM, signal_handler);
  209. signal(SIGHUP, signal_handler);
  210. signal(SIGINT, signal_handler);
  211. /* keep running until the transport stops */
  212. while (client_state != Exit) {
  213. sleep (1);
  214. }
  215. jack_client_close (client);
  216. exit (0);
  217. }