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.

245 lines
5.9KB

  1. /** @file simple_client.c
  2. *
  3. * @brief This simple 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_port1, *output_port2;
  18. jack_client_t *client;
  19. #ifndef M_PI
  20. #define M_PI (3.14159265)
  21. #endif
  22. #define TABLE_SIZE (200)
  23. typedef struct
  24. {
  25. float sine[TABLE_SIZE];
  26. int left_phase;
  27. int right_phase;
  28. }
  29. paTestData;
  30. static void signal_handler(int sig)
  31. {
  32. jack_client_close(client);
  33. fprintf(stderr, "signal received, exiting ...\n");
  34. exit(0);
  35. }
  36. /* a simple state machine for this client */
  37. volatile enum {
  38. Init,
  39. Run,
  40. Exit
  41. } client_state = Init;
  42. /**
  43. * The process callback for this JACK application is called in a
  44. * special realtime thread once for each audio cycle.
  45. *
  46. * This client follows a simple rule: when the JACK transport is
  47. * running, copy the input port to the output. When it stops, exit.
  48. */
  49. int
  50. process (jack_nframes_t nframes, void *arg)
  51. {
  52. jack_default_audio_sample_t *in, *out1, *out2;
  53. paTestData *data = (paTestData*)arg;
  54. int i;
  55. in = jack_port_get_buffer (input_port, nframes);
  56. out1 = jack_port_get_buffer (output_port1, nframes);
  57. out2 = jack_port_get_buffer (output_port2, nframes);
  58. for( i=0; i<nframes; i++ )
  59. {
  60. out1[i] = data->sine[data->left_phase]; /* left */
  61. out2[i] = data->sine[data->right_phase]; /* right */
  62. data->left_phase += 1;
  63. if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
  64. data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
  65. if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
  66. }
  67. return 0;
  68. }
  69. /**
  70. * JACK calls this shutdown_callback if the server ever shuts down or
  71. * decides to disconnect the client.
  72. */
  73. void
  74. jack_shutdown (void *arg)
  75. {
  76. exit (1);
  77. }
  78. int
  79. main (int argc, char *argv[])
  80. {
  81. const char **ports;
  82. const char *client_name;
  83. const char *server_name = NULL;
  84. jack_options_t options = JackNullOption;
  85. jack_status_t status;
  86. paTestData data;
  87. int i;
  88. if (argc >= 2) { /* client name specified? */
  89. client_name = argv[1];
  90. if (argc >= 3) { /* server name specified? */
  91. server_name = argv[2];
  92. options |= JackServerName;
  93. }
  94. } else { /* use basename of argv[0] */
  95. client_name = strrchr(argv[0], '/');
  96. if (client_name == 0) {
  97. client_name = argv[0];
  98. } else {
  99. client_name++;
  100. }
  101. }
  102. for( i=0; i<TABLE_SIZE; i++ )
  103. {
  104. data.sine[i] = 0.2 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
  105. }
  106. data.left_phase = data.right_phase = 0;
  107. /* open a client connection to the JACK server */
  108. client = jack_client_open (client_name, options, &status, server_name);
  109. if (client == NULL) {
  110. fprintf (stderr, "jack_client_open() failed, "
  111. "status = 0x%2.0x\n", status);
  112. if (status & JackServerFailed) {
  113. fprintf (stderr, "Unable to connect to JACK server\n");
  114. }
  115. exit (1);
  116. }
  117. if (status & JackServerStarted) {
  118. fprintf (stderr, "JACK server started\n");
  119. }
  120. if (status & JackNameNotUnique) {
  121. client_name = jack_get_client_name(client);
  122. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  123. }
  124. /* tell the JACK server to call `process()' whenever
  125. there is work to be done.
  126. */
  127. jack_set_process_callback (client, process, &data);
  128. /* tell the JACK server to call `jack_shutdown()' if
  129. it ever shuts down, either entirely, or if it
  130. just decides to stop calling us.
  131. */
  132. jack_on_shutdown (client, jack_shutdown, 0);
  133. /* create two ports */
  134. input_port = jack_port_register (client, "input",
  135. JACK_DEFAULT_AUDIO_TYPE,
  136. JackPortIsInput, 0);
  137. output_port1 = jack_port_register (client, "output1",
  138. JACK_DEFAULT_AUDIO_TYPE,
  139. JackPortIsOutput, 0);
  140. output_port2 = jack_port_register (client, "output2",
  141. JACK_DEFAULT_AUDIO_TYPE,
  142. JackPortIsOutput, 0);
  143. if ((input_port == NULL) || (output_port1 == NULL) || (output_port2 == NULL)) {
  144. fprintf(stderr, "no more JACK ports available\n");
  145. exit (1);
  146. }
  147. /* Tell the JACK server that we are ready to roll. Our
  148. * process() callback will start running now. */
  149. if (jack_activate (client)) {
  150. fprintf (stderr, "cannot activate client");
  151. exit (1);
  152. }
  153. /* Connect the ports. You can't do this before the client is
  154. * activated, because we can't make connections to clients
  155. * that aren't running. Note the confusing (but necessary)
  156. * orientation of the driver backend ports: playback ports are
  157. * "input" to the backend, and capture ports are "output" from
  158. * it.
  159. */
  160. ports = jack_get_ports (client, NULL, NULL,
  161. JackPortIsPhysical|JackPortIsOutput);
  162. if (ports == NULL) {
  163. fprintf(stderr, "no physical capture ports\n");
  164. exit (1);
  165. }
  166. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  167. fprintf (stderr, "cannot connect input ports\n");
  168. }
  169. free (ports);
  170. ports = jack_get_ports (client, NULL, NULL,
  171. JackPortIsPhysical|JackPortIsInput);
  172. if (ports == NULL) {
  173. fprintf(stderr, "no physical playback ports\n");
  174. exit (1);
  175. }
  176. if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
  177. fprintf (stderr, "cannot connect output ports\n");
  178. }
  179. if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
  180. fprintf (stderr, "cannot connect output ports\n");
  181. }
  182. free (ports);
  183. /* install a signal handler to properly quits jack client */
  184. #ifdef WIN32
  185. signal(SIGINT, signal_handler);
  186. signal(SIGABRT, signal_handler);
  187. signal(SIGTERM, signal_handler);
  188. #else
  189. signal(SIGQUIT, signal_handler);
  190. signal(SIGTERM, signal_handler);
  191. signal(SIGHUP, signal_handler);
  192. signal(SIGINT, signal_handler);
  193. #endif
  194. /* keep running until the transport stops */
  195. while (client_state != Exit) {
  196. #ifdef WIN32
  197. Sleep(1000);
  198. #else
  199. sleep (1);
  200. #endif
  201. }
  202. jack_client_close (client);
  203. exit (0);
  204. }