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.

254 lines
6.1KB

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