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 (Log* log, int channels, int frames)
34 : AudioProcessor (log)
35 , _negative_delay_remaining (0)
39 /* We need a buffer to keep some data in */
40 _buffers.reset (new AudioBuffers (channels, _frames));
41 _buffers->make_silent ();
42 } else if (_frames < 0) {
43 /* We can do -ve delays just by chopping off
44 the start, so no buffer needed.
46 _negative_delay_remaining = -_frames;
51 DelayLine::process_audio (shared_ptr<AudioBuffers> data)
54 /* We have some buffers, so we are moving the audio later */
56 /* Copy the input data */
57 AudioBuffers input (*data.get ());
59 int to_do = data->frames ();
61 /* Write some of our buffer to the output */
62 int const from_buffer = min (to_do, _buffers->frames());
63 data->copy_from (_buffers.get(), from_buffer, 0, 0);
66 /* Write some of the input to the output */
67 int const from_input = to_do;
68 data->copy_from (&input, from_input, 0, from_buffer);
70 int const left_in_buffer = _buffers->frames() - from_buffer;
72 /* Shuffle our buffer down */
73 _buffers->move (from_buffer, 0, left_in_buffer);
75 /* Copy remaining input data to our buffer */
76 _buffers->copy_from (&input, input.frames() - from_input, from_input, left_in_buffer);
80 /* Chop the initial data off until _negative_delay_remaining
81 is zero, then just pass data.
84 int const to_do = min (data->frames(), _negative_delay_remaining);
86 data->move (to_do, 0, data->frames() - to_do);
87 data->set_frames (data->frames() - to_do);
88 _negative_delay_remaining -= to_do;
96 DelayLine::process_end ()
99 _buffers->make_silent ();