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.

185 lines
4.7KB

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