C++11 tidying.
[dcpomatic.git] / src / lib / audio_delay.cc
1 /*
2     Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "audio_delay.h"
23 #include "audio_buffers.h"
24 #include "dcpomatic_assert.h"
25 #include <iostream>
26
27
28 using std::cout;
29 using std::make_shared;
30 using std::shared_ptr;
31
32
33 AudioDelay::AudioDelay (int samples)
34         : _samples (samples)
35 {
36
37 }
38
39
40 shared_ptr<AudioBuffers>
41 AudioDelay::run (shared_ptr<const AudioBuffers> in)
42 {
43         /* You can't call this with varying channel counts */
44         DCPOMATIC_ASSERT (!_tail || in->channels() == _tail->channels());
45
46         shared_ptr<AudioBuffers> out (new AudioBuffers (in->channels(), in->frames()));
47
48         if (in->frames() > _samples) {
49
50                 if (!_tail) {
51                         /* No tail; use silence */
52                         out->make_silent (0, _samples);
53                 } else {
54                         /* Copy tail */
55                         out->copy_from (_tail.get(), _samples, 0, 0);
56                 }
57
58                 /* Copy in to out */
59                 out->copy_from (in.get(), in->frames() - _samples, 0, _samples);
60
61                 /* Keep tail */
62                 if (!_tail) {
63                         _tail = make_shared<AudioBuffers>(in->channels(), _samples);
64                 }
65                 _tail->copy_from (in.get(), _samples, in->frames() - _samples, 0);
66
67         } else {
68
69                 /* First part of the tail into the output */
70                 if (_tail) {
71                         out->copy_from (_tail.get(), out->frames(), 0, 0);
72                 } else {
73                         out->make_silent ();
74                         _tail = make_shared<AudioBuffers>(out->channels(), _samples);
75                         _tail->make_silent ();
76                 }
77
78                 /* Shuffle the tail down */
79                 _tail->move (_tail->frames() - out->frames(), out->frames(), 0);
80
81                 /* Copy input into the tail */
82                 _tail->copy_from (in.get(), in->frames(), 0, _tail->frames() - in->frames());
83         }
84
85         return out;
86 }
87
88
89 void
90 AudioDelay::flush ()
91 {
92         _tail.reset ();
93 }