** Warning: Cannot open xref database.
1 /*
2 * Copyright (C) 2001 Steve Harris
3 *
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.
8 *
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.
13 *
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.
17 *
18 * $Id: impulse_grabber.c,v 1.4 2003/08/27 16:26:23 joq Exp $
19 */
20
21 #include <stdio.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <getopt.h>
27
28 #include <jack/jack.h>
29
30 jack_port_t *input_port;
31 jack_port_t *output_port;
32
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
39 int
40 process (jack_nframes_t nframes, void *arg)
41
42 {
43 jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
44 jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
45 unsigned int i;
46
47 if (grab_finished) {
48 return 0;
49 } else if (impulse_sent) {
50 for(i=0; i<nframes && response_pos < response_duration; i++) {
51 response[response_pos++] = in[i];
52 }
53 if (response_pos >= response_duration) {
54 grab_finished = 1;
55 }
56 for (i=0; i<nframes; i++) {
57 out[i] = 0.0f;;
58 }
59 } else {
60 out[0] = 1.0f;
61 for (i=1; i<nframes; i++) {
62 out[i] = 0.0f;
63 }
64 impulse_sent = 1;
65 }
66
67 return 0;
68 }
69
70 void
71 jack_shutdown (void *arg)
72 {
73 exit (1);
74 }
75
76 int
77 main (int argc, char *argv[])
78
79 {
80 jack_client_t *client;
81 const char **ports;
82 float fs; // The sample rate
83 float peak;
84 unsigned long peak_sample;
85 unsigned int i;
86 float duration = 0.0f;
87 unsigned int c_format = 0;
88 int longopt_index = 0;
89 int c;
90 extern int optind, opterr;
91 int show_usage = 0;
92 char *optstring = "d:f";
93 struct option long_options[] = {
94 { "help", 1, 0, 'h' },
95 { "duration", 1, 0, 'd' },
96 { "format", 1, 0, 'f' },
97 { 0, 0, 0, 0 }
98 };
99
100 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
101 switch (c) {
102 case 1:
103 // end of opts, but don't care
104 break;
105 case 'h':
106 show_usage++;
107 break;
108 case 'd':
109 duration = (float)atof(optarg);
110 break;
111 case 'f':
112 if (*optarg == 'c' || *optarg == 'C') {
113 c_format = 1;
114 }
115 break;
116 default:
117 show_usage++;
118 break;
119 }
120 }
121 if (show_usage || duration <= 0.0f) {
122 fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
123 exit(1);
124 }
125
126 /* try to become a client of the JACK server */
127
128 if ((client = jack_client_new("impulse_grabber")) == 0) {
129 fprintf (stderr, "jack server not running?\n");
130 return 1;
131 }
132
133 /* tell the JACK server to call `process()' whenever
134 there is work to be done.
135 */
136
137 jack_set_process_callback (client, process, 0);
138
139 /* tell the JACK server to call `jack_shutdown()' if
140 it ever shuts down, either entirely, or if it
141 just decides to stop calling us.
142 */
143
144 jack_on_shutdown (client, jack_shutdown, 0);
145
146 /* display the current sample rate. once the client is activated
147 (see below), you should rely on your own sample rate
148 callback (see above) for this value.
149 */
150
151 fs = jack_get_sample_rate(client);
152 response_duration = (unsigned long) (fs * duration);
153 response = malloc(response_duration * sizeof(float));
154 fprintf(stderr,
155 "Grabbing %f seconds (%lu samples) of impulse response\n",
156 duration, response_duration);
157
158 /* create two ports */
159
160 input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
161 output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
162
163 /* tell the JACK server that we are ready to roll */
164
165 if (jack_activate (client)) {
166 fprintf (stderr, "cannot activate client");
167 return 1;
168 }
169
170 /* connect the ports. Note: you can't do this before
171 the client is activated (this may change in the future).
172 */
173
174 if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
175 fprintf(stderr, "Cannot find any physical capture ports");
176 exit(1);
177 }
178
179 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
180 fprintf (stderr, "cannot connect input ports\n");
181 }
182
183 free (ports);
184
185 if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
186 fprintf(stderr, "Cannot find any physical playback ports");
187 exit(1);
188 }
189
190 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
191 fprintf (stderr, "cannot connect output ports\n");
192 }
193
194 free (ports);
195
196 /* Wait for grab to finish */
197 while (!grab_finished) {
198 sleep (1);
199 }
200 jack_client_close (client);
201
202 peak = response[0];
203 peak_sample = 0;
204 if (c_format) {
205 printf("impulse[%lu] = {", response_duration);
206 for (i=0; i<response_duration; i++) {
207 if (i % 4 != 0) {
208 printf(" ");
209 } else {
210 printf("\n\t");
211 }
212 printf("\"%+1.10f\"", response[i]);
213 if (i < response_duration - 1) {
214 printf(",");
215 }
216 if (fabs(response[i]) > peak) {
217 peak = fabs(response[i]);
218 peak_sample = i;
219 }
220 }
221 printf("\n};\n");
222 } else {
223 for (i=0; i<response_duration; i++) {
224 printf("%1.12f\n", response[i]);
225 if (fabs(response[i]) > peak) {
226 peak = fabs(response[i]);
227 peak_sample = i;
228 }
229 }
230 }
231 fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
232
233 exit (0);
234 }
235
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.