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.

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