BOOST_FOREACH.
[dcpomatic.git] / src / lib / audio_merger.cc
1 /*
2     Copyright (C) 2013-2017 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 /** @file  src/audio_merger.cc
21  *  @brief AudioMerger class.
22  */
23
24 #include "audio_merger.h"
25 #include "dcpomatic_time.h"
26 #include <iostream>
27
28 using std::pair;
29 using std::min;
30 using std::max;
31 using std::list;
32 using std::cout;
33 using std::make_pair;
34 using std::shared_ptr;
35 using boost::optional;
36 using namespace dcpomatic;
37
38 AudioMerger::AudioMerger (int frame_rate)
39         : _frame_rate (frame_rate)
40 {
41
42 }
43
44 Frame
45 AudioMerger::frames (DCPTime t) const
46 {
47         return t.frames_floor (_frame_rate);
48 }
49
50 /** Pull audio up to a given time; after this call, no more data can be pushed
51  *  before the specified time.
52  *  @param time Time to pull up to.
53  *  @return Blocks of merged audio up to `time'.
54  */
55 list<pair<shared_ptr<AudioBuffers>, DCPTime> >
56 AudioMerger::pull (DCPTime time)
57 {
58         list<pair<shared_ptr<AudioBuffers>, DCPTime> > out;
59
60         list<Buffer> new_buffers;
61
62         _buffers.sort (AudioMerger::BufferComparator());
63         for (auto i: _buffers) {
64                 if (i.period().to <= time) {
65                         /* Completely within the pull period */
66                         DCPOMATIC_ASSERT (i.audio->frames() > 0);
67                         out.push_back (make_pair (i.audio, i.time));
68                 } else if (i.time < time) {
69                         /* Overlaps the end of the pull period */
70                         int32_t const overlap = frames(DCPTime(time - i.time));
71                         /* Though time > i.time, overlap could be 0 if the difference in time is less than one frame */
72                         if (overlap > 0) {
73                                 shared_ptr<AudioBuffers> audio(new AudioBuffers(i.audio, overlap, 0));
74                                 out.push_back (make_pair(audio, i.time));
75                                 i.audio->trim_start (overlap);
76                                 i.time += DCPTime::from_frames(overlap, _frame_rate);
77                                 DCPOMATIC_ASSERT (i.audio->frames() > 0);
78                                 new_buffers.push_back (i);
79                         }
80                 } else {
81                         /* Not involved */
82                         DCPOMATIC_ASSERT (i.audio->frames() > 0);
83                         new_buffers.push_back (i);
84                 }
85         }
86
87         _buffers = new_buffers;
88
89         for (list<pair<shared_ptr<AudioBuffers>, DCPTime> >::const_iterator i = out.begin(); i != out.end(); ++i) {
90                 DCPOMATIC_ASSERT (i->first->frames() > 0);
91         }
92
93         return out;
94 }
95
96 /** Push some data into the merger at a given time */
97 void
98 AudioMerger::push (std::shared_ptr<const AudioBuffers> audio, DCPTime time)
99 {
100         DCPOMATIC_ASSERT (audio->frames() > 0);
101
102         DCPTimePeriod period (time, time + DCPTime::from_frames (audio->frames(), _frame_rate));
103
104         /* Mix any overlapping parts of this new block with existing ones */
105         for (auto i: _buffers) {
106                 optional<DCPTimePeriod> overlap = i.period().overlap (period);
107                 if (overlap) {
108                         int32_t const offset = frames(DCPTime(overlap->from - i.time));
109                         int32_t const frames_to_mix = frames(overlap->duration());
110                         if (i.time < time) {
111                                 i.audio->accumulate_frames(audio.get(), frames_to_mix, 0, offset);
112                         } else {
113                                 i.audio->accumulate_frames(audio.get(), frames_to_mix, offset, 0);
114                         }
115                 }
116         }
117
118         list<DCPTimePeriod> periods;
119         for (auto i: _buffers) {
120                 periods.push_back (i.period ());
121         }
122
123         /* Add the non-overlapping parts */
124         for (auto i: subtract(period, periods)) {
125                 list<Buffer>::iterator before = _buffers.end();
126                 list<Buffer>::iterator after = _buffers.end();
127                 for (list<Buffer>::iterator j = _buffers.begin(); j != _buffers.end(); ++j) {
128                         if (j->period().to == i.from) {
129                                 before = j;
130                         }
131                         if (j->period().from == i.to) {
132                                 after = j;
133                         }
134                 }
135
136                 /* Get the part of audio that we want to use */
137                 shared_ptr<AudioBuffers> part (new AudioBuffers(audio, frames(i.to) - frames(i.from), frames(DCPTime(i.from - time))));
138
139                 if (before == _buffers.end() && after == _buffers.end()) {
140                         if (part->frames() > 0) {
141                                 /* New buffer */
142                                 _buffers.push_back (Buffer (part, time, _frame_rate));
143                         }
144                 } else if (before != _buffers.end() && after == _buffers.end()) {
145                         /* We have an existing buffer before this one; append new data to it */
146                         before->audio->append (part);
147                 } else if (before ==_buffers.end() && after != _buffers.end()) {
148                         /* We have an existing buffer after this one; append it to the new data and replace */
149                         part->append (after->audio);
150                         after->audio = part;
151                         after->time = time;
152                 } else {
153                         /* We have existing buffers both before and after; coalesce them all */
154                         before->audio->append (part);
155                         before->audio->append (after->audio);
156                         _buffers.erase (after);
157                 }
158         }
159 }
160
161 void
162 AudioMerger::clear ()
163 {
164         _buffers.clear ();
165 }