Fix scvim regsitry file for updated filename (thanks Carlo Capocasa)
[supercollider.git] / server / supernova / audio_backend / jack_backend.hpp
blobd6a3f0c3a5c543764afe4e49bb34e40b5db92dbb
1 // native jack backend
2 // Copyright (C) 2009, 2010 Tim Blechmann
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
19 #ifndef AUDIO_BACKEND_JACK_BACKEND_HPP
20 #define AUDIO_BACKEND_JACK_BACKEND_HPP
22 #include <iostream>
23 #include <string>
24 #include <vector>
26 #include <boost/lexical_cast.hpp>
28 #include <jack/jack.h>
29 #include <jack/thread.h>
31 #include "utilities/branch_hints.hpp"
33 #include "audio_backend_common.hpp"
34 #include "cpu_time_info.hpp"
36 namespace nova
39 /** jack backend
41 * the jack callback thread is pinned to the first cpu of the system
43 * \todo later it may be interesting to directly map the io busses to the jack port regions
44 * \todo rethink the use of output port lock
46 template <typename engine_functor,
47 typename sample_type = float,
48 bool blocking = false
50 class jack_backend:
51 public detail::audio_delivery_helper<sample_type, jack_default_audio_sample_t, blocking, false>,
52 public detail::audio_settings_basic,
53 protected engine_functor
55 typedef detail::audio_delivery_helper<sample_type, jack_default_audio_sample_t, blocking, false> super;
57 public:
58 jack_backend(void):
59 client(NULL), time_is_synced(false)
62 ~jack_backend(void)
64 if (audio_is_active())
65 deactivate_audio();
67 close_client();
70 uint32_t get_audio_blocksize(void) const
72 return blocksize_;
75 public:
76 void open_client(std::string const & server_name, std::string const & name, uint32_t input_port_count,
77 uint32_t output_port_count, uint32_t blocksize)
79 blocksize_ = blocksize;
81 /* open client */
82 client = server_name.empty() ? jack_client_open(name.c_str(), JackNoStartServer, &status)
83 : jack_client_open(name.c_str(), jack_options_t(JackNoStartServer | JackServerName),
84 &status, server_name.c_str());
85 boost::atomic_thread_fence(boost::memory_order_release); // ensure visibility on other threads
87 if (status & JackServerFailed)
88 throw std::runtime_error("Unable to connect to JACK server");
90 if (status & JackNameNotUnique) {
91 const char * client_name = jack_get_client_name(client);
92 std::cout << "unique client name: " << client_name << std::endl;
95 /* initialize callbacks */
96 jack_set_thread_init_callback (client, jack_thread_init_callback, this);
97 jack_set_process_callback (client, jack_process_callback, this);
98 jack_set_xrun_callback(client, jack_xrun_callback, this);
99 jack_on_info_shutdown(client, (JackInfoShutdownCallback)jack_on_info_shutdown_callback, NULL);
101 /* register ports */
102 input_ports.clear();
103 for (uint32_t i = 0; i != input_port_count; ++i) {
104 std::string portname ("input_");
105 portname += boost::lexical_cast<std::string>(i+1);
106 jack_port_t * port =
107 jack_port_register(client, portname.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
108 input_ports.push_back(port);
110 input_channels = input_port_count;
111 super::input_samples.resize(input_port_count);
113 output_ports.clear();
114 for (uint32_t i = 0; i != output_port_count; ++i) {
115 std::string portname ("output_");
116 portname += boost::lexical_cast<std::string>(i+1);
117 jack_port_t * port =
118 jack_port_register(client, portname.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
119 output_ports.push_back(port);
121 output_channels = output_port_count;
122 super::output_samples.resize(output_port_count);
124 samplerate_ = jack_get_sample_rate(client);
125 jack_frames = jack_get_buffer_size(client);
127 if (jack_frames % blocksize_)
128 throw std::runtime_error("Jack buffer size is not a multiple of blocksize");
131 void close_client(void)
133 if (client) {
134 jack_client_close(client);
135 client = NULL;
139 bool audio_is_opened(void)
141 return client != NULL;
144 bool audio_is_active(void)
146 return is_active;
149 void activate_audio(void)
151 is_active = true;
152 jack_activate(client);
155 void deactivate_audio(void)
157 jack_deactivate(client);
158 is_active = false;
161 void get_cpuload(float & peak, float & average) const
163 cpu_time_accumulator.get(peak, average);
166 int connect_input(int channel, const char * portname)
168 if (channel >= input_ports.size())
169 return -1;
170 return jack_connect(client, portname, jack_port_name(input_ports[channel]));
173 int connect_output(int channel, const char * portname)
175 if (channel >= output_ports.size())
176 return -1;
177 return jack_connect(client, jack_port_name(output_ports[channel]), portname);
180 int connect_all_inputs(const char * client_name)
182 const char **ports = jack_get_ports (client, client_name, NULL, JackPortIsOutput);
184 if (!ports)
185 return -1;
187 std::size_t i = 0;
188 while (ports[i]) {
189 if (i == input_ports.size())
190 break;
192 int err = jack_connect(client, ports[i], jack_port_name(input_ports[i]));
193 if (err)
194 return err;
195 ++i;
198 free(ports);
199 return 0;
202 int connect_all_outputs(const char * client_name)
204 const char **ports = jack_get_ports (client, client_name, NULL, JackPortIsInput);
206 if (!ports)
207 return -1;
209 std::size_t i = 0;
210 while (ports[i]) {
211 if (i == output_ports.size())
212 break;
214 int err = jack_connect(client, jack_port_name(output_ports[i]), ports[i]);
215 if (err)
216 return err;
217 ++i;
220 free(ports);
221 return 0;
224 int max_realtime_priority(void) const
226 return jack_client_max_real_time_priority(client);
229 int realtime_priority(void) const
231 return jack_client_real_time_priority(client);
234 private:
235 static void jack_thread_init_callback(void * arg)
237 boost::atomic_thread_fence(boost::memory_order_acquire);
238 jack_backend * self = static_cast<jack_backend*>(arg);
239 if (jack_client_thread_id(self->client) == pthread_self())
240 engine_functor::init_thread();
241 else
242 name_thread("Jack Helper");
245 static int jack_process_callback(jack_nframes_t frames, void * arg)
247 return static_cast<jack_backend*>(arg)->perform(frames);
250 static int jack_on_info_shutdown_callback(jack_status_t code, const char *reason, void *arg)
252 std::cerr << "Jack server was shut down: " << reason << std::endl;
253 std::cerr << "Exiting ..." << std::endl;
254 exit(0); // TODO: later we may want to call a function
257 static int jack_xrun_callback(void * arg)
259 return static_cast<jack_backend*>(arg)->handle_xrun();
262 int handle_xrun(void)
264 time_is_synced = false;
265 engine_functor::log_("Jack: xrun detected - resyncing clock\n");
266 return 0;
269 int perform(jack_nframes_t frames)
271 if (unlikely(!time_is_synced)) {
272 engine_functor::sync_clock();
273 time_is_synced = true;
276 /* get port regions */
277 jack_default_audio_sample_t * inputs[input_channels];
278 jack_default_audio_sample_t * outputs[output_channels];
279 for (uint16_t i = 0; i != input_channels; ++i)
280 inputs[i] = (jack_default_audio_sample_t*) jack_port_get_buffer(input_ports[i], frames);
282 for (uint16_t i = 0; i != output_channels; ++i)
283 outputs[i] = (jack_default_audio_sample_t*) jack_port_get_buffer(output_ports[i], frames);
285 jack_nframes_t processed = 0;
286 while (processed != frames) {
287 super::fetch_inputs((const float**)inputs, blocksize_, input_channels);
288 engine_functor::run_tick();
289 super::deliver_outputs(outputs, blocksize_, output_channels);
290 processed += blocksize_;
293 cpu_time_accumulator.update(jack_cpu_load(client));
295 return 0;
298 static int jack_buffersize_callback(jack_nframes_t frames, void * arg)
300 return static_cast<jack_backend*>(arg)->buffer_size_callback(frames);
303 int buffer_size_callback(jack_nframes_t frames)
305 jack_frames = frames;
306 if (jack_frames % blocksize_)
307 /* we need a multiple of the blocksize */
308 return 1;
309 return 0;
312 jack_client_t * client;
313 jack_status_t status;
315 bool is_active;
316 bool time_is_synced;
317 uint32_t blocksize_;
319 std::vector<jack_port_t*> input_ports, output_ports;
320 jack_nframes_t jack_frames;
321 cpu_time_info cpu_time_accumulator;
324 } /* namespace nova */
327 #endif /* AUDIO_BACKEND_JACK_BACKEND_HPP */