ardour
port_insert.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000,2007 Paul Davis
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.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #include <string>
21 
22 #include "pbd/xml++.h"
23 
24 #include "ardour/audio_port.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/delivery.h"
27 #include "ardour/io.h"
28 #include "ardour/mtdm.h"
29 #include "ardour/port_insert.h"
30 #include "ardour/session.h"
31 #include "ardour/types.h"
32 
33 #include "i18n.h"
34 
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38 
39 string
40 PortInsert::name_and_id_new_insert (Session& s, uint32_t& bitslot)
41 {
42  bitslot = s.next_insert_id ();
43  return string_compose (_("insert %1"), bitslot+ 1);
44 }
45 
46 PortInsert::PortInsert (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm)
47  : IOProcessor (s, true, true, name_and_id_new_insert (s, _bitslot), "", DataType::AUDIO, true)
48  , _out (new Delivery (s, _output, pannable, mm, _name, Delivery::Insert))
49 {
50  _mtdm = 0;
51  _latency_detect = false;
54 }
55 
57 {
59  delete _mtdm;
60 }
61 
62 void
64 {
65  delete _mtdm;
66  _mtdm = new MTDM (_session.frame_rate());
68  _latency_detect = true;
70 }
71 
72 void
74 {
76  _latency_detect = false;
77 }
78 
79 void
81 {
83 }
84 
87 {
88  /* because we deliver and collect within the same cycle,
89  all I/O is necessarily delayed by at least frames_per_cycle().
90 
91  if the return port for insert has its own latency, we
92  need to take that into account too.
93  */
94 
95  if (_measured_latency == 0) {
96  return _session.engine().samples_per_cycle() + _input->latency();
97  } else {
98  return _measured_latency;
99  }
100 }
101 
102 void
103 PortInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
104 {
105  if (_output->n_ports().n_total() == 0) {
106  return;
107  }
108 
109  if (_latency_detect) {
110 
111  if (_input->n_ports().n_audio() != 0) {
112 
113  AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
114  Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
115  Sample* out = outbuf.data();
116 
117  _mtdm->process (nframes, in, out);
118 
119  outbuf.set_written (true);
120  }
121 
122  return;
123 
124  } else if (_latency_flush_frames) {
125 
126  /* wait for the entire input buffer to drain before picking up input again so that we can't
127  hear the remnants of whatever MTDM pumped into the pipeline.
128  */
129 
130  silence (nframes);
131 
132  if (_latency_flush_frames > nframes) {
133  _latency_flush_frames -= nframes;
134  } else {
136  }
137 
138  return;
139  }
140 
141  if (!_active && !_pending_active) {
142  /* deliver silence */
143  silence (nframes);
144  goto out;
145  }
146 
147  _out->run (bufs, start_frame, end_frame, nframes, true);
148  _input->collect_input (bufs, nframes, ChanCount::ZERO);
149 
150  out:
152 }
153 
154 XMLNode&
156 {
157  return state (true);
158 }
159 
160 XMLNode&
161 PortInsert::state (bool full)
162 {
163  XMLNode& node = IOProcessor::state(full);
164  char buf[32];
165  node.add_property ("type", "port");
166  snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
167  node.add_property ("bitslot", buf);
168  snprintf (buf, sizeof (buf), "%" PRId64, _measured_latency);
169  node.add_property("latency", buf);
170  snprintf (buf, sizeof (buf), "%u", _session.get_block_size());
171  node.add_property("block_size", buf);
172 
173  return node;
174 }
175 
176 int
177 PortInsert::set_state (const XMLNode& node, int version)
178 {
179  XMLNodeList nlist = node.children();
180  XMLNodeIterator niter;
181  XMLPropertyList plist;
182  const XMLProperty *prop;
183 
184  const XMLNode* insert_node = &node;
185 
186  // legacy sessions: search for child Redirect node
187  for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
188  if ((*niter)->name() == "Redirect") {
189  insert_node = *niter;
190  break;
191  }
192  }
193 
194  IOProcessor::set_state (*insert_node, version);
195 
196  if ((prop = node.property ("type")) == 0) {
197  error << _("XML node describing port insert is missing the `type' field") << endmsg;
198  return -1;
199  }
200 
201  if (prop->value() != "port") {
202  error << _("non-port insert XML used for port plugin insert") << endmsg;
203  return -1;
204  }
205 
206  uint32_t blocksize = 0;
207  if ((prop = node.property ("block_size")) != 0) {
208  sscanf (prop->value().c_str(), "%u", &blocksize);
209  }
210 
211  //if the jack period is the same as when the value was saved, we can recall our latency..
212  if ( (_session.get_block_size() == blocksize) && (prop = node.property ("latency")) != 0) {
213  uint32_t latency = 0;
214  sscanf (prop->value().c_str(), "%u", &latency);
216  }
217 
218  if (!node.property ("ignore-bitslot")) {
219  if ((prop = node.property ("bitslot")) == 0) {
221  } else {
223  sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
225  }
226  }
227 
228  return 0;
229 }
230 
233 {
234  /* because we deliver and collect within the same cycle,
235  all I/O is necessarily delayed by at least frames_per_cycle().
236 
237  if the return port for insert has its own latency, we
238  need to take that into account too.
239  */
240 
241  if (_measured_latency == 0) {
242  return _session.engine().samples_per_cycle() + _input->signal_latency();
243  } else {
244  return _measured_latency;
245  }
246 }
247 
249 bool
251 {
252 #ifndef PLATFORM_WINDOWS
253  assert (!AudioEngine::instance()->process_lock().trylock());
254 #endif
255 
256  /* for an insert, processor input corresponds to IO output, and vice versa */
257 
258  if (_input->ensure_io (in, false, this) != 0) {
259  return false;
260  }
261 
262  if (_output->ensure_io (out, false, this) != 0) {
263  return false;
264  }
265 
266  return Processor::configure_io (in, out);
267 }
268 
269 bool
271 {
272  out = in;
273  return true;
274 }
275 
276 bool
277 PortInsert::set_name (const std::string& name)
278 {
279  bool ret = Processor::set_name (name);
280 
281  ret = (ret && _input->set_name (name) && _output->set_name (name));
282 
283  return ret;
284 }
285 
286 void
288 {
290 
291  _out->activate ();
292 }
293 
294 void
296 {
298 
299  _out->deactivate ();
300 }
XMLNodeList::iterator XMLNodeIterator
Definition: xml++.h:48
ARDOUR::Session & _session
void run(BufferSet &bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool)
Definition: port_insert.cc:103
framecnt_t _latency_flush_frames
Definition: port_insert.h:87
const std::string & value() const
Definition: xml++.h:159
Definition: mtdm.h:26
pframes_t samples_per_cycle() const
Definition: audioengine.cc:983
std::list< XMLProperty * > XMLPropertyList
Definition: xml++.h:50
int process(size_t len, float *inp, float *out)
Definition: mtdm.cc:54
bool configure_io(ChanCount in, ChanCount out)
Definition: port_insert.cc:250
uint32_t pframes_t
Definition: types.h:61
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
const XMLNodeList & children(const std::string &str=std::string()) const
Definition: xml++.cc:329
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
void stop_latency_detection()
Definition: port_insert.cc:73
static AudioEngine * instance()
Definition: audioengine.h:196
framecnt_t frame_rate() const
Definition: session.h:365
boost::shared_ptr< IO > _output
Definition: io_processor.h:84
void unmark_insert_id(uint32_t)
Definition: session.cc:4513
std::list< XMLNode * > XMLNodeList
Definition: xml++.h:44
XMLNode & get_state(void)
Definition: port_insert.cc:155
int set_state(const XMLNode &, int version)
#define _(Text)
Definition: i18n.h:11
virtual void deactivate()
Definition: processor.h:80
int64_t framecnt_t
Definition: types.h:76
XMLProperty * property(const char *)
Definition: xml++.cc:413
float Sample
Definition: types.h:54
Definition: amp.h:29
bool set_name(const std::string &name)
Definition: port_insert.cc:277
virtual bool set_name(const std::string &str)
virtual bool configure_io(ChanCount in, ChanCount out)
Definition: processor.cc:246
int64_t framepos_t
Definition: types.h:66
framecnt_t signal_latency() const
Definition: port_insert.cc:232
void silence(framecnt_t nframes)
XMLProperty * add_property(const char *name, const std::string &value)
XMLNode & state(bool full_state)
bool can_support_io_configuration(const ChanCount &in, ChanCount &out)
Definition: port_insert.cc:270
const char * name
boost::shared_ptr< Delivery > _out
Definition: port_insert.h:82
pframes_t get_block_size() const
Definition: session.h:393
framecnt_t _measured_latency
Definition: port_insert.h:88
boost::shared_ptr< IO > _input
Definition: io_processor.h:83
Definition: xml++.h:95
void set_measured_latency(framecnt_t)
Definition: port_insert.cc:80
Definition: debug.h:30
void start_latency_detection()
Definition: port_insert.cc:63
virtual void activate()
Definition: processor.h:79
void mark_insert_id(uint32_t)
Definition: session.cc:4477
static const ChanCount ZERO
Definition: chan_count.h:149
AudioEngine & engine()
Definition: session.h:546
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
framecnt_t latency() const
Definition: port_insert.cc:86
XMLNode & state(bool full)
Definition: port_insert.cc:161
int set_state(const XMLNode &, int version)
Definition: port_insert.cc:177
uint32_t next_insert_id()
Definition: session.cc:4361