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;
51 AudioContent::AudioContent (shared_ptr<const Film> film)
54 , _audio_delay (Config::instance()->default_audio_delay ())
59 AudioContent::AudioContent (shared_ptr<const Film> film, DCPTime s)
62 , _audio_delay (Config::instance()->default_audio_delay ())
67 AudioContent::AudioContent (shared_ptr<const Film> film, boost::filesystem::path p)
70 , _audio_delay (Config::instance()->default_audio_delay ())
75 AudioContent::AudioContent (shared_ptr<const Film> film, cxml::ConstNodePtr node)
76 : Content (film, node)
78 _audio_gain = node->number_child<double> ("AudioGain");
79 _audio_delay = node->number_child<int> ("AudioDelay");
82 AudioContent::AudioContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
85 shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
86 DCPOMATIC_ASSERT (ref);
88 for (size_t i = 0; i < c.size(); ++i) {
89 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
91 if (ac->audio_gain() != ref->audio_gain()) {
92 throw JoinError (_("Content to be joined must have the same audio gain."));
95 if (ac->audio_delay() != ref->audio_delay()) {
96 throw JoinError (_("Content to be joined must have the same audio delay."));
100 _audio_gain = ref->audio_gain ();
101 _audio_delay = ref->audio_delay ();
105 AudioContent::as_xml (xmlpp::Node* node) const
107 boost::mutex::scoped_lock lm (_mutex);
108 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
109 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
114 AudioContent::set_audio_gain (double g)
117 boost::mutex::scoped_lock lm (_mutex);
121 signal_changed (AudioContentProperty::AUDIO_GAIN);
125 AudioContent::set_audio_delay (int d)
128 boost::mutex::scoped_lock lm (_mutex);
132 signal_changed (AudioContentProperty::AUDIO_DELAY);
136 AudioContent::technical_summary () const
138 string s = "audio :";
139 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
140 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
147 AudioContent::set_audio_mapping (AudioMapping mapping)
150 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
151 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
152 for (int j = 0; j < i->channels(); ++j) {
153 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
154 stream_mapping.set (j, k, mapping.get (c, k));
158 i->set_mapping (stream_mapping);
161 signal_changed (AudioContentProperty::AUDIO_STREAMS);
165 AudioContent::audio_mapping () const
168 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
169 channels += i->channels ();
172 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
177 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
178 AudioMapping mapping = i->mapping ();
179 for (int j = 0; j < mapping.input_channels(); ++j) {
180 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
181 if (k < mapping.output_channels()) {
182 merged.set (c, k, mapping.get (j, k));
193 /** @return the frame rate that this content should be resampled to in order
194 * that it is in sync with the active video content at its start time.
197 AudioContent::resampled_audio_frame_rate () const
199 /* Resample to a DCI-approved sample rate */
200 double t = has_rate_above_48k() ? 96000 : 48000;
202 FrameRateChange frc = film()->active_frame_rate_change (position ());
204 /* Compensate if the DCP is being run at a different frame rate
205 to the source; that is, if the video is run such that it will
206 look different in the DCP compared to the source (slower or faster).
209 if (frc.change_speed) {
217 AudioContent::processing_description () const
219 vector<AudioStreamPtr> streams = audio_streams ();
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_audio_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_audio_frame_rate ());
257 if (!not_resampled && resampled) {
259 return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
261 return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_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, audio_streams ()) {
273 if (i->frame_rate() > 48000) {
281 /** @return User-visible names of each of our audio channels */
283 AudioContent::audio_channel_names () const
288 BOOST_FOREACH (AudioStreamPtr i, audio_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 (audio_streams().size() == 1) {
303 stream = audio_streams().front ();
307 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
308 p.push_back (UserProperty (_("Audio"), _("Content audio frame rate"), stream->frame_rate(), _("Hz")));
311 shared_ptr<const Film> film = _film.lock ();
312 DCPOMATIC_ASSERT (film);
314 FrameRateChange const frc = film->active_frame_rate_change (position ());
315 ContentTime const c (full_length(), frc);
318 UserProperty (_("Length"), _("Full length in video frames at content rate"), c.frames_round(frc.source))
325 _("Full length in audio frames at content rate"),
326 c.frames_round (stream->frame_rate ())
331 p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_audio_frame_rate (), _("Hz")));
332 p.push_back (UserProperty (_("Length"), _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
338 _("Full length in audio frames at DCP rate"),
339 c.frames_round (resampled_audio_frame_rate ())