2 Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/audio_merger.cc
21 * @brief AudioMerger class.
24 #include "audio_merger.h"
25 #include "dcpomatic_time.h"
34 using boost::shared_ptr;
35 using boost::optional;
37 AudioMerger::AudioMerger (int frame_rate)
38 : _frame_rate (frame_rate)
44 AudioMerger::frames (DCPTime t) const
46 return t.frames_floor (_frame_rate);
49 /** Pull audio up to a given time; after this call, no more data can be pushed
50 * before the specified time.
51 * @param time Time to pull up to.
52 * @return Blocks of merged audio up to `time'.
54 list<pair<shared_ptr<AudioBuffers>, DCPTime> >
55 AudioMerger::pull (DCPTime time)
57 list<pair<shared_ptr<AudioBuffers>, DCPTime> > out;
59 list<Buffer> new_buffers;
61 _buffers.sort (AudioMerger::BufferComparator());
62 BOOST_FOREACH (Buffer i, _buffers) {
63 if (i.period().to <= time) {
64 /* Completely within the pull period */
65 DCPOMATIC_ASSERT (i.audio->frames() > 0);
66 out.push_back (make_pair (i.audio, i.time));
67 } else if (i.time < time) {
68 /* Overlaps the end of the pull period */
69 shared_ptr<AudioBuffers> audio (new AudioBuffers (i.audio->channels(), frames(DCPTime(time - i.time))));
70 /* Though time > i.time, audio->frames() could be 0 if the difference in time is less than one frame */
71 if (audio->frames() > 0) {
72 audio->copy_from (i.audio.get(), audio->frames(), 0, 0);
73 out.push_back (make_pair (audio, i.time));
74 i.audio->trim_start (audio->frames ());
75 i.time += DCPTime::from_frames(audio->frames(), _frame_rate);
76 DCPOMATIC_ASSERT (i.audio->frames() > 0);
77 new_buffers.push_back (i);
81 DCPOMATIC_ASSERT (i.audio->frames() > 0);
82 new_buffers.push_back (i);
86 _buffers = new_buffers;
88 for (list<pair<shared_ptr<AudioBuffers>, DCPTime> >::const_iterator i = out.begin(); i != out.end(); ++i) {
89 DCPOMATIC_ASSERT (i->first->frames() > 0);
95 /** Push some data into the merger at a given time */
97 AudioMerger::push (boost::shared_ptr<const AudioBuffers> audio, DCPTime time)
99 DCPOMATIC_ASSERT (audio->frames() > 0);
101 DCPTimePeriod period (time, time + DCPTime::from_frames (audio->frames(), _frame_rate));
103 /* Mix any overlapping parts of this new block with existing ones */
104 BOOST_FOREACH (Buffer i, _buffers) {
105 optional<DCPTimePeriod> overlap = i.period().overlap (period);
107 int32_t const offset = frames(DCPTime(overlap->from - i.time));
108 int32_t const frames_to_mix = frames(overlap->duration());
110 i.audio->accumulate_frames(audio.get(), frames_to_mix, 0, offset);
112 i.audio->accumulate_frames(audio.get(), frames_to_mix, offset, 0);
117 list<DCPTimePeriod> periods;
118 BOOST_FOREACH (Buffer i, _buffers) {
119 periods.push_back (i.period ());
122 /* Add the non-overlapping parts */
123 BOOST_FOREACH (DCPTimePeriod i, subtract (period, periods)) {
124 list<Buffer>::iterator before = _buffers.end();
125 list<Buffer>::iterator after = _buffers.end();
126 for (list<Buffer>::iterator j = _buffers.begin(); j != _buffers.end(); ++j) {
127 if (j->period().to == i.from) {
130 if (j->period().from == i.to) {
135 /* Get the part of audio that we want to use */
136 shared_ptr<AudioBuffers> part (new AudioBuffers (audio->channels(), frames(i.to) - frames(i.from)));
137 part->copy_from (audio.get(), part->frames(), frames(DCPTime(i.from - time)), 0);
139 if (before == _buffers.end() && after == _buffers.end()) {
141 DCPOMATIC_ASSERT (part->frames() > 0);
142 _buffers.push_back (Buffer (part, time, _frame_rate));
143 } else if (before != _buffers.end() && after == _buffers.end()) {
144 /* We have an existing buffer before this one; append new data to it */
145 before->audio->append (part);
146 } else if (before ==_buffers.end() && after != _buffers.end()) {
147 /* We have an existing buffer after this one; append it to the new data and replace */
148 part->append (after->audio);
152 /* We have existing buffers both before and after; coalesce them all */
153 before->audio->append (part);
154 before->audio->append (after->audio);
155 _buffers.erase (after);
161 AudioMerger::clear ()