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.

165 lines
4.0KB

  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. /**
  16. * The process callback for this JACK application is called in a
  17. * special realtime thread once for each audio cycle.
  18. *
  19. * This client does nothing more than copy data from its input
  20. * port to its output port. It will exit when stopped by
  21. * the user (e.g. using Ctrl-C on a unix-ish operating system)
  22. */
  23. int
  24. process (jack_nframes_t nframes, void *arg)
  25. {
  26. jack_default_audio_sample_t *in, *out;
  27. in = jack_port_get_buffer (input_port, nframes);
  28. out = jack_port_get_buffer (output_port, nframes);
  29. memcpy (out, in,
  30. sizeof (jack_default_audio_sample_t) * nframes);
  31. return 0;
  32. }
  33. /**
  34. * JACK calls this shutdown_callback if the server ever shuts down or
  35. * decides to disconnect the client.
  36. */
  37. void
  38. jack_shutdown (void *arg)
  39. {
  40. exit (1);
  41. }
  42. int
  43. main (int argc, char *argv[])
  44. {
  45. const char **ports;
  46. const char *client_name = "simple";
  47. const char *server_name = NULL;
  48. jack_options_t options = JackNullOption;
  49. jack_status_t status;
  50. /* open a client connection to the JACK server */
  51. client = jack_client_open (client_name, options, &status, server_name);
  52. if (client == NULL) {
  53. fprintf (stderr, "jack_client_open() failed, "
  54. "status = 0x%2.0x\n", status);
  55. if (status & JackServerFailed) {
  56. fprintf (stderr, "Unable to connect to JACK server\n");
  57. }
  58. exit (1);
  59. }
  60. if (status & JackServerStarted) {
  61. fprintf (stderr, "JACK server started\n");
  62. }
  63. if (status & JackNameNotUnique) {
  64. client_name = jack_get_client_name(client);
  65. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  66. }
  67. /* tell the JACK server to call `process()' whenever
  68. there is work to be done.
  69. */
  70. jack_set_process_callback (client, process, 0);
  71. /* tell the JACK server to call `jack_shutdown()' if
  72. it ever shuts down, either entirely, or if it
  73. just decides to stop calling us.
  74. */
  75. jack_on_shutdown (client, jack_shutdown, 0);
  76. /* display the current sample rate.
  77. */
  78. printf ("engine sample rate: %" PRIu32 "\n",
  79. jack_get_sample_rate (client));
  80. /* create two ports */
  81. input_port = jack_port_register (client, "input",
  82. JACK_DEFAULT_AUDIO_TYPE,
  83. JackPortIsInput, 0);
  84. output_port = jack_port_register (client, "output",
  85. JACK_DEFAULT_AUDIO_TYPE,
  86. JackPortIsOutput, 0);
  87. if ((input_port == NULL) || (output_port == NULL)) {
  88. fprintf(stderr, "no more JACK ports available\n");
  89. exit (1);
  90. }
  91. /* Tell the JACK server that we are ready to roll. Our
  92. * process() callback will start running now. */
  93. if (jack_activate (client)) {
  94. fprintf (stderr, "cannot activate client");
  95. exit (1);
  96. }
  97. /* Connect the ports. You can't do this before the client is
  98. * activated, because we can't make connections to clients
  99. * that aren't running. Note the confusing (but necessary)
  100. * orientation of the driver backend ports: playback ports are
  101. * "input" to the backend, and capture ports are "output" from
  102. * it.
  103. */
  104. ports = jack_get_ports (client, NULL, NULL,
  105. JackPortIsPhysical|JackPortIsOutput);
  106. if (ports == NULL) {
  107. fprintf(stderr, "no physical capture ports\n");
  108. exit (1);
  109. }
  110. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  111. fprintf (stderr, "cannot connect input ports\n");
  112. }
  113. free (ports);
  114. ports = jack_get_ports (client, NULL, NULL,
  115. JackPortIsPhysical|JackPortIsInput);
  116. if (ports == NULL) {
  117. fprintf(stderr, "no physical playback ports\n");
  118. exit (1);
  119. }
  120. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  121. fprintf (stderr, "cannot connect output ports\n");
  122. }
  123. free (ports);
  124. /* keep running until stopped by the user */
  125. sleep (-1);
  126. /* this is never reached but if the program
  127. had some other way to exit besides being killed,
  128. they would be important to call.
  129. */
  130. jack_client_close (client);
  131. exit (0);
  132. }