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.

217 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. #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. exit (1);
  70. }
  71. int
  72. main (int argc, char *argv[])
  73. {
  74. const char **ports;
  75. const char *client_name = "latent";
  76. const char *server_name = NULL;
  77. jack_options_t options = JackNullOption;
  78. jack_status_t status;
  79. if (argc == 2)
  80. latency = atoi(argv[1]);
  81. delay_line = malloc( latency * sizeof(jack_default_audio_sample_t));
  82. if (delay_line == NULL) {
  83. fprintf (stderr, "no memory");
  84. exit(1);
  85. }
  86. memset (delay_line, 0, latency * sizeof(jack_default_audio_sample_t));
  87. /* open a client connection to the JACK server */
  88. client = jack_client_open (client_name, options, &status, server_name);
  89. if (client == NULL) {
  90. fprintf (stderr, "jack_client_open() failed, "
  91. "status = 0x%2.0x\n", status);
  92. if (status & JackServerFailed) {
  93. fprintf (stderr, "Unable to connect to JACK server\n");
  94. }
  95. exit (1);
  96. }
  97. if (status & JackServerStarted) {
  98. fprintf (stderr, "JACK server started\n");
  99. }
  100. if (status & JackNameNotUnique) {
  101. client_name = jack_get_client_name(client);
  102. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  103. }
  104. /* tell the JACK server to call `process()' whenever
  105. there is work to be done.
  106. */
  107. jack_set_process_callback (client, process, 0);
  108. /* tell the JACK server to call `latency()' whenever
  109. the latency needs to be recalculated.
  110. */
  111. if (jack_set_latency_callback)
  112. jack_set_latency_callback (client, latency_cb, 0);
  113. /* tell the JACK server to call `jack_shutdown()' if
  114. it ever shuts down, either entirely, or if it
  115. just decides to stop calling us.
  116. */
  117. jack_on_shutdown (client, jack_shutdown, 0);
  118. /* display the current sample rate.
  119. */
  120. printf ("engine sample rate: %" PRIu32 "\n",
  121. jack_get_sample_rate (client));
  122. /* create two ports */
  123. input_port = jack_port_register (client, "input",
  124. JACK_DEFAULT_AUDIO_TYPE,
  125. JackPortIsInput, 0);
  126. output_port = jack_port_register (client, "output",
  127. JACK_DEFAULT_AUDIO_TYPE,
  128. JackPortIsOutput, 0);
  129. if ((input_port == NULL) || (output_port == NULL)) {
  130. fprintf(stderr, "no more JACK ports available\n");
  131. exit (1);
  132. }
  133. /* Tell the JACK server that we are ready to roll. Our
  134. * process() callback will start running now. */
  135. if (jack_activate (client)) {
  136. fprintf (stderr, "cannot activate client");
  137. exit (1);
  138. }
  139. /* Connect the ports. You can't do this before the client is
  140. * activated, because we can't make connections to clients
  141. * that aren't running. Note the confusing (but necessary)
  142. * orientation of the driver backend ports: playback ports are
  143. * "input" to the backend, and capture ports are "output" from
  144. * it.
  145. */
  146. ports = jack_get_ports (client, NULL, NULL,
  147. JackPortIsPhysical|JackPortIsOutput);
  148. if (ports == NULL) {
  149. fprintf(stderr, "no physical capture ports\n");
  150. exit (1);
  151. }
  152. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  153. fprintf (stderr, "cannot connect input ports\n");
  154. }
  155. free (ports);
  156. ports = jack_get_ports (client, NULL, NULL,
  157. JackPortIsPhysical|JackPortIsInput);
  158. if (ports == NULL) {
  159. fprintf(stderr, "no physical playback ports\n");
  160. exit (1);
  161. }
  162. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  163. fprintf (stderr, "cannot connect output ports\n");
  164. }
  165. free (ports);
  166. /* keep running until stopped by the user */
  167. jack_sleep (-1);
  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. }