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 /** Construct a DelayLine delaying by some number of bytes.
29 * @param d Number of bytes to delay by; +ve moves data later.
31 DelayLine::DelayLine (int d)
34 , _negative_delay_remaining (0)
37 /* We need a buffer to keep some data in */
38 _buffer = new uint8_t[d];
39 memset (_buffer, 0, d);
41 /* We can do -ve delays just by chopping off
42 the start, so no buffer needed.
44 _negative_delay_remaining = -d;
48 DelayLine::~DelayLine ()
54 DelayLine::feed (uint8_t* data, int size)
60 /* Copy the input data */
62 memcpy (input, data, size);
66 /* Write some of our buffer to the output */
67 int const from_buffer = min (to_do, _delay);
68 memcpy (data, _buffer, from_buffer);
71 /* Write some of the input to the output */
72 int const from_input = min (to_do, size);
73 memcpy (data + from_buffer, input, from_input);
75 int const left_in_buffer = _delay - from_buffer;
77 /* Shuffle our buffer down */
78 memmove (_buffer, _buffer + from_buffer, left_in_buffer);
80 /* Copy remaining input data to our buffer */
81 memcpy (_buffer + left_in_buffer, input + from_input, size - from_input);
83 } else if (_delay < 0) {
85 /* Chop the initial data off until _negative_delay_remaining
86 is zero, then just pass data.
89 int const to_do = min (size, _negative_delay_remaining);
90 available = size - to_do;
91 memmove (data, data + to_do, available);
92 _negative_delay_remaining -= to_do;
99 /** With -ve delays, the DelayLine will have data to give after
100 * all input data has been passed to ::feed().
101 * Call this method after passing all input data.
103 * @param buffer Pointer to buffer of _delay bytes in length,
104 * which will be filled with remaining data.
107 DelayLine::get_remaining (uint8_t* buffer)
109 memset (buffer, 0, -_delay);