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.

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