+
+
+void
+AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
+{
+ pos = pos.round (film->audio_frame_rate());
+}
+
+
+void
+AudioContent::modify_trim_start (ContentTime& trim) const
+{
+ DCPOMATIC_ASSERT (!_streams.empty());
+ /* 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<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;
+}
+