Use "64" suffix in windows 64bit build to match existing installer
[jack2.git] / example-clients / netmaster.c
blob0245fcb6115957a96307cd741fdbfc299ed3d8f7
1 /*
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.
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #ifndef WIN32
24 #include <unistd.h>
25 #endif
26 #include <math.h>
27 #include <signal.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <assert.h>
32 #include <jack/net.h>
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");
43 exit(0);
46 static void
47 usage ()
49 fprintf (stderr, "\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);
57 int
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";
65 int option_index;
66 int opt;
68 struct option long_options[] =
70 {"buffer size", 1, 0, 'b'},
71 {"sample rate", 1, 0, 'r'},
72 {"hostname", 1, 0, 'a'},
73 {"port", 1, 0, 'p'},
74 {0, 0, 0, 0}
77 while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != -1) {
79 switch (opt) {
81 case 'b':
82 buffer_size = atoi(optarg);
83 break;
85 case 'r':
86 sample_rate = atoi(optarg);
87 break;
89 case 'a':
90 multicast_ip = strdup(optarg);
91 break;
93 case 'p':
94 udp_port = atoi(optarg);
95 break;
97 case 'h':
98 usage();
99 return -1;
103 int i;
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 };
106 jack_slave_t result;
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");
115 return 1;
118 printf("Slave is running...\n");
120 /* install a signal handler to properly quits jack client */
121 #ifdef WIN32
122 signal(SIGINT, signal_handler);
123 signal(SIGABRT, signal_handler);
124 signal(SIGTERM, signal_handler);
125 #else
126 signal(SIGQUIT, signal_handler);
127 signal(SIGTERM, signal_handler);
128 signal(SIGHUP, signal_handler);
129 signal(SIGINT, signal_handler);
130 #endif
132 // Allocate buffers
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...)
151 //usleep(5*1000000);
152 printf("Wait...\n");
153 //sleep(10);
154 usleep(1000000);
155 printf("Wait...OK\n");
157 while (1) {
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");
168 break;
171 usleep(10000);
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");
175 break;
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");
181 break;
184 usleep(10000);
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");
188 break;
191 usleep(wait_usec);
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);
207 exit (0);