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.

182 lines
4.7KB

  1. /*
  2. Copyright (C) 2009 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #ifndef WIN32
  19. #include <unistd.h>
  20. #endif
  21. #include <math.h>
  22. #include <signal.h>
  23. #include <getopt.h>
  24. #include <string.h>
  25. #include <jack/net.h>
  26. jack_net_master_t* net;
  27. #define BUFFER_SIZE 512
  28. #define SAMPLE_RATE 44100
  29. static void signal_handler(int sig)
  30. {
  31. jack_net_master_close(net);
  32. fprintf(stderr, "signal received, exiting ...\n");
  33. exit(0);
  34. }
  35. static void
  36. usage ()
  37. {
  38. fprintf (stderr, "\n"
  39. "usage: jack_net_master \n"
  40. " [ -b buffer size (default = %d) ]\n"
  41. " [ -r sample rate (default = %d) ]\n"
  42. " [ -a hostname (default = %s) ]\n"
  43. " [ -p port (default = %d) ]\n", BUFFER_SIZE, SAMPLE_RATE, DEFAULT_MULTICAST_IP, DEFAULT_PORT);
  44. }
  45. int
  46. main (int argc, char *argv[])
  47. {
  48. int buffer_size = BUFFER_SIZE;
  49. int sample_rate = SAMPLE_RATE;
  50. int port = DEFAULT_PORT;
  51. char* multicast_ip = DEFAULT_MULTICAST_IP;
  52. const char *options = "b:r:a:p:";
  53. int option_index;
  54. int opt;
  55. struct option long_options[] =
  56. {
  57. {"buffer size", 1, 0, 'b'},
  58. {"sample rate", 1, 0, 'r'},
  59. {"hostname", 1, 0, 'a'},
  60. {"port", 1, 0, 'p'},
  61. {0, 0, 0, 0}
  62. };
  63. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  64. switch (opt) {
  65. case 'b':
  66. buffer_size = atoi(optarg);
  67. break;
  68. case 'r':
  69. sample_rate = atoi(optarg);
  70. break;
  71. case 'a':
  72. multicast_ip = strdup(optarg);
  73. break;
  74. case 'p':
  75. port = atoi(optarg);
  76. break;
  77. case 'h':
  78. usage();
  79. return -1;
  80. }
  81. }
  82. int i;
  83. jack_master_t request = { buffer_size, sample_rate, "master" };
  84. jack_slave_t result;
  85. float** audio_input_buffer;
  86. float** audio_output_buffer;
  87. int wait_usec = (int) ((((float)buffer_size) * 1000000) / ((float)sample_rate));
  88. printf("Waiting for a slave...\n");
  89. if ((net = jack_net_master_open(DEFAULT_MULTICAST_IP, DEFAULT_PORT, "net_master", &request, &result)) == 0) {
  90. fprintf(stderr, "jack server not running?\n");
  91. return 1;
  92. }
  93. printf("Slave is running...\n");
  94. /* install a signal handler to properly quits jack client */
  95. #ifdef WIN32
  96. signal(SIGINT, signal_handler);
  97. signal(SIGABRT, signal_handler);
  98. signal(SIGTERM, signal_handler);
  99. #else
  100. signal(SIGQUIT, signal_handler);
  101. signal(SIGTERM, signal_handler);
  102. signal(SIGHUP, signal_handler);
  103. signal(SIGINT, signal_handler);
  104. #endif
  105. // Allocate buffers
  106. audio_input_buffer = calloc(result.audio_input, sizeof(float*));
  107. for (i = 0; i < result.audio_input; i++) {
  108. audio_input_buffer[i] = calloc(buffer_size, sizeof(float));
  109. }
  110. audio_output_buffer = calloc(result.audio_output, sizeof(float*));
  111. for (i = 0; i < result.audio_output; i++) {
  112. audio_output_buffer[i] = calloc(buffer_size, sizeof(float));
  113. }
  114. /*
  115. Run until interrupted.
  116. WARNING !! : this code is given for demonstration purpose. For proper timing bevahiour
  117. it has to be called in a real-time context (which is *not* the case here...)
  118. */
  119. while (1) {
  120. // Copy input to output
  121. for (i = 0; i < result.audio_input; i++) {
  122. memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
  123. }
  124. if (jack_net_master_send(net, result.audio_output, audio_output_buffer, 0, NULL) < 0) {
  125. printf("jack_net_master_send failure, exiting\n");
  126. break;
  127. }
  128. if (jack_net_master_recv(net, result.audio_input, audio_input_buffer, 0, NULL) < 0) {
  129. printf("jack_net_master_recv failure, exiting\n");
  130. break;
  131. }
  132. usleep(wait_usec);
  133. };
  134. // Wait for application end
  135. jack_net_master_close(net);
  136. for (i = 0; i < result.audio_input; i++) {
  137. free(audio_input_buffer[i]);
  138. }
  139. free(audio_input_buffer);
  140. for (i = 0; i < result.audio_output; i++) {
  141. free(audio_output_buffer[i]);
  142. }
  143. free(audio_output_buffer);
  144. exit (0);
  145. }