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.

163 lines
4.0KB

  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_slave_t* net;
  27. static void signal_handler(int sig)
  28. {
  29. jack_net_slave_close(net);
  30. fprintf(stderr, "signal received, exiting ...\n");
  31. exit(0);
  32. }
  33. static void
  34. usage ()
  35. {
  36. fprintf (stderr, "\n"
  37. "usage: jack_net_slave \n"
  38. " [ -C capture channels (default = 2)]\n"
  39. " [ -P playback channels (default = 2) ]\n"
  40. " [ -a hostname (default = %s) ]\n"
  41. " [ -p port (default = %d)]\n", DEFAULT_MULTICAST_IP, DEFAULT_PORT);
  42. }
  43. static int net_process(jack_nframes_t buffer_size,
  44. int audio_input,
  45. float** audio_input_buffer,
  46. int midi_input,
  47. void** midi_input_buffer,
  48. int audio_output,
  49. float** audio_output_buffer,
  50. int midi_output,
  51. void** midi_output_buffer,
  52. void* data)
  53. {
  54. int i;
  55. // Copy input to output
  56. for (i = 0; i < audio_input; i++) {
  57. memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
  58. }
  59. return 0;
  60. }
  61. int
  62. main (int argc, char *argv[])
  63. {
  64. int audio_input = 2;
  65. int audio_output = 2;
  66. int port = DEFAULT_PORT;
  67. char* multicast_ip = DEFAULT_MULTICAST_IP;
  68. const char *options = "C:P:a:p:";
  69. int option_index;
  70. int opt;
  71. struct option long_options[] =
  72. {
  73. {"audio input", 1, 0, 'C'},
  74. {"audio output", 1, 0, 'P'},
  75. {"hostname", 1, 0, 'a'},
  76. {"port", 1, 0, 'p'},
  77. {0, 0, 0, 0}
  78. };
  79. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  80. switch (opt) {
  81. case 'C':
  82. audio_input = atoi(optarg);
  83. break;
  84. case 'P':
  85. audio_output = atoi(optarg);
  86. break;
  87. case 'a':
  88. multicast_ip = strdup(optarg);
  89. break;
  90. case 'p':
  91. port = atoi(optarg);
  92. break;
  93. case 'h':
  94. usage();
  95. return -1;
  96. }
  97. }
  98. jack_slave_t request = { audio_input, audio_output, 0, 0, DEFAULT_MTU, -1, JackSlowMode };
  99. jack_master_t result;
  100. printf("Waiting for a master...\n");
  101. if ((net = jack_net_slave_open(DEFAULT_MULTICAST_IP, DEFAULT_PORT, "net_slave", &request, &result)) == 0) {
  102. fprintf(stderr, "jack server not running?\n");
  103. return 1;
  104. }
  105. printf("Slave is found and running...\n");
  106. jack_set_net_slave_process_callback(net, net_process, NULL);
  107. if (jack_net_slave_activate(net) != 0) {
  108. fprintf(stderr, "Cannot sactivate client\n");
  109. return 1;
  110. }
  111. /* install a signal handler to properly quits jack client */
  112. #ifdef WIN32
  113. signal(SIGINT, signal_handler);
  114. signal(SIGABRT, signal_handler);
  115. signal(SIGTERM, signal_handler);
  116. #else
  117. signal(SIGQUIT, signal_handler);
  118. signal(SIGTERM, signal_handler);
  119. signal(SIGHUP, signal_handler);
  120. signal(SIGINT, signal_handler);
  121. #endif
  122. /* run until interrupted */
  123. while (1) {
  124. #ifdef WIN32
  125. Sleep(1000);
  126. #else
  127. sleep(1);
  128. #endif
  129. };
  130. // Wait for application end
  131. jack_net_slave_deactivate(net);
  132. jack_net_slave_close(net);
  133. exit (0);
  134. }