Merge master.
[dcpomatic.git] / src / lib / audio_merger.h
1 /*
2     Copyright (C) 2013 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 "util.h"
22
23 template <class T, class F>
24 class AudioMerger
25 {
26 public:
27         AudioMerger (int channels, boost::function<F (T)> t_to_f, boost::function<T (F)> f_to_t)
28                 : _buffers (new AudioBuffers (channels, 0))
29                 , _last_pull (0)
30                 , _t_to_f (t_to_f)
31                 , _f_to_t (f_to_t)
32         {}
33
34         /** Pull audio up to a given time; after this call, no more data can be pushed
35          *  before the specified time.
36          */
37         TimedAudioBuffers<T>
38         pull (T time)
39         {
40                 assert (time >= _last_pull);
41                 
42                 TimedAudioBuffers<T> out;
43                 
44                 F const to_return = _t_to_f (time - _last_pull);
45                 out.audio.reset (new AudioBuffers (_buffers->channels(), to_return));
46                 /* And this is how many we will get from our buffer */
47                 F const to_return_from_buffers = min (to_return, _buffers->frames ());
48                 
49                 /* Copy the data that we have to the back end of the return buffer */
50                 out.audio->copy_from (_buffers.get(), to_return_from_buffers, 0, to_return - to_return_from_buffers);
51                 /* Silence any gap at the start */
52                 out.audio->make_silent (0, to_return - to_return_from_buffers);
53                 
54                 out.time = _last_pull;
55                 _last_pull = time;
56                 
57                 /* And remove the data we're returning from our buffers */
58                 if (_buffers->frames() > to_return_from_buffers) {
59                         _buffers->move (to_return_from_buffers, 0, _buffers->frames() - to_return_from_buffers);
60                 }
61                 _buffers->set_frames (_buffers->frames() - to_return_from_buffers);
62
63                 return out;
64         }
65
66         void
67         push (boost::shared_ptr<const AudioBuffers> audio, T time)
68         {
69                 assert (time >= _last_pull);
70
71                 F frame = _t_to_f (time);
72                 F after = max (_buffers->frames(), frame + audio->frames() - _t_to_f (_last_pull));
73                 _buffers->ensure_size (after);
74                 _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_last_pull), audio->frames ());
75                 _buffers->set_frames (after);
76         }
77
78         F min (F a, int b)
79         {
80                 if (a < b) {
81                         return a;
82                 }
83
84                 return b;
85         }
86
87         F max (int a, F b)
88         {
89                 if (a > b) {
90                         return a;
91                 }
92
93                 return b;
94         }
95                 
96         TimedAudioBuffers<T>
97         flush ()
98         {
99                 if (_buffers->frames() == 0) {
100                         return TimedAudioBuffers<T> ();
101                 }
102
103                 return TimedAudioBuffers<T> (_buffers, _last_pull);
104         }
105
106         void
107         clear (DCPTime t)
108         {
109                 _last_pull = t;
110                 _buffers.reset (new AudioBuffers (_buffers->channels(), 0));
111         }
112         
113 private:
114         boost::shared_ptr<AudioBuffers> _buffers;
115         T _last_pull;
116         boost::function<F (T)> _t_to_f;
117         boost::function<T (F)> _f_to_t;
118 };