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.

202 lines
4.9KB

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