Various fixes to push audio vaguely in the right direction.
[dcpomatic.git] / src / lib / audio_merger.cc
1 /*
2     Copyright (C) 2013-2016 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_merger.h"
21 #include "dcpomatic_time.h"
22 #include <iostream>
23
24 using std::pair;
25 using std::min;
26 using std::max;
27 using std::make_pair;
28 using boost::shared_ptr;
29
30 AudioMerger::AudioMerger (int channels, int frame_rate)
31         : _buffers (new AudioBuffers (channels, 0))
32         , _last_pull (0)
33         , _frame_rate (frame_rate)
34 {
35
36 }
37
38 /** Pull audio up to a given time; after this call, no more data can be pushed
39  *  before the specified time.
40  */
41 pair<shared_ptr<AudioBuffers>, DCPTime>
42 AudioMerger::pull (DCPTime time)
43 {
44         /* Number of frames to return */
45         Frame const to_return = time.frames_floor (_frame_rate) - _last_pull.frames_floor (_frame_rate);
46         shared_ptr<AudioBuffers> out (new AudioBuffers (_buffers->channels(), to_return));
47
48         /* And this is how many we will get from our buffer */
49         Frame const to_return_from_buffers = min (to_return, Frame (_buffers->frames()));
50
51         /* Copy the data that we have to the back end of the return buffer */
52         out->copy_from (_buffers.get(), to_return_from_buffers, 0, to_return - to_return_from_buffers);
53         /* Silence any gap at the start */
54         out->make_silent (0, to_return - to_return_from_buffers);
55
56         DCPTime out_time = _last_pull;
57         _last_pull = time;
58
59         /* And remove the data we're returning from our buffers */
60         if (_buffers->frames() > to_return_from_buffers) {
61                 _buffers->move (to_return_from_buffers, 0, _buffers->frames() - to_return_from_buffers);
62         }
63         _buffers->set_frames (_buffers->frames() - to_return_from_buffers);
64
65         return make_pair (out, out_time);
66 }
67
68 void
69 AudioMerger::push (boost::shared_ptr<const AudioBuffers> audio, DCPTime time)
70 {
71         DCPOMATIC_ASSERT (time >= _last_pull);
72
73         Frame const frame = time.frames_floor (_frame_rate);
74         Frame after = max (Frame (_buffers->frames()), frame + audio->frames() - _last_pull.frames_floor (_frame_rate));
75         _buffers->ensure_size (after);
76         _buffers->accumulate_frames (audio.get(), 0, frame - _last_pull.frames_floor (_frame_rate), audio->frames ());
77         _buffers->set_frames (after);
78 }