Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / audio_delay.cc
1 /*
2     Copyright (C) 2015 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 "audio_delay.h"
21 #include "audio_buffers.h"
22 #include "dcpomatic_assert.h"
23 #include <iostream>
24
25 using std::cout;
26 using boost::shared_ptr;
27
28 AudioDelay::AudioDelay (int samples)
29         : _samples (samples)
30 {
31
32 }
33
34 shared_ptr<AudioBuffers>
35 AudioDelay::run (shared_ptr<const AudioBuffers> in)
36 {
37         /* You can't call this with varying channel counts */
38         DCPOMATIC_ASSERT (!_tail || in->channels() == _tail->channels());
39
40         shared_ptr<AudioBuffers> out (new AudioBuffers (in->channels(), in->frames()));
41
42         if (in->frames() > _samples) {
43
44                 if (!_tail) {
45                         /* No tail; use silence */
46                         out->make_silent (0, _samples);
47                 } else {
48                         /* Copy tail */
49                         out->copy_from (_tail.get(), _samples, 0, 0);
50                 }
51
52                 /* Copy in to out */
53                 out->copy_from (in.get(), in->frames() - _samples, 0, _samples);
54
55                 /* Keep tail */
56                 if (!_tail) {
57                         _tail.reset (new AudioBuffers (in->channels(), _samples));
58                 }
59                 _tail->copy_from (in.get(), _samples, in->frames() - _samples, 0);
60
61         } else {
62
63                 /* First part of the tail into the output */
64                 if (_tail) {
65                         out->copy_from (_tail.get(), out->frames(), 0, 0);
66                 } else {
67                         out->make_silent ();
68                         _tail.reset (new AudioBuffers (out->channels(), _samples));
69                         _tail->make_silent ();
70                 }
71
72                 /* Shuffle the tail down */
73                 _tail->move (out->frames(), 0, _tail->frames() - out->frames());
74
75                 /* Copy input into the tail */
76                 _tail->copy_from (in.get(), in->frames(), 0, _tail->frames() - in->frames());
77         }
78
79         return out;
80 }
81
82 void
83 AudioDelay::flush ()
84 {
85         _tail.reset ();
86 }