Merge branch 'new-env-test'
[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         : _negative_delay_remaining (0)
35 {
36         if (frames > 0) {
37                 /* We need a buffer to keep some data in */
38                 _buffers.reset (new AudioBuffers (channels, frames));
39                 _buffers->make_silent ();
40         } else if (frames < 0) {
41                 /* We can do -ve delays just by chopping off
42                    the start, so no buffer needed.
43                 */
44                 _negative_delay_remaining = -frames;
45         }
46 }
47
48 DelayLine::~DelayLine ()
49 {
50
51 }
52
53 void
54 DelayLine::feed (shared_ptr<AudioBuffers> data)
55 {
56         if (_buffers) {
57                 /* We have some buffers, so we are moving the audio later */
58
59                 /* Copy the input data */
60                 AudioBuffers input (*data.get ());
61
62                 int to_do = data->frames ();
63
64                 /* Write some of our buffer to the output */
65                 int const from_buffer = min (to_do, _buffers->frames());
66                 data->copy_from (_buffers.get(), from_buffer, 0, 0);
67                 to_do -= from_buffer;
68
69                 /* Write some of the input to the output */
70                 int const from_input = to_do;
71                 data->copy_from (&input, from_input, 0, from_buffer);
72
73                 int const left_in_buffer = _buffers->frames() - from_buffer;
74
75                 /* Shuffle our buffer down */
76                 _buffers->move (from_buffer, 0, left_in_buffer);
77
78                 /* Copy remaining input data to our buffer */
79                 _buffers->copy_from (&input, input.frames() - from_input, from_input, left_in_buffer);
80
81         } else {
82
83                 /* Chop the initial data off until _negative_delay_remaining
84                    is zero, then just pass data.
85                 */
86
87                 int const to_do = min (data->frames(), _negative_delay_remaining);
88                 if (to_do) {
89                         data->move (to_do, 0, data->frames() - to_do);
90                         data->set_frames (data->frames() - to_do);
91                         _negative_delay_remaining -= to_do;
92                 }
93         }
94 }