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.
28 #include <jack/jack.h>
30 jack_port_t
*input_port
;
31 jack_port_t
*output_port
;
33 unsigned int impulse_sent
= 0;
35 unsigned long response_duration
;
36 unsigned long response_pos
;
37 int grab_finished
= 0;
38 jack_client_t
*client
;
40 static void signal_handler(int sig
)
42 jack_client_close(client
);
43 fprintf(stderr
, "signal received, exiting ...\n");
48 process (jack_nframes_t nframes
, void *arg
)
51 jack_default_audio_sample_t
*out
= (jack_default_audio_sample_t
*) jack_port_get_buffer (output_port
, nframes
);
52 jack_default_audio_sample_t
*in
= (jack_default_audio_sample_t
*) jack_port_get_buffer (input_port
, nframes
);
57 } else if (impulse_sent
) {
58 for(i
=0; i
<nframes
&& response_pos
< response_duration
; i
++) {
59 response
[response_pos
++] = in
[i
];
61 if (response_pos
>= response_duration
) {
64 for (i
=0; i
<nframes
; i
++) {
69 for (i
=1; i
<nframes
; i
++) {
79 jack_shutdown (void *arg
)
81 fprintf(stderr
, "JACK shut down, exiting ...\n");
86 main (int argc
, char *argv
[])
89 float fs
; // The sample rate
91 unsigned long peak_sample
;
93 float duration
= 0.0f
;
94 unsigned int c_format
= 0;
95 int longopt_index
= 0;
97 extern int optind
, opterr
;
99 char *optstring
= "d:f:h";
100 struct option long_options
[] = {
101 { "help", 1, 0, 'h' },
102 { "duration", 1, 0, 'd' },
103 { "format", 1, 0, 'f' },
107 while ((c
= getopt_long (argc
, argv
, optstring
, long_options
, &longopt_index
)) != -1) {
110 // end of opts, but don't care
116 duration
= (float)atof(optarg
);
119 if (*optarg
== 'c' || *optarg
== 'C') {
128 if (show_usage
|| duration
<= 0.0f
) {
129 fprintf(stderr
, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
133 /* try to become a client of the JACK server */
135 if ((client
= jack_client_open("impulse_grabber", JackNullOption
, NULL
)) == 0) {
136 fprintf (stderr
, "JACK server not running?\n");
140 /* tell the JACK server to call `process()' whenever
141 there is work to be done.
144 jack_set_process_callback (client
, process
, 0);
146 /* tell the JACK server to call `jack_shutdown()' if
147 it ever shuts down, either entirely, or if it
148 just decides to stop calling us.
151 jack_on_shutdown (client
, jack_shutdown
, 0);
153 /* display the current sample rate. once the client is activated
154 (see below), you should rely on your own sample rate
155 callback (see above) for this value.
158 fs
= jack_get_sample_rate(client
);
159 response_duration
= (unsigned long) (fs
* duration
);
160 response
= malloc(response_duration
* sizeof(float));
162 "Grabbing %f seconds (%lu samples) of impulse response\n",
163 duration
, response_duration
);
165 /* create two ports */
167 input_port
= jack_port_register (client
, "input", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
168 output_port
= jack_port_register (client
, "output", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
170 /* tell the JACK server that we are ready to roll */
172 if (jack_activate (client
)) {
173 fprintf (stderr
, "cannot activate client");
177 /* connect the ports. Note: you can't do this before
178 the client is activated (this may change in the future).
181 if ((ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
)) == NULL
) {
182 fprintf(stderr
, "Cannot find any physical capture ports");
186 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
187 fprintf (stderr
, "cannot connect input ports\n");
192 if ((ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
)) == NULL
) {
193 fprintf(stderr
, "Cannot find any physical playback ports");
197 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
198 fprintf (stderr
, "cannot connect output ports\n");
203 /* install a signal handler to properly quits jack client */
204 signal(SIGQUIT
, signal_handler
);
205 signal(SIGTERM
, signal_handler
);
206 signal(SIGHUP
, signal_handler
);
207 signal(SIGINT
, signal_handler
);
209 /* Wait for grab to finish */
210 while (!grab_finished
) {
213 jack_client_close (client
);
218 printf("impulse[%lu] = {", response_duration
);
219 for (i
=0; i
<response_duration
; i
++) {
225 printf("\"%+1.10f\"", response
[i
]);
226 if (i
< response_duration
- 1) {
229 if (fabs(response
[i
]) > peak
) {
230 peak
= fabs(response
[i
]);
236 for (i
=0; i
<response_duration
; i
++) {
237 printf("%1.12f\n", response
[i
]);
238 if (fabs(response
[i
]) > peak
) {
239 peak
= fabs(response
[i
]);
244 fprintf(stderr
, "Peak value was %f at sample %lu\n", peak
, peak_sample
);