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 (Content* parent, shared_ptr<const Film> film)
53 : ContentPart (parent, film)
55 , _audio_delay (Config::instance()->default_audio_delay ())
60 AudioContent::AudioContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node)
61 : ContentPart (parent, film)
63 _audio_gain = node->number_child<double> ("AudioGain");
64 _audio_delay = node->number_child<int> ("AudioDelay");
65 _audio_video_frame_rate = node->optional_number_child<double> ("AudioVideoFrameRate");
68 AudioContent::AudioContent (Content* parent, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
69 : ContentPart (parent, film)
71 shared_ptr<AudioContent> ref = c[0]->audio;
72 DCPOMATIC_ASSERT (ref);
74 for (size_t i = 1; i < c.size(); ++i) {
75 if (c[i]->audio->audio_gain() != ref->audio_gain()) {
76 throw JoinError (_("Content to be joined must have the same audio gain."));
79 if (c[i]->audio->audio_delay() != ref->audio_delay()) {
80 throw JoinError (_("Content to be joined must have the same audio delay."));
83 if (ac->audio_video_frame_rate() != ref->audio_video_frame_rate()) {
84 throw JoinError (_("Content to be joined must have the same video frame rate."));
88 _audio_gain = ref->audio_gain ();
89 _audio_delay = ref->audio_delay ();
90 /* Preserve the optional<> part of this */
91 _audio_video_frame_rate = ref->_audio_video_frame_rate;
92 _streams = ref->streams ();
96 AudioContent::as_xml (xmlpp::Node* node) const
98 boost::mutex::scoped_lock lm (_mutex);
99 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
100 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
101 if (_audio_video_frame_rate) {
102 node->add_child("AudioVideoFrameRate")->add_child_text (raw_convert<string> (_audio_video_frame_rate.get()));
107 AudioContent::set_audio_gain (double g)
109 maybe_set (_audio_gain, g, AudioContentProperty::AUDIO_GAIN);
113 AudioContent::set_audio_delay (int d)
115 maybe_set (_audio_delay, d, AudioContentProperty::AUDIO_DELAY);
119 AudioContent::technical_summary () const
121 string s = "audio :";
122 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
123 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
130 AudioContent::set_audio_mapping (AudioMapping mapping)
133 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
134 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
135 for (int j = 0; j < i->channels(); ++j) {
136 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
137 stream_mapping.set (j, k, mapping.get (c, k));
141 i->set_mapping (stream_mapping);
144 _parent->signal_changed (AudioContentProperty::AUDIO_STREAMS);
148 AudioContent::audio_mapping () const
151 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
152 channels += i->channels ();
155 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
160 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
161 AudioMapping mapping = i->mapping ();
162 for (int j = 0; j < mapping.input_channels(); ++j) {
163 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
164 if (k < mapping.output_channels()) {
165 merged.set (c, k, mapping.get (j, k));
176 /** @return the frame rate that this content should be resampled to in order
177 * that it is in sync with the active video content at its start time.
180 AudioContent::resampled_audio_frame_rate () const
182 /* Resample to a DCI-approved sample rate */
183 double t = has_rate_above_48k() ? 96000 : 48000;
185 shared_ptr<const Film> film = _film.lock ();
186 DCPOMATIC_ASSERT (film);
187 FrameRateChange frc (audio_video_frame_rate(), film->video_frame_rate());
189 /* Compensate if the DCP is being run at a different frame rate
190 to the source; that is, if the video is run such that it will
191 look different in the DCP compared to the source (slower or faster).
194 if (frc.change_speed) {
202 AudioContent::processing_description () const
204 if (streams().empty ()) {
208 /* Possible answers are:
209 1. all audio will be resampled from x to y.
210 2. all audio will be resampled to y (from a variety of rates)
211 3. some audio will be resampled to y (from a variety of rates)
212 4. nothing will be resampled.
215 bool not_resampled = false;
216 bool resampled = false;
219 optional<int> common_frame_rate;
220 BOOST_FOREACH (AudioStreamPtr i, streams()) {
221 if (i->frame_rate() != resampled_audio_frame_rate()) {
224 not_resampled = true;
227 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
230 common_frame_rate = i->frame_rate ();
233 if (not_resampled && !resampled) {
234 return _("Audio will not be resampled");
237 if (not_resampled && resampled) {
238 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
241 if (!not_resampled && resampled) {
243 return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
245 return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
252 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
254 AudioContent::has_rate_above_48k () const
256 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
257 if (i->frame_rate() > 48000) {
265 /** @return User-visible names of each of our audio channels */
267 AudioContent::audio_channel_names () const
272 BOOST_FOREACH (AudioStreamPtr i, streams ()) {
273 for (int j = 0; j < i->channels(); ++j) {
274 n.push_back (String::compose ("%1:%2", t, j + 1));
283 AudioContent::add_properties (list<UserProperty>& p) const
285 shared_ptr<const AudioStream> stream;
286 if (streams().size() == 1) {
287 stream = streams().front ();
291 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
292 p.push_back (UserProperty (_("Audio"), _("Content audio frame rate"), stream->frame_rate(), _("Hz")));
295 shared_ptr<const Film> film = _film.lock ();
296 DCPOMATIC_ASSERT (film);
298 FrameRateChange const frc (audio_video_frame_rate(), 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 frames at content rate"),
310 c.frames_round (stream->frame_rate ())
315 p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_audio_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 frames at DCP rate"),
323 c.frames_round (resampled_audio_frame_rate ())
330 AudioContent::set_audio_video_frame_rate (double r)
333 boost::mutex::scoped_lock lm (_mutex);
334 _audio_video_frame_rate = r;
337 signal_changed (AudioContentProperty::AUDIO_VIDEO_FRAME_RATE);
341 AudioContent::audio_video_frame_rate () const
344 boost::mutex::scoped_lock lm (_mutex);
345 if (_audio_video_frame_rate) {
346 return _audio_video_frame_rate.get ();
350 /* No frame rate specified, so assume this content has been
351 prepared for any concurrent video content.
353 return film()->active_frame_rate_change(position()).source;
356 AudioContent::set_streams (vector<AudioStreamPtr> streams)
359 boost::mutex::scoped_lock lm (_mutex);
363 _parent->signal_changed (AudioContentProperty::AUDIO_STREAMS);
367 AudioContent::stream () const
369 boost::mutex::scoped_lock lm (_mutex);
370 DCPOMATIC_ASSERT (_streams.size() == 1);
371 return _streams.front ();
375 AudioContent::add_stream (AudioStreamPtr stream)
378 boost::mutex::scoped_lock lm (_mutex);
379 _streams.push_back (stream);
382 _parent->signal_changed (AudioContentProperty::AUDIO_STREAMS);
386 AudioContent::set_stream (AudioStreamPtr stream)
389 boost::mutex::scoped_lock lm (_mutex);
391 _streams.push_back (stream);
394 _parent->signal_changed (AudioContentProperty::AUDIO_STREAMS);