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.

198 lines
4.7KB

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