Correct JackPortAudioDriver.
[jack2.git] / example-clients / impulse_grabber.c
blob7084c2e9eb28280bb9dda9eae7adb5e208e0e18a
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 fprintf(stderr, "JACK shut down, exiting ...\n");
82 exit (1);
85 int
86 main (int argc, char *argv[])
88 const char **ports;
89 float fs; // The sample rate
90 float peak;
91 unsigned long peak_sample;
92 unsigned int i;
93 float duration = 0.0f;
94 unsigned int c_format = 0;
95 int longopt_index = 0;
96 int c;
97 extern int optind, opterr;
98 int show_usage = 0;
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' },
104 { 0, 0, 0, 0 }
107 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
108 switch (c) {
109 case 1:
110 // end of opts, but don't care
111 break;
112 case 'h':
113 show_usage++;
114 break;
115 case 'd':
116 duration = (float)atof(optarg);
117 break;
118 case 'f':
119 if (*optarg == 'c' || *optarg == 'C') {
120 c_format = 1;
122 break;
123 default:
124 show_usage++;
125 break;
128 if (show_usage || duration <= 0.0f) {
129 fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
130 exit(1);
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");
137 return 1;
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));
161 fprintf(stderr,
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");
174 return 1;
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");
183 exit(1);
186 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
187 fprintf (stderr, "cannot connect input ports\n");
190 free (ports);
192 if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
193 fprintf(stderr, "Cannot find any physical playback ports");
194 exit(1);
197 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
198 fprintf (stderr, "cannot connect output ports\n");
201 free (ports);
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) {
211 sleep (1);
213 jack_client_close (client);
215 peak = response[0];
216 peak_sample = 0;
217 if (c_format) {
218 printf("impulse[%lu] = {", response_duration);
219 for (i=0; i<response_duration; i++) {
220 if (i % 4 != 0) {
221 printf(" ");
222 } else {
223 printf("\n\t");
225 printf("\"%+1.10f\"", response[i]);
226 if (i < response_duration - 1) {
227 printf(",");
229 if (fabs(response[i]) > peak) {
230 peak = fabs(response[i]);
231 peak_sample = i;
234 printf("\n};\n");
235 } else {
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]);
240 peak_sample = i;
244 fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
246 exit (0);