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.

193 lines
4.6KB

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