netmaster.c 4.9 KB
Newer Older
1
2
/*
    Copyright (C) 2009 Grame
sletz's avatar
sletz committed
3

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <math.h>
#include <signal.h>
#include <getopt.h>
#include <string.h>
sletz's avatar
sletz committed
29
#include <assert.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <jack/net.h>

jack_net_master_t* net;

#define BUFFER_SIZE 512
#define SAMPLE_RATE 44100

static void signal_handler(int sig)
{
	jack_net_master_close(net);
	fprintf(stderr, "signal received, exiting ...\n");
	exit(0);
}

static void
usage ()
{
	fprintf (stderr, "\n"
sletz's avatar
sletz committed
49
50
51
52
53
    "usage: jack_net_master \n"
    "              [ -b buffer size (default = %d) ]\n"
    "              [ -r sample rate (default = %d) ]\n"
    "              [ -a hostname (default = %s) ]\n"
    "              [ -p port (default = %d) ]\n", BUFFER_SIZE, SAMPLE_RATE, DEFAULT_MULTICAST_IP, DEFAULT_PORT);
54
55
56
57
58
59
60
}

int
main (int argc, char *argv[])
{
    int buffer_size = BUFFER_SIZE;
    int sample_rate = SAMPLE_RATE;
Stephane Letz's avatar
Stephane Letz committed
61
    int udp_port = DEFAULT_PORT;
sletz's avatar
sletz committed
62
    const char* multicast_ip = DEFAULT_MULTICAST_IP;
Stephane Letz's avatar
Stephane Letz committed
63
 	const char *options = "b:r:a:p:h";
64
65
    int option_index;
	int opt;
sletz's avatar
sletz committed
66

67
68
69
70
71
72
73
74
    struct option long_options[] =
	{
		{"buffer size", 1, 0, 'b'},
		{"sample rate", 1, 0, 'r'},
		{"hostname", 1, 0, 'a'},
		{"port", 1, 0, 'p'},
		{0, 0, 0, 0}
	};
sletz's avatar
sletz committed
75

76
	while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != -1) {
sletz's avatar
sletz committed
77

78
		switch (opt) {
sletz's avatar
sletz committed
79

80
81
82
		case 'b':
			buffer_size = atoi(optarg);
			break;
sletz's avatar
sletz committed
83

84
85
86
		case 'r':
			sample_rate = atoi(optarg);
			break;
sletz's avatar
sletz committed
87

88
89
90
		case 'a':
            multicast_ip = strdup(optarg);
            break;
sletz's avatar
sletz committed
91

92
		case 'p':
Stephane Letz's avatar
Stephane Letz committed
93
			udp_port = atoi(optarg);
94
			break;
sletz's avatar
sletz committed
95

96
97
98
99
100
		case 'h':
			usage();
			return -1;
		}
	}
sletz's avatar
sletz committed
101

102
    int i;
103
104
    //jack_master_t request = { 4, 4, -1, -1, buffer_size, sample_rate, "master", -1 };
    jack_master_t request = { -1, -1, -1, -1, buffer_size, sample_rate, "master", 6 };
105
106
107
108
    jack_slave_t result;
    float** audio_input_buffer;
    float** audio_output_buffer;
    int wait_usec = (int) ((((float)buffer_size) * 1000000) / ((float)sample_rate));
sletz's avatar
sletz committed
109

110
111
    printf("Waiting for a slave...\n");

Stephane Letz's avatar
Stephane Letz committed
112
    if ((net = jack_net_master_open(multicast_ip, udp_port, "net_master", &request, &result))  == 0) {
sletz's avatar
sletz committed
113
        fprintf(stderr, "NetJack master can not be opened\n");
114
115
		return 1;
	}
sletz's avatar
sletz committed
116
117

    printf("Slave is running...\n");
sletz's avatar
sletz committed
118

119
120
121
122
123
124
125
126
127
128
129
    /* install a signal handler to properly quits jack client */
#ifdef WIN32
	signal(SIGINT, signal_handler);
    signal(SIGABRT, signal_handler);
	signal(SIGTERM, signal_handler);
#else
	signal(SIGQUIT, signal_handler);
	signal(SIGTERM, signal_handler);
	signal(SIGHUP, signal_handler);
	signal(SIGINT, signal_handler);
#endif
sletz's avatar
sletz committed
130

131
    // Allocate buffers
sletz's avatar
sletz committed
132
    
sletz's avatar
sletz committed
133
    audio_input_buffer = (float**)calloc(result.audio_input, sizeof(float*));
134
    for (i = 0; i < result.audio_input; i++) {
sletz's avatar
sletz committed
135
        audio_input_buffer[i] = (float*)calloc(buffer_size, sizeof(float));
136
    }
sletz's avatar
sletz committed
137

sletz's avatar
sletz committed
138
    audio_output_buffer = (float**)calloc(result.audio_output, sizeof(float*));
139
    for (i = 0; i < result.audio_output; i++) {
sletz's avatar
sletz committed
140
        audio_output_buffer[i] = (float*)calloc(buffer_size, sizeof(float));
141
    }
sletz's avatar
sletz committed
142

sletz's avatar
sletz committed
143
144
    /*
    Run until interrupted.
sletz's avatar
sletz committed
145

sletz's avatar
sletz committed
146
147
148
    WARNING !! : this code is given for demonstration purpose. For proper timing bevahiour
    it has to be called in a real-time context (which is *not* the case here...)
    */
149
150
151
    
    //usleep(5*1000000);
  
152
  	while (1) {
sletz's avatar
sletz committed
153

154
        // Copy input to output
sletz's avatar
sletz committed
155
        assert(result.audio_input == result.audio_output);
156
157
158
        for (i = 0; i < result.audio_input; i++) {
            memcpy(audio_output_buffer[i], audio_input_buffer[i], buffer_size * sizeof(float));
        }
159
   
160
161
162
163
        if (jack_net_master_send(net, result.audio_output, audio_output_buffer, 0, NULL) < 0) {
            printf("jack_net_master_send failure, exiting\n");
            break;
        }
164
         
165
166
167
168
        if (jack_net_master_recv(net, result.audio_input, audio_input_buffer, 0, NULL) < 0) {
            printf("jack_net_master_recv failure, exiting\n");
            break;
        }
169
        
170
171
        usleep(wait_usec);
	};
sletz's avatar
sletz committed
172

173
174
    // Wait for application end
    jack_net_master_close(net);
sletz's avatar
sletz committed
175

176
177
178
179
    for (i = 0; i < result.audio_input; i++) {
        free(audio_input_buffer[i]);
    }
    free(audio_input_buffer);
sletz's avatar
sletz committed
180

181
182
183
184
    for (i = 0; i < result.audio_output; i++) {
          free(audio_output_buffer[i]);
    }
    free(audio_output_buffer);
sletz's avatar
sletz committed
185

186
187
    exit (0);
}