Merge branch 'master' into port_register_notification_defer
[jack2.git] / example-clients / impulse_grabber.c
blob0a1fd70607d97e7364ce19f9fff45b25af2cd0f3
1 /*
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.
20 #include <stdio.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <math.h>
26 #include <getopt.h>
28 #include <jack/jack.h>
30 jack_port_t *input_port;
31 jack_port_t *output_port;
33 unsigned int impulse_sent = 0;
34 float *response;
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");
44 exit(0);
47 static int
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);
53 unsigned int i;
55 if (grab_finished) {
56 return 0;
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) {
62 grab_finished = 1;
64 for (i=0; i<nframes; i++) {
65 out[i] = 0.0f;;
67 } else {
68 out[0] = 1.0f;
69 for (i=1; i<nframes; i++) {
70 out[i] = 0.0f;
72 impulse_sent = 1;
75 return 0;
78 static void
79 jack_shutdown (void *arg)
81 exit (1);
84 int
85 main (int argc, char *argv[])
87 const char **ports;
88 float fs; // The sample rate
89 float peak;
90 unsigned long peak_sample;
91 unsigned int i;
92 float duration = 0.0f;
93 unsigned int c_format = 0;
94 int longopt_index = 0;
95 int c;
96 extern int optind, opterr;
97 int show_usage = 0;
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' },
103 { 0, 0, 0, 0 }
106 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
107 switch (c) {
108 case 1:
109 // end of opts, but don't care
110 break;
111 case 'h':
112 show_usage++;
113 break;
114 case 'd':
115 duration = (float)atof(optarg);
116 break;
117 case 'f':
118 if (*optarg == 'c' || *optarg == 'C') {
119 c_format = 1;
121 break;
122 default:
123 show_usage++;
124 break;
127 if (show_usage || duration <= 0.0f) {
128 fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
129 exit(1);
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");
136 return 1;
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));
160 fprintf(stderr,
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");
173 return 1;
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");
182 exit(1);
185 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
186 fprintf (stderr, "cannot connect input ports\n");
189 free (ports);
191 if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
192 fprintf(stderr, "Cannot find any physical playback ports");
193 exit(1);
196 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
197 fprintf (stderr, "cannot connect output ports\n");
200 free (ports);
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) {
210 sleep (1);
212 jack_client_close (client);
214 peak = response[0];
215 peak_sample = 0;
216 if (c_format) {
217 printf("impulse[%lu] = {", response_duration);
218 for (i=0; i<response_duration; i++) {
219 if (i % 4 != 0) {
220 printf(" ");
221 } else {
222 printf("\n\t");
224 printf("\"%+1.10f\"", response[i]);
225 if (i < response_duration - 1) {
226 printf(",");
228 if (fabs(response[i]) > peak) {
229 peak = fabs(response[i]);
230 peak_sample = i;
233 printf("\n};\n");
234 } else {
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]);
239 peak_sample = i;
243 fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
245 exit (0);