30#ifndef APF_JACK_POLICY_H
31#define APF_JACK_POLICY_H
33#ifdef APF_JACK_POLICY_DEBUG
44#ifndef APF_MIMOPROCESSOR_INTERFACE_POLICY
45#define APF_MIMOPROCESSOR_INTERFACE_POLICY apf::jack_policy
58 using sample_type = sample_t;
66 nframes_t block_size()
const {
return this->
buffer_size(); }
79 template<
typename X>
class Xput;
83 using iterator =
const sample_type*;
84 static const bool is_input =
true;
85 static std::string prefix_name() {
return "input_prefix"; }
86 static std::string default_prefix() {
return "in_"; }
91 using iterator = sample_type*;
92 static const bool is_input =
false;
93 static std::string prefix_name() {
return "output_prefix"; }
94 static std::string default_prefix() {
return "out_"; }
97 virtual int jack_process_callback(nframes_t nframes)
100 assert(nframes == this->block_size());
107 catch (std::exception& e)
109#ifdef APF_JACK_POLICY_DEBUG
110 printf(
"Error in process callback: %s\n", e.what());
117 virtual void process() = 0;
120template<
typename interface_policy,
typename native_handle_type>
124struct thread_traits<jack_policy, pthread_t>
126 static void update_priority(
const jack_policy& obj, pthread_t thread_id)
noexcept
128 if (obj.is_realtime())
130#ifdef APF_JACK_POLICY_DEBUG
131 printf(
"Trying to set priority...");
133 struct sched_param param;
134 param.sched_priority = obj.get_real_time_priority();
135 if (pthread_setschedparam(thread_id, SCHED_FIFO, ¶m))
139#ifdef APF_JACK_POLICY_DEBUG
140 printf(
"Can't set scheduling priority %d for thread!\n", param.sched_priority);
148#ifdef APF_JACK_POLICY_DEBUG
149 struct sched_param param;
151 pthread_getschedparam(thread_id, &policy, ¶m);
152 printf(
"worker thread: policy=%s, priority=%d\n",
153 (policy == SCHED_FIFO) ?
"SCHED_FIFO" :
154 (policy == SCHED_RR) ?
"SCHED_RR" :
155 (policy == SCHED_OTHER) ?
"SCHED_OTHER" :
157 param.sched_priority);
164class jack_policy::Xput
167 using iterator =
typename X::iterator;
169 struct buffer_type : has_begin_and_end<iterator> {
friend class Xput; };
173 this->buffer._begin =
static_cast<sample_type*
>(
174 jack_port_get_buffer(_port, _parent.block_size()));
175 this->buffer._end = this->buffer._begin + _parent.block_size();
178 std::string port_name()
const {
return _port_name; }
185 ~Xput() { _parent.unregister_port(_port); }
188 Xput(
const Xput&); Xput& operator=(
const Xput&);
191 JackClient::port_t* _port;
193 JackClient::port_t* _init_port(
const parameter_map& p,
jack_policy& parent);
195 const std::string _port_name;
200jack_policy::Xput<X>::_init_port(
const parameter_map& p, jack_policy& parent)
202 auto name = std::string();
205 if (p.has_key(
"port_name"))
207 name = p[
"port_name"];
215 auto id = std::string();
219 id = p.get(
"id",
"");
223 static int next_id = 1;
227 name = p.get(X::prefix_name(), X::default_prefix()) + id;
229 JackClient::port_t* rport = X::is_input ? parent.register_in_port(name) : parent.register_out_port(name);
231 throw std::runtime_error(
"Could not register JACK port!");
237jack_policy::Xput<X>::Xput(jack_policy& parent,
const parameter_map& p)
239 , _port(_init_port(p, _parent))
241 , _port_name(_port ? jack_port_name(_port) :
"")
244 std::string connect_to = p.get(
"connect-to",
"");
245 if (connect_to !=
"")
249 _parent.connect_ports(connect_to, _port_name);
253 _parent.connect_ports(_port_name, connect_to);
258class jack_policy::Input :
public Xput<i_am_in>
262 : Xput<i_am_in>(parent, p)
268class jack_policy::Output :
public Xput<i_am_out>
271 Output(
jack_policy& parent,
const parameter_map& p)
272 : Xput<i_am_out>(parent, p)
C++ wrapper for a JACK client.
nframes_t sample_rate() const
bool deactivate() const
Deactivate JACK client.
nframes_t buffer_size() const
bool activate() const
Activate JACK client.
@ use_jack_process_callback
JACK audio callback (jack_process_callback()) is called after activate()
interface_policy using JACK.
jack_policy(const parameter_map &p=parameter_map())
Constructor.
Several more or less useful iterators and some macros.
JACK client (C++ wrapper for JACK).
std::string A2S(const T &input)
Converter "Anything to String".
Audio Processing Framework.
A "dictionary" for parameters.
A "dictionary" for parameters.