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.

221 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 <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_default_audio_sample_t*)jack_port_get_buffer (output_port1, nframes);
  49. out2 = (jack_default_audio_sample_t*)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. int my_option = JackNullOption | JackServerName;
  85. options = (jack_options_t)my_option;
  86. }
  87. } else { /* use basename of argv[0] */
  88. client_name = strrchr(argv[0], '/');
  89. if (client_name == 0) {
  90. client_name = argv[0];
  91. } else {
  92. client_name++;
  93. }
  94. }
  95. for( i=0; i<TABLE_SIZE; i++ )
  96. {
  97. data.sine[i] = 0.2 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
  98. }
  99. data.left_phase = data.right_phase = 0;
  100. /* open a client connection to the JACK server */
  101. client = jack_client_open (client_name, options, &status, server_name);
  102. if (client == NULL) {
  103. fprintf (stderr, "jack_client_open() failed, "
  104. "status = 0x%2.0x\n", status);
  105. if (status & JackServerFailed) {
  106. fprintf (stderr, "Unable to connect to JACK server\n");
  107. }
  108. exit (1);
  109. }
  110. if (status & JackServerStarted) {
  111. fprintf (stderr, "JACK server started\n");
  112. }
  113. if (status & JackNameNotUnique) {
  114. client_name = jack_get_client_name(client);
  115. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  116. }
  117. /* tell the JACK server to call `process()' whenever
  118. there is work to be done.
  119. */
  120. jack_set_process_callback (client, process, &data);
  121. /* tell the JACK server to call `jack_shutdown()' if
  122. it ever shuts down, either entirely, or if it
  123. just decides to stop calling us.
  124. */
  125. jack_on_shutdown (client, jack_shutdown, 0);
  126. /* create two ports */
  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 ((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|JackPortIsInput);
  152. if (ports == NULL) {
  153. fprintf(stderr, "no physical playback ports\n");
  154. exit (1);
  155. }
  156. if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
  157. fprintf (stderr, "cannot connect output ports\n");
  158. }
  159. if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
  160. fprintf (stderr, "cannot connect output ports\n");
  161. }
  162. free (ports);
  163. /* install a signal handler to properly quits jack client */
  164. #ifdef WIN32
  165. signal(SIGINT, signal_handler);
  166. signal(SIGABRT, signal_handler);
  167. signal(SIGTERM, signal_handler);
  168. #else
  169. signal(SIGQUIT, signal_handler);
  170. signal(SIGTERM, signal_handler);
  171. signal(SIGHUP, signal_handler);
  172. signal(SIGINT, signal_handler);
  173. #endif
  174. /* keep running until the Ctrl+C */
  175. while (1) {
  176. #ifdef WIN32
  177. Sleep(1000);
  178. #else
  179. sleep (1);
  180. #endif
  181. }
  182. jack_client_close (client);
  183. exit (0);
  184. }