JACK tools
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.

205 lines
4.7KB

  1. /** @file simple_client.c
  2. *
  3. * @brief This simple client demonstrates the 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. /* a simple state machine for this client */
  16. volatile enum {
  17. Init,
  18. Run,
  19. Exit
  20. } client_state = Init;
  21. /**
  22. * The process callback for this JACK application is called in a
  23. * special realtime thread once for each audio cycle.
  24. *
  25. * This client follows a simple rule: when the JACK transport is
  26. * running, copy the input port to the output. When it stops, exit.
  27. */
  28. int
  29. _process (jack_nframes_t nframes)
  30. {
  31. jack_default_audio_sample_t *in, *out;
  32. jack_transport_state_t ts = jack_transport_query(client, NULL);
  33. if (ts == JackTransportRolling) {
  34. if (client_state == Init)
  35. client_state = Run;
  36. in = jack_port_get_buffer (input_port, nframes);
  37. out = jack_port_get_buffer (output_port, nframes);
  38. memcpy (out, in,
  39. sizeof (jack_default_audio_sample_t) * nframes);
  40. } else if (ts == JackTransportStopped) {
  41. if (client_state == Run)
  42. client_state = Exit;
  43. }
  44. return 0;
  45. }
  46. int
  47. process (jack_nframes_t nframes, void* arg)
  48. {
  49. jack_client_t* client = (jack_client_t*) arg;
  50. while ((nframes = jack_thread_wait (client, _process (nframes))) != 0);
  51. return 0;
  52. }
  53. /**
  54. * JACK calls this shutdown_callback if the server ever shuts down or
  55. * decides to disconnect the client.
  56. */
  57. void
  58. jack_shutdown (void *arg)
  59. {
  60. exit (1);
  61. }
  62. int
  63. main (int argc, char *argv[])
  64. {
  65. const char **ports;
  66. const char *client_name;
  67. const char *server_name = NULL;
  68. jack_options_t options = JackNullOption;
  69. jack_status_t status;
  70. if (argc >= 2) { /* client name specified? */
  71. client_name = argv[1];
  72. if (argc >= 3) { /* server name specified? */
  73. server_name = argv[2];
  74. options |= JackServerName;
  75. }
  76. } else { /* use basename of argv[0] */
  77. client_name = strrchr(argv[0], '/');
  78. if (client_name == 0) {
  79. client_name = argv[0];
  80. } else {
  81. client_name++;
  82. }
  83. }
  84. /* open a client connection to the JACK server */
  85. client = jack_client_open (client_name, options, &status, server_name);
  86. if (client == NULL) {
  87. fprintf (stderr, "jack_client_open() failed, "
  88. "status = 0x%2.0x\n", status);
  89. if (status & JackServerFailed) {
  90. fprintf (stderr, "Unable to connect to JACK server\n");
  91. }
  92. exit (1);
  93. }
  94. if (status & JackServerStarted) {
  95. fprintf (stderr, "JACK server started\n");
  96. }
  97. if (status & JackNameNotUnique) {
  98. client_name = jack_get_client_name(client);
  99. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  100. }
  101. /* tell the JACK server to call `process()' whenever
  102. there is work to be done.
  103. */
  104. jack_set_process_callback (client, process, client);
  105. /* tell the JACK server to call `jack_shutdown()' if
  106. it ever shuts down, either entirely, or if it
  107. just decides to stop calling us.
  108. */
  109. jack_on_shutdown (client, jack_shutdown, 0);
  110. /* display the current sample rate.
  111. */
  112. printf ("engine sample rate: %" PRIu32 "\n",
  113. jack_get_sample_rate (client));
  114. /* create two ports */
  115. input_port = jack_port_register (client, "input",
  116. JACK_DEFAULT_AUDIO_TYPE,
  117. JackPortIsInput, 0);
  118. output_port = jack_port_register (client, "output",
  119. JACK_DEFAULT_AUDIO_TYPE,
  120. JackPortIsOutput, 0);
  121. if ((input_port == NULL) || (output_port == NULL)) {
  122. fprintf(stderr, "no more JACK ports available\n");
  123. exit (1);
  124. }
  125. /* Tell the JACK server that we are ready to roll. Our
  126. * process() callback will start running now. */
  127. if (jack_activate (client)) {
  128. fprintf (stderr, "cannot activate client");
  129. exit (1);
  130. }
  131. /* Connect the ports. You can't do this before the client is
  132. * activated, because we can't make connections to clients
  133. * that aren't running. Note the confusing (but necessary)
  134. * orientation of the driver backend ports: playback ports are
  135. * "input" to the backend, and capture ports are "output" from
  136. * it.
  137. */
  138. ports = jack_get_ports (client, NULL, NULL,
  139. JackPortIsPhysical|JackPortIsOutput);
  140. if (ports == NULL) {
  141. fprintf(stderr, "no physical capture ports\n");
  142. exit (1);
  143. }
  144. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  145. fprintf (stderr, "cannot connect input ports\n");
  146. }
  147. free (ports);
  148. ports = jack_get_ports (client, NULL, NULL,
  149. JackPortIsPhysical|JackPortIsInput);
  150. if (ports == NULL) {
  151. fprintf(stderr, "no physical playback ports\n");
  152. exit (1);
  153. }
  154. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  155. fprintf (stderr, "cannot connect output ports\n");
  156. }
  157. free (ports);
  158. /* keep running until the transport stops */
  159. while (client_state != Exit) {
  160. sleep (1);
  161. }
  162. jack_client_close (client);
  163. exit (0);
  164. }