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 <dcp/raw_convert.h>
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
37 using std::dynamic_pointer_cast;
40 using std::make_shared;
42 using std::setprecision;
43 using std::shared_ptr;
46 using boost::optional;
47 using dcp::raw_convert;
48 using namespace dcpomatic;
51 /** Something stream-related has changed */
52 int const AudioContentProperty::STREAMS = 200;
53 int const AudioContentProperty::GAIN = 201;
54 int const AudioContentProperty::DELAY = 202;
55 int const AudioContentProperty::LANGUAGE = 203;
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");
93 auto lang = node->optional_node_child ("Language");
95 _language = dcp::LanguageTag (lang->content());
98 /* Backwards compatibility */
99 auto r = node->optional_number_child<double>("AudioVideoFrameRate");
101 _parent->set_video_frame_rate (r.get());
106 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
107 : ContentPart (parent)
109 auto ref = c[0]->audio;
110 DCPOMATIC_ASSERT (ref);
112 for (size_t i = 1; i < c.size(); ++i) {
113 if (c[i]->audio->gain() != ref->gain()) {
114 throw JoinError (_("Content to be joined must have the same audio gain."));
117 if (c[i]->audio->delay() != ref->delay()) {
118 throw JoinError (_("Content to be joined must have the same audio delay."));
121 if (c[i]->audio->language() != ref->language()) {
122 throw JoinError (_("Content to be joined must have the same audio language."));
126 _gain = ref->gain ();
127 _delay = ref->delay ();
128 _streams = ref->streams ();
129 _language = ref->language ();
134 AudioContent::as_xml (xmlpp::Node* node) const
136 boost::mutex::scoped_lock lm (_mutex);
137 node->add_child("AudioGain")->add_child_text(raw_convert<string>(_gain));
138 node->add_child("AudioDelay")->add_child_text(raw_convert<string>(_delay));
140 node->add_child("Language")->add_child_text(_language->to_string());
146 AudioContent::set_gain (double g)
148 maybe_set (_gain, g, AudioContentProperty::GAIN);
153 AudioContent::set_delay (int d)
155 maybe_set (_delay, d, AudioContentProperty::DELAY);
160 AudioContent::set_language (optional<dcp::LanguageTag> language)
162 maybe_set (_language, language, AudioContentProperty::LANGUAGE);
167 AudioContent::technical_summary () const
169 string s = "audio: ";
170 for (auto i: streams()) {
171 s += String::compose ("stream channels %1 rate %2 ", i->channels(), i->frame_rate());
179 AudioContent::set_mapping (AudioMapping mapping)
181 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
184 for (auto i: streams()) {
185 AudioMapping stream_mapping (i->channels(), MAX_DCP_AUDIO_CHANNELS);
186 for (int j = 0; j < i->channels(); ++j) {
187 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
188 stream_mapping.set (j, k, mapping.get(c, k));
192 i->set_mapping (stream_mapping);
198 AudioContent::mapping () const
201 for (auto i: streams()) {
202 channels += i->channels ();
205 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
210 for (auto i: streams()) {
211 auto mapping = i->mapping ();
212 for (int j = 0; j < mapping.input_channels(); ++j) {
213 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
214 if (k < mapping.output_channels()) {
215 merged.set (c, k, mapping.get(j, k));
227 /** @return the frame rate that this content should be resampled to in order
228 * that it is in sync with the active video content at its start time.
231 AudioContent::resampled_frame_rate (shared_ptr<const Film> film) const
233 double t = film->audio_frame_rate ();
235 FrameRateChange frc (film, _parent);
237 /* Compensate if the DCP is being run at a different frame rate
238 to the source; that is, if the video is run such that it will
239 look different in the DCP compared to the source (slower or faster).
242 if (frc.change_speed) {
250 AudioContent::processing_description (shared_ptr<const Film> film) const
252 if (streams().empty()) {
256 /* Possible answers are:
257 1. all audio will be resampled from x to y.
258 2. all audio will be resampled to y (from a variety of rates)
259 3. some audio will be resampled to y (from a variety of rates)
260 4. nothing will be resampled.
263 bool not_resampled = false;
264 bool resampled = false;
267 optional<int> common_frame_rate;
268 for (auto i: streams()) {
269 if (i->frame_rate() != resampled_frame_rate(film)) {
272 not_resampled = true;
275 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
278 common_frame_rate = i->frame_rate ();
281 if (not_resampled && !resampled) {
282 return _("Audio will not be resampled");
285 if (not_resampled && resampled) {
286 return String::compose (_("Some audio will be resampled to %1Hz"), resampled_frame_rate(film));
289 if (!not_resampled && resampled) {
291 return String::compose (_("Audio will be resampled from %1Hz to %2Hz"), common_frame_rate.get(), resampled_frame_rate(film));
293 return String::compose (_("Audio will be resampled to %1Hz"), resampled_frame_rate(film));
301 /** @return User-visible names of each of our audio channels */
303 AudioContent::channel_names () const
305 vector<NamedChannel> n;
309 for (auto i: streams()) {
310 for (int j = 0; j < i->channels(); ++j) {
311 n.push_back (NamedChannel(String::compose ("%1:%2", stream, j + 1), index++));
321 AudioContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
323 shared_ptr<const AudioStream> stream;
324 if (streams().size() == 1) {
325 stream = streams().front();
329 p.push_back (UserProperty(UserProperty::AUDIO, _("Channels"), stream->channels()));
330 p.push_back (UserProperty(UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
333 FrameRateChange const frc (_parent->active_video_frame_rate(film), film->video_frame_rate());
334 ContentTime const c (_parent->full_length(film), frc);
337 UserProperty (UserProperty::LENGTH, _("Full length in video frames at content rate"), c.frames_round(frc.source))
343 UserProperty::LENGTH,
344 _("Full length in audio samples at content rate"),
345 c.frames_round (stream->frame_rate ())
350 p.push_back (UserProperty(UserProperty::AUDIO, _("DCP sample rate"), resampled_frame_rate(film), _("Hz")));
351 p.push_back (UserProperty(UserProperty::LENGTH, _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
356 UserProperty::LENGTH,
357 _("Full length in audio samples at DCP rate"),
358 c.frames_round(resampled_frame_rate(film))
366 AudioContent::set_streams (vector<AudioStreamPtr> streams)
368 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
371 boost::mutex::scoped_lock lm (_mutex);
378 AudioContent::stream () const
380 boost::mutex::scoped_lock lm (_mutex);
381 DCPOMATIC_ASSERT (_streams.size() == 1);
382 return _streams.front ();
387 AudioContent::add_stream (AudioStreamPtr stream)
389 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
392 boost::mutex::scoped_lock lm (_mutex);
393 _streams.push_back (stream);
399 AudioContent::set_stream (AudioStreamPtr stream)
401 ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
404 boost::mutex::scoped_lock lm (_mutex);
406 _streams.push_back (stream);
412 AudioContent::take_settings_from (shared_ptr<const AudioContent> c)
415 set_delay (c->_delay);
420 while (i < _streams.size() && j < c->_streams.size()) {
421 _streams[i]->set_mapping (c->_streams[j]->mapping());
429 AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
431 pos = pos.round (film->audio_frame_rate());
436 AudioContent::modify_trim_start (ContentTime& trim) const
438 DCPOMATIC_ASSERT (!_streams.empty());
439 /* XXX: we're in trouble if streams have different rates */
440 trim = trim.round (_streams.front()->frame_rate());