1 |
/* jack_test1.c */ |
---|
2 |
|
---|
3 |
#include <stdio.h> |
---|
4 |
#include <stdlib.h> |
---|
5 |
#include <math.h> |
---|
6 |
|
---|
7 |
#include <jack/jack.h> |
---|
8 |
|
---|
9 |
|
---|
10 |
jack_client_t *client; |
---|
11 |
jack_port_t **iports; |
---|
12 |
jack_port_t **oports; |
---|
13 |
|
---|
14 |
unsigned int num_of_ports = 2; |
---|
15 |
unsigned int seconds_to_run = 120; |
---|
16 |
|
---|
17 |
int ret = 0; |
---|
18 |
|
---|
19 |
/* shutdown process.. */ |
---|
20 |
void shutdown_0 (void *arg) |
---|
21 |
{ |
---|
22 |
fprintf(stderr, "\n*** shutdown ***\n"); |
---|
23 |
ret = 0; |
---|
24 |
} |
---|
25 |
|
---|
26 |
|
---|
27 |
/* stand-alone process. mix all the input ports to each output port. */ |
---|
28 |
int process_0 (jack_nframes_t frames, void *arg) |
---|
29 |
{ |
---|
30 |
jack_default_audio_sample_t *ibuf; |
---|
31 |
jack_default_audio_sample_t *obuf; |
---|
32 |
jack_nframes_t k; |
---|
33 |
int i; |
---|
34 |
|
---|
35 |
for (i = 0; i < num_of_ports; i++) { |
---|
36 |
obuf = (jack_default_audio_sample_t*) |
---|
37 |
jack_port_get_buffer(oports[i], frames); |
---|
38 |
for (k = 0; k < frames; k++) { |
---|
39 |
obuf[k] = 0.9; |
---|
40 |
} |
---|
41 |
} |
---|
42 |
return ret; |
---|
43 |
} |
---|
44 |
|
---|
45 |
int main(int argc, char *argv[]) |
---|
46 |
{ |
---|
47 |
char client_name[33]; |
---|
48 |
char iport_name[33]; |
---|
49 |
char oport_name[33]; |
---|
50 |
const char **pports; |
---|
51 |
int i; |
---|
52 |
|
---|
53 |
/* seconds_to_run: default = 60 seconds. */ |
---|
54 |
if (argc > 1) |
---|
55 |
seconds_to_run = (unsigned int) atoi(argv[1]); |
---|
56 |
fprintf(stderr, "seconds to run: %u\n", seconds_to_run); |
---|
57 |
|
---|
58 |
/* num_of_ports: default = 2 ports. */ |
---|
59 |
if (argc > 2) |
---|
60 |
num_of_ports = (unsigned int) atoi(argv[2]); |
---|
61 |
fprintf(stderr, "num.of ports: %u\n", num_of_ports); |
---|
62 |
|
---|
63 |
sprintf(client_name, "jack_test_dc"); |
---|
64 |
fprintf(stderr, "client_name: %s\n", client_name); |
---|
65 |
|
---|
66 |
fprintf(stderr, "%s: client_new\n", client_name); |
---|
67 |
client = jack_client_new(client_name); |
---|
68 |
if (!client) { |
---|
69 |
fprintf(stderr, "%s: jackd not running?\n", client_name); |
---|
70 |
return 1; |
---|
71 |
} |
---|
72 |
|
---|
73 |
iports = (jack_port_t **) malloc(num_of_ports * sizeof(jack_port_t *)); |
---|
74 |
oports = (jack_port_t **) malloc(num_of_ports * sizeof(jack_port_t *)); |
---|
75 |
|
---|
76 |
fprintf(stderr, "%s: port_register\n", client_name); |
---|
77 |
for (i = 0; i < num_of_ports; i++) { |
---|
78 |
sprintf(iport_name, "in_%d", i); |
---|
79 |
iports[i] = jack_port_register(client, iport_name, |
---|
80 |
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); |
---|
81 |
if (iports[i] == NULL) { |
---|
82 |
fprintf(stderr, "%s:%s port registration failed\n", |
---|
83 |
client_name, iport_name); |
---|
84 |
goto exit1; |
---|
85 |
} |
---|
86 |
sprintf(oport_name, "out_%d", i); |
---|
87 |
oports[i] = jack_port_register(client, oport_name, |
---|
88 |
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); |
---|
89 |
if (oports[i] == NULL) { |
---|
90 |
fprintf(stderr, "%s:%s port registration failed\n", |
---|
91 |
client_name, oport_name); |
---|
92 |
goto exit1; |
---|
93 |
} |
---|
94 |
} |
---|
95 |
|
---|
96 |
fprintf(stderr, "%s: set_process_callback\n", client_name); |
---|
97 |
jack_set_process_callback(client, process_0, 0); |
---|
98 |
|
---|
99 |
fprintf(stderr, "%s: on_shutdown\n", client_name); |
---|
100 |
jack_on_shutdown(client, shutdown_0, 0); |
---|
101 |
|
---|
102 |
fprintf(stderr, "%s: activate\n", client_name); |
---|
103 |
jack_activate(client); |
---|
104 |
|
---|
105 |
/* try to connect to available physical outputs. */ |
---|
106 |
pports = jack_get_ports(client, 0, 0, |
---|
107 |
JackPortIsInput | JackPortIsPhysical); |
---|
108 |
if (pports) { |
---|
109 |
for (i = 0; i < num_of_ports && pports[i]; i++) { |
---|
110 |
sprintf(oport_name, "%s:out_%d", client_name, i); |
---|
111 |
fprintf(stderr, "%s: connect: %s -> %s\n", |
---|
112 |
client_name, oport_name, pports[i]); |
---|
113 |
if (jack_connect(client, oport_name, pports[i]) != 0) { |
---|
114 |
fprintf(stderr, "%s: connect failed\n"); |
---|
115 |
goto exit2; |
---|
116 |
} |
---|
117 |
} |
---|
118 |
free(pports); |
---|
119 |
} |
---|
120 |
|
---|
121 |
/* ok, we're up and running... */ |
---|
122 |
for (i = seconds_to_run; i > 0; --i) { |
---|
123 |
fprintf(stderr, "%s: running...%3d\r", client_name, i); |
---|
124 |
sleep(1); |
---|
125 |
} |
---|
126 |
|
---|
127 |
exit2: |
---|
128 |
|
---|
129 |
fprintf(stderr, "%s: deactivate\n", client_name); |
---|
130 |
jack_deactivate(client); |
---|
131 |
|
---|
132 |
exit1: |
---|
133 |
|
---|
134 |
fprintf(stderr, "%s: close\n", client_name); |
---|
135 |
jack_client_close(client); |
---|
136 |
|
---|
137 |
free(iports); |
---|
138 |
free(oports); |
---|
139 |
|
---|
140 |
return 0; |
---|
141 |
} |
---|
142 |
|
---|
143 |
/* end of jack_test1.c */ |
---|