2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_content.h"
22 #include "exceptions.h"
24 #include "frame_rate_change.h"
25 #include "raw_convert.h"
26 #include "compose.hpp"
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
37 using std::stringstream;
41 using std::setprecision;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using boost::optional;
46 /** Something stream-related has changed */
47 int const AudioContentProperty::STREAMS = 200;
48 int const AudioContentProperty::GAIN = 201;
49 int const AudioContentProperty::DELAY = 202;
51 AudioContent::AudioContent (Content* parent)
52 : ContentPart (parent)
54 , _delay (Config::instance()->default_audio_delay ())
59 shared_ptr<AudioContent>
60 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node)
62 if (!node->optional_number_child<double> ("AudioGain")) {
63 return shared_ptr<AudioContent> ();
66 return shared_ptr<AudioContent> (new AudioContent (parent, node));
69 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
70 : ContentPart (parent)
72 _gain = node->number_child<double> ("AudioGain");
73 _delay = node->number_child<int> ("AudioDelay");
75 /* Backwards compatibility */
76 optional<double> r = node->optional_number_child<double> ("AudioVideoFrameRate");
78 _parent->set_video_frame_rate (r.get ());
82 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
83 : ContentPart (parent)
85 shared_ptr<AudioContent> ref = c[0]->audio;
86 DCPOMATIC_ASSERT (ref);
88 for (size_t i = 1; i < c.size(); ++i) {
89 if (c[i]->audio->gain() != ref->gain()) {
90 throw JoinError (_("Content to be joined must have the same audio gain."));
93 if (c[i]->audio->delay() != ref->delay()) {
94 throw JoinError (_("Content to be joined must have the same audio delay."));
99 _delay = ref->delay ();
100 _streams = ref->streams ();
104 AudioContent::as_xml (xmlpp::Node* node) const
106 boost::mutex::scoped_lock lm (_mutex);
107 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_gain));
108 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_delay));
112 AudioContent::set_gain (double g)
114 maybe_set (_gain, g, AudioContentProperty::GAIN);
118 AudioContent::set_delay (int d)
120 maybe_set (_delay, d, AudioContentProperty::DELAY);
124 AudioContent::technical_summary () const
126 string s = "audio :";
127 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
128 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
135 AudioContent::set_mapping (AudioMapping mapping)
138 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
139 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
140 for (int j = 0; j < i->channels(); ++j) {
141 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
142 stream_mapping.set (j, k, mapping.get (c, k));
146 i->set_mapping (stream_mapping);
149 _parent->signal_changed (AudioContentProperty::STREAMS);
153 AudioContent::mapping () const
156 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
157 channels += i->channels ();
160 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
165 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
166 AudioMapping mapping = i->mapping ();
167 for (int j = 0; j < mapping.input_channels(); ++j) {
168 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
169 if (k < mapping.output_channels()) {
170 merged.set (c, k, mapping.get (j, k));
181 /** @return the frame rate that this content should be resampled to in order
182 * that it is in sync with the active video content at its start time.
185 AudioContent::resampled_frame_rate () const
187 /* Resample to a DCI-approved sample rate */
188 double t = has_rate_above_48k() ? 96000 : 48000;
190 FrameRateChange frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
192 /* Compensate if the DCP is being run at a different frame rate
193 to the source; that is, if the video is run such that it will
194 look different in the DCP compared to the source (slower or faster).
197 if (frc.change_speed) {
205 AudioContent::processing_description () const
207 if (streams().empty ()) {
211 /* Possible answers are:
212 1. all audio will be resampled from x to y.
213 2. all audio will be resampled to y (from a variety of rates)
214 3. some audio will be resampled to y (from a variety of rates)
215 4. nothing will be resampled.
218 bool not_resampled = false;
219 bool resampled = false;
222 optional<int> common_frame_rate;
223 BOOST_FOREACH (AudioStreamPtr i, streams()) {
224 if (i->frame_rate() != resampled_frame_rate()) {
227 not_resampled = true;
230 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
233 common_frame_rate = i->frame_rate ();
236 if (not_resampled && !resampled) {
237 return _("Audio will not be resampled");
240 if (not_resampled && resampled) {
241 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_frame_rate ());
244 if (!not_resampled && resampled) {
246 return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_frame_rate ());
248 return String::compose (_("Audio will be resampled to %1kHz"), resampled_frame_rate ());
255 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
257 AudioContent::has_rate_above_48k () const
259 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
260 if (i->frame_rate() > 48000) {
268 /** @return User-visible names of each of our audio channels */
270 AudioContent::channel_names () const
275 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
276 for (int j = 0; j < i->channels(); ++j) {
277 n.push_back (String::compose ("%1:%2", t, j + 1));
286 AudioContent::add_properties (list<UserProperty>& p) const
288 shared_ptr<const AudioStream> stream;
289 if (streams().size() == 1) {
290 stream = streams().front ();
294 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
295 p.push_back (UserProperty (_("Audio"), _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
298 FrameRateChange const frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
299 ContentTime const c (_parent->full_length(), frc);
302 UserProperty (_("Length"), _("Full length in video frames at content rate"), c.frames_round(frc.source))
309 _("Full length in audio samples at content rate"),
310 c.frames_round (stream->frame_rate ())
315 p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_frame_rate (), _("Hz")));
316 p.push_back (UserProperty (_("Length"), _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
322 _("Full length in audio samples at DCP rate"),
323 c.frames_round (resampled_frame_rate ())
330 AudioContent::set_streams (vector<AudioStreamPtr> streams)
333 boost::mutex::scoped_lock lm (_mutex);
337 _parent->signal_changed (AudioContentProperty::STREAMS);
341 AudioContent::stream () const
343 boost::mutex::scoped_lock lm (_mutex);
344 DCPOMATIC_ASSERT (_streams.size() == 1);
345 return _streams.front ();
349 AudioContent::add_stream (AudioStreamPtr stream)
352 boost::mutex::scoped_lock lm (_mutex);
353 _streams.push_back (stream);
356 _parent->signal_changed (AudioContentProperty::STREAMS);
360 AudioContent::set_stream (AudioStreamPtr stream)
363 boost::mutex::scoped_lock lm (_mutex);
365 _streams.push_back (stream);
368 _parent->signal_changed (AudioContentProperty::STREAMS);