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