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.

220 lines
5.2KB

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