2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "delay_line.h"
28 using boost::shared_ptr;
30 /** @param channels Number of channels of audio.
31 * @param frames Delay in frames, +ve to move audio later.
33 DelayLine::DelayLine (int channels, int frames)
36 /* We need a buffer to keep some data in */
37 _buffers.reset (new AudioBuffers (channels, frames));
38 _buffers->make_silent ();
39 } else if (frames < 0) {
40 /* We can do -ve delays just by chopping off
41 the start, so no buffer needed.
43 _negative_delay_remaining = -frames;
47 DelayLine::~DelayLine ()
53 DelayLine::feed (shared_ptr<AudioBuffers> data)
56 /* We have some buffers, so we are moving the audio later */
58 /* Copy the input data */
59 AudioBuffers input (*data.get ());
61 int to_do = data->frames ();
63 /* Write some of our buffer to the output */
64 int const from_buffer = min (to_do, _buffers->frames());
65 data->copy_from (_buffers.get(), from_buffer, 0, 0);
68 /* Write some of the input to the output */
69 int const from_input = to_do;
70 data->copy_from (&input, from_input, 0, from_buffer);
72 int const left_in_buffer = _buffers->frames() - from_buffer;
74 /* Shuffle our buffer down */
75 _buffers->move (from_buffer, 0, left_in_buffer);
77 /* Copy remaining input data to our buffer */
78 _buffers->copy_from (&input, input.frames() - from_input, from_input, left_in_buffer);
82 /* Chop the initial data off until _negative_delay_remaining
83 is zero, then just pass data.
86 int const to_do = min (data->frames(), _negative_delay_remaining);
88 data->move (to_do, 0, data->frames() - to_do);
89 data->set_frames (data->frames() - to_do);
90 _negative_delay_remaining -= to_do;