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.

314 lines
7.4KB

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