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