Make a new AudioBuffers constructor and use it to avoid some use of the more complica...
authorCarl Hetherington <cth@carlh.net>
Sun, 16 Feb 2020 21:23:28 +0000 (22:23 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 16 Feb 2020 21:23:28 +0000 (22:23 +0100)
src/lib/audio_buffers.cc
src/lib/audio_buffers.h
src/lib/audio_merger.cc
src/lib/player.cc
src/lib/writer.cc

index 63204dd..cceb126 100644 (file)
@@ -52,6 +52,12 @@ AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
        copy_from (other.get(), other->_frames, 0, 0);
 }
 
+AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other, int32_t frames_to_copy, int32_t read_offset)
+{
+       allocate (other->_channels, frames_to_copy);
+       copy_from (other.get(), frames_to_copy, read_offset, 0);
+}
+
 AudioBuffers &
 AudioBuffers::operator= (AudioBuffers const & other)
 {
index 64ad4ba..63e4fac 100644 (file)
@@ -41,6 +41,7 @@ public:
        AudioBuffers (int channels, int32_t frames);
        AudioBuffers (AudioBuffers const &);
        explicit AudioBuffers (boost::shared_ptr<const AudioBuffers>);
+       AudioBuffers (boost::shared_ptr<const AudioBuffers> other, int32_t frames_to_copy, int32_t read_offset);
        ~AudioBuffers ();
 
        AudioBuffers & operator= (AudioBuffers const &);
index 667068f..74bb771 100644 (file)
@@ -67,13 +67,13 @@ AudioMerger::pull (DCPTime time)
                        out.push_back (make_pair (i.audio, i.time));
                } else if (i.time < time) {
                        /* Overlaps the end of the pull period */
-                       shared_ptr<AudioBuffers> audio (new AudioBuffers (i.audio->channels(), frames(DCPTime(time - i.time))));
-                       /* Though time > i.time, audio->frames() could be 0 if the difference in time is less than one frame */
-                       if (audio->frames() > 0) {
-                               audio->copy_from (i.audio.get(), audio->frames(), 0, 0);
-                               out.push_back (make_pair (audio, i.time));
-                               i.audio->trim_start (audio->frames ());
-                               i.time += DCPTime::from_frames(audio->frames(), _frame_rate);
+                       int32_t const overlap = frames(DCPTime(time - i.time));
+                       /* Though time > i.time, overlap could be 0 if the difference in time is less than one frame */
+                       if (overlap > 0) {
+                               shared_ptr<AudioBuffers> audio(new AudioBuffers(i.audio, overlap, 0));
+                               out.push_back (make_pair(audio, i.time));
+                               i.audio->trim_start (overlap);
+                               i.time += DCPTime::from_frames(overlap, _frame_rate);
                                DCPOMATIC_ASSERT (i.audio->frames() > 0);
                                new_buffers.push_back (i);
                        }
@@ -134,8 +134,7 @@ AudioMerger::push (boost::shared_ptr<const AudioBuffers> audio, DCPTime time)
                }
 
                /* Get the part of audio that we want to use */
-               shared_ptr<AudioBuffers> part (new AudioBuffers (audio->channels(), frames(i.to) - frames(i.from)));
-               part->copy_from (audio.get(), part->frames(), frames(DCPTime(i.from - time)), 0);
+               shared_ptr<AudioBuffers> part (new AudioBuffers(audio, frames(i.to) - frames(i.from), frames(DCPTime(i.from - time))));
 
                if (before == _buffers.end() && after == _buffers.end()) {
                        if (part->frames() > 0) {
index bc0aa3b..304f8c7 100644 (file)
@@ -900,9 +900,7 @@ Player::audio (weak_ptr<Piece> wp, AudioStreamPtr stream, ContentAudio content_a
                if (remaining_frames == 0) {
                        return;
                }
-               shared_ptr<AudioBuffers> cut (new AudioBuffers (content_audio.audio->channels(), remaining_frames));
-               cut->copy_from (content_audio.audio.get(), remaining_frames, 0, 0);
-               content_audio.audio = cut;
+               content_audio.audio.reset (new AudioBuffers(content_audio.audio, remaining_frames, 0));
        }
 
        DCPOMATIC_ASSERT (content_audio.audio->frames() > 0);
@@ -1195,8 +1193,7 @@ Player::discard_audio (shared_ptr<const AudioBuffers> audio, DCPTime time, DCPTi
        if (remaining_frames <= 0) {
                return make_pair(shared_ptr<AudioBuffers>(), DCPTime());
        }
-       shared_ptr<AudioBuffers> cut (new AudioBuffers (audio->channels(), remaining_frames));
-       cut->copy_from (audio.get(), remaining_frames, discard_frames, 0);
+       shared_ptr<AudioBuffers> cut (new AudioBuffers(audio, remaining_frames, discard_frames));
        return make_pair(cut, time + discard_time);
 }
 
index d85db56..c1b66cd 100644 (file)
@@ -284,15 +284,12 @@ Writer::write (shared_ptr<const AudioBuffers> audio, DCPTime const time)
                        };
 
                        if (part_frames[0]) {
-                               shared_ptr<AudioBuffers> part (new AudioBuffers (audio->channels(), part_frames[0]));
-                               part->copy_from (audio.get(), part_frames[0], 0, 0);
+                               shared_ptr<AudioBuffers> part (new AudioBuffers(audio, part_frames[0], 0));
                                _audio_reel->write (part);
                        }
 
                        if (part_frames[1]) {
-                               shared_ptr<AudioBuffers> part (new AudioBuffers (audio->channels(), part_frames[1]));
-                               part->copy_from (audio.get(), part_frames[1], part_frames[0], 0);
-                               audio = part;
+                               audio.reset (new AudioBuffers(audio, part_frames[1], part_frames[0]));
                        } else {
                                audio.reset ();
                        }