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.

251 lines
5.7KB

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