2 * Copyright (C) 2001 Steve Harris
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.
27 #include <jack/jack.h>
29 jack_port_t
*input_port
;
30 jack_port_t
*output_port
;
32 unsigned int impulse_sent
= 0;
34 unsigned long response_duration
;
35 unsigned long response_pos
;
36 int grab_finished
= 0;
39 process (jack_nframes_t nframes
, void *arg
)
42 jack_default_audio_sample_t
*out
= (jack_default_audio_sample_t
*) jack_port_get_buffer (output_port
, nframes
);
43 jack_default_audio_sample_t
*in
= (jack_default_audio_sample_t
*) jack_port_get_buffer (input_port
, nframes
);
48 } else if (impulse_sent
) {
49 for(i
=0; i
<nframes
&& response_pos
< response_duration
; i
++) {
50 response
[response_pos
++] = in
[i
];
52 if (response_pos
>= response_duration
) {
55 for (i
=0; i
<nframes
; i
++) {
60 for (i
=1; i
<nframes
; i
++) {
70 jack_shutdown (void *arg
)
76 main (int argc
, char *argv
[])
79 jack_client_t
*client
;
81 float fs
; // The sample rate
83 unsigned long peak_sample
;
85 float duration
= 0.0f
;
86 unsigned int c_format
= 0;
87 int longopt_index
= 0;
89 extern int optind
, opterr
;
91 char *optstring
= "d:f:h";
92 struct option long_options
[] = {
93 { "help", 1, 0, 'h' },
94 { "duration", 1, 0, 'd' },
95 { "format", 1, 0, 'f' },
99 while ((c
= getopt_long (argc
, argv
, optstring
, long_options
, &longopt_index
)) != -1) {
102 // end of opts, but don't care
108 duration
= (float)atof(optarg
);
111 if (*optarg
== 'c' || *optarg
== 'C') {
120 if (show_usage
|| duration
<= 0.0f
) {
121 fprintf(stderr
, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
125 /* try to become a client of the JACK server */
127 if ((client
= jack_client_new("impulse_grabber")) == 0) {
128 fprintf (stderr
, "jack server not running?\n");
132 /* tell the JACK server to call `process()' whenever
133 there is work to be done.
136 jack_set_process_callback (client
, process
, 0);
138 /* tell the JACK server to call `jack_shutdown()' if
139 it ever shuts down, either entirely, or if it
140 just decides to stop calling us.
143 jack_on_shutdown (client
, jack_shutdown
, 0);
145 /* display the current sample rate. once the client is activated
146 (see below), you should rely on your own sample rate
147 callback (see above) for this value.
150 fs
= jack_get_sample_rate(client
);
151 response_duration
= (unsigned long) (fs
* duration
);
152 response
= malloc(response_duration
* sizeof(float));
154 "Grabbing %f seconds (%lu samples) of impulse response\n",
155 duration
, response_duration
);
157 /* create two ports */
159 input_port
= jack_port_register (client
, "input", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
160 output_port
= jack_port_register (client
, "output", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
162 /* tell the JACK server that we are ready to roll */
164 if (jack_activate (client
)) {
165 fprintf (stderr
, "cannot activate client");
169 /* connect the ports. Note: you can't do this before
170 the client is activated (this may change in the future).
173 if ((ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
)) == NULL
) {
174 fprintf(stderr
, "Cannot find any physical capture ports");
178 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
179 fprintf (stderr
, "cannot connect input ports\n");
184 if ((ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
)) == NULL
) {
185 fprintf(stderr
, "Cannot find any physical playback ports");
189 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
190 fprintf (stderr
, "cannot connect output ports\n");
195 /* Wait for grab to finish */
196 while (!grab_finished
) {
199 jack_client_close (client
);
204 printf("impulse[%lu] = {", response_duration
);
205 for (i
=0; i
<response_duration
; i
++) {
211 printf("\"%+1.10f\"", response
[i
]);
212 if (i
< response_duration
- 1) {
215 if (fabs(response
[i
]) > peak
) {
216 peak
= fabs(response
[i
]);
222 for (i
=0; i
<response_duration
; i
++) {
223 printf("%1.12f\n", response
[i
]);
224 if (fabs(response
[i
]) > peak
) {
225 peak
= fabs(response
[i
]);
230 fprintf(stderr
, "Peak value was %f at sample %lu\n", peak
, peak_sample
);