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.

236 lines
5.7KB

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