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.

181 lines
4.5KB

  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_set_process_callback (client, process, 0);
  82. /* tell the JACK server to call `jack_shutdown()' if
  83. it ever shuts down, either entirely, or if it
  84. just decides to stop calling us.
  85. */
  86. jack_on_shutdown (client, jack_shutdown, 0);
  87. jack_set_session_callback( client, session_callback, NULL );
  88. /* display the current sample rate.
  89. */
  90. printf ("engine sample rate: %" PRIu32 "\n",
  91. jack_get_sample_rate (client));
  92. /* create two ports */
  93. input_port = jack_port_register (client, "input",
  94. JACK_DEFAULT_AUDIO_TYPE,
  95. JackPortIsInput, 0);
  96. output_port = jack_port_register (client, "output",
  97. JACK_DEFAULT_AUDIO_TYPE,
  98. JackPortIsOutput, 0);
  99. if ((input_port == NULL) || (output_port == NULL)) {
  100. fprintf(stderr, "no more JACK ports available\n");
  101. exit (1);
  102. }
  103. /* Tell the JACK server that we are ready to roll. Our
  104. * process() callback will start running now. */
  105. if (jack_activate (client)) {
  106. fprintf (stderr, "cannot activate client");
  107. exit (1);
  108. }
  109. /* Connect the ports. You can't do this before the client is
  110. * activated, because we can't make connections to clients
  111. * that aren't running. Note the confusing (but necessary)
  112. * orientation of the driver backend ports: playback ports are
  113. * "input" to the backend, and capture ports are "output" from
  114. * it.
  115. */
  116. ports = jack_get_ports (client, NULL, NULL,
  117. JackPortIsPhysical|JackPortIsOutput);
  118. if (ports == NULL) {
  119. fprintf(stderr, "no physical capture ports\n");
  120. exit (1);
  121. }
  122. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  123. fprintf (stderr, "cannot connect input ports\n");
  124. }
  125. free (ports);
  126. ports = jack_get_ports (client, NULL, NULL,
  127. JackPortIsPhysical|JackPortIsInput);
  128. if (ports == NULL) {
  129. fprintf(stderr, "no physical playback ports\n");
  130. exit (1);
  131. }
  132. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  133. fprintf (stderr, "cannot connect output ports\n");
  134. }
  135. free (ports);
  136. /* keep running until stopped by the user */
  137. sleep (-1);
  138. /* this is never reached but if the program
  139. had some other way to exit besides being killed,
  140. they would be important to call.
  141. */
  142. jack_client_close (client);
  143. exit (0);
  144. }