WIP: more
[dcpomatic.git] / src / lib / audio_decoder.cc
index f7f147bd92e9b80f58cc46f5bd91c34fb551d6fd..f7eb550ddf2049fed3668191f10865f348061316 100644 (file)
 
 */
 
-#include "audio_decoder.h"
+
 #include "audio_buffers.h"
 #include "audio_content.h"
+#include "audio_decoder.h"
+#include "compose.hpp"
 #include "dcpomatic_log.h"
 #include "log.h"
-#include "resampler.h"
-#include "compose.hpp"
 #include <iostream>
 
 #include "i18n.h"
 
+
 using std::cout;
 using std::map;
 using std::pair;
@@ -37,10 +38,10 @@ using std::make_shared;
 using boost::optional;
 using namespace dcpomatic;
 
-AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, bool fast)
+
+AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content)
        : DecoderPart (parent)
        , _content (content)
-       , _fast (fast)
 {
        /* Set up _positions so that we have one for each stream */
        for (auto i: content->streams ()) {
@@ -48,9 +49,10 @@ AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> cont
        }
 }
 
+
 /** @param time_already_delayed true if the delay should not be added to time */
 void
-AudioDecoder::emit (shared_ptr<const Film> film, AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time, bool time_already_delayed)
+AudioDecoder::emit (AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time, bool time_already_delayed)
 {
        if (ignore ()) {
                return;
@@ -62,7 +64,7 @@ AudioDecoder::emit (shared_ptr<const Film> film, AudioStreamPtr stream, shared_p
         */
        static Frame const slack_frames = 48000 / 24;
 
-       int const resampled_rate = _content->resampled_frame_rate(film);
+       auto const frame_rate = stream->frame_rate();
        if (!time_already_delayed) {
                time += ContentTime::from_seconds (_content->delay() / 1000.0);
        }
@@ -81,62 +83,35 @@ AudioDecoder::emit (shared_ptr<const Film> film, AudioStreamPtr stream, shared_p
                        silence (_content->delay ());
                }
                reset = true;
-       } else if (std::abs(_positions[stream] - time.frames_round(resampled_rate)) > slack_frames) {
+       } else if (std::abs(_positions[stream] - time.frames_round(frame_rate)) > slack_frames) {
                reset = true;
                LOG_GENERAL (
                        "Reset audio position: was %1, new data at %2, slack: %3 frames",
                        _positions[stream],
-                       time.frames_round(resampled_rate),
-                       std::abs(_positions[stream] - time.frames_round(resampled_rate))
+                       time.frames_round(frame_rate),
+                       std::abs(_positions[stream] - time.frames_round(frame_rate))
                        );
        }
 
        if (reset) {
-               _positions[stream] = time.frames_round (resampled_rate);
-       }
-
-       shared_ptr<Resampler> resampler;
-       auto i = _resamplers.find(stream);
-       if (i != _resamplers.end ()) {
-               resampler = i->second;
-       } else {
-               if (stream->frame_rate() != resampled_rate) {
-                       LOG_GENERAL (
-                               "Creating new resampler from %1 to %2 with %3 channels",
-                               stream->frame_rate(),
-                               resampled_rate,
-                               stream->channels()
-                               );
-
-                       resampler = make_shared<Resampler>(stream->frame_rate(), resampled_rate, stream->channels());
-                       if (_fast) {
-                               resampler->set_fast ();
-                       }
-                       _resamplers[stream] = resampler;
-               }
-       }
-
-       if (resampler) {
-               auto ro = resampler->run (data);
-               if (ro->frames() == 0) {
-                       return;
-               }
-               data = ro;
+               _positions[stream] = time.frames_round (frame_rate);
        }
 
-       Data(stream, ContentAudio (data, _positions[stream]));
+       Data(stream, data, _positions[stream]);
        _positions[stream] += data->frames();
 }
 
+
 /** @return Time just after the last thing that was emitted from a given stream */
 ContentTime
 AudioDecoder::stream_position (shared_ptr<const Film> film, AudioStreamPtr stream) const
 {
        auto i = _positions.find (stream);
        DCPOMATIC_ASSERT (i != _positions.end ());
-       return ContentTime::from_frames (i->second, _content->resampled_frame_rate(film));
+       return ContentTime::from_frames (i->second, stream->frame_rate());
 }
 
+
 boost::optional<ContentTime>
 AudioDecoder::position (shared_ptr<const Film> film) const
 {
@@ -151,36 +126,26 @@ AudioDecoder::position (shared_ptr<const Film> film) const
        return p;
 }
 
+
 void
 AudioDecoder::seek ()
 {
-       for (auto i: _resamplers) {
-               i.second->flush ();
-               i.second->reset ();
-       }
-
        for (auto& i: _positions) {
                i.second = 0;
        }
 }
 
+
 void
 AudioDecoder::flush ()
 {
-       for (auto const& i: _resamplers) {
-               auto ro = i.second->flush ();
-               if (ro->frames() > 0) {
-                       Data (i.first, ContentAudio (ro, _positions[i.first]));
-                       _positions[i.first] += ro->frames();
-               }
-       }
-
        if (_content->delay() < 0) {
                /* Finish off with the gap caused by the delay */
                silence (-_content->delay ());
        }
 }
 
+
 void
 AudioDecoder::silence (int milliseconds)
 {
@@ -188,6 +153,6 @@ AudioDecoder::silence (int milliseconds)
                int const samples = ContentTime::from_seconds(milliseconds / 1000.0).frames_round(i->frame_rate());
                auto silence = make_shared<AudioBuffers>(i->channels(), samples);
                silence->make_silent ();
-               Data (i, ContentAudio (silence, _positions[i]));
+               Data (i, silence, _positions[i]);
        }
 }