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. int type_save;
  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_client %s", event->client_uuid);
  43. event->command_line = strdup (retval);
  44. type_save = event->type;
  45. jack_session_reply( client, event );
  46. if (type_save == JackSessionSaveAndQuit) {
  47. simple_quit = 1;
  48. }
  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. ports = jack_get_ports (client, NULL, NULL,
  127. JackPortIsPhysical|JackPortIsOutput);
  128. if (ports == NULL) {
  129. fprintf(stderr, "no physical capture ports\n");
  130. exit (1);
  131. }
  132. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  133. fprintf (stderr, "cannot connect input ports\n");
  134. }
  135. free (ports);
  136. ports = jack_get_ports (client, NULL, NULL,
  137. JackPortIsPhysical|JackPortIsInput);
  138. if (ports == NULL) {
  139. fprintf(stderr, "no physical playback ports\n");
  140. exit (1);
  141. }
  142. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  143. fprintf (stderr, "cannot connect output ports\n");
  144. }
  145. free (ports);
  146. /* keep running until until we get a quit event */
  147. while (!simple_quit)
  148. sleep(1);
  149. jack_client_close (client);
  150. exit (0);
  151. }