Merge remote-tracking branch 'origin/master' into 1.0
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
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"
25 #include "film.h"
26 #include "exceptions.h"
27 #include "config.h"
28 #include "frame_rate_change.h"
29 #include "audio_processor.h"
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::cout;
35 using std::vector;
36 using boost::shared_ptr;
37 using boost::dynamic_pointer_cast;
38 using dcp::raw_convert;
39
40 int const AudioContentProperty::AUDIO_CHANNELS = 200;
41 int const AudioContentProperty::AUDIO_LENGTH = 201;
42 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
43 int const AudioContentProperty::AUDIO_GAIN = 203;
44 int const AudioContentProperty::AUDIO_DELAY = 204;
45 int const AudioContentProperty::AUDIO_MAPPING = 205;
46 int const AudioContentProperty::AUDIO_PROCESSOR = 206;
47
48 AudioContent::AudioContent (shared_ptr<const Film> f)
49         : Content (f)
50         , _audio_gain (0)
51         , _audio_delay (Config::instance()->default_audio_delay ())
52         , _audio_processor (0)
53 {
54
55 }
56
57 AudioContent::AudioContent (shared_ptr<const Film> f, DCPTime s)
58         : Content (f, s)
59         , _audio_gain (0)
60         , _audio_delay (Config::instance()->default_audio_delay ())
61         , _audio_processor (0)
62 {
63
64 }
65
66 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
67         : Content (f, p)
68         , _audio_gain (0)
69         , _audio_delay (Config::instance()->default_audio_delay ())
70         , _audio_processor (0)
71 {
72
73 }
74
75 AudioContent::AudioContent (shared_ptr<const Film> f, cxml::ConstNodePtr node)
76         : Content (f, node)
77         , _audio_processor (0)
78 {
79         _audio_gain = node->number_child<float> ("AudioGain");
80         _audio_delay = node->number_child<int> ("AudioDelay");
81         if (node->optional_string_child ("AudioProcessor")) {
82                 _audio_processor = AudioProcessor::from_id (node->string_child ("AudioProcessor"));
83         }
84 }
85
86 AudioContent::AudioContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
87         : Content (f, c)
88 {
89         shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
90         assert (ref);
91         
92         for (size_t i = 0; i < c.size(); ++i) {
93                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
94
95                 if (ac->audio_gain() != ref->audio_gain()) {
96                         throw JoinError (_("Content to be joined must have the same audio gain."));
97                 }
98
99                 if (ac->audio_delay() != ref->audio_delay()) {
100                         throw JoinError (_("Content to be joined must have the same audio delay."));
101                 }
102         }
103
104         _audio_gain = ref->audio_gain ();
105         _audio_delay = ref->audio_delay ();
106         _audio_processor = ref->audio_processor ();
107 }
108
109 void
110 AudioContent::as_xml (xmlpp::Node* node) const
111 {
112         boost::mutex::scoped_lock lm (_mutex);
113         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
114         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
115         if (_audio_processor) {
116                 node->add_child("AudioProcessor")->add_child_text (_audio_processor->id ());
117         }
118 }
119
120
121 void
122 AudioContent::set_audio_gain (double g)
123 {
124         {
125                 boost::mutex::scoped_lock lm (_mutex);
126                 _audio_gain = g;
127         }
128         
129         signal_changed (AudioContentProperty::AUDIO_GAIN);
130 }
131
132 void
133 AudioContent::set_audio_delay (int d)
134 {
135         {
136                 boost::mutex::scoped_lock lm (_mutex);
137                 _audio_delay = d;
138         }
139         
140         signal_changed (AudioContentProperty::AUDIO_DELAY);
141 }
142
143 void
144 AudioContent::set_audio_processor (AudioProcessor const * p)
145 {
146         {
147                 boost::mutex::scoped_lock lm (_mutex);
148                 _audio_processor = p;
149         }
150
151         /* The channel count might have changed, so reset the mapping */
152         AudioMapping m (processed_audio_channels ());
153         m.make_default ();
154         set_audio_mapping (m);
155
156         signal_changed (AudioContentProperty::AUDIO_PROCESSOR);
157 }
158
159 boost::signals2::connection
160 AudioContent::analyse_audio (boost::function<void()> finished)
161 {
162         shared_ptr<const Film> film = _film.lock ();
163         assert (film);
164         
165         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
166         boost::signals2::connection c = job->Finished.connect (finished);
167         JobManager::instance()->add (job);
168
169         return c;
170 }
171
172 boost::filesystem::path
173 AudioContent::audio_analysis_path () const
174 {
175         shared_ptr<const Film> film = _film.lock ();
176         if (!film) {
177                 return boost::filesystem::path ();
178         }
179
180         boost::filesystem::path p = film->audio_analysis_dir ();
181         p /= digest() + "_" + audio_mapping().digest();
182         return p;
183 }
184
185 string
186 AudioContent::technical_summary () const
187 {
188         return String::compose (
189                 "audio: channels %1, length %2, content rate %3, resampled rate %4",
190                 audio_channels(),
191                 audio_length().seconds(),
192                 audio_frame_rate(),
193                 resampled_audio_frame_rate()
194                 );
195 }
196
197 void
198 AudioContent::set_audio_mapping (AudioMapping)
199 {
200         signal_changed (AudioContentProperty::AUDIO_MAPPING);
201 }
202
203 /** @return the frame rate that this content should be resampled to in order
204  *  that it is in sync with the active video content at its start time.
205  */
206 int
207 AudioContent::resampled_audio_frame_rate () const
208 {
209         shared_ptr<const Film> film = _film.lock ();
210         assert (film);
211         
212         /* Resample to a DCI-approved sample rate */
213         double t = dcp_audio_frame_rate (audio_frame_rate ());
214
215         FrameRateChange frc = film->active_frame_rate_change (position ());
216
217         /* Compensate if the DCP is being run at a different frame rate
218            to the source; that is, if the video is run such that it will
219            look different in the DCP compared to the source (slower or faster).
220         */
221
222         if (frc.change_speed) {
223                 t /= frc.speed_up;
224         }
225
226         return rint (t);
227 }
228
229 int
230 AudioContent::processed_audio_channels () const
231 {
232         if (!audio_processor ()) {
233                 return audio_channels ();
234         }
235
236         return audio_processor()->out_channels (audio_channels ());
237 }
238