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::AUDIO_STREAMS = 200;
48 int const AudioContentProperty::AUDIO_GAIN = 201;
49 int const AudioContentProperty::AUDIO_DELAY = 202;
50 int const AudioContentProperty::AUDIO_VIDEO_FRAME_RATE = 203;
52 AudioContent::AudioContent (shared_ptr<const Film> film)
55 , _audio_delay (Config::instance()->default_audio_delay ())
60 AudioContent::AudioContent (shared_ptr<const Film> film, DCPTime s)
63 , _audio_delay (Config::instance()->default_audio_delay ())
68 AudioContent::AudioContent (shared_ptr<const Film> film, boost::filesystem::path p)
71 , _audio_delay (Config::instance()->default_audio_delay ())
76 AudioContent::AudioContent (shared_ptr<const Film> film, cxml::ConstNodePtr node)
77 : Content (film, node)
79 _audio_gain = node->number_child<double> ("AudioGain");
80 _audio_delay = node->number_child<int> ("AudioDelay");
81 _audio_video_frame_rate = node->optional_number_child<double> ("AudioVideoFrameRate");
84 AudioContent::AudioContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
87 shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
88 DCPOMATIC_ASSERT (ref);
90 for (size_t i = 0; i < c.size(); ++i) {
91 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
93 if (ac->audio_gain() != ref->audio_gain()) {
94 throw JoinError (_("Content to be joined must have the same audio gain."));
97 if (ac->audio_delay() != ref->audio_delay()) {
98 throw JoinError (_("Content to be joined must have the same audio delay."));
101 if (ac->audio_video_frame_rate() != ref->audio_video_frame_rate()) {
102 throw JoinError (_("Content to be joined must have the same video frame rate."));
106 _audio_gain = ref->audio_gain ();
107 _audio_delay = ref->audio_delay ();
108 /* Preserve the optional<> part of this */
109 _audio_video_frame_rate = ref->_audio_video_frame_rate;
113 AudioContent::as_xml (xmlpp::Node* node) const
115 boost::mutex::scoped_lock lm (_mutex);
116 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
117 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
118 if (_audio_video_frame_rate) {
119 node->add_child("AudioVideoFrameRate")->add_child_text (raw_convert<string> (_audio_video_frame_rate.get()));
125 AudioContent::set_audio_gain (double g)
128 boost::mutex::scoped_lock lm (_mutex);
132 signal_changed (AudioContentProperty::AUDIO_GAIN);
136 AudioContent::set_audio_delay (int d)
139 boost::mutex::scoped_lock lm (_mutex);
143 signal_changed (AudioContentProperty::AUDIO_DELAY);
147 AudioContent::technical_summary () const
149 string s = "audio :";
150 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
151 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
158 AudioContent::set_audio_mapping (AudioMapping mapping)
161 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
162 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
163 for (int j = 0; j < i->channels(); ++j) {
164 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
165 stream_mapping.set (j, k, mapping.get (c, k));
169 i->set_mapping (stream_mapping);
172 signal_changed (AudioContentProperty::AUDIO_STREAMS);
176 AudioContent::audio_mapping () const
179 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
180 channels += i->channels ();
183 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
188 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
189 AudioMapping mapping = i->mapping ();
190 for (int j = 0; j < mapping.input_channels(); ++j) {
191 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
192 if (k < mapping.output_channels()) {
193 merged.set (c, k, mapping.get (j, k));
204 /** @return the frame rate that this content should be resampled to in order
205 * that it is in sync with the active video content at its start time.
208 AudioContent::resampled_audio_frame_rate () const
210 /* Resample to a DCI-approved sample rate */
211 double t = has_rate_above_48k() ? 96000 : 48000;
213 FrameRateChange frc (audio_video_frame_rate(), film()->video_frame_rate());
215 /* Compensate if the DCP is being run at a different frame rate
216 to the source; that is, if the video is run such that it will
217 look different in the DCP compared to the source (slower or faster).
220 if (frc.change_speed) {
228 AudioContent::processing_description () const
230 vector<AudioStreamPtr> streams = audio_streams ();
231 if (streams.empty ()) {
235 /* Possible answers are:
236 1. all audio will be resampled from x to y.
237 2. all audio will be resampled to y (from a variety of rates)
238 3. some audio will be resampled to y (from a variety of rates)
239 4. nothing will be resampled.
242 bool not_resampled = false;
243 bool resampled = false;
246 optional<int> common_frame_rate;
247 BOOST_FOREACH (AudioStreamPtr i, streams) {
248 if (i->frame_rate() != resampled_audio_frame_rate()) {
251 not_resampled = true;
254 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
257 common_frame_rate = i->frame_rate ();
260 if (not_resampled && !resampled) {
261 return _("Audio will not be resampled");
264 if (not_resampled && resampled) {
265 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
268 if (!not_resampled && resampled) {
270 return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
272 return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
279 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
281 AudioContent::has_rate_above_48k () const
283 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
284 if (i->frame_rate() > 48000) {
292 /** @return User-visible names of each of our audio channels */
294 AudioContent::audio_channel_names () const
299 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
300 for (int j = 0; j < i->channels(); ++j) {
301 n.push_back (String::compose ("%1:%2", t, j + 1));
310 AudioContent::add_properties (list<UserProperty>& p) const
312 shared_ptr<const AudioStream> stream;
313 if (audio_streams().size() == 1) {
314 stream = audio_streams().front ();
318 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
319 p.push_back (UserProperty (_("Audio"), _("Content audio frame rate"), stream->frame_rate(), _("Hz")));
322 FrameRateChange const frc (audio_video_frame_rate(), film()->video_frame_rate());
323 ContentTime const c (full_length(), frc);
326 UserProperty (_("Length"), _("Full length in video frames at content rate"), c.frames_round(frc.source))
333 _("Full length in audio frames at content rate"),
334 c.frames_round (stream->frame_rate ())
339 p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_audio_frame_rate (), _("Hz")));
340 p.push_back (UserProperty (_("Length"), _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
346 _("Full length in audio frames at DCP rate"),
347 c.frames_round (resampled_audio_frame_rate ())
354 AudioContent::set_audio_video_frame_rate (double r)
357 boost::mutex::scoped_lock lm (_mutex);
358 _audio_video_frame_rate = r;
361 signal_changed (AudioContentProperty::AUDIO_VIDEO_FRAME_RATE);
365 AudioContent::audio_video_frame_rate () const
368 boost::mutex::scoped_lock lm (_mutex);
369 if (_audio_video_frame_rate) {
370 return _audio_video_frame_rate.get ();
374 /* No frame rate specified, so assume this content has been
375 prepared for any concurrent video content.
377 return film()->active_frame_rate_change(position()).source;