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.

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