cleanup...
[jack.git] / example-clients / impulse_grabber.c
blobe4b60f80831ad75ce2807c7f4280c5e6ee68355e
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 <math.h>
25 #include <getopt.h>
27 #include <jack/jack.h>
29 jack_port_t *input_port;
30 jack_port_t *output_port;
32 unsigned int impulse_sent = 0;
33 float *response;
34 unsigned long response_duration;
35 unsigned long response_pos;
36 int grab_finished = 0;
38 int
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);
44 unsigned int i;
46 if (grab_finished) {
47 return 0;
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) {
53 grab_finished = 1;
55 for (i=0; i<nframes; i++) {
56 out[i] = 0.0f;;
58 } else {
59 out[0] = 1.0f;
60 for (i=1; i<nframes; i++) {
61 out[i] = 0.0f;
63 impulse_sent = 1;
66 return 0;
69 void
70 jack_shutdown (void *arg)
72 exit (1);
75 int
76 main (int argc, char *argv[])
79 jack_client_t *client;
80 const char **ports;
81 float fs; // The sample rate
82 float peak;
83 unsigned long peak_sample;
84 unsigned int i;
85 float duration = 0.0f;
86 unsigned int c_format = 0;
87 int longopt_index = 0;
88 int c;
89 extern int optind, opterr;
90 int show_usage = 0;
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' },
96 { 0, 0, 0, 0 }
99 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
100 switch (c) {
101 case 1:
102 // end of opts, but don't care
103 break;
104 case 'h':
105 show_usage++;
106 break;
107 case 'd':
108 duration = (float)atof(optarg);
109 break;
110 case 'f':
111 if (*optarg == 'c' || *optarg == 'C') {
112 c_format = 1;
114 break;
115 default:
116 show_usage++;
117 break;
120 if (show_usage || duration <= 0.0f) {
121 fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
122 exit(1);
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");
129 return 1;
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));
153 fprintf(stderr,
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");
166 return 1;
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");
175 exit(1);
178 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
179 fprintf (stderr, "cannot connect input ports\n");
182 free (ports);
184 if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
185 fprintf(stderr, "Cannot find any physical playback ports");
186 exit(1);
189 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
190 fprintf (stderr, "cannot connect output ports\n");
193 free (ports);
195 /* Wait for grab to finish */
196 while (!grab_finished) {
197 sleep (1);
199 jack_client_close (client);
201 peak = response[0];
202 peak_sample = 0;
203 if (c_format) {
204 printf("impulse[%lu] = {", response_duration);
205 for (i=0; i<response_duration; i++) {
206 if (i % 4 != 0) {
207 printf(" ");
208 } else {
209 printf("\n\t");
211 printf("\"%+1.10f\"", response[i]);
212 if (i < response_duration - 1) {
213 printf(",");
215 if (fabs(response[i]) > peak) {
216 peak = fabs(response[i]);
217 peak_sample = i;
220 printf("\n};\n");
221 } else {
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]);
226 peak_sample = i;
230 fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
232 exit (0);