jack2 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.

215 lines
5.5KB

  1. /** @file simple_client.c
  2. *
  3. * @brief This simple through client demonstrates the basic features of JACK
  4. * as they would be used by many applications.
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include <signal.h>
  12. #ifndef WIN32
  13. #include <unistd.h>
  14. #endif
  15. #include <jack/jack.h>
  16. jack_port_t *input_port;
  17. jack_port_t *output_port;
  18. jack_client_t *client;
  19. static void signal_handler ( int sig )
  20. {
  21. jack_client_close ( client );
  22. fprintf ( stderr, "signal received, exiting ...\n" );
  23. exit ( 0 );
  24. }
  25. /**
  26. * The process callback for this JACK application is called in a
  27. * special realtime thread once for each audio cycle.
  28. *
  29. * This client follows a simple rule: when the JACK transport is
  30. * running, copy the input port to the output. When it stops, exit.
  31. */
  32. int
  33. process ( jack_nframes_t nframes, void *arg )
  34. {
  35. jack_default_audio_sample_t *in, *out;
  36. in = jack_port_get_buffer ( input_port, nframes );
  37. out = jack_port_get_buffer ( output_port, nframes );
  38. memcpy ( out, in, nframes * sizeof ( jack_default_audio_sample_t ) );
  39. return 0;
  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;
  55. const char *server_name = NULL;
  56. jack_options_t options = JackNullOption;
  57. jack_status_t status;
  58. if ( argc >= 2 ) /* client name specified? */
  59. {
  60. client_name = argv[1];
  61. if ( argc >= 3 ) /* server name specified? */
  62. {
  63. server_name = argv[2];
  64. options |= JackServerName;
  65. }
  66. }
  67. else /* use basename of argv[0] */
  68. {
  69. client_name = strrchr ( argv[0], '/' );
  70. if ( client_name == 0 )
  71. {
  72. client_name = argv[0];
  73. }
  74. else
  75. {
  76. client_name++;
  77. }
  78. }
  79. /* open a client connection to the JACK server */
  80. client = jack_client_open ( client_name, options, &status, server_name );
  81. if ( client == NULL )
  82. {
  83. fprintf ( stderr, "jack_client_open() failed, "
  84. "status = 0x%2.0x\n", status );
  85. if ( status & JackServerFailed )
  86. {
  87. fprintf ( stderr, "Unable to connect to JACK server\n" );
  88. }
  89. exit ( 1 );
  90. }
  91. if ( status & JackServerStarted )
  92. {
  93. fprintf ( stderr, "JACK server started\n" );
  94. }
  95. if ( status & JackNameNotUnique )
  96. {
  97. client_name = jack_get_client_name ( client );
  98. fprintf ( stderr, "unique name `%s' assigned\n", client_name );
  99. }
  100. /* tell the JACK server to call `process()' whenever
  101. there is work to be done.
  102. */
  103. jack_set_process_callback ( client, process, 0 );
  104. /* tell the JACK server to call `jack_shutdown()' if
  105. it ever shuts down, either entirely, or if it
  106. just decides to stop calling us.
  107. */
  108. jack_on_shutdown ( client, jack_shutdown, 0 );
  109. /* create two ports */
  110. input_port = jack_port_register ( client, "input",
  111. JACK_DEFAULT_AUDIO_TYPE,
  112. JackPortIsInput, 0 );
  113. output_port = jack_port_register ( client, "output",
  114. JACK_DEFAULT_AUDIO_TYPE,
  115. JackPortIsOutput, 0 );
  116. if ( ( input_port == NULL ) || ( output_port == NULL ) )
  117. {
  118. fprintf ( stderr, "no more JACK ports available\n" );
  119. exit ( 1 );
  120. }
  121. /* Tell the JACK server that we are ready to roll. Our
  122. * process() callback will start running now. */
  123. if ( jack_activate ( client ) )
  124. {
  125. fprintf ( stderr, "cannot activate client" );
  126. exit ( 1 );
  127. }
  128. /* Connect the ports. You can't do this before the client is
  129. * activated, because we can't make connections to clients
  130. * that aren't running. Note the confusing (but necessary)
  131. * orientation of the driver backend ports: playback ports are
  132. * "input" to the backend, and capture ports are "output" from
  133. * it.
  134. */
  135. ports = jack_get_ports ( client, NULL, NULL,
  136. JackPortIsPhysical|JackPortIsOutput );
  137. if ( ports == NULL )
  138. {
  139. fprintf ( stderr, "no physical capture ports\n" );
  140. exit ( 1 );
  141. }
  142. if ( jack_connect ( client, ports[0], jack_port_name ( input_port ) ) )
  143. {
  144. fprintf ( stderr, "cannot connect input ports\n" );
  145. }
  146. free ( ports );
  147. ports = jack_get_ports ( client, NULL, NULL,
  148. JackPortIsPhysical|JackPortIsInput );
  149. if ( ports == NULL )
  150. {
  151. fprintf ( stderr, "no physical playback ports\n" );
  152. exit ( 1 );
  153. }
  154. if ( jack_connect ( client, jack_port_name ( output_port ), ports[0] ) )
  155. {
  156. fprintf ( stderr, "cannot connect output ports\n" );
  157. }
  158. free ( ports );
  159. /* install a signal handler to properly quits jack client */
  160. #ifdef WIN32
  161. signal ( SIGINT, signal_handler );
  162. signal ( SIGABRT, signal_handler );
  163. signal ( SIGTERM, signal_handler );
  164. #else
  165. signal ( SIGQUIT, signal_handler );
  166. signal ( SIGTERM, signal_handler );
  167. signal ( SIGHUP, signal_handler );
  168. signal ( SIGINT, signal_handler );
  169. #endif
  170. /* keep running until the transport stops */
  171. while (1)
  172. {
  173. #ifdef WIN32
  174. Sleep ( 1000 );
  175. #else
  176. sleep ( 1 );
  177. #endif
  178. }
  179. jack_client_close ( client );
  180. exit ( 0 );
  181. }