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.

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