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.

218 lines
5.8KB

  1. /** @file thru_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_ports;
  17. jack_port_t **output_ports;
  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. int i;
  36. jack_default_audio_sample_t *in, *out;
  37. for ( i = 0; i < 2; i++ )
  38. {
  39. in = jack_port_get_buffer ( input_ports[i], nframes );
  40. out = jack_port_get_buffer ( output_ports[i], nframes );
  41. memcpy ( out, in, nframes * sizeof ( jack_default_audio_sample_t ) );
  42. }
  43. return 0;
  44. }
  45. /**
  46. * JACK calls this shutdown_callback if the server ever shuts down or
  47. * decides to disconnect the client.
  48. */
  49. void
  50. jack_shutdown ( void *arg )
  51. {
  52. free ( input_ports );
  53. free ( output_ports );
  54. exit ( 1 );
  55. }
  56. int
  57. main ( int argc, char *argv[] )
  58. {
  59. int i;
  60. const char **ports;
  61. const char *client_name;
  62. const char *server_name = NULL;
  63. jack_options_t options = JackNullOption;
  64. jack_status_t status;
  65. if ( argc >= 2 ) /* client name specified? */
  66. {
  67. client_name = argv[1];
  68. if ( argc >= 3 ) /* server name specified? */
  69. {
  70. server_name = argv[2];
  71. options |= JackServerName;
  72. }
  73. }
  74. else /* use basename of argv[0] */
  75. {
  76. client_name = strrchr ( argv[0], '/' );
  77. if ( client_name == 0 )
  78. {
  79. client_name = argv[0];
  80. }
  81. else
  82. {
  83. client_name++;
  84. }
  85. }
  86. /* open a client connection to the JACK server */
  87. client = jack_client_open ( client_name, options, &status, server_name );
  88. if ( client == NULL )
  89. {
  90. fprintf ( stderr, "jack_client_open() failed, "
  91. "status = 0x%2.0x\n", status );
  92. if ( status & JackServerFailed )
  93. {
  94. fprintf ( stderr, "Unable to connect to JACK server\n" );
  95. }
  96. exit ( 1 );
  97. }
  98. if ( status & JackServerStarted )
  99. {
  100. fprintf ( stderr, "JACK server started\n" );
  101. }
  102. if ( status & JackNameNotUnique )
  103. {
  104. client_name = jack_get_client_name ( client );
  105. fprintf ( stderr, "unique name `%s' assigned\n", client_name );
  106. }
  107. /* tell the JACK server to call `process()' whenever
  108. there is work to be done.
  109. */
  110. jack_set_process_callback ( client, process, 0 );
  111. /* tell the JACK server to call `jack_shutdown()' if
  112. it ever shuts down, either entirely, or if it
  113. just decides to stop calling us.
  114. */
  115. jack_on_shutdown ( client, jack_shutdown, 0 );
  116. /* create two ports pairs*/
  117. input_ports = ( jack_port_t** ) calloc ( 2, sizeof ( jack_port_t* ) );
  118. output_ports = ( jack_port_t** ) calloc ( 2, sizeof ( jack_port_t* ) );
  119. char port_name[16];
  120. for ( i = 0; i < 2; i++ )
  121. {
  122. sprintf ( port_name, "input_%d", i + 1 );
  123. input_ports[i] = jack_port_register ( client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
  124. sprintf ( port_name, "output_%d", i + 1 );
  125. output_ports[i] = jack_port_register ( client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
  126. if ( ( input_ports[i] == NULL ) || ( output_ports[i] == NULL ) )
  127. {
  128. fprintf ( stderr, "no more JACK ports available\n" );
  129. exit ( 1 );
  130. }
  131. }
  132. /* Tell the JACK server that we are ready to roll. Our
  133. * process() callback will start running now. */
  134. if ( jack_activate ( client ) )
  135. {
  136. fprintf ( stderr, "cannot activate client" );
  137. exit ( 1 );
  138. }
  139. /* Connect the ports. You can't do this before the client is
  140. * activated, because we can't make connections to clients
  141. * that aren't running. Note the confusing (but necessary)
  142. * orientation of the driver backend ports: playback ports are
  143. * "input" to the backend, and capture ports are "output" from
  144. * it.
  145. */
  146. ports = jack_get_ports ( client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput );
  147. if ( ports == NULL )
  148. {
  149. fprintf ( stderr, "no physical capture ports\n" );
  150. exit ( 1 );
  151. }
  152. for ( i = 0; i < 2; i++ )
  153. if ( jack_connect ( client, ports[i], jack_port_name ( input_ports[i] ) ) )
  154. fprintf ( stderr, "cannot connect input ports\n" );
  155. free ( ports );
  156. ports = jack_get_ports ( client, NULL, NULL, JackPortIsPhysical|JackPortIsInput );
  157. if ( ports == NULL )
  158. {
  159. fprintf ( stderr, "no physical playback ports\n" );
  160. exit ( 1 );
  161. }
  162. for ( i = 0; i < 2; i++ )
  163. if ( jack_connect ( client, jack_port_name ( output_ports[i] ), ports[i] ) )
  164. fprintf ( stderr, "cannot connect input ports\n" );
  165. free ( ports );
  166. /* install a signal handler to properly quits jack client */
  167. #ifdef WIN32
  168. signal ( SIGINT, signal_handler );
  169. signal ( SIGABRT, signal_handler );
  170. signal ( SIGTERM, signal_handler );
  171. #else
  172. signal ( SIGQUIT, signal_handler );
  173. signal ( SIGTERM, signal_handler );
  174. signal ( SIGHUP, signal_handler );
  175. signal ( SIGINT, signal_handler );
  176. #endif
  177. /* keep running until the transport stops */
  178. while (1)
  179. {
  180. #ifdef WIN32
  181. Sleep ( 1000 );
  182. #else
  183. sleep ( 1 );
  184. #endif
  185. }
  186. jack_client_close ( client );
  187. exit ( 0 );
  188. }