2 // Copyright (C) 2009, 2010 Tim Blechmann
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.
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
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"
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,
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
;
59 client(NULL
), time_is_synced(false)
64 if (audio_is_active())
70 uint32_t get_audio_blocksize(void) const
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
;
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
);
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);
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);
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)
134 jack_client_close(client
);
139 bool audio_is_opened(void)
141 return client
!= NULL
;
144 bool audio_is_active(void)
149 void activate_audio(void)
152 jack_activate(client
);
155 void deactivate_audio(void)
157 jack_deactivate(client
);
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())
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())
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
);
189 if (i
== input_ports
.size())
192 int err
= jack_connect(client
, ports
[i
], jack_port_name(input_ports
[i
]));
202 int connect_all_outputs(const char * client_name
)
204 const char **ports
= jack_get_ports (client
, client_name
, NULL
, JackPortIsInput
);
211 if (i
== output_ports
.size())
214 int err
= jack_connect(client
, jack_port_name(output_ports
[i
]), ports
[i
]);
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
);
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();
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");
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
));
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 */
312 jack_client_t
* client
;
313 jack_status_t status
;
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 */