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.

191 lines
4.8KB

  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. char *
  34. session_callback (jack_session_event_t code, const char *path, const char *uuid, void *arg)
  35. {
  36. char retval[100];
  37. printf ("session notification\n");
  38. printf ("path %s, uuid %s, type: %s\n", path, uuid, code == JackSessionSave ? "save" : "quit");
  39. if (code == JackSessionQuit)
  40. exit (1);
  41. snprintf (retval, 100, "jack_simple_client %s", uuid);
  42. return strdup (retval);
  43. }
  44. /**
  45. * JACK calls this shutdown_callback if the server ever shuts down or
  46. * decides to disconnect the client.
  47. */
  48. void
  49. jack_shutdown (void *arg)
  50. {
  51. exit (1);
  52. }
  53. int
  54. main (int argc, char *argv[])
  55. {
  56. const char **ports;
  57. const char *client_name = "simple";
  58. const char *server_name = NULL;
  59. jack_options_t options = JackNullOption;
  60. jack_status_t status;
  61. /* open a client connection to the JACK server */
  62. if( argc == 1 )
  63. client = jack_client_open (client_name, options, &status, server_name);
  64. else if( argc == 2 )
  65. client = jack_client_open (client_name, options |JackSessionUUID, &status, argv[1] );
  66. if (client == NULL) {
  67. fprintf (stderr, "jack_client_open() failed, "
  68. "status = 0x%2.0x\n", status);
  69. if (status & JackServerFailed) {
  70. fprintf (stderr, "Unable to connect to JACK server\n");
  71. }
  72. exit (1);
  73. }
  74. if (status & JackServerStarted) {
  75. fprintf (stderr, "JACK server started\n");
  76. }
  77. if (status & JackNameNotUnique) {
  78. client_name = jack_get_client_name(client);
  79. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  80. }
  81. /* tell the JACK server to call `process()' whenever
  82. there is work to be done.
  83. */
  84. jack_client_set_cookie (client, "info", "simple");
  85. jack_client_set_cookie (client, "info2", "no");
  86. jack_set_process_callback (client, process, 0);
  87. /* tell the JACK server to call `jack_shutdown()' if
  88. it ever shuts down, either entirely, or if it
  89. just decides to stop calling us.
  90. */
  91. jack_on_shutdown (client, jack_shutdown, 0);
  92. /* tell the JACK server to call `session_callback()' if
  93. the session is saved.
  94. */
  95. jack_set_session_callback (client, session_callback, NULL);
  96. /* display the current sample rate.
  97. */
  98. printf ("engine sample rate: %" PRIu32 "\n",
  99. jack_get_sample_rate (client));
  100. /* create two ports */
  101. input_port = jack_port_register (client, "input",
  102. JACK_DEFAULT_AUDIO_TYPE,
  103. JackPortIsInput, 0);
  104. output_port = jack_port_register (client, "output",
  105. JACK_DEFAULT_AUDIO_TYPE,
  106. JackPortIsOutput, 0);
  107. if ((input_port == NULL) || (output_port == NULL)) {
  108. fprintf(stderr, "no more JACK ports available\n");
  109. exit (1);
  110. }
  111. /* Tell the JACK server that we are ready to roll. Our
  112. * process() callback will start running now. */
  113. if (jack_activate (client)) {
  114. fprintf (stderr, "cannot activate client");
  115. exit (1);
  116. }
  117. /* Connect the ports. You can't do this before the client is
  118. * activated, because we can't make connections to clients
  119. * that aren't running. Note the confusing (but necessary)
  120. * orientation of the driver backend ports: playback ports are
  121. * "input" to the backend, and capture ports are "output" from
  122. * it.
  123. */
  124. ports = jack_get_ports (client, NULL, NULL,
  125. JackPortIsPhysical|JackPortIsOutput);
  126. if (ports == NULL) {
  127. fprintf(stderr, "no physical capture ports\n");
  128. exit (1);
  129. }
  130. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  131. fprintf (stderr, "cannot connect input ports\n");
  132. }
  133. free (ports);
  134. ports = jack_get_ports (client, NULL, NULL,
  135. JackPortIsPhysical|JackPortIsInput);
  136. if (ports == NULL) {
  137. fprintf(stderr, "no physical playback ports\n");
  138. exit (1);
  139. }
  140. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  141. fprintf (stderr, "cannot connect output ports\n");
  142. }
  143. free (ports);
  144. /* keep running until stopped by the user */
  145. sleep (-1);
  146. /* this is never reached but if the program
  147. had some other way to exit besides being killed,
  148. they would be important to call.
  149. */
  150. jack_client_close (client);
  151. exit (0);
  152. }