Do the delay line in floats with an AudioBuffer.
[dcpomatic.git] / src / lib / delay_line.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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 <stdint.h>
21 #include <cstring>
22 #include <algorithm>
23 #include <iostream>
24 #include "delay_line.h"
25 #include "util.h"
26
27 using std::min;
28 using boost::shared_ptr;
29
30 /** @param channels Number of channels of audio.
31  *  @param frames Delay in frames, +ve to move audio later.
32  */
33 DelayLine::DelayLine (int channels, int frames)
34 {
35         if (frames > 0) {
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.
42                 */
43                 _negative_delay_remaining = -frames;
44         }
45 }
46
47 DelayLine::~DelayLine ()
48 {
49
50 }
51
52 void
53 DelayLine::feed (shared_ptr<AudioBuffers> data)
54 {
55         if (_buffers) {
56                 /* We have some buffers, so we are moving the audio later */
57
58                 /* Copy the input data */
59                 AudioBuffers input (*data.get ());
60
61                 int to_do = data->frames ();
62
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);
66                 to_do -= from_buffer;
67
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);
71
72                 int const left_in_buffer = _buffers->frames() - from_buffer;
73
74                 /* Shuffle our buffer down */
75                 _buffers->move (from_buffer, 0, left_in_buffer);
76
77                 /* Copy remaining input data to our buffer */
78                 _buffers->copy_from (&input, input.frames() - from_input, from_input, left_in_buffer);
79
80         } else {
81
82                 /* Chop the initial data off until _negative_delay_remaining
83                    is zero, then just pass data.
84                 */
85
86                 int const to_do = min (data->frames(), _negative_delay_remaining);
87                 if (to_do) {
88                         data->move (to_do, 0, data->frames() - to_do);
89                         data->set_frames (data->frames() - to_do);
90                         _negative_delay_remaining -= to_do;
91                 }
92         }
93 }