compile and configuration fixes from OSX but useful everywhere
[jack.git] / tools / capture_client.c
blob481346363f6986e598ea1180d7e34301189b62b1
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2003 Jack O'Quin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * 2002/08/23 - modify for libsndfile 1.0.0 <andy@alsaplayer.org>
20 * 2003/05/26 - use ringbuffers - joq
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sndfile.h>
30 #include <pthread.h>
31 #include <getopt.h>
32 #include <jack/jack.h>
33 #include <jack/ringbuffer.h>
35 typedef struct _thread_info {
36 pthread_t thread_id;
37 SNDFILE *sf;
38 jack_nframes_t duration;
39 jack_nframes_t rb_size;
40 jack_client_t *client;
41 unsigned int channels;
42 int bitdepth;
43 char *path;
44 volatile int can_capture;
45 volatile int can_process;
46 volatile int status;
47 } jack_thread_info_t;
49 /* JACK data */
50 unsigned int nports;
51 jack_port_t **ports;
52 jack_default_audio_sample_t **in;
53 jack_nframes_t nframes;
54 const size_t sample_size = sizeof(jack_default_audio_sample_t);
56 /* Synchronization between process thread and disk thread. */
57 #define DEFAULT_RB_SIZE 16384 /* ringbuffer size in frames */
58 jack_ringbuffer_t *rb;
59 pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER;
60 pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
61 long overruns = 0;
64 void *
65 disk_thread (void *arg)
67 jack_thread_info_t *info = (jack_thread_info_t *) arg;
68 static jack_nframes_t total_captured = 0;
69 jack_nframes_t samples_per_frame = info->channels;
70 size_t bytes_per_frame = samples_per_frame * sample_size;
71 void *framebuf = malloc (bytes_per_frame);
73 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
74 pthread_mutex_lock (&disk_thread_lock);
76 info->status = 0;
78 while (1) {
80 /* Write the data one frame at a time. This is
81 * inefficient, but makes things simpler. */
82 while (info->can_capture &&
83 (jack_ringbuffer_read_space (rb) >= bytes_per_frame)) {
85 jack_ringbuffer_read (rb, framebuf, bytes_per_frame);
87 if (sf_writef_float (info->sf, framebuf, 1) != 1) {
88 char errstr[256];
89 sf_error_str (0, errstr, sizeof (errstr) - 1);
90 fprintf (stderr,
91 "cannot write sndfile (%s)\n",
92 errstr);
93 info->status = EIO; /* write failed */
94 goto done;
97 if (++total_captured >= info->duration) {
98 printf ("disk thread finished\n");
99 goto done;
103 /* wait until process() signals more data */
104 pthread_cond_wait (&data_ready, &disk_thread_lock);
107 done:
108 pthread_mutex_unlock (&disk_thread_lock);
109 free (framebuf);
110 return 0;
114 process (jack_nframes_t nframes, void *arg)
116 int chn;
117 size_t i;
118 jack_thread_info_t *info = (jack_thread_info_t *) arg;
120 /* Do nothing until we're ready to begin. */
121 if ((!info->can_process) || (!info->can_capture))
122 return 0;
124 for (chn = 0; chn < nports; chn++)
125 in[chn] = jack_port_get_buffer (ports[chn], nframes);
127 /* Sndfile requires interleaved data. It is simpler here to
128 * just queue interleaved samples to a single ringbuffer. */
129 for (i = 0; i < nframes; i++) {
130 for (chn = 0; chn < nports; chn++) {
131 if (jack_ringbuffer_write (rb, (void *) (in[chn]+i),
132 sample_size)
133 < sample_size)
134 overruns++;
138 /* Tell the disk thread there is work to do. If it is already
139 * running, the lock will not be available. We can't wait
140 * here in the process() thread, but we don't need to signal
141 * in that case, because the disk thread will read all the
142 * data queued before waiting again. */
143 if (pthread_mutex_trylock (&disk_thread_lock) == 0) {
144 pthread_cond_signal (&data_ready);
145 pthread_mutex_unlock (&disk_thread_lock);
148 return 0;
151 void
152 jack_shutdown (void *arg)
154 fprintf (stderr, "JACK shutdown\n");
155 // exit (0);
156 abort();
159 void
160 setup_disk_thread (jack_thread_info_t *info)
162 SF_INFO sf_info;
163 int short_mask;
165 sf_info.samplerate = jack_get_sample_rate (info->client);
166 sf_info.channels = info->channels;
168 switch (info->bitdepth) {
169 case 8: short_mask = SF_FORMAT_PCM_U8;
170 break;
171 case 16: short_mask = SF_FORMAT_PCM_16;
172 break;
173 case 24: short_mask = SF_FORMAT_PCM_24;
174 break;
175 case 32: short_mask = SF_FORMAT_PCM_32;
176 break;
177 default: short_mask = SF_FORMAT_PCM_16;
178 break;
180 sf_info.format = SF_FORMAT_WAV|short_mask;
182 if ((info->sf = sf_open (info->path, SFM_WRITE, &sf_info)) == NULL) {
183 char errstr[256];
184 sf_error_str (0, errstr, sizeof (errstr) - 1);
185 fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
186 jack_client_close (info->client);
187 exit (1);
190 info->duration *= sf_info.samplerate;
191 info->can_capture = 0;
193 pthread_create (&info->thread_id, NULL, disk_thread, info);
196 void
197 run_disk_thread (jack_thread_info_t *info)
199 info->can_capture = 1;
200 pthread_join (info->thread_id, NULL);
201 sf_close (info->sf);
202 if (overruns > 0) {
203 fprintf (stderr,
204 "jackrec failed with %ld overruns.\n", overruns);
205 fprintf (stderr, " try a bigger buffer than -B %"
206 PRIu32 ".\n", info->rb_size);
207 info->status = EPIPE;
211 void
212 setup_ports (int sources, char *source_names[], jack_thread_info_t *info)
214 unsigned int i;
215 size_t in_size;
217 /* Allocate data structures that depend on the number of ports. */
218 nports = sources;
219 ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
220 in_size = nports * sizeof (jack_default_audio_sample_t *);
221 in = (jack_default_audio_sample_t **) malloc (in_size);
222 rb = jack_ringbuffer_create (nports * sample_size * info->rb_size);
224 /* When JACK is running realtime, jack_activate() will have
225 * called mlockall() to lock our pages into memory. But, we
226 * still need to touch any newly allocated pages before
227 * process() starts using them. Otherwise, a page fault could
228 * create a delay that would force JACK to shut us down. */
229 memset(in, 0, in_size);
230 memset(rb->buf, 0, rb->size);
232 for (i = 0; i < nports; i++) {
233 char name[64];
235 sprintf (name, "input%d", i+1);
237 if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
238 fprintf (stderr, "cannot register input port \"%s\"!\n", name);
239 jack_client_close (info->client);
240 exit (1);
244 for (i = 0; i < nports; i++) {
245 if (jack_connect (info->client, source_names[i], jack_port_name (ports[i]))) {
246 fprintf (stderr, "cannot connect input port %s to %s\n", jack_port_name (ports[i]), source_names[i]);
247 jack_client_close (info->client);
248 exit (1);
252 info->can_process = 1; /* process() can start, now */
256 main (int argc, char *argv[])
259 jack_client_t *client;
260 jack_thread_info_t thread_info;
261 int c;
262 int longopt_index = 0;
263 extern int optind, opterr;
264 int show_usage = 0;
265 char *optstring = "d:f:b:B:h";
266 struct option long_options[] = {
267 { "help", 0, 0, 'h' },
268 { "duration", 1, 0, 'd' },
269 { "file", 1, 0, 'f' },
270 { "bitdepth", 1, 0, 'b' },
271 { "bufsize", 1, 0, 'B' },
272 { 0, 0, 0, 0 }
275 memset (&thread_info, 0, sizeof (thread_info));
276 thread_info.rb_size = DEFAULT_RB_SIZE;
277 opterr = 0;
279 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
280 switch (c) {
281 case 1:
282 /* getopt signals end of '-' options */
283 break;
285 case 'h':
286 show_usage++;
287 break;
288 case 'd':
289 thread_info.duration = atoi (optarg);
290 break;
291 case 'f':
292 thread_info.path = optarg;
293 break;
294 case 'b':
295 thread_info.bitdepth = atoi (optarg);
296 break;
297 case 'B':
298 thread_info.rb_size = atoi (optarg);
299 break;
300 default:
301 fprintf (stderr, "error\n");
302 show_usage++;
303 break;
307 if (show_usage || thread_info.path == NULL || optind == argc) {
308 fprintf (stderr, "usage: jackrec -f filename [ -d second ] [ -b bitdepth ] [ -B bufsize ] port1 [ port2 ... ]\n");
309 exit (1);
312 if ((client = jack_client_new ("jackrec")) == 0) {
313 fprintf (stderr, "jack server not running?\n");
314 exit (1);
317 thread_info.client = client;
318 thread_info.channels = argc - optind;
319 thread_info.can_process = 0;
321 setup_disk_thread (&thread_info);
323 jack_set_process_callback (client, process, &thread_info);
324 jack_on_shutdown (client, jack_shutdown, &thread_info);
326 if (jack_activate (client)) {
327 fprintf (stderr, "cannot activate client");
330 setup_ports (argc - optind, &argv[optind], &thread_info);
332 run_disk_thread (&thread_info);
334 jack_client_close (client);
336 jack_ringbuffer_free (rb);
338 exit (0);