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.

224 lines
5.1KB

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