Merge master.
[dcpomatic.git] / src / lib / audio_merger.cc
1 /*
2     Copyright (C) 2013-2014 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_buffers.h"
21 #include "audio_merger.h"
22
23 using std::min;
24 using std::max;
25 using boost::shared_ptr;
26
27 AudioMerger::AudioMerger (int channels, int frame_rate)
28         : _buffers (new AudioBuffers (channels, 0))
29         , _frame_rate (frame_rate)
30         , _last_pull (0)
31 {
32
33 }
34
35
36 TimedAudioBuffers
37 AudioMerger::pull (DCPTime time)
38 {
39         assert (time >= _last_pull);
40         
41         TimedAudioBuffers out;
42         
43         int64_t const to_return = DCPTime (time - _last_pull).frames (_frame_rate);
44         out.audio.reset (new AudioBuffers (_buffers->channels(), to_return));
45         /* And this is how many we will get from our buffer */
46         int64_t const to_return_from_buffers = min (to_return, int64_t (_buffers->frames ()));
47         
48         /* Copy the data that we have to the back end of the return buffer */
49         out.audio->copy_from (_buffers.get(), to_return_from_buffers, 0, to_return - to_return_from_buffers);
50         /* Silence any gap at the start */
51         out.audio->make_silent (0, to_return - to_return_from_buffers);
52         
53         out.time = _last_pull;
54         _last_pull = time;
55         
56         /* And remove the data we're returning from our buffers */
57         if (_buffers->frames() > to_return_from_buffers) {
58                 _buffers->move (to_return_from_buffers, 0, _buffers->frames() - to_return_from_buffers);
59         }
60         _buffers->set_frames (_buffers->frames() - to_return_from_buffers);
61         
62         return out;
63 }
64
65 void
66 AudioMerger::push (shared_ptr<const AudioBuffers> audio, DCPTime time)
67 {
68         assert (time >= _last_pull);
69         
70         int64_t frame = time.frames (_frame_rate);
71         int64_t after = max (int64_t (_buffers->frames()), frame + audio->frames() - _last_pull.frames (_frame_rate));
72         _buffers->ensure_size (after);
73         _buffers->accumulate_frames (audio.get(), 0, frame - _last_pull.frames (_frame_rate), audio->frames ());
74         _buffers->set_frames (after);
75 }
76
77 TimedAudioBuffers
78 AudioMerger::flush ()
79 {
80         if (_buffers->frames() == 0) {
81                 return TimedAudioBuffers ();
82         }
83         
84         return TimedAudioBuffers (_buffers, _last_pull);
85 }
86
87 void
88 AudioMerger::clear (DCPTime t)
89 {
90         _last_pull = t;
91         _buffers.reset (new AudioBuffers (_buffers->channels(), 0));
92 }