2 Copyright (C) 2013-2016 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/>.
21 #include "audio_content.h"
23 #include "exceptions.h"
25 #include "frame_rate_change.h"
26 #include "raw_convert.h"
27 #include "compose.hpp"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <boost/foreach.hpp>
38 using std::stringstream;
42 using std::setprecision;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::optional;
47 /** Something stream-related has changed */
48 int const AudioContentProperty::STREAMS = 200;
49 int const AudioContentProperty::GAIN = 201;
50 int const AudioContentProperty::DELAY = 202;
52 AudioContent::AudioContent (Content* parent)
53 : ContentPart (parent)
55 , _delay (Config::instance()->default_audio_delay ())
60 shared_ptr<AudioContent>
61 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
64 /* With old metadata FFmpeg content has the audio-related tags even with no
65 audio streams, so check for that.
67 if (node->string_child("Type") == "FFmpeg" && node->node_children("AudioStream").empty()) {
68 return shared_ptr<AudioContent> ();
71 /* Otherwise we can drop through to the newer logic */
74 if (!node->optional_number_child<double> ("AudioGain")) {
75 return shared_ptr<AudioContent> ();
78 /* Can't use make_shared here as the constructor is private */
79 return shared_ptr<AudioContent> (new AudioContent (parent, node));
82 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
83 : ContentPart (parent)
85 _gain = node->number_child<double> ("AudioGain");
86 _delay = node->number_child<int> ("AudioDelay");
88 /* Backwards compatibility */
89 optional<double> r = node->optional_number_child<double> ("AudioVideoFrameRate");
91 _parent->set_video_frame_rate (r.get ());
95 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
96 : ContentPart (parent)
98 shared_ptr<AudioContent> ref = c[0]->audio;
99 DCPOMATIC_ASSERT (ref);
101 for (size_t i = 1; i < c.size(); ++i) {
102 if (c[i]->audio->gain() != ref->gain()) {
103 throw JoinError (_("Content to be joined must have the same audio gain."));
106 if (c[i]->audio->delay() != ref->delay()) {
107 throw JoinError (_("Content to be joined must have the same audio delay."));
111 _gain = ref->gain ();
112 _delay = ref->delay ();
113 _streams = ref->streams ();
117 AudioContent::as_xml (xmlpp::Node* node) const
119 boost::mutex::scoped_lock lm (_mutex);
120 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_gain));
121 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_delay));
125 AudioContent::set_gain (double g)
127 maybe_set (_gain, g, AudioContentProperty::GAIN);
131 AudioContent::set_delay (int d)
133 maybe_set (_delay, d, AudioContentProperty::DELAY);
137 AudioContent::technical_summary () const
139 string s = "audio :";
140 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
141 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
148 AudioContent::set_mapping (AudioMapping mapping)
151 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
152 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
153 for (int j = 0; j < i->channels(); ++j) {
154 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
155 stream_mapping.set (j, k, mapping.get (c, k));
159 i->set_mapping (stream_mapping);
162 _parent->signal_changed (AudioContentProperty::STREAMS);
166 AudioContent::mapping () const
169 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
170 channels += i->channels ();
173 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
178 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
179 AudioMapping mapping = i->mapping ();
180 for (int j = 0; j < mapping.input_channels(); ++j) {
181 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
182 if (k < mapping.output_channels()) {
183 merged.set (c, k, mapping.get (j, k));
194 /** @return the frame rate that this content should be resampled to in order
195 * that it is in sync with the active video content at its start time.
198 AudioContent::resampled_frame_rate () const
200 /* Resample to a DCI-approved sample rate */
201 double t = has_rate_above_48k() ? 96000 : 48000;
203 FrameRateChange frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
205 /* Compensate if the DCP is being run at a different frame rate
206 to the source; that is, if the video is run such that it will
207 look different in the DCP compared to the source (slower or faster).
210 if (frc.change_speed) {
218 AudioContent::processing_description () const
220 if (streams().empty ()) {
224 /* Possible answers are:
225 1. all audio will be resampled from x to y.
226 2. all audio will be resampled to y (from a variety of rates)
227 3. some audio will be resampled to y (from a variety of rates)
228 4. nothing will be resampled.
231 bool not_resampled = false;
232 bool resampled = false;
235 optional<int> common_frame_rate;
236 BOOST_FOREACH (AudioStreamPtr i, streams()) {
237 if (i->frame_rate() != resampled_frame_rate()) {
240 not_resampled = true;
243 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
246 common_frame_rate = i->frame_rate ();
249 if (not_resampled && !resampled) {
250 return _("Audio will not be resampled");
253 if (not_resampled && resampled) {
254 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_frame_rate ());
257 if (!not_resampled && resampled) {
259 return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_frame_rate ());
261 return String::compose (_("Audio will be resampled to %1kHz"), resampled_frame_rate ());
268 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
270 AudioContent::has_rate_above_48k () const
272 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
273 if (i->frame_rate() > 48000) {
281 /** @return User-visible names of each of our audio channels */
283 AudioContent::channel_names () const
288 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
289 for (int j = 0; j < i->channels(); ++j) {
290 n.push_back (String::compose ("%1:%2", t, j + 1));
299 AudioContent::add_properties (list<UserProperty>& p) const
301 shared_ptr<const AudioStream> stream;
302 if (streams().size() == 1) {
303 stream = streams().front ();
307 p.push_back (UserProperty (UserProperty::AUDIO, _("Channels"), stream->channels ()));
308 p.push_back (UserProperty (UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
311 FrameRateChange const frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
312 ContentTime const c (_parent->full_length(), frc);
315 UserProperty (UserProperty::LENGTH, _("Full length in video frames at content rate"), c.frames_round(frc.source))
321 UserProperty::LENGTH,
322 _("Full length in audio samples at content rate"),
323 c.frames_round (stream->frame_rate ())
328 p.push_back (UserProperty (UserProperty::AUDIO, _("DCP frame rate"), resampled_frame_rate (), _("Hz")));
329 p.push_back (UserProperty (UserProperty::LENGTH, _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
334 UserProperty::LENGTH,
335 _("Full length in audio samples at DCP rate"),
336 c.frames_round (resampled_frame_rate ())
343 AudioContent::set_streams (vector<AudioStreamPtr> streams)
346 boost::mutex::scoped_lock lm (_mutex);
350 _parent->signal_changed (AudioContentProperty::STREAMS);
354 AudioContent::stream () const
356 boost::mutex::scoped_lock lm (_mutex);
357 DCPOMATIC_ASSERT (_streams.size() == 1);
358 return _streams.front ();
362 AudioContent::add_stream (AudioStreamPtr stream)
365 boost::mutex::scoped_lock lm (_mutex);
366 _streams.push_back (stream);
369 _parent->signal_changed (AudioContentProperty::STREAMS);
373 AudioContent::set_stream (AudioStreamPtr stream)
376 boost::mutex::scoped_lock lm (_mutex);
378 _streams.push_back (stream);
381 _parent->signal_changed (AudioContentProperty::STREAMS);