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.

254 lines
5.9KB

  1. /** @file simple_client.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. // possibly do something else after signaling next clients in the graph
  62. }
  63. return 0;
  64. }
  65. /*
  66. static void* jack_thread(void *arg)
  67. {
  68. jack_client_t* client = (jack_client_t*) arg;
  69. while (1) {
  70. jack_nframes_t frames;
  71. int status;
  72. // cycle 1
  73. frames = jack_cycle_wait (client);
  74. status = _process(frames);
  75. jack_cycle_signal (client, status);
  76. // cycle 2
  77. frames = jack_cycle_wait (client);
  78. status = _process(frames);
  79. jack_cycle_signal (client, status);
  80. // cycle 3
  81. frames = jack_cycle_wait (client);
  82. status = _process(frames);
  83. jack_cycle_signal (client, status);
  84. // cycle 4
  85. frames = jack_cycle_wait (client);
  86. status = _process(frames);
  87. jack_cycle_signal (client, status);
  88. }
  89. return 0;
  90. }
  91. */
  92. /**
  93. * JACK calls this shutdown_callback if the server ever shuts down or
  94. * decides to disconnect the client.
  95. */
  96. static void
  97. jack_shutdown (void *arg)
  98. {
  99. exit (1);
  100. }
  101. int
  102. main (int argc, char *argv[])
  103. {
  104. const char **ports;
  105. const char *client_name;
  106. const char *server_name = NULL;
  107. jack_options_t options = JackNullOption;
  108. jack_status_t status;
  109. if (argc >= 2) { /* client name specified? */
  110. client_name = argv[1];
  111. if (argc >= 3) { /* server name specified? */
  112. server_name = argv[2];
  113. options |= JackServerName;
  114. }
  115. } else { /* use basename of argv[0] */
  116. client_name = strrchr(argv[0], '/');
  117. if (client_name == 0) {
  118. client_name = argv[0];
  119. } else {
  120. client_name++;
  121. }
  122. }
  123. /* open a client connection to the JACK server */
  124. client = jack_client_open (client_name, options, &status, server_name);
  125. if (client == NULL) {
  126. fprintf (stderr, "jack_client_open() failed, "
  127. "status = 0x%2.0x\n", status);
  128. if (status & JackServerFailed) {
  129. fprintf (stderr, "Unable to connect to JACK server\n");
  130. }
  131. exit (1);
  132. }
  133. if (status & JackServerStarted) {
  134. fprintf (stderr, "JACK server started\n");
  135. }
  136. if (status & JackNameNotUnique) {
  137. client_name = jack_get_client_name(client);
  138. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  139. }
  140. /* tell the JACK server to call `process()' whenever
  141. there is work to be done.
  142. */
  143. if (jack_set_process_thread(client, jack_thread, client) < 0)
  144. exit(1);
  145. /* tell the JACK server to call `jack_shutdown()' if
  146. it ever shuts down, either entirely, or if it
  147. just decides to stop calling us.
  148. */
  149. jack_on_shutdown (client, jack_shutdown, 0);
  150. /* display the current sample rate.
  151. */
  152. printf ("engine sample rate: %" PRIu32 "\n",
  153. jack_get_sample_rate (client));
  154. /* create two ports */
  155. input_port = jack_port_register (client, "input",
  156. JACK_DEFAULT_AUDIO_TYPE,
  157. JackPortIsInput, 0);
  158. output_port = jack_port_register (client, "output",
  159. JACK_DEFAULT_AUDIO_TYPE,
  160. JackPortIsOutput, 0);
  161. if ((input_port == NULL) || (output_port == NULL)) {
  162. fprintf(stderr, "no more JACK ports available\n");
  163. exit (1);
  164. }
  165. /* Tell the JACK server that we are ready to roll. Our
  166. * process() callback will start running now. */
  167. if (jack_activate (client)) {
  168. fprintf (stderr, "cannot activate client");
  169. exit (1);
  170. }
  171. /* Connect the ports. You can't do this before the client is
  172. * activated, because we can't make connections to clients
  173. * that aren't running. Note the confusing (but necessary)
  174. * orientation of the driver backend ports: playback ports are
  175. * "input" to the backend, and capture ports are "output" from
  176. * it.
  177. */
  178. ports = jack_get_ports (client, NULL, NULL,
  179. JackPortIsPhysical|JackPortIsOutput);
  180. if (ports == NULL) {
  181. fprintf(stderr, "no physical capture ports\n");
  182. exit (1);
  183. }
  184. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  185. fprintf (stderr, "cannot connect input ports\n");
  186. }
  187. free (ports);
  188. ports = jack_get_ports (client, NULL, NULL,
  189. JackPortIsPhysical|JackPortIsInput);
  190. if (ports == NULL) {
  191. fprintf(stderr, "no physical playback ports\n");
  192. exit (1);
  193. }
  194. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  195. fprintf (stderr, "cannot connect output ports\n");
  196. }
  197. free (ports);
  198. /* install a signal handler to properly quits jack client */
  199. signal(SIGQUIT, signal_handler);
  200. signal(SIGTERM, signal_handler);
  201. signal(SIGHUP, signal_handler);
  202. signal(SIGINT, signal_handler);
  203. /* keep running until the transport stops */
  204. while (client_state != Exit) {
  205. sleep (1);
  206. }
  207. jack_client_close (client);
  208. exit (0);
  209. }