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.

264 lines
6.0KB

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