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.

263 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. exit (1);
  106. }
  107. int
  108. main (int argc, char *argv[])
  109. {
  110. const char **ports;
  111. const char *client_name;
  112. const char *server_name = NULL;
  113. jack_options_t options = JackNullOption;
  114. jack_status_t status;
  115. if (argc >= 2) { /* client name specified? */
  116. client_name = argv[1];
  117. if (argc >= 3) { /* server name specified? */
  118. server_name = argv[2];
  119. options |= JackServerName;
  120. }
  121. } else { /* use basename of argv[0] */
  122. client_name = strrchr(argv[0], '/');
  123. if (client_name == 0) {
  124. client_name = argv[0];
  125. } else {
  126. client_name++;
  127. }
  128. }
  129. /* open a client connection to the JACK server */
  130. client = jack_client_open (client_name, options, &status, server_name);
  131. if (client == NULL) {
  132. fprintf (stderr, "jack_client_open() failed, "
  133. "status = 0x%2.0x\n", status);
  134. if (status & JackServerFailed) {
  135. fprintf (stderr, "Unable to connect to JACK server\n");
  136. }
  137. exit (1);
  138. }
  139. if (status & JackServerStarted) {
  140. fprintf (stderr, "JACK server started\n");
  141. }
  142. if (status & JackNameNotUnique) {
  143. client_name = jack_get_client_name(client);
  144. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  145. }
  146. /* tell the JACK server to call `process()' whenever
  147. there is work to be done.
  148. */
  149. if (jack_set_process_thread(client, jack_thread, client) < 0)
  150. exit(1);
  151. /* tell the JACK server to call `jack_shutdown()' if
  152. it ever shuts down, either entirely, or if it
  153. just decides to stop calling us.
  154. */
  155. jack_on_shutdown (client, jack_shutdown, 0);
  156. /* display the current sample rate.
  157. */
  158. printf ("engine sample rate: %" PRIu32 "\n",
  159. jack_get_sample_rate (client));
  160. /* create two ports */
  161. input_port = jack_port_register (client, "input",
  162. JACK_DEFAULT_AUDIO_TYPE,
  163. JackPortIsInput, 0);
  164. output_port = jack_port_register (client, "output",
  165. JACK_DEFAULT_AUDIO_TYPE,
  166. JackPortIsOutput, 0);
  167. if ((input_port == NULL) || (output_port == NULL)) {
  168. fprintf(stderr, "no more JACK ports available\n");
  169. exit (1);
  170. }
  171. /* Tell the JACK server that we are ready to roll. Our
  172. * process() callback will start running now. */
  173. if (jack_activate (client)) {
  174. fprintf (stderr, "cannot activate client");
  175. exit (1);
  176. }
  177. /* Connect the ports. You can't do this before the client is
  178. * activated, because we can't make connections to clients
  179. * that aren't running. Note the confusing (but necessary)
  180. * orientation of the driver backend ports: playback ports are
  181. * "input" to the backend, and capture ports are "output" from
  182. * it.
  183. */
  184. ports = jack_get_ports (client, NULL, NULL,
  185. JackPortIsPhysical|JackPortIsOutput);
  186. if (ports == NULL) {
  187. fprintf(stderr, "no physical capture ports\n");
  188. exit (1);
  189. }
  190. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  191. fprintf (stderr, "cannot connect input ports\n");
  192. }
  193. free (ports);
  194. ports = jack_get_ports (client, NULL, NULL,
  195. JackPortIsPhysical|JackPortIsInput);
  196. if (ports == NULL) {
  197. fprintf(stderr, "no physical playback ports\n");
  198. exit (1);
  199. }
  200. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  201. fprintf (stderr, "cannot connect output ports\n");
  202. }
  203. free (ports);
  204. /* install a signal handler to properly quits jack client */
  205. signal(SIGQUIT, signal_handler);
  206. signal(SIGTERM, signal_handler);
  207. signal(SIGHUP, signal_handler);
  208. signal(SIGINT, signal_handler);
  209. /* keep running until the transport stops */
  210. while (client_state != Exit) {
  211. sleep (1);
  212. }
  213. jack_client_close (client);
  214. exit (0);
  215. }