Fix crashes when using templates in some cases (#2491).
[dcpomatic.git] / src / lib / audio_content.cc
index 05c388a3e711812dc3ad3ceef1c39d8b01002edd..7083de714dcf411946c65598cdaf07d23e52ee32 100644 (file)
@@ -22,6 +22,7 @@
 #include "audio_content.h"
 #include "compose.hpp"
 #include "config.h"
+#include "constants.h"
 #include "exceptions.h"
 #include "film.h"
 #include "frame_rate_change.h"
@@ -312,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());
@@ -405,7 +409,9 @@ AudioContent::take_settings_from (shared_ptr<const AudioContent> c)
        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;
        }
@@ -422,9 +428,11 @@ AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
 void
 AudioContent::modify_trim_start(shared_ptr<const Film> film, ContentTime& trim) const
 {
-       DCPOMATIC_ASSERT (!_streams.empty());
-       /* XXX: we're in trouble if streams have different rates */
-       trim = trim.round (_streams.front()->frame_rate());
+       /* 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);
 }