Various work on certificate handling for screens; need XML config here, now.
[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 (Log* log, int channels, int frames)
34         : AudioProcessor (log)
35         , _negative_delay_remaining (0)
36         , _frames (frames)
37 {
38         if (_frames > 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.
45                 */
46                 _negative_delay_remaining = -_frames;
47         }
48 }
49
50 void
51 DelayLine::process_audio (shared_ptr<AudioBuffers> data)
52 {
53         if (_buffers) {
54                 /* We have some buffers, so we are moving the audio later */
55
56                 /* Copy the input data */
57                 AudioBuffers input (*data.get ());
58
59                 int to_do = data->frames ();
60
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);
64                 to_do -= from_buffer;
65
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);
69
70                 int const left_in_buffer = _buffers->frames() - from_buffer;
71
72                 /* Shuffle our buffer down */
73                 _buffers->move (from_buffer, 0, left_in_buffer);
74
75                 /* Copy remaining input data to our buffer */
76                 _buffers->copy_from (&input, input.frames() - from_input, from_input, left_in_buffer);
77
78         } else {
79
80                 /* Chop the initial data off until _negative_delay_remaining
81                    is zero, then just pass data.
82                 */
83
84                 int const to_do = min (data->frames(), _negative_delay_remaining);
85                 if (to_do) {
86                         data->move (to_do, 0, data->frames() - to_do);
87                         data->set_frames (data->frames() - to_do);
88                         _negative_delay_remaining -= to_do;
89                 }
90         }
91
92         Audio (data);
93 }
94
95 void
96 DelayLine::process_end ()
97 {
98         if (_frames < 0) {
99                 _buffers->make_silent ();
100                 Audio (_buffers);
101         }
102 }