Fix crashes when using templates in some cases (#2491).
[dcpomatic.git] / src / lib / audio_content.cc
index 32e713ff2eda9cde84caa7260a969b6b20137351..7083de714dcf411946c65598cdaf07d23e52ee32 100644 (file)
 #include "audio_content.h"
 #include "compose.hpp"
 #include "config.h"
+#include "constants.h"
 #include "exceptions.h"
 #include "film.h"
 #include "frame_rate_change.h"
 #include "maths_util.h"
+#include "video_content.h"
 #include <dcp/raw_convert.h>
 #include <libcxml/cxml.h>
 #include <libxml++/libxml++.h>
@@ -53,6 +55,9 @@ using namespace dcpomatic;
 int const AudioContentProperty::STREAMS = 200;
 int const AudioContentProperty::GAIN = 201;
 int const AudioContentProperty::DELAY = 202;
+int const AudioContentProperty::FADE_IN = 203;
+int const AudioContentProperty::FADE_OUT = 204;
+int const AudioContentProperty::USE_SAME_FADES_AS_VIDEO = 205;
 
 
 AudioContent::AudioContent (Content* parent)
@@ -90,12 +95,9 @@ AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
 {
        _gain = node->number_child<double> ("AudioGain");
        _delay = node->number_child<int> ("AudioDelay");
-
-       /* Backwards compatibility */
-       auto r = node->optional_number_child<double>("AudioVideoFrameRate");
-       if (r) {
-               _parent->set_video_frame_rate (r.get());
-       }
+       _fade_in = ContentTime(node->optional_number_child<ContentTime::Type>("AudioFadeIn").get_value_or(0));
+       _fade_out = ContentTime(node->optional_number_child<ContentTime::Type>("AudioFadeOut").get_value_or(0));
+       _use_same_fades_as_video = node->optional_bool_child("AudioUseSameFadesAsVideo").get_value_or(false);
 }
 
 
@@ -127,6 +129,9 @@ AudioContent::as_xml (xmlpp::Node* node) const
        boost::mutex::scoped_lock lm (_mutex);
        node->add_child("AudioGain")->add_child_text(raw_convert<string>(_gain));
        node->add_child("AudioDelay")->add_child_text(raw_convert<string>(_delay));
+       node->add_child("AudioFadeIn")->add_child_text(raw_convert<string>(_fade_in.get()));
+       node->add_child("AudioFadeOut")->add_child_text(raw_convert<string>(_fade_out.get()));
+       node->add_child("AudioUseSameFadesAsVideo")->add_child_text(_use_same_fades_as_video ? "1" : "0");
 }
 
 
@@ -308,7 +313,10 @@ AudioContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p
 
        if (stream) {
                p.push_back (UserProperty(UserProperty::AUDIO, _("Channels"), stream->channels()));
-               p.push_back (UserProperty(UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
+               p.push_back (UserProperty(UserProperty::AUDIO, _("Content sample rate"), stream->frame_rate(), _("Hz")));
+               if (auto bits = stream->bit_depth()) {
+                       p.push_back(UserProperty(UserProperty::AUDIO, _("Content bit depth"), *bits, _("bits")));
+               }
        }
 
        FrameRateChange const frc (_parent->active_video_frame_rate(film), film->video_frame_rate());
@@ -394,12 +402,16 @@ AudioContent::take_settings_from (shared_ptr<const AudioContent> c)
 {
        set_gain (c->_gain);
        set_delay (c->_delay);
+       set_fade_in (c->fade_in());
+       set_fade_out (c->fade_out());
 
        size_t i = 0;
        size_t j = 0;
 
        while (i < _streams.size() && j < c->_streams.size()) {
-               _streams[i]->set_mapping (c->_streams[j]->mapping());
+               auto mapping = _streams[i]->mapping();
+               mapping.take_from(c->_streams[j]->mapping());
+               _streams[i]->set_mapping(mapping);
                ++i;
                ++j;
        }
@@ -414,9 +426,96 @@ AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
 
 
 void
-AudioContent::modify_trim_start (ContentTime& trim) const
+AudioContent::modify_trim_start(shared_ptr<const Film> film, ContentTime& trim) const
+{
+       /* When this trim is used it the audio will have been resampled, and using the
+        * DCP rate here reduces the chance of rounding errors causing audio glitches
+        * due to errors in placement of audio frames (#2373).
+        */
+       trim = trim.round(film ? film->audio_frame_rate() : 48000);
+}
+
+
+ContentTime
+AudioContent::fade_in () const
 {
-       DCPOMATIC_ASSERT (!_streams.empty());
-       /* XXX: we're in trouble if streams have different rates */
-       trim = trim.round (_streams.front()->frame_rate());
+       boost::mutex::scoped_lock lm (_mutex);
+       if (_use_same_fades_as_video && _parent->video) {
+               return dcpomatic::ContentTime::from_frames(_parent->video->fade_in(), _parent->video_frame_rate().get_value_or(24));
+       }
+
+       return _fade_in;
 }
+
+
+ContentTime
+AudioContent::fade_out () const
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       if (_use_same_fades_as_video && _parent->video) {
+               return dcpomatic::ContentTime::from_frames(_parent->video->fade_out(), _parent->video_frame_rate().get_value_or(24));
+       }
+
+       return _fade_out;
+}
+
+
+void
+AudioContent::set_fade_in (ContentTime t)
+{
+       maybe_set (_fade_in, t, AudioContentProperty::FADE_IN);
+}
+
+
+void
+AudioContent::set_fade_out (ContentTime t)
+{
+       maybe_set (_fade_out, t, AudioContentProperty::FADE_OUT);
+}
+
+
+void
+AudioContent::set_use_same_fades_as_video (bool s)
+{
+       maybe_set (_use_same_fades_as_video, s, AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
+}
+
+
+vector<float>
+AudioContent::fade (AudioStreamPtr stream, Frame frame, Frame length, int frame_rate) const
+{
+       auto const in = fade_in().frames_round(frame_rate);
+       auto const out = fade_out().frames_round(frame_rate);
+
+       /* Where the start trim ends, at frame_rate */
+       auto const trim_start = _parent->trim_start().frames_round(frame_rate);
+       /* Where the end trim starts within the whole length of the content, at frame_rate */
+       auto const trim_end = ContentTime(ContentTime::from_frames(stream->length(), stream->frame_rate()) - _parent->trim_end()).frames_round(frame_rate);
+
+       if (
+               (in == 0  || (frame >= (trim_start + in))) &&
+               (out == 0 || ((frame + length) < (trim_end - out)))
+          ) {
+               /* This section starts after the fade in and ends before the fade out */
+               return {};
+       }
+
+       /* Start position relative to the start of the fade in */
+       auto in_start = frame - trim_start;
+       /* Start position relative to the start of the fade out */
+       auto out_start = frame - (trim_end - out);
+
+       vector<float> coeffs(length);
+       for (auto coeff = 0; coeff < length; ++coeff) {
+               coeffs[coeff] = 1.0;
+               if (in) {
+                       coeffs[coeff] *= logarithmic_fade_in_curve(static_cast<float>(in_start + coeff) / in);
+               }
+               if (out) {
+                       coeffs[coeff] *= logarithmic_fade_out_curve(static_cast<float>(out_start + coeff) / out);
+               }
+       }
+
+       return coeffs;
+}
+