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.

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