X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Faudio_content.cc;h=82a6a84d1d38d90c875371b9592d9f16d3d7305a;hp=748cbb7d0d6403b487ec540abd2ab2e8768b3d66;hb=597e27d830ebe88d5c06b3099ab5e429c97d0e3b;hpb=ddd767fd647b2fd585d75ada000d3d01c4c43cb2 diff --git a/src/lib/audio_content.cc b/src/lib/audio_content.cc index 748cbb7d0..82a6a84d1 100644 --- a/src/lib/audio_content.cc +++ b/src/lib/audio_content.cc @@ -25,6 +25,8 @@ #include "exceptions.h" #include "film.h" #include "frame_rate_change.h" +#include "maths_util.h" +#include "video_content.h" #include #include #include @@ -52,6 +54,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) @@ -89,6 +94,9 @@ AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node) { _gain = node->number_child ("AudioGain"); _delay = node->number_child ("AudioDelay"); + _fade_in = ContentTime(node->optional_number_child("AudioFadeIn").get_value_or(0)); + _fade_out = ContentTime(node->optional_number_child("AudioFadeOut").get_value_or(0)); + _use_same_fades_as_video = node->optional_bool_child("AudioUseSameFadesAsVideo").get_value_or(false); /* Backwards compatibility */ auto r = node->optional_number_child("AudioVideoFrameRate"); @@ -98,7 +106,7 @@ AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node) } -AudioContent::AudioContent (Content* parent, vector > c) +AudioContent::AudioContent (Content* parent, vector> c) : ContentPart (parent) { auto ref = c[0]->audio; @@ -126,6 +134,9 @@ AudioContent::as_xml (xmlpp::Node* node) const boost::mutex::scoped_lock lm (_mutex); node->add_child("AudioGain")->add_child_text(raw_convert(_gain)); node->add_child("AudioDelay")->add_child_text(raw_convert(_delay)); + node->add_child("AudioFadeIn")->add_child_text(raw_convert(_fade_in.get())); + node->add_child("AudioFadeOut")->add_child_text(raw_convert(_fade_out.get())); + node->add_child("AudioUseSameFadesAsVideo")->add_child_text(_use_same_fades_as_video ? "1" : "0"); } @@ -393,6 +404,8 @@ AudioContent::take_settings_from (shared_ptr 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; @@ -419,3 +432,88 @@ AudioContent::modify_trim_start (ContentTime& trim) const /* XXX: we're in trouble if streams have different rates */ trim = trim.round (_streams.front()->frame_rate()); } + + +ContentTime +AudioContent::fade_in () const +{ + 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 +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 coeffs(length); + for (auto coeff = 0; coeff < length; ++coeff) { + coeffs[coeff] = 1.0; + if (in) { + coeffs[coeff] *= logarithmic_fade_in_curve(static_cast(in_start + coeff) / in); + } + if (out) { + coeffs[coeff] *= logarithmic_fade_out_curve(static_cast(out_start + coeff) / out); + } + } + + return coeffs; +} +