2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "audio_content.h"
23 #include "compose.hpp"
25 #include "exceptions.h"
27 #include "frame_rate_change.h"
28 #include "maths_util.h"
29 #include <dcp/raw_convert.h>
30 #include <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
38 using std::dynamic_pointer_cast;
41 using std::make_shared;
43 using std::setprecision;
44 using std::shared_ptr;
47 using boost::optional;
48 using dcp::raw_convert;
49 using namespace dcpomatic;
52 /** Something stream-related has changed */
53 int const AudioContentProperty::STREAMS = 200;
54 int const AudioContentProperty::GAIN = 201;
55 int const AudioContentProperty::DELAY = 202;
58 AudioContent::AudioContent (Content* parent)
59 : ContentPart (parent)
60 , _delay (Config::instance()->default_audio_delay())
66 shared_ptr<AudioContent>
67 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
70 /* With old metadata FFmpeg content has the audio-related tags even with no
71 audio streams, so check for that.
73 if (node->string_child("Type") == "FFmpeg" && node->node_children("AudioStream").empty()) {
77 /* Otherwise we can drop through to the newer logic */
80 if (!node->optional_number_child<double> ("AudioGain")) {
84 return make_shared<AudioContent>(parent, node);
88 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
89 : ContentPart (parent)
91 _gain = node->number_child<double> ("AudioGain");
92 _delay = node->number_child<int> ("AudioDelay");
94 /* Backwards compatibility */
95 auto r = node->optional_number_child<double>("AudioVideoFrameRate");
97 _parent->set_video_frame_rate (r.get());
102 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content>> c)
103 : ContentPart (parent)
105 auto ref = c[0]->audio;
106 DCPOMATIC_ASSERT (ref);
108 for (size_t i = 1; i < c.size(); ++i) {
109 if (c[i]->audio->gain() != ref->gain()) {
110 throw JoinError (_("Content to be joined must have the same audio gain."));
113 if (c[i]->audio->delay() != ref->delay()) {
114 throw JoinError (_("Content to be joined must have the same audio delay."));
118 _gain = ref->gain ();
119 _delay = ref->delay ();
120 _streams = ref->streams ();
125 AudioContent::as_xml (xmlpp::Node* node) const
127 boost::mutex::scoped_lock lm (_mutex);
128 node->add_child("AudioGain")->add_child_text(raw_convert<string>(_gain));
129 node->add_child("AudioDelay")->add_child_text(raw_convert<string>(_delay));
134 AudioContent::set_gain (double g)
136 maybe_set (_gain, g, AudioContentProperty::GAIN);
141 AudioContent::set_delay (int d)
143 maybe_set (_delay, d, AudioContentProperty::DELAY);
148 AudioContent::technical_summary () const
150 string s = "audio: ";
151 for (auto i: streams()) {
152 s += String::compose ("stream channels %1 rate %2 ", i->channels(), i->frame_rate());
160 AudioContent::set_mapping (AudioMapping mapping)
162 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
165 for (auto i: streams()) {
166 AudioMapping stream_mapping (i->channels(), MAX_DCP_AUDIO_CHANNELS);
167 for (int j = 0; j < i->channels(); ++j) {
168 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
169 stream_mapping.set (j, k, mapping.get(c, k));
173 i->set_mapping (stream_mapping);
179 AudioContent::mapping () const
182 for (auto i: streams()) {
183 channels += i->channels ();
186 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
191 for (auto i: streams()) {
192 auto mapping = i->mapping ();
193 for (int j = 0; j < mapping.input_channels(); ++j) {
194 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
195 if (k < mapping.output_channels()) {
196 merged.set (c, k, mapping.get(j, k));
208 /** @return the frame rate that this content should be resampled to in order
209 * that it is in sync with the active video content at its start time.
212 AudioContent::resampled_frame_rate (shared_ptr<const Film> film) const
214 double t = film->audio_frame_rate ();
216 FrameRateChange frc (film, _parent);
218 /* Compensate if the DCP is being run at a different frame rate
219 to the source; that is, if the video is run such that it will
220 look different in the DCP compared to the source (slower or faster).
223 if (frc.change_speed) {
231 AudioContent::processing_description (shared_ptr<const Film> film) const
233 if (streams().empty()) {
237 /* Possible answers are:
238 1. all audio will be resampled from x to y.
239 2. all audio will be resampled to y (from a variety of rates)
240 3. some audio will be resampled to y (from a variety of rates)
241 4. nothing will be resampled.
244 bool not_resampled = false;
245 bool resampled = false;
248 optional<int> common_frame_rate;
249 for (auto i: streams()) {
250 if (i->frame_rate() != resampled_frame_rate(film)) {
253 not_resampled = true;
256 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
259 common_frame_rate = i->frame_rate ();
262 if (not_resampled && !resampled) {
263 return _("Audio will not be resampled");
266 if (not_resampled && resampled) {
267 return String::compose (_("Some audio will be resampled to %1Hz"), resampled_frame_rate(film));
270 if (!not_resampled && resampled) {
272 return String::compose (_("Audio will be resampled from %1Hz to %2Hz"), common_frame_rate.get(), resampled_frame_rate(film));
274 return String::compose (_("Audio will be resampled to %1Hz"), resampled_frame_rate(film));
282 /** @return User-visible names of each of our audio channels */
284 AudioContent::channel_names () const
286 vector<NamedChannel> n;
290 for (auto i: streams()) {
291 for (int j = 0; j < i->channels(); ++j) {
292 n.push_back (NamedChannel(String::compose ("%1:%2", stream, j + 1), index++));
302 AudioContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
304 shared_ptr<const AudioStream> stream;
305 if (streams().size() == 1) {
306 stream = streams().front();
310 p.push_back (UserProperty(UserProperty::AUDIO, _("Channels"), stream->channels()));
311 p.push_back (UserProperty(UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
314 FrameRateChange const frc (_parent->active_video_frame_rate(film), film->video_frame_rate());
315 ContentTime const c (_parent->full_length(film), frc);
318 UserProperty (UserProperty::LENGTH, _("Full length in video frames at content rate"), c.frames_round(frc.source))
324 UserProperty::LENGTH,
325 _("Full length in audio samples at content rate"),
326 c.frames_round (stream->frame_rate ())
331 p.push_back (UserProperty(UserProperty::AUDIO, _("DCP sample rate"), resampled_frame_rate(film), _("Hz")));
332 p.push_back (UserProperty(UserProperty::LENGTH, _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
337 UserProperty::LENGTH,
338 _("Full length in audio samples at DCP rate"),
339 c.frames_round(resampled_frame_rate(film))
347 AudioContent::set_streams (vector<AudioStreamPtr> streams)
349 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
352 boost::mutex::scoped_lock lm (_mutex);
359 AudioContent::stream () const
361 boost::mutex::scoped_lock lm (_mutex);
362 DCPOMATIC_ASSERT (_streams.size() == 1);
363 return _streams.front ();
368 AudioContent::add_stream (AudioStreamPtr stream)
370 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
373 boost::mutex::scoped_lock lm (_mutex);
374 _streams.push_back (stream);
380 AudioContent::set_stream (AudioStreamPtr stream)
382 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
385 boost::mutex::scoped_lock lm (_mutex);
387 _streams.push_back (stream);
393 AudioContent::take_settings_from (shared_ptr<const AudioContent> c)
396 set_delay (c->_delay);
401 while (i < _streams.size() && j < c->_streams.size()) {
402 _streams[i]->set_mapping (c->_streams[j]->mapping());
410 AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
412 pos = pos.round (film->audio_frame_rate());
417 AudioContent::modify_trim_start (ContentTime& trim) const
419 DCPOMATIC_ASSERT (!_streams.empty());
420 /* XXX: we're in trouble if streams have different rates */
421 trim = trim.round (_streams.front()->frame_rate());