2 Copyright (C) 2013-2014 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"
21 #include "analyse_audio_job.h"
22 #include "job_manager.h"
24 #include "exceptions.h"
26 #include "frame_rate_change.h"
27 #include "audio_processor.h"
28 #include <libcxml/cxml.h>
29 #include <dcp/raw_convert.h>
36 using std::stringstream;
38 using std::setprecision;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using dcp::raw_convert;
43 int const AudioContentProperty::AUDIO_CHANNELS = 200;
44 int const AudioContentProperty::AUDIO_LENGTH = 201;
45 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
46 int const AudioContentProperty::AUDIO_GAIN = 203;
47 int const AudioContentProperty::AUDIO_DELAY = 204;
48 int const AudioContentProperty::AUDIO_MAPPING = 205;
49 int const AudioContentProperty::AUDIO_PROCESSOR = 206;
51 AudioContent::AudioContent (shared_ptr<const Film> f)
54 , _audio_delay (Config::instance()->default_audio_delay ())
55 , _audio_processor (0)
60 AudioContent::AudioContent (shared_ptr<const Film> f, DCPTime s)
63 , _audio_delay (Config::instance()->default_audio_delay ())
64 , _audio_processor (0)
69 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
72 , _audio_delay (Config::instance()->default_audio_delay ())
73 , _audio_processor (0)
78 AudioContent::AudioContent (shared_ptr<const Film> f, cxml::ConstNodePtr node)
80 , _audio_processor (0)
82 _audio_gain = node->number_child<float> ("AudioGain");
83 _audio_delay = node->number_child<int> ("AudioDelay");
84 if (node->optional_string_child ("AudioProcessor")) {
85 _audio_processor = AudioProcessor::from_id (node->string_child ("AudioProcessor"));
89 AudioContent::AudioContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
92 shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
95 for (size_t i = 0; i < c.size(); ++i) {
96 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
98 if (ac->audio_gain() != ref->audio_gain()) {
99 throw JoinError (_("Content to be joined must have the same audio gain."));
102 if (ac->audio_delay() != ref->audio_delay()) {
103 throw JoinError (_("Content to be joined must have the same audio delay."));
107 _audio_gain = ref->audio_gain ();
108 _audio_delay = ref->audio_delay ();
109 _audio_processor = ref->audio_processor ();
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_processor) {
119 node->add_child("AudioProcessor")->add_child_text (_audio_processor->id ());
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::set_audio_processor (AudioProcessor const * p)
150 boost::mutex::scoped_lock lm (_mutex);
151 _audio_processor = p;
154 /* The channel count might have changed, so reset the mapping */
155 AudioMapping m (processed_audio_channels ());
157 set_audio_mapping (m);
159 signal_changed (AudioContentProperty::AUDIO_PROCESSOR);
162 boost::signals2::connection
163 AudioContent::analyse_audio (boost::function<void()> finished)
165 shared_ptr<const Film> film = _film.lock ();
168 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
169 boost::signals2::connection c = job->Finished.connect (finished);
170 JobManager::instance()->add (job);
175 boost::filesystem::path
176 AudioContent::audio_analysis_path () const
178 shared_ptr<const Film> film = _film.lock ();
180 return boost::filesystem::path ();
183 boost::filesystem::path p = film->audio_analysis_dir ();
184 p /= digest().get_value_or ("X") + "_" + audio_mapping().digest();
189 AudioContent::technical_summary () const
191 return String::compose (
192 "audio: channels %1, length %2, content rate %3, resampled rate %4",
194 audio_length().seconds(),
196 resampled_audio_frame_rate()
201 AudioContent::set_audio_mapping (AudioMapping)
203 signal_changed (AudioContentProperty::AUDIO_MAPPING);
206 /** @return the frame rate that this content should be resampled to in order
207 * that it is in sync with the active video content at its start time.
210 AudioContent::resampled_audio_frame_rate () const
212 shared_ptr<const Film> film = _film.lock ();
215 /* Resample to a DCI-approved sample rate */
216 double t = dcp_audio_frame_rate (audio_frame_rate ());
218 FrameRateChange frc = film->active_frame_rate_change (position ());
220 /* Compensate if the DCP is being run at a different frame rate
221 to the source; that is, if the video is run such that it will
222 look different in the DCP compared to the source (slower or faster).
225 if (frc.change_speed) {
233 AudioContent::processed_audio_channels () const
235 if (!audio_processor ()) {
236 return audio_channels ();
239 return audio_processor()->out_channels (audio_channels ());
243 AudioContent::processing_description () const
247 if (audio_frame_rate() != resampled_audio_frame_rate ()) {
249 from << fixed << setprecision(3) << (audio_frame_rate() / 1000.0);
251 to << fixed << setprecision(3) << (resampled_audio_frame_rate() / 1000.0);
253 d << String::compose (_("Audio will be resampled from %1kHz to %2kHz."), from.str(), to.str());
255 d << _("Audio will not be resampled.");