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
)
85 main (int argc
, char *argv
[])
88 float fs
; // The sample rate
90 unsigned long peak_sample
;
92 float duration
= 0.0f
;
93 unsigned int c_format
= 0;
94 int longopt_index
= 0;
96 extern int optind
, opterr
;
98 char *optstring
= "d:f:h";
99 struct option long_options
[] = {
100 { "help", 1, 0, 'h' },
101 { "duration", 1, 0, 'd' },
102 { "format", 1, 0, 'f' },
106 while ((c
= getopt_long (argc
, argv
, optstring
, long_options
, &longopt_index
)) != -1) {
109 // end of opts, but don't care
115 duration
= (float)atof(optarg
);
118 if (*optarg
== 'c' || *optarg
== 'C') {
127 if (show_usage
|| duration
<= 0.0f
) {
128 fprintf(stderr
, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
132 /* try to become a client of the JACK server */
134 if ((client
= jack_client_open("impulse_grabber", JackNullOption
, NULL
)) == 0) {
135 fprintf (stderr
, "jack server not running?\n");
139 /* tell the JACK server to call `process()' whenever
140 there is work to be done.
143 jack_set_process_callback (client
, process
, 0);
145 /* tell the JACK server to call `jack_shutdown()' if
146 it ever shuts down, either entirely, or if it
147 just decides to stop calling us.
150 jack_on_shutdown (client
, jack_shutdown
, 0);
152 /* display the current sample rate. once the client is activated
153 (see below), you should rely on your own sample rate
154 callback (see above) for this value.
157 fs
= jack_get_sample_rate(client
);
158 response_duration
= (unsigned long) (fs
* duration
);
159 response
= malloc(response_duration
* sizeof(float));
161 "Grabbing %f seconds (%lu samples) of impulse response\n",
162 duration
, response_duration
);
164 /* create two ports */
166 input_port
= jack_port_register (client
, "input", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
167 output_port
= jack_port_register (client
, "output", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
169 /* tell the JACK server that we are ready to roll */
171 if (jack_activate (client
)) {
172 fprintf (stderr
, "cannot activate client");
176 /* connect the ports. Note: you can't do this before
177 the client is activated (this may change in the future).
180 if ((ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
)) == NULL
) {
181 fprintf(stderr
, "Cannot find any physical capture ports");
185 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
186 fprintf (stderr
, "cannot connect input ports\n");
191 if ((ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
)) == NULL
) {
192 fprintf(stderr
, "Cannot find any physical playback ports");
196 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
197 fprintf (stderr
, "cannot connect output ports\n");
202 /* install a signal handler to properly quits jack client */
203 signal(SIGQUIT
, signal_handler
);
204 signal(SIGTERM
, signal_handler
);
205 signal(SIGHUP
, signal_handler
);
206 signal(SIGINT
, signal_handler
);
208 /* Wait for grab to finish */
209 while (!grab_finished
) {
212 jack_client_close (client
);
217 printf("impulse[%lu] = {", response_duration
);
218 for (i
=0; i
<response_duration
; i
++) {
224 printf("\"%+1.10f\"", response
[i
]);
225 if (i
< response_duration
- 1) {
228 if (fabs(response
[i
]) > peak
) {
229 peak
= fabs(response
[i
]);
235 for (i
=0; i
<response_duration
; i
++) {
236 printf("%1.12f\n", response
[i
]);
237 if (fabs(response
[i
]) > peak
) {
238 peak
= fabs(response
[i
]);
243 fprintf(stderr
, "Peak value was %f at sample %lu\n", peak
, peak_sample
);