]> mj.ucw.cz Git - misc.git/blob - ursaryd/jt.c
Added experimental utility for controlling jackd in the Ursarium
[misc.git] / ursaryd / jt.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4
5 #include <jack/jack.h>
6
7 typedef jack_default_audio_sample_t sample_t;
8
9 static jack_client_t *jc;
10 static jack_port_t *inport[2], *outport[4];
11
12 static int process_callback(jack_nframes_t nframes, void *arg __attribute__((unused)))
13 {
14         for (int ch=0; ch<2; ch++) {
15                 sample_t *in = jack_port_get_buffer(inport[ch], nframes);
16                 sample_t *out = jack_port_get_buffer(outport[ch], nframes);
17                 sample_t *out2 = jack_port_get_buffer(outport[2+ch], nframes);
18                 for (jack_nframes_t i=0; i<nframes; i++) {
19                         out[i] = in[i];
20                         out2[i] = in[i];
21                 }
22         }
23         return 0;
24 }
25
26 static void port_callback(jack_port_id_t id, int reg, void *arg __attribute__((unused)))
27 {
28         jack_port_t *p = jack_port_by_id(jc, id);
29         if (!p) {
30                 puts("port_callback: lookup failed");
31                 return;
32         }
33         const char *name = jack_port_name(p);
34         printf("%s port %s\n", (reg ? "Registered" : "Unregistered"), name);
35
36         const char *ss = strstr(name, ":from_slave_");
37         if (reg && ss) {
38                 char *to = (ss[12] == '1' ? "brum:in1" : "brum:in2");
39                 printf("\t... connecting to %s\n", to);
40                 if (jack_connect(jc, name, to))
41                         puts("jack_connect failed");
42         }
43 }
44
45 int main(void)
46 {
47         jc = jack_client_open("brum", JackNoStartServer, NULL);
48         if (!jc) {
49                 puts("jack_client_open failed");
50                 return 1;
51         }
52
53         for (int i=0; i<2; i++) {
54                 char name[16];
55                 sprintf(name, "in%d", i+1);
56                 inport[i] = jack_port_register(jc, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
57                 if (!inport[i]) {
58                         puts("jack_port_register failed");
59                         return 1;
60                 }
61         }
62
63         for (int i=0; i<4; i++) {
64                 char name[16];
65                 sprintf(name, "out%d", i+1);
66                 outport[i] = jack_port_register(jc, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
67                 if (!outport[i]) {
68                         puts("jack_port_register failed");
69                         return 1;
70                 }
71         }
72
73         jack_set_process_callback(jc, process_callback, NULL);
74         jack_set_port_registration_callback(jc, port_callback, NULL);
75
76         if (jack_activate(jc)) {
77                 puts("jack_activate failed");
78                 return 1;
79         }
80
81         puts("Connecting ports...");
82         for (int i=1; i<=4; i++) {
83                 char a[32], b[32];
84                 sprintf(a, "brum:out%d", i);
85                 sprintf(b, "system:playback_%d", i);
86                 if (jack_connect(jc, a, b))
87                         printf("Failed to connect %s -> %s\n", a, b);
88         }
89
90         puts("Running...");
91         char xxx[16];
92         read(0, xxx, sizeof(xxx));
93
94         puts("exiting");
95         jack_client_close(jc);
96         return 0;
97 }