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.

195 lines
4.5KB

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