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.

218 lines
5.3KB

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