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 <libcxml/cxml.h>
21 #include <dcp/raw_convert.h>
22 #include "audio_content.h"
23 #include "analyse_audio_job.h"
24 #include "job_manager.h"
26 #include "exceptions.h"
28 #include "frame_rate_change.h"
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37 using dcp::raw_convert;
39 int const AudioContentProperty::AUDIO_CHANNELS = 200;
40 int const AudioContentProperty::AUDIO_LENGTH = 201;
41 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
42 int const AudioContentProperty::AUDIO_GAIN = 203;
43 int const AudioContentProperty::AUDIO_DELAY = 204;
44 int const AudioContentProperty::AUDIO_MAPPING = 205;
46 AudioContent::AudioContent (shared_ptr<const Film> f)
49 , _audio_delay (Config::instance()->default_audio_delay ())
54 AudioContent::AudioContent (shared_ptr<const Film> f, DCPTime s)
57 , _audio_delay (Config::instance()->default_audio_delay ())
62 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
65 , _audio_delay (Config::instance()->default_audio_delay ())
70 AudioContent::AudioContent (shared_ptr<const Film> f, cxml::ConstNodePtr node)
73 _audio_gain = node->number_child<float> ("AudioGain");
74 _audio_delay = node->number_child<int> ("AudioDelay");
77 AudioContent::AudioContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
80 shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
83 for (size_t i = 0; i < c.size(); ++i) {
84 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
86 if (ac->audio_gain() != ref->audio_gain()) {
87 throw JoinError (_("Content to be joined must have the same audio gain."));
90 if (ac->audio_delay() != ref->audio_delay()) {
91 throw JoinError (_("Content to be joined must have the same audio delay."));
95 _audio_gain = ref->audio_gain ();
96 _audio_delay = ref->audio_delay ();
100 AudioContent::as_xml (xmlpp::Node* node) const
102 boost::mutex::scoped_lock lm (_mutex);
103 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
104 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
109 AudioContent::set_audio_gain (double g)
112 boost::mutex::scoped_lock lm (_mutex);
116 signal_changed (AudioContentProperty::AUDIO_GAIN);
120 AudioContent::set_audio_delay (int d)
123 boost::mutex::scoped_lock lm (_mutex);
127 signal_changed (AudioContentProperty::AUDIO_DELAY);
130 boost::signals2::connection
131 AudioContent::analyse_audio (boost::function<void()> finished)
133 shared_ptr<const Film> film = _film.lock ();
136 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
137 boost::signals2::connection c = job->Finished.connect (finished);
138 JobManager::instance()->add (job);
143 boost::filesystem::path
144 AudioContent::audio_analysis_path () const
146 shared_ptr<const Film> film = _film.lock ();
148 return boost::filesystem::path ();
151 boost::filesystem::path p = film->audio_analysis_dir ();
152 p /= digest() + "_" + audio_mapping().digest();
157 AudioContent::technical_summary () const
159 return String::compose (
160 "audio: channels %1, length %2, content rate %3, resampled rate %4",
162 audio_length().seconds(),
164 resampled_audio_frame_rate()
169 AudioContent::set_audio_mapping (AudioMapping)
171 signal_changed (AudioContentProperty::AUDIO_MAPPING);
174 /** @return the frame rate that this content should be resampled to in order
175 * that it is in sync with the active video content at its start time.
178 AudioContent::resampled_audio_frame_rate () const
180 shared_ptr<const Film> film = _film.lock ();
183 /* Resample to a DCI-approved sample rate */
184 double t = dcp_audio_frame_rate (audio_frame_rate ());
186 FrameRateChange frc = film->active_frame_rate_change (position ());
188 /* Compensate if the DCP is being run at a different frame rate
189 to the source; that is, if the video is run such that it will
190 look different in the DCP compared to the source (slower or faster).
193 if (frc.change_speed) {