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.

224 lines
5.3KB

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