2 Copyright (C) 2009 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 jack_net_master_t
* net
;
36 #define BUFFER_SIZE 512
37 #define SAMPLE_RATE 44100
39 static void signal_handler(int sig
)
41 jack_net_master_close(net
);
42 fprintf(stderr
, "signal received, exiting ...\n");
50 "usage: jack_net_master \n"
51 " [ -b buffer size (default = %d) ]\n"
52 " [ -r sample rate (default = %d) ]\n"
53 " [ -a hostname (default = %s) ]\n"
54 " [ -p port (default = %d) ]\n", BUFFER_SIZE
, SAMPLE_RATE
, DEFAULT_MULTICAST_IP
, DEFAULT_PORT
);
58 main (int argc
, char *argv
[])
60 int buffer_size
= BUFFER_SIZE
;
61 int sample_rate
= SAMPLE_RATE
;
62 int udp_port
= DEFAULT_PORT
;
63 const char* multicast_ip
= DEFAULT_MULTICAST_IP
;
64 const char *options
= "b:r:a:p:h";
68 struct option long_options
[] =
70 {"buffer size", 1, 0, 'b'},
71 {"sample rate", 1, 0, 'r'},
72 {"hostname", 1, 0, 'a'},
77 while ((opt
= getopt_long (argc
, argv
, options
, long_options
, &option_index
)) != -1) {
82 buffer_size
= atoi(optarg
);
86 sample_rate
= atoi(optarg
);
90 multicast_ip
= strdup(optarg
);
94 udp_port
= atoi(optarg
);
104 //jack_master_t request = { 4, 4, -1, -1, buffer_size, sample_rate, "master", -1 };
105 jack_master_t request
= { -1, -1, -1, -1, buffer_size
, sample_rate
, "net_master", 6, true };
107 float** audio_input_buffer
;
108 float** audio_output_buffer
;
109 int wait_usec
= (int) ((((float)buffer_size
) * 1000000) / ((float)sample_rate
));
111 printf("Waiting for a slave...\n");
113 if ((net
= jack_net_master_open(multicast_ip
, udp_port
, &request
, &result
)) == 0) {
114 fprintf(stderr
, "NetJack master can not be opened\n");
118 printf("Slave is running...\n");
120 /* install a signal handler to properly quits jack client */
122 signal(SIGINT
, signal_handler
);
123 signal(SIGABRT
, signal_handler
);
124 signal(SIGTERM
, signal_handler
);
126 signal(SIGQUIT
, signal_handler
);
127 signal(SIGTERM
, signal_handler
);
128 signal(SIGHUP
, signal_handler
);
129 signal(SIGINT
, signal_handler
);
134 audio_input_buffer
= (float**)calloc(result
.audio_input
, sizeof(float*));
135 for (i
= 0; i
< result
.audio_input
; i
++) {
136 audio_input_buffer
[i
] = (float*)calloc(buffer_size
, sizeof(float));
139 audio_output_buffer
= (float**)calloc(result
.audio_output
, sizeof(float*));
140 for (i
= 0; i
< result
.audio_output
; i
++) {
141 audio_output_buffer
[i
] = (float*)calloc(buffer_size
, sizeof(float));
145 Run until interrupted.
147 WARNING !! : this code is given for demonstration purpose. For proper timing bevahiour
148 it has to be called in a real-time context (which is *not* the case here...)
155 printf("Wait...OK\n");
159 // Copy input to output
160 assert(result
.audio_input
== result
.audio_output
);
161 for (i
= 0; i
< result
.audio_input
; i
++) {
162 memcpy(audio_output_buffer
[i
], audio_input_buffer
[i
], buffer_size
* sizeof(float));
166 if (jack_net_master_send(net, result.audio_output, audio_output_buffer, 0, NULL) < 0) {
167 printf("jack_net_master_send failure, exiting\n");
173 if (jack_net_master_recv(net, result.audio_input, audio_input_buffer, 0, NULL) < 0) {
174 printf("jack_net_master_recv failure, exiting\n");
179 if (jack_net_master_send_slice(net
, result
.audio_output
, audio_output_buffer
, 0, NULL
, BUFFER_SIZE
/2) < 0) {
180 printf("jack_net_master_send failure, exiting\n");
186 if (jack_net_master_recv_slice(net
, result
.audio_input
, audio_input_buffer
, 0, NULL
, BUFFER_SIZE
/2) < 0) {
187 printf("jack_net_master_recv failure, exiting\n");
194 // Wait for application end
195 jack_net_master_close(net
);
197 for (i
= 0; i
< result
.audio_input
; i
++) {
198 free(audio_input_buffer
[i
]);
200 free(audio_input_buffer
);
202 for (i
= 0; i
< result
.audio_output
; i
++) {
203 free(audio_output_buffer
[i
]);
205 free(audio_output_buffer
);