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.

207 lines
5.0KB

  1. /** @file simple_session_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. * this version also adds session manager functionality.
  6. */
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <inttypes.h>
  13. #include <jack/jack.h>
  14. #include <jack/types.h>
  15. #include <jack/session.h>
  16. jack_port_t *input_port;
  17. jack_port_t *output_port;
  18. jack_client_t *client;
  19. int simple_quit = 0;
  20. /**
  21. * The process callback for this JACK application is called in a
  22. * special realtime thread once for each audio cycle.
  23. *
  24. * This client does nothing more than copy data from its input
  25. * port to its output port. It will exit when stopped by
  26. * the user (e.g. using Ctrl-C on a unix-ish operating system)
  27. */
  28. int
  29. process (jack_nframes_t nframes, void *arg)
  30. {
  31. jack_default_audio_sample_t *in, *out;
  32. in = jack_port_get_buffer (input_port, nframes);
  33. out = jack_port_get_buffer (output_port, nframes);
  34. memcpy (out, in,
  35. sizeof (jack_default_audio_sample_t) * nframes);
  36. return 0;
  37. }
  38. void
  39. session_callback (jack_session_event_t *event, void *arg)
  40. {
  41. char retval[100];
  42. printf ("session notification\n");
  43. printf ("path %s, uuid %s, type: %s\n", event->session_dir, event->client_uuid, event->type == JackSessionSave ? "save" : "quit");
  44. snprintf (retval, 100, "jack_simple_session_client %s", event->client_uuid);
  45. event->command_line = strdup (retval);
  46. jack_session_reply( client, event );
  47. if (event->type == JackSessionSaveAndQuit) {
  48. simple_quit = 1;
  49. }
  50. jack_session_event_free (event);
  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 = "simple";
  66. jack_status_t status;
  67. /* open a client connection to the JACK server */
  68. if( argc == 1 )
  69. client = jack_client_open (client_name, JackNullOption, &status );
  70. else if( argc == 2 )
  71. client = jack_client_open (client_name, JackSessionID, &status, argv[1] );
  72. if (client == NULL) {
  73. fprintf (stderr, "jack_client_open() failed, "
  74. "status = 0x%2.0x\n", status);
  75. if (status & JackServerFailed) {
  76. fprintf (stderr, "Unable to connect to JACK server\n");
  77. }
  78. exit (1);
  79. }
  80. if (status & JackServerStarted) {
  81. fprintf (stderr, "JACK server started\n");
  82. }
  83. if (status & JackNameNotUnique) {
  84. client_name = jack_get_client_name(client);
  85. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  86. }
  87. /* tell the JACK server to call `process()' whenever
  88. there is work to be done.
  89. */
  90. jack_set_process_callback (client, process, 0);
  91. /* tell the JACK server to call `jack_shutdown()' if
  92. it ever shuts down, either entirely, or if it
  93. just decides to stop calling us.
  94. */
  95. jack_on_shutdown (client, jack_shutdown, 0);
  96. /* tell the JACK server to call `session_callback()' if
  97. the session is saved.
  98. */
  99. jack_set_session_callback (client, session_callback, NULL);
  100. /* display the current sample rate.
  101. */
  102. printf ("engine sample rate: %" PRIu32 "\n",
  103. jack_get_sample_rate (client));
  104. /* create two ports */
  105. input_port = jack_port_register (client, "input",
  106. JACK_DEFAULT_AUDIO_TYPE,
  107. JackPortIsInput, 0);
  108. output_port = jack_port_register (client, "output",
  109. JACK_DEFAULT_AUDIO_TYPE,
  110. JackPortIsOutput, 0);
  111. if ((input_port == NULL) || (output_port == NULL)) {
  112. fprintf(stderr, "no more JACK ports available\n");
  113. exit (1);
  114. }
  115. /* Tell the JACK server that we are ready to roll. Our
  116. * process() callback will start running now. */
  117. if (jack_activate (client)) {
  118. fprintf (stderr, "cannot activate client");
  119. exit (1);
  120. }
  121. /* Connect the ports. You can't do this before the client is
  122. * activated, because we can't make connections to clients
  123. * that aren't running. Note the confusing (but necessary)
  124. * orientation of the driver backend ports: playback ports are
  125. * "input" to the backend, and capture ports are "output" from
  126. * it.
  127. */
  128. /* only do the autoconnect when not reloading from a session.
  129. * in case of a session reload, the SM will restore our connections
  130. */
  131. if (argc==1) {
  132. ports = jack_get_ports (client, NULL, NULL,
  133. JackPortIsPhysical|JackPortIsOutput);
  134. if (ports == NULL) {
  135. fprintf(stderr, "no physical capture ports\n");
  136. exit (1);
  137. }
  138. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  139. fprintf (stderr, "cannot connect input ports\n");
  140. }
  141. free (ports);
  142. ports = jack_get_ports (client, NULL, NULL,
  143. JackPortIsPhysical|JackPortIsInput);
  144. if (ports == NULL) {
  145. fprintf(stderr, "no physical playback ports\n");
  146. exit (1);
  147. }
  148. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  149. fprintf (stderr, "cannot connect output ports\n");
  150. }
  151. free (ports);
  152. }
  153. /* keep running until until we get a quit event */
  154. while (!simple_quit)
  155. #ifdef WIN32
  156. Sleep(1*1000);
  157. #else
  158. sleep(1);
  159. #endif
  160. jack_client_close (client);
  161. exit (0);
  162. }