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.

186 lines
4.8KB

  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 <assert.h>
  26. #include <jack/net.h>
  27. jack_net_master_t* net;
  28. #define BUFFER_SIZE 512
  29. #define SAMPLE_RATE 44100
  30. static void signal_handler(int sig)
  31. {
  32. jack_net_master_close(net);
  33. fprintf(stderr, "signal received, exiting ...\n");
  34. exit(0);
  35. }
  36. static void
  37. usage ()
  38. {
  39. fprintf (stderr, "\n"
  40. "usage: jack_net_master \n"
  41. " [ -b buffer size (default = %d) ]\n"
  42. " [ -r sample rate (default = %d) ]\n"
  43. " [ -a hostname (default = %s) ]\n"
  44. " [ -p port (default = %d) ]\n", BUFFER_SIZE, SAMPLE_RATE, DEFAULT_MULTICAST_IP, DEFAULT_PORT);
  45. }
  46. int
  47. main (int argc, char *argv[])
  48. {
  49. int buffer_size = BUFFER_SIZE;
  50. int sample_rate = SAMPLE_RATE;
  51. int udp_port = DEFAULT_PORT;
  52. const char* multicast_ip = DEFAULT_MULTICAST_IP;
  53. const char *options = "b:r:a:p:h";
  54. int option_index;
  55. int opt;
  56. struct option long_options[] =
  57. {
  58. {"buffer size", 1, 0, 'b'},
  59. {"sample rate", 1, 0, 'r'},
  60. {"hostname", 1, 0, 'a'},
  61. {"port", 1, 0, 'p'},
  62. {0, 0, 0, 0}
  63. };
  64. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != -1) {
  65. switch (opt) {
  66. case 'b':
  67. buffer_size = atoi(optarg);
  68. break;
  69. case 'r':
  70. sample_rate = atoi(optarg);
  71. break;
  72. case 'a':
  73. multicast_ip = strdup(optarg);
  74. break;
  75. case 'p':
  76. udp_port = atoi(optarg);
  77. break;
  78. case 'h':
  79. usage();
  80. return -1;
  81. }
  82. }
  83. int i;
  84. jack_master_t request = { 4, 4, -1, -1, buffer_size, sample_rate, "master" };
  85. //jack_master_t request = { -1, -1, -1, -1, buffer_size, sample_rate, "master" };
  86. jack_slave_t result;
  87. float** audio_input_buffer;
  88. float** audio_output_buffer;
  89. int wait_usec = (int) ((((float)buffer_size) * 1000000) / ((float)sample_rate));
  90. printf("Waiting for a slave...\n");
  91. if ((net = jack_net_master_open(multicast_ip, udp_port, "net_master", &request, &result)) == 0) {
  92. fprintf(stderr, "NetJack master can not be opened\n");
  93. return 1;
  94. }
  95. printf("Slave is running...\n");
  96. /* install a signal handler to properly quits jack client */
  97. #ifdef WIN32
  98. signal(SIGINT, signal_handler);
  99. signal(SIGABRT, signal_handler);
  100. signal(SIGTERM, signal_handler);
  101. #else
  102. signal(SIGQUIT, signal_handler);
  103. signal(SIGTERM, signal_handler);
  104. signal(SIGHUP, signal_handler);
  105. signal(SIGINT, signal_handler);
  106. #endif
  107. // Allocate buffers
  108. audio_input_buffer = (float**)calloc(result.audio_input, sizeof(float*));
  109. for (i = 0; i < result.audio_input; i++) {
  110. audio_input_buffer[i] = (float*)calloc(buffer_size, sizeof(float));
  111. }
  112. audio_output_buffer = (float**)calloc(result.audio_output, sizeof(float*));
  113. for (i = 0; i < result.audio_output; i++) {
  114. audio_output_buffer[i] = (float*)calloc(buffer_size, sizeof(float));
  115. }
  116. /*
  117. Run until interrupted.
  118. WARNING !! : this code is given for demonstration purpose. For proper timing bevahiour
  119. it has to be called in a real-time context (which is *not* the case here...)
  120. */
  121. while (1) {
  122. // Copy input to output
  123. assert(result.audio_input == result.audio_output);
  124. for (i = 0; i < result.audio_input; i++) {
  125. memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
  126. }
  127. if (jack_net_master_send(net, result.audio_output, audio_output_buffer, 0, NULL) < 0) {
  128. printf("jack_net_master_send failure, exiting\n");
  129. break;
  130. }
  131. if (jack_net_master_recv(net, result.audio_input, audio_input_buffer, 0, NULL) < 0) {
  132. printf("jack_net_master_recv failure, exiting\n");
  133. break;
  134. }
  135. usleep(wait_usec);
  136. };
  137. // Wait for application end
  138. jack_net_master_close(net);
  139. for (i = 0; i < result.audio_input; i++) {
  140. free(audio_input_buffer[i]);
  141. }
  142. free(audio_input_buffer);
  143. for (i = 0; i < result.audio_output; i++) {
  144. free(audio_output_buffer[i]);
  145. }
  146. free(audio_output_buffer);
  147. exit (0);
  148. }