2 Copyright (C) 2006, 2013 Paul Davis
3 Copyright (C) 2013, 2014 Robin Gareus <robin@gareus.org>
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pbd/compose.h"
25 #include "ardour/debug.h"
26 #include "ardour/audio_buffer.h"
27 #include "ardour/midi_buffer.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/delayline.h"
33 using namespace ARDOUR;
35 DelayLine::DelayLine (Session& s, const std::string& name)
36 : Processor (s, string_compose ("latency-compensation-%1", name))
43 , _pending_flush(false)
47 DelayLine::~DelayLine ()
53 DelayLine::run (BufferSet& bufs, framepos_t /* start_frame */, framepos_t /* end_frame */, pframes_t nsamples, bool)
55 const uint32_t chn = _configured_output.n_audio();
59 const frameoffset_t pending_delay = _pending_delay;
60 const frameoffset_t delay_diff = _delay - pending_delay;
61 const bool pending_flush = _pending_flush;
62 _pending_flush = false;
64 /* run() and set_delay() may be called in parallel by
66 * if a larger buffer is needed, it is allocated in
67 * set_delay(), here it is just swap'ed in place
70 assert(_pending_bsiz >= _bsiz);
72 const size_t boff = _pending_bsiz - _bsiz;
74 /* write offset is retained. copy existing data to new buffer */
75 frameoffset_t wl = _bsiz - _woff;
76 memcpy(_pending_buf.get(), _buf.get(), sizeof(Sample) * _woff * chn);
77 memcpy(_pending_buf.get() + (_pending_bsiz - wl) * chn, _buf.get() + _woff * chn, sizeof(Sample) * wl * chn);
79 /* new buffer is all zero by default, fade into the existing data copied above */
80 frameoffset_t wo = _pending_bsiz - wl;
81 for (pframes_t pos = 0; pos < FADE_LEN; ++pos) {
82 const gain_t gain = (gain_t)pos / (gain_t)FADE_LEN;
83 for (c = 0; c < _configured_input.n_audio(); ++c) {
84 _pending_buf.get()[ wo * chn + c ] *= gain;
85 wo = (wo + 1) % (_pending_bsiz + 1);
89 /* read-pointer will be moved and may up anywhere..
90 * copy current data for smooth fade-out below
92 frameoffset_t roold = _roff;
93 frameoffset_t ro = _roff;
99 ro -= (_pending_bsiz +1) * floor(ro / (float)(_pending_bsiz +1));
101 ro = ro % (_pending_bsiz + 1);
102 for (pframes_t pos = 0; pos < FADE_LEN; ++pos) {
103 for (c = 0; c < _configured_input.n_audio(); ++c) {
104 _pending_buf.get()[ ro * chn + c ] = _buf.get()[ roold * chn + c ];
105 ro = (ro + 1) % (_pending_bsiz + 1);
106 roold = (roold + 1) % (_bsiz + 1);
116 _bsiz = _pending_bsiz;
120 /* there may be no buffer when delay == 0.
121 * we also need to check audio-channels in case all audio-channels
122 * were removed in which case no new buffer was allocated. */
123 Sample *buf = _buf.get();
124 if (buf && _configured_output.n_audio() > 0) {
126 assert (_bsiz >= pending_delay);
127 const framecnt_t rbs = _bsiz + 1;
129 if (pending_delay != _delay || pending_flush) {
130 const pframes_t fade_len = (nsamples >= FADE_LEN) ? FADE_LEN : nsamples / 2;
132 DEBUG_TRACE (DEBUG::LatencyCompensation,
133 string_compose ("Old %1 delay: %2 bufsiz: %3 offset-diff: %4 write-offset: %5 read-offset: %6\n",
134 name(), _delay, _bsiz, ((_woff - _roff + rbs) % rbs), _woff, _roff));
136 // fade out at old position
138 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++c) {
139 Sample * const data = i->data();
140 for (pframes_t pos = 0; pos < fade_len; ++pos) {
141 const gain_t gain = (gain_t)(fade_len - pos) / (gain_t)fade_len;
142 buf[ _woff * chn + c ] = data[ pos ];
143 data[ pos ] = buf[ _roff * chn + c ] * gain;
144 _roff = (_roff + 1) % rbs;
145 _woff = (_woff + 1) % rbs;
150 DEBUG_TRACE (DEBUG::LatencyCompensation,
151 string_compose ("Flush buffer: %1\n", name()));
152 memset(buf, 0, _configured_output.n_audio() * rbs * sizeof (Sample));
155 // adjust read pointer
156 _roff += _delay - pending_delay;
159 _roff -= rbs * floor(_roff / (float)rbs);
163 // fade in at new position
165 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++c) {
166 Sample * const data = i->data();
167 for (pframes_t pos = fade_len; pos < 2 * fade_len; ++pos) {
168 const gain_t gain = (gain_t)(pos - fade_len) / (gain_t)fade_len;
169 buf[ _woff * chn + c ] = data[ pos ];
170 data[ pos ] = buf[ _roff * chn + c ] * gain;
171 _roff = (_roff + 1) % rbs;
172 _woff = (_woff + 1) % rbs;
177 _delay = pending_delay;
179 DEBUG_TRACE (DEBUG::LatencyCompensation,
180 string_compose ("New %1 delay: %2 bufsiz: %3 offset-diff: %4 write-offset: %5 read-offset: %6\n",
181 name(), _delay, _bsiz, ((_woff - _roff + rbs) % rbs), _woff, _roff));
184 assert(_delay == ((_woff - _roff + rbs) % rbs));
187 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++c) {
188 Sample * const data = i->data();
189 for (pframes_t pos = p0; pos < nsamples; ++pos) {
190 buf[ _woff * chn + c ] = data[ pos ];
191 data[ pos ] = buf[ _roff * chn + c ];
192 _roff = (_roff + 1) % rbs;
193 _woff = (_woff + 1) % rbs;
198 if (_midi_buf.get()) {
199 _delay = pending_delay;
201 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
202 if (i != bufs.midi_begin()) { break; } // XXX only one buffer for now
204 MidiBuffer* dly = _midi_buf.get();
207 dly->silence(nsamples);
210 // If the delay time changes, iterate over all events in the dly-buffer
211 // and adjust the time in-place. <= 0 becomes 0.
213 // iterate over all events in dly-buffer and subtract one cycle
214 // (nsamples) from the timestamp, bringing them closer to de-queue.
215 for (MidiBuffer::iterator m = dly->begin(); m != dly->end(); ++m) {
216 MidiBuffer::TimeType *t = m.timeptr();
217 if (*t > nsamples + delay_diff) {
218 *t -= nsamples + delay_diff;
224 // delay events in current-buffer, in place.
225 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
226 MidiBuffer::TimeType *t = m.timeptr();
230 // move events from dly-buffer into current-buffer until nsamples
231 // and remove them from the dly-buffer
232 for (MidiBuffer::iterator m = dly->begin(); m != dly->end();) {
233 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*m, false);
234 if (ev.time() >= nsamples) {
241 // move events after nsamples from current-buffer into dly-buffer
242 // and trim current-buffer after nsamples
243 for (MidiBuffer::iterator m = mb.begin(); m != mb.end();) {
244 const Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*m, false);
245 if (ev.time() < nsamples) {
249 dly->insert_event(ev);
255 _delay = pending_delay;
259 DelayLine::set_delay(framecnt_t signal_delay)
261 if (signal_delay < 0) {
263 cerr << "WARNING: latency compensation is not possible.\n";
266 const framecnt_t rbs = signal_delay + 1;
268 DEBUG_TRACE (DEBUG::LatencyCompensation,
269 string_compose ("%1 set_delay to %2 samples for %3 channels\n",
270 name(), signal_delay, _configured_output.n_audio()));
272 if (signal_delay <= _bsiz) {
273 _pending_delay = signal_delay;
278 if (_pending_bsiz < signal_delay) {
279 cerr << "LatComp: buffer resize in progress. "<< name() << "pending: "<< _pending_bsiz <<" want: " << signal_delay <<"\n"; // XXX
281 _pending_delay = signal_delay;
286 if (_configured_output.n_audio() > 0 ) {
287 _pending_buf.reset(new Sample[_configured_output.n_audio() * rbs]);
288 memset(_pending_buf.get(), 0, _configured_output.n_audio() * rbs * sizeof (Sample));
289 _pending_bsiz = signal_delay;
291 _pending_buf.reset();
295 _pending_delay = signal_delay;
297 DEBUG_TRACE (DEBUG::LatencyCompensation,
298 string_compose ("allocated buffer for %1 of size %2\n",
299 name(), signal_delay));
303 DelayLine::can_support_io_configuration (const ChanCount& in, ChanCount& out)
310 DelayLine::configure_io (ChanCount in, ChanCount out)
312 if (out != in) { // always 1:1
316 // TODO realloc buffers if channel count changes..
317 // TODO support multiple midi buffers
319 DEBUG_TRACE (DEBUG::LatencyCompensation,
320 string_compose ("configure IO: %1 Ain: %2 Aout: %3 Min: %4 Mout: %5\n",
321 name(), in.n_audio(), out.n_audio(), in.n_midi(), out.n_midi()));
323 if (in.n_midi() > 0 && !_midi_buf) {
324 _midi_buf.reset(new MidiBuffer(16384));
327 return Processor::configure_io (in, out);
333 _pending_flush = true;
337 DelayLine::state (bool full_state)
339 XMLNode& node (Processor::state (full_state));
340 node.add_property("type", "delay");