2 Copyright (C) 2013-2015 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>
36 using std::stringstream;
38 using std::setprecision;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
43 /** Something stream-related has changed */
44 int const AudioContentProperty::AUDIO_STREAMS = 200;
45 int const AudioContentProperty::AUDIO_GAIN = 201;
46 int const AudioContentProperty::AUDIO_DELAY = 202;
48 AudioContent::AudioContent (shared_ptr<const Film> film)
51 , _audio_delay (Config::instance()->default_audio_delay ())
56 AudioContent::AudioContent (shared_ptr<const Film> film, DCPTime s)
59 , _audio_delay (Config::instance()->default_audio_delay ())
64 AudioContent::AudioContent (shared_ptr<const Film> film, boost::filesystem::path p)
67 , _audio_delay (Config::instance()->default_audio_delay ())
72 AudioContent::AudioContent (shared_ptr<const Film> film, cxml::ConstNodePtr node)
73 : Content (film, node)
75 _audio_gain = node->number_child<float> ("AudioGain");
76 _audio_delay = node->number_child<int> ("AudioDelay");
79 AudioContent::AudioContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
82 shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
83 DCPOMATIC_ASSERT (ref);
85 for (size_t i = 0; i < c.size(); ++i) {
86 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
88 if (ac->audio_gain() != ref->audio_gain()) {
89 throw JoinError (_("Content to be joined must have the same audio gain."));
92 if (ac->audio_delay() != ref->audio_delay()) {
93 throw JoinError (_("Content to be joined must have the same audio delay."));
97 _audio_gain = ref->audio_gain ();
98 _audio_delay = ref->audio_delay ();
102 AudioContent::as_xml (xmlpp::Node* node) const
104 boost::mutex::scoped_lock lm (_mutex);
105 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
106 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
111 AudioContent::set_audio_gain (double g)
114 boost::mutex::scoped_lock lm (_mutex);
118 signal_changed (AudioContentProperty::AUDIO_GAIN);
122 AudioContent::set_audio_delay (int d)
125 boost::mutex::scoped_lock lm (_mutex);
129 signal_changed (AudioContentProperty::AUDIO_DELAY);
133 AudioContent::technical_summary () const
135 string s = "audio :";
136 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
137 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
144 AudioContent::set_audio_mapping (AudioMapping mapping)
147 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
148 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
149 for (int j = 0; j < i->channels(); ++j) {
150 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
151 stream_mapping.set (j, k, mapping.get (c, k));
155 i->set_mapping (stream_mapping);
158 signal_changed (AudioContentProperty::AUDIO_STREAMS);
162 AudioContent::audio_mapping () const
165 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
166 channels += i->channels ();
169 AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
173 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
174 AudioMapping mapping = i->mapping ();
175 for (int j = 0; j < mapping.input_channels(); ++j) {
176 for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
177 merged.set (c, k, mapping.get (j, k));
187 /** @return the frame rate that this content should be resampled to in order
188 * that it is in sync with the active video content at its start time.
191 AudioContent::resampled_audio_frame_rate () const
193 shared_ptr<const Film> film = _film.lock ();
194 DCPOMATIC_ASSERT (film);
196 /* Resample to a DCI-approved sample rate */
197 double t = has_rate_above_48k() ? 96000 : 48000;
199 FrameRateChange frc = film->active_frame_rate_change (position ());
201 /* Compensate if the DCP is being run at a different frame rate
202 to the source; that is, if the video is run such that it will
203 look different in the DCP compared to the source (slower or faster).
206 if (frc.change_speed) {
214 AudioContent::processing_description () const
216 vector<AudioStreamPtr> streams = audio_streams ();
217 if (streams.empty ()) {
221 /* Possible answers are:
222 1. all audio will be resampled from x to y.
223 2. all audio will be resampled to y (from a variety of rates)
224 3. some audio will be resampled to y (from a variety of rates)
225 4. nothing will be resampled.
228 bool not_resampled = false;
229 bool resampled = false;
232 optional<int> common_frame_rate;
233 BOOST_FOREACH (AudioStreamPtr i, streams) {
234 if (i->frame_rate() != resampled_audio_frame_rate()) {
237 not_resampled = true;
240 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
243 common_frame_rate = i->frame_rate ();
246 if (not_resampled && !resampled) {
247 return _("Audio will not be resampled");
250 if (not_resampled && resampled) {
251 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
254 if (!not_resampled && resampled) {
256 return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
258 return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
265 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
267 AudioContent::has_rate_above_48k () const
269 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
270 if (i->frame_rate() > 48000) {
278 /** @return User-visible names of each of our audio channels */
280 AudioContent::audio_channel_names () const
285 BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
286 for (int j = 0; j < i->channels(); ++j) {
287 n.push_back (String::compose ("%1:%2", t, j + 1));