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.

169 lines
4.1KB

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