Imported Upstream version 3.0
[debian/gnuradio] / gr-audio-jack / src / audio_jack_source.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <audio_jack_source.h>
28 #include <gr_io_signature.h>
29 #include <gr_prefs.h>
30 #include <stdio.h>
31 #include <iostream>
32 #include <stdexcept>
33 #include <gri_jack.h>
34
35 #ifndef NO_PTHREAD
36 #include <pthread.h>
37 #endif
38
39 typedef jack_default_audio_sample_t sample_t;
40
41
42 // Number of jack buffers in the ringbuffer
43 // TODO: make it to match at least the quantity of items passed to work()
44 static const unsigned int N_BUFFERS = 16;
45
46 static std::string 
47 default_device_name ()
48 {
49   return gr_prefs::singleton()->get_string("audio_jack", "default_input_device", "gr_source");
50 }
51
52
53 int
54 jack_source_process (jack_nframes_t nframes, void *arg)
55 {
56   audio_jack_source *self = (audio_jack_source *)arg;
57   unsigned int write_size = nframes*sizeof(sample_t);
58
59   if (jack_ringbuffer_write_space (self->d_ringbuffer) < write_size) {
60     self->d_noverruns++;
61     // FIXME: move this fputs out, we shouldn't use blocking calls in process()
62     fputs ("jO", stderr);
63     return 0;
64   }
65
66   char *buffer = (char *) jack_port_get_buffer (self->d_jack_input_port, nframes);
67
68   jack_ringbuffer_write (self->d_ringbuffer, buffer, write_size);
69
70 #ifndef NO_PTHREAD
71   // Tell the source thread there is data in the ringbuffer.
72   // If it is already running, the lock will not be available.
73   // We can't wait here in the process() thread, but we don't
74   // need to signal in that case, because the source thread will 
75   // check for data availability.
76
77   if (pthread_mutex_trylock (&self->d_jack_process_lock) == 0) {
78     pthread_cond_signal (&self->d_ringbuffer_ready);
79     pthread_mutex_unlock (&self->d_jack_process_lock);
80   }
81 #endif
82
83   return 0;
84 }
85
86 // ----------------------------------------------------------------
87
88 audio_jack_source_sptr
89 audio_jack_make_source (int sampling_rate, const std::string dev, bool ok_to_block)
90 {
91   return audio_jack_source_sptr (new audio_jack_source (sampling_rate, dev, ok_to_block));
92 }
93
94 audio_jack_source::audio_jack_source (int sampling_rate,
95                                       const std::string device_name,
96                                       bool ok_to_block)
97   : gr_sync_block ("audio_jack_source",
98                    gr_make_io_signature (0, 0, 0),
99                    gr_make_io_signature (0, 0, 0)),
100     d_sampling_rate (sampling_rate),
101     d_device_name (device_name.empty() ? default_device_name() : device_name),
102     d_ok_to_block(ok_to_block),
103     d_jack_client (0),
104     d_ringbuffer (0),
105     d_noverruns (0)
106 {
107 #ifndef NO_PTHREAD
108     pthread_cond_init(&d_ringbuffer_ready, NULL);;
109     pthread_mutex_init(&d_jack_process_lock, NULL);
110 #endif
111
112   // try to become a client of the JACK server
113   if ((d_jack_client = jack_client_new (d_device_name.c_str ())) == 0) {
114     fprintf (stderr, "audio_jack_source[%s]: jack server not running?\n",
115              d_device_name.c_str());
116     throw std::runtime_error ("audio_jack_source");
117   }
118
119   // tell the JACK server to call `jack_source_process()' whenever
120   // there is work to be done.
121   jack_set_process_callback (d_jack_client, &jack_source_process, (void*)this);
122
123   // tell the JACK server to call `jack_shutdown()' if
124   // it ever shuts down, either entirely, or if it
125   // just decides to stop calling us.
126
127   //jack_on_shutdown (d_jack_client, &jack_shutdown, (void*)this);
128  
129   d_jack_input_port = jack_port_register (d_jack_client, "in", 
130                                           JACK_DEFAULT_AUDIO_TYPE,
131                                           JackPortIsInput, 0);
132
133
134   d_jack_buffer_size = jack_get_buffer_size (d_jack_client);
135
136   set_output_multiple (d_jack_buffer_size);
137
138   d_ringbuffer = jack_ringbuffer_create (N_BUFFERS*d_jack_buffer_size*sizeof(sample_t));
139   if (d_ringbuffer == NULL)
140     bail ("jack_ringbuffer_create failed", 0);
141
142   assert(sizeof(float)==sizeof(sample_t));
143   set_output_signature (gr_make_io_signature (1, 1, sizeof (sample_t)));
144
145
146   jack_nframes_t sample_rate = jack_get_sample_rate (d_jack_client);
147
148   if ((jack_nframes_t)sampling_rate != sample_rate){
149     fprintf (stderr, "audio_jack_source[%s]: unable to support sampling rate %d\n",
150              d_device_name.c_str (), sampling_rate);
151     fprintf (stderr, "  card requested %d instead.\n", sample_rate);
152   }
153 }
154
155
156 bool
157 audio_jack_source::check_topology (int ninputs, int noutputs)
158 {
159   // tell the JACK server that we are ready to roll 
160   if (jack_activate (d_jack_client))
161     throw std::runtime_error ("audio_jack_source");
162
163   return true;
164 }
165
166 audio_jack_source::~audio_jack_source ()
167 {
168   jack_client_close (d_jack_client);
169   jack_ringbuffer_free (d_ringbuffer);
170 }
171
172 int
173 audio_jack_source::work (int noutput_items,
174                          gr_vector_const_void_star &input_items,
175                          gr_vector_void_star &output_items)
176 {
177   // read_size and work_size are in bytes
178   unsigned int read_size;
179
180   // Minimize latency
181   noutput_items = std::min (noutput_items, (int)d_jack_buffer_size);
182
183   int work_size = noutput_items*sizeof(sample_t);
184
185   while (work_size > 0) {
186     unsigned int read_space;    // bytes
187
188 #ifdef NO_PTHREAD
189     while ((read_space=jack_ringbuffer_read_space (d_ringbuffer)) < 
190            d_jack_buffer_size*sizeof(sample_t)) {
191       usleep(1000000*((d_jack_buffer_size-read_space/sizeof(sample_t))/d_sampling_rate));
192     }
193 #else
194     // JACK actually requires POSIX
195
196     pthread_mutex_lock (&d_jack_process_lock);
197     while ((read_space=jack_ringbuffer_read_space (d_ringbuffer)) < 
198                     d_jack_buffer_size*sizeof(sample_t)) {
199
200       // wait until jack_source_process() signals more data
201       pthread_cond_wait (&d_ringbuffer_ready, &d_jack_process_lock);
202     }
203     pthread_mutex_unlock (&d_jack_process_lock);
204 #endif
205
206     read_space -= read_space%(d_jack_buffer_size*sizeof(sample_t));
207     read_size = std::min(read_space, (unsigned int)work_size);
208
209     if (jack_ringbuffer_read (d_ringbuffer, (char *) output_items[0],
210                               read_size) < read_size) {
211       bail ("jack_ringbuffer_read failed", 0);
212     }
213     work_size -= read_size;
214   }
215
216   return noutput_items;
217 }
218
219 void
220 audio_jack_source::output_error_msg (const char *msg, int err)
221 {
222   fprintf (stderr, "audio_jack_source[%s]: %s: %d\n",
223            d_device_name.c_str (), msg,  err);
224 }
225
226 void
227 audio_jack_source::bail (const char *msg, int err) throw (std::runtime_error)
228 {
229   output_error_msg (msg, err);
230   throw std::runtime_error ("audio_jack_source");
231 }