jack1 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.

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