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"
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36 using dcp::raw_convert;
38 int const AudioContentProperty::AUDIO_CHANNELS = 200;
39 int const AudioContentProperty::AUDIO_LENGTH = 201;
40 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
41 int const AudioContentProperty::AUDIO_GAIN = 203;
42 int const AudioContentProperty::AUDIO_DELAY = 204;
43 int const AudioContentProperty::AUDIO_MAPPING = 205;
45 AudioContent::AudioContent (shared_ptr<const Film> f, DCPTime s)
48 , _audio_delay (Config::instance()->default_audio_delay ())
53 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
56 , _audio_delay (Config::instance()->default_audio_delay ())
61 AudioContent::AudioContent (shared_ptr<const Film> f, cxml::ConstNodePtr node)
64 _audio_gain = node->number_child<float> ("AudioGain");
65 _audio_delay = node->number_child<int> ("AudioDelay");
68 AudioContent::AudioContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
71 shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
74 for (size_t i = 0; i < c.size(); ++i) {
75 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
77 if (ac->audio_gain() != ref->audio_gain()) {
78 throw JoinError (_("Content to be joined must have the same audio gain."));
81 if (ac->audio_delay() != ref->audio_delay()) {
82 throw JoinError (_("Content to be joined must have the same audio delay."));
86 _audio_gain = ref->audio_gain ();
87 _audio_delay = ref->audio_delay ();
91 AudioContent::as_xml (xmlpp::Node* node) const
93 boost::mutex::scoped_lock lm (_mutex);
94 node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
95 node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
100 AudioContent::set_audio_gain (float g)
103 boost::mutex::scoped_lock lm (_mutex);
107 signal_changed (AudioContentProperty::AUDIO_GAIN);
111 AudioContent::set_audio_delay (int d)
114 boost::mutex::scoped_lock lm (_mutex);
118 signal_changed (AudioContentProperty::AUDIO_DELAY);
121 boost::signals2::connection
122 AudioContent::analyse_audio (boost::function<void()> finished)
124 shared_ptr<const Film> film = _film.lock ();
127 shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
128 boost::signals2::connection c = job->Finished.connect (finished);
129 JobManager::instance()->add (job);
134 boost::filesystem::path
135 AudioContent::audio_analysis_path () const
137 shared_ptr<const Film> film = _film.lock ();
139 return boost::filesystem::path ();
142 boost::filesystem::path p = film->audio_analysis_dir ();
143 p /= digest() + "_" + audio_mapping().digest();
148 AudioContent::technical_summary () const
150 return String::compose (
151 "audio: channels %1, length %2, content rate %3, resampled rate %4",
153 audio_length().seconds(),
155 resampled_audio_frame_rate()
160 AudioContent::set_audio_mapping (AudioMapping)
162 signal_changed (AudioContentProperty::AUDIO_MAPPING);
165 /** @return the frame rate that this content should be resampled to in order
166 * that it is in sync with the active video content at its start time.
169 AudioContent::resampled_audio_frame_rate () const
171 shared_ptr<const Film> film = _film.lock ();
174 /* Resample to a DCI-approved sample rate */
175 double t = dcp_audio_frame_rate (audio_frame_rate ());
177 FrameRateChange frc = film->active_frame_rate_change (position ());
179 /* Compensate if the DCP is being run at a different frame rate
180 to the source; that is, if the video is run such that it will
181 look different in the DCP compared to the source (slower or faster).
184 if (frc.change_speed) {