~ubuntu-branches/ubuntu/trusty/jack-audio-connection-kit/trusty

« back to all changes in this revision

Viewing changes to example-clients/impulse_grabber.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-12-06 11:05:15 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081206110515-xa9v9pajr9jqvfvg
Tags: 0.115.6-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - Redirect stderr in bash completion (Debian #504488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
19
 
 
20
#include <stdio.h>
 
21
#include <errno.h>
 
22
#include <unistd.h>
 
23
#include <stdlib.h>
 
24
#include <math.h>
 
25
#include <getopt.h>
 
26
 
 
27
#include <jack/jack.h>
 
28
 
 
29
jack_port_t *input_port;
 
30
jack_port_t *output_port;
 
31
 
 
32
unsigned int impulse_sent = 0;
 
33
float *response;
 
34
unsigned long response_duration;
 
35
unsigned long response_pos;
 
36
int grab_finished = 0;
 
37
 
 
38
int
 
39
process (jack_nframes_t nframes, void *arg)
 
40
 
 
41
{
 
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;
 
45
 
 
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];
 
51
                }
 
52
                if (response_pos >=  response_duration) {
 
53
                        grab_finished = 1;
 
54
                }       
 
55
                for (i=0; i<nframes; i++) {
 
56
                        out[i] = 0.0f;;
 
57
                }
 
58
        } else {
 
59
                out[0] = 1.0f;
 
60
                for (i=1; i<nframes; i++) {
 
61
                        out[i] = 0.0f;
 
62
                }
 
63
                impulse_sent = 1;
 
64
        }
 
65
 
 
66
        return 0;      
 
67
}
 
68
 
 
69
void
 
70
jack_shutdown (void *arg)
 
71
{
 
72
        exit (1);
 
73
}
 
74
 
 
75
int
 
76
main (int argc, char *argv[])
 
77
 
 
78
{
 
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";
 
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 }
 
97
        };
 
98
 
 
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;
 
113
                        }
 
114
                        break;
 
115
                default:
 
116
                        show_usage++;
 
117
                        break;
 
118
                }
 
119
        }
 
120
        if (show_usage || duration <= 0.0f) {
 
121
                fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
 
122
                exit(1);
 
123
        }
 
124
 
 
125
        /* try to become a client of the JACK server */
 
126
 
 
127
        if ((client = jack_client_new("impulse_grabber")) == 0) {
 
128
                fprintf (stderr, "jack server not running?\n");
 
129
                return 1;
 
130
        }
 
131
 
 
132
        /* tell the JACK server to call `process()' whenever
 
133
           there is work to be done.
 
134
        */
 
135
 
 
136
        jack_set_process_callback (client, process, 0);
 
137
 
 
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.
 
141
        */
 
142
 
 
143
        jack_on_shutdown (client, jack_shutdown, 0);
 
144
 
 
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.
 
148
        */
 
149
 
 
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);
 
156
 
 
157
        /* create two ports */
 
158
 
 
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);
 
161
 
 
162
        /* tell the JACK server that we are ready to roll */
 
163
 
 
164
        if (jack_activate (client)) {
 
165
                fprintf (stderr, "cannot activate client");
 
166
                return 1;
 
167
        }
 
168
 
 
169
        /* connect the ports. Note: you can't do this before
 
170
           the client is activated (this may change in the future).
 
171
        */
 
172
 
 
173
        if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
 
174
                fprintf(stderr, "Cannot find any physical capture ports");
 
175
                exit(1);
 
176
        }
 
177
 
 
178
        if (jack_connect (client, ports[0], jack_port_name (input_port))) {
 
179
                fprintf (stderr, "cannot connect input ports\n");
 
180
        }
 
181
 
 
182
        free (ports);
 
183
        
 
184
        if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
 
185
                fprintf(stderr, "Cannot find any physical playback ports");
 
186
                exit(1);
 
187
        }
 
188
 
 
189
        if (jack_connect (client, jack_port_name (output_port), ports[0])) {
 
190
                fprintf (stderr, "cannot connect output ports\n");
 
191
        }
 
192
 
 
193
        free (ports);
 
194
 
 
195
        /* Wait for grab to finish */
 
196
        while (!grab_finished) {
 
197
                sleep (1);
 
198
        }
 
199
        jack_client_close (client);
 
200
 
 
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");
 
210
                        }
 
211
                        printf("\"%+1.10f\"", response[i]);
 
212
                        if (i < response_duration - 1) {
 
213
                                printf(",");
 
214
                        }
 
215
                        if (fabs(response[i]) > peak) {
 
216
                                peak = fabs(response[i]);
 
217
                                peak_sample = i;
 
218
                        }
 
219
                }
 
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;
 
227
                        }
 
228
                }
 
229
        }
 
230
        fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
 
231
 
 
232
        exit (0);
 
233
}