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.

209 lines
5.1KB

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