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 "nova-tt/name_thread.hpp"
32 #include "nova-tt/thread_affinity.hpp"
33 #include "utilities/branch_hints.hpp"
35 #include "audio_backend_common.hpp"
36 #include "cpu_time_info.hpp"
43 * the jack callback thread is pinned to the first cpu of the system
45 * \todo later it may be interesting to directly map the io busses to the jack port regions
46 * \todo rethink the use of output port lock
48 template <typename engine_functor
,
49 typename sample_type
= float,
53 public detail::audio_delivery_helper
<sample_type
, jack_default_audio_sample_t
, blocking
, false>,
54 public detail::audio_settings_basic
,
55 protected engine_functor
57 typedef detail::audio_delivery_helper
<sample_type
, jack_default_audio_sample_t
, blocking
, false> super
;
61 client(NULL
), time_is_synced(false)
66 if (audio_is_active())
72 uint32_t get_audio_blocksize(void) const
78 void open_client(std::string
const & server_name
, std::string
const & name
, uint32_t input_port_count
,
79 uint32_t output_port_count
, uint32_t blocksize
)
81 blocksize_
= blocksize
;
84 client
= server_name
.empty() ? jack_client_open(name
.c_str(), JackNoStartServer
, &status
)
85 : jack_client_open(name
.c_str(), jack_options_t(JackNoStartServer
| JackServerName
),
86 &status
, server_name
.c_str());
87 boost::atomic_thread_fence(boost::memory_order_release
); // ensure visibility on other threads
89 if (status
& JackServerFailed
)
90 throw std::runtime_error("Unable to connect to JACK server");
92 if (status
& JackNameNotUnique
) {
93 const char * client_name
= jack_get_client_name(client
);
94 std::cout
<< "unique client name: " << client_name
<< std::endl
;
97 /* initialize callbacks */
98 jack_set_thread_init_callback (client
, jack_thread_init_callback
, this);
99 jack_set_process_callback (client
, jack_process_callback
, this);
100 jack_set_xrun_callback(client
, jack_xrun_callback
, this);
101 jack_on_info_shutdown(client
, (JackInfoShutdownCallback
)jack_on_info_shutdown_callback
, NULL
);
105 for (uint32_t i
= 0; i
!= input_port_count
; ++i
) {
106 std::string
portname ("input_");
107 portname
+= boost::lexical_cast
<std::string
>(i
+1);
109 jack_port_register(client
, portname
.c_str(), JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
110 input_ports
.push_back(port
);
112 input_channels
= input_port_count
;
113 super::input_samples
.resize(input_port_count
);
115 output_ports
.clear();
116 for (uint32_t i
= 0; i
!= output_port_count
; ++i
) {
117 std::string
portname ("output_");
118 portname
+= boost::lexical_cast
<std::string
>(i
+1);
120 jack_port_register(client
, portname
.c_str(), JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
121 output_ports
.push_back(port
);
123 output_channels
= output_port_count
;
124 super::output_samples
.resize(output_port_count
);
126 samplerate_
= jack_get_sample_rate(client
);
127 jack_frames
= jack_get_buffer_size(client
);
129 if (jack_frames
% blocksize_
)
130 throw std::runtime_error("Jack buffer size is not a multiple of blocksize");
133 void close_client(void)
136 jack_client_close(client
);
141 bool audio_is_opened(void)
143 return client
!= NULL
;
146 bool audio_is_active(void)
151 void activate_audio(void)
154 jack_activate(client
);
157 void deactivate_audio(void)
159 jack_deactivate(client
);
163 void get_cpuload(float & peak
, float & average
) const
165 cpu_time_accumulator
.get(peak
, average
);
168 int connect_input(int channel
, const char * portname
)
170 if (channel
>= input_ports
.size())
172 return jack_connect(client
, portname
, jack_port_name(input_ports
[channel
]));
175 int connect_output(int channel
, const char * portname
)
177 if (channel
>= output_ports
.size())
179 return jack_connect(client
, jack_port_name(output_ports
[channel
]), portname
);
182 int connect_all_inputs(const char * client_name
)
184 const char **ports
= jack_get_ports (client
, client_name
, NULL
, JackPortIsOutput
);
191 if (i
== input_ports
.size())
194 int err
= jack_connect(client
, ports
[i
], jack_port_name(input_ports
[i
]));
204 int connect_all_outputs(const char * client_name
)
206 const char **ports
= jack_get_ports (client
, client_name
, NULL
, JackPortIsInput
);
213 if (i
== output_ports
.size())
216 int err
= jack_connect(client
, jack_port_name(output_ports
[i
]), ports
[i
]);
226 int max_realtime_priority(void) const
228 return jack_client_max_real_time_priority(client
);
231 int realtime_priority(void) const
233 return jack_client_real_time_priority(client
);
237 static void jack_thread_init_callback(void * arg
)
239 boost::atomic_thread_fence(boost::memory_order_acquire
);
240 jack_backend
* self
= static_cast<jack_backend
*>(arg
);
241 if (jack_client_thread_id(self
->client
) == pthread_self())
242 engine_functor::init_thread();
244 name_thread("Jack Helper");
247 static int jack_process_callback(jack_nframes_t frames
, void * arg
)
249 return static_cast<jack_backend
*>(arg
)->perform(frames
);
252 static int jack_on_info_shutdown_callback(jack_status_t code
, const char *reason
, void *arg
)
254 std::cerr
<< "Jack server was shut down: " << reason
<< std::endl
;
255 std::cerr
<< "Exiting ..." << std::endl
;
256 exit(0); // TODO: later we may want to call a function
259 static int jack_xrun_callback(void * arg
)
261 return static_cast<jack_backend
*>(arg
)->handle_xrun();
264 int handle_xrun(void)
266 time_is_synced
= false;
267 engine_functor::log_("Jack: xrun detected - resyncing clock\n");
271 int perform(jack_nframes_t frames
)
273 if (unlikely(!time_is_synced
)) {
274 engine_functor::sync_clock();
275 time_is_synced
= true;
278 /* get port regions */
279 jack_default_audio_sample_t
* inputs
[input_channels
];
280 jack_default_audio_sample_t
* outputs
[output_channels
];
281 for (uint16_t i
= 0; i
!= input_channels
; ++i
)
282 inputs
[i
] = (jack_default_audio_sample_t
*) jack_port_get_buffer(input_ports
[i
], frames
);
284 for (uint16_t i
= 0; i
!= output_channels
; ++i
)
285 outputs
[i
] = (jack_default_audio_sample_t
*) jack_port_get_buffer(output_ports
[i
], frames
);
287 jack_nframes_t processed
= 0;
288 while (processed
!= frames
) {
289 super::fetch_inputs((const float**)inputs
, blocksize_
, input_channels
);
290 engine_functor::run_tick();
291 super::deliver_outputs(outputs
, blocksize_
, output_channels
);
292 processed
+= blocksize_
;
295 cpu_time_accumulator
.update(jack_cpu_load(client
));
300 static int jack_buffersize_callback(jack_nframes_t frames
, void * arg
)
302 return static_cast<jack_backend
*>(arg
)->buffer_size_callback(frames
);
305 int buffer_size_callback(jack_nframes_t frames
)
307 jack_frames
= frames
;
308 if (jack_frames
% blocksize_
)
309 /* we need a multiple of the blocksize */
314 jack_client_t
* client
;
315 jack_status_t status
;
321 std::vector
<jack_port_t
*> input_ports
, output_ports
;
322 jack_nframes_t jack_frames
;
323 cpu_time_info cpu_time_accumulator
;
326 } /* namespace nova */
329 #endif /* AUDIO_BACKEND_JACK_BACKEND_HPP */