JACK example clients
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.

216 lines
5.2KB

  1. /** @file latent_client.c
  2. *
  3. * @brief This simple client demonstrates the most 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 <inttypes.h>
  12. #include <jack/jack.h>
  13. jack_port_t *input_port;
  14. jack_port_t *output_port;
  15. jack_client_t *client;
  16. jack_default_audio_sample_t *delay_line;
  17. jack_nframes_t delay_index;
  18. jack_nframes_t latency = 1024;
  19. /**
  20. * The process callback for this JACK application is called in a
  21. * special realtime thread once for each audio cycle.
  22. *
  23. * This client does nothing more than copy data from its input
  24. * port to its output port. It will exit when stopped by
  25. * the user (e.g. using Ctrl-C on a unix-ish operating system)
  26. */
  27. int
  28. process (jack_nframes_t nframes, void *arg)
  29. {
  30. jack_default_audio_sample_t *in, *out;
  31. int k;
  32. in = jack_port_get_buffer (input_port, nframes);
  33. out = jack_port_get_buffer (output_port, nframes);
  34. for (k=0; k<nframes; k++) {
  35. out[k] = delay_line[delay_index];
  36. delay_line[delay_index] = in[k];
  37. delay_index = (delay_index + 1) % latency;
  38. }
  39. return 0;
  40. }
  41. void
  42. latency_cb (jack_latency_callback_mode_t mode, void *arg)
  43. {
  44. jack_latency_range_t range;
  45. if (mode == JackCaptureLatency) {
  46. jack_port_get_latency_range (input_port, mode, &range);
  47. range.min += latency;
  48. range.max += latency;
  49. jack_port_set_latency_range (output_port, mode, &range);
  50. } else {
  51. jack_port_get_latency_range (output_port, mode, &range);
  52. range.min += latency;
  53. range.max += latency;
  54. jack_port_set_latency_range (input_port, mode, &range);
  55. }
  56. }
  57. /**
  58. * JACK calls this shutdown_callback if the server ever shuts down or
  59. * decides to disconnect the client.
  60. */
  61. void
  62. jack_shutdown (void *arg)
  63. {
  64. fprintf(stderr, "JACK shut down, exiting ...\n");
  65. exit (1);
  66. }
  67. int
  68. main (int argc, char *argv[])
  69. {
  70. const char **ports;
  71. const char *client_name = "latent";
  72. const char *server_name = NULL;
  73. jack_options_t options = JackNullOption;
  74. jack_status_t status;
  75. if (argc == 2)
  76. latency = atoi(argv[1]);
  77. delay_line = malloc( latency * sizeof(jack_default_audio_sample_t));
  78. if (delay_line == NULL) {
  79. fprintf (stderr, "no memory");
  80. exit(1);
  81. }
  82. memset (delay_line, 0, latency * sizeof(jack_default_audio_sample_t));
  83. /* open a client connection to the JACK server */
  84. client = jack_client_open (client_name, options, &status, server_name);
  85. if (client == NULL) {
  86. fprintf (stderr, "jack_client_open() failed, "
  87. "status = 0x%2.0x\n", status);
  88. if (status & JackServerFailed) {
  89. fprintf (stderr, "Unable to connect to JACK server\n");
  90. }
  91. exit (1);
  92. }
  93. if (status & JackServerStarted) {
  94. fprintf (stderr, "JACK server started\n");
  95. }
  96. if (status & JackNameNotUnique) {
  97. client_name = jack_get_client_name(client);
  98. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  99. }
  100. /* tell the JACK server to call `process()' whenever
  101. there is work to be done.
  102. */
  103. jack_set_process_callback (client, process, 0);
  104. /* tell the JACK server to call `latency()' whenever
  105. the latency needs to be recalculated.
  106. */
  107. if (jack_set_latency_callback)
  108. jack_set_latency_callback (client, latency_cb, 0);
  109. /* tell the JACK server to call `jack_shutdown()' if
  110. it ever shuts down, either entirely, or if it
  111. just decides to stop calling us.
  112. */
  113. jack_on_shutdown (client, jack_shutdown, 0);
  114. /* display the current sample rate.
  115. */
  116. printf ("engine sample rate: %" PRIu32 "\n",
  117. jack_get_sample_rate (client));
  118. /* create two ports */
  119. input_port = jack_port_register (client, "input",
  120. JACK_DEFAULT_AUDIO_TYPE,
  121. JackPortIsInput, 0);
  122. output_port = jack_port_register (client, "output",
  123. JACK_DEFAULT_AUDIO_TYPE,
  124. JackPortIsOutput, 0);
  125. if ((input_port == NULL) || (output_port == NULL)) {
  126. fprintf(stderr, "no more JACK ports available\n");
  127. exit (1);
  128. }
  129. /* Tell the JACK server that we are ready to roll. Our
  130. * process() callback will start running now. */
  131. if (jack_activate (client)) {
  132. fprintf (stderr, "cannot activate client");
  133. exit (1);
  134. }
  135. /* Connect the ports. You can't do this before the client is
  136. * activated, because we can't make connections to clients
  137. * that aren't running. Note the confusing (but necessary)
  138. * orientation of the driver backend ports: playback ports are
  139. * "input" to the backend, and capture ports are "output" from
  140. * it.
  141. */
  142. ports = jack_get_ports (client, NULL, NULL,
  143. JackPortIsPhysical|JackPortIsOutput);
  144. if (ports == NULL) {
  145. fprintf(stderr, "no physical capture ports\n");
  146. exit (1);
  147. }
  148. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  149. fprintf (stderr, "cannot connect input ports\n");
  150. }
  151. free (ports);
  152. ports = jack_get_ports (client, NULL, NULL,
  153. JackPortIsPhysical|JackPortIsInput);
  154. if (ports == NULL) {
  155. fprintf(stderr, "no physical playback ports\n");
  156. exit (1);
  157. }
  158. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  159. fprintf (stderr, "cannot connect output ports\n");
  160. }
  161. free (ports);
  162. /* keep running until stopped by the user */
  163. #ifdef WIN32
  164. Sleep (-1);
  165. #else
  166. sleep (-1);
  167. #endif
  168. /* this is never reached but if the program
  169. had some other way to exit besides being killed,
  170. they would be important to call.
  171. */
  172. jack_client_close (client);
  173. exit (0);
  174. }