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.

177 lines
4.4KB

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