Allow setup of the frame rate that audio content is prepared for.
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2016 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 "film.h"
22 #include "exceptions.h"
23 #include "config.h"
24 #include "frame_rate_change.h"
25 #include "raw_convert.h"
26 #include "compose.hpp"
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::cout;
36 using std::vector;
37 using std::stringstream;
38 using std::fixed;
39 using std::list;
40 using std::pair;
41 using std::setprecision;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using boost::optional;
45
46 /** Something stream-related has changed */
47 int const AudioContentProperty::AUDIO_STREAMS = 200;
48 int const AudioContentProperty::AUDIO_GAIN = 201;
49 int const AudioContentProperty::AUDIO_DELAY = 202;
50 int const AudioContentProperty::AUDIO_VIDEO_FRAME_RATE = 203;
51
52 AudioContent::AudioContent (shared_ptr<const Film> film)
53         : Content (film)
54         , _audio_gain (0)
55         , _audio_delay (Config::instance()->default_audio_delay ())
56 {
57
58 }
59
60 AudioContent::AudioContent (shared_ptr<const Film> film, DCPTime s)
61         : Content (film, s)
62         , _audio_gain (0)
63         , _audio_delay (Config::instance()->default_audio_delay ())
64 {
65
66 }
67
68 AudioContent::AudioContent (shared_ptr<const Film> film, boost::filesystem::path p)
69         : Content (film, p)
70         , _audio_gain (0)
71         , _audio_delay (Config::instance()->default_audio_delay ())
72 {
73
74 }
75
76 AudioContent::AudioContent (shared_ptr<const Film> film, cxml::ConstNodePtr node)
77         : Content (film, node)
78 {
79         _audio_gain = node->number_child<double> ("AudioGain");
80         _audio_delay = node->number_child<int> ("AudioDelay");
81         _audio_video_frame_rate = node->optional_number_child<double> ("AudioVideoFrameRate");
82 }
83
84 AudioContent::AudioContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
85         : Content (film, c)
86 {
87         shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
88         DCPOMATIC_ASSERT (ref);
89
90         for (size_t i = 0; i < c.size(); ++i) {
91                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
92
93                 if (ac->audio_gain() != ref->audio_gain()) {
94                         throw JoinError (_("Content to be joined must have the same audio gain."));
95                 }
96
97                 if (ac->audio_delay() != ref->audio_delay()) {
98                         throw JoinError (_("Content to be joined must have the same audio delay."));
99                 }
100
101                 if (ac->audio_video_frame_rate() != ref->audio_video_frame_rate()) {
102                         throw JoinError (_("Content to be joined must have the same video frame rate."));
103                 }
104         }
105
106         _audio_gain = ref->audio_gain ();
107         _audio_delay = ref->audio_delay ();
108         /* Preserve the optional<> part of this */
109         _audio_video_frame_rate = ref->_audio_video_frame_rate;
110 }
111
112 void
113 AudioContent::as_xml (xmlpp::Node* node) const
114 {
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_video_frame_rate) {
119                 node->add_child("AudioVideoFrameRate")->add_child_text (raw_convert<string> (_audio_video_frame_rate.get()));
120         }
121 }
122
123
124 void
125 AudioContent::set_audio_gain (double g)
126 {
127         {
128                 boost::mutex::scoped_lock lm (_mutex);
129                 _audio_gain = g;
130         }
131
132         signal_changed (AudioContentProperty::AUDIO_GAIN);
133 }
134
135 void
136 AudioContent::set_audio_delay (int d)
137 {
138         {
139                 boost::mutex::scoped_lock lm (_mutex);
140                 _audio_delay = d;
141         }
142
143         signal_changed (AudioContentProperty::AUDIO_DELAY);
144 }
145
146 string
147 AudioContent::technical_summary () const
148 {
149         string s = "audio :";
150         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
151                 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
152         }
153
154         return s;
155 }
156
157 void
158 AudioContent::set_audio_mapping (AudioMapping mapping)
159 {
160         int c = 0;
161         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
162                 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
163                 for (int j = 0; j < i->channels(); ++j) {
164                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
165                                 stream_mapping.set (j, k, mapping.get (c, k));
166                         }
167                         ++c;
168                 }
169                 i->set_mapping (stream_mapping);
170         }
171
172         signal_changed (AudioContentProperty::AUDIO_STREAMS);
173 }
174
175 AudioMapping
176 AudioContent::audio_mapping () const
177 {
178         int channels = 0;
179         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
180                 channels += i->channels ();
181         }
182
183         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
184         merged.make_zero ();
185
186         int c = 0;
187         int s = 0;
188         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
189                 AudioMapping mapping = i->mapping ();
190                 for (int j = 0; j < mapping.input_channels(); ++j) {
191                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
192                                 if (k < mapping.output_channels()) {
193                                         merged.set (c, k, mapping.get (j, k));
194                                 }
195                         }
196                         ++c;
197                 }
198                 ++s;
199         }
200
201         return merged;
202 }
203
204 /** @return the frame rate that this content should be resampled to in order
205  *  that it is in sync with the active video content at its start time.
206  */
207 int
208 AudioContent::resampled_audio_frame_rate () const
209 {
210         /* Resample to a DCI-approved sample rate */
211         double t = has_rate_above_48k() ? 96000 : 48000;
212
213         FrameRateChange frc (audio_video_frame_rate(), film()->video_frame_rate());
214
215         /* Compensate if the DCP is being run at a different frame rate
216            to the source; that is, if the video is run such that it will
217            look different in the DCP compared to the source (slower or faster).
218         */
219
220         if (frc.change_speed) {
221                 t /= frc.speed_up;
222         }
223
224         return lrint (t);
225 }
226
227 string
228 AudioContent::processing_description () const
229 {
230         vector<AudioStreamPtr> streams = audio_streams ();
231         if (streams.empty ()) {
232                 return "";
233         }
234
235         /* Possible answers are:
236            1. all audio will be resampled from x to y.
237            2. all audio will be resampled to y (from a variety of rates)
238            3. some audio will be resampled to y (from a variety of rates)
239            4. nothing will be resampled.
240         */
241
242         bool not_resampled = false;
243         bool resampled = false;
244         bool same = true;
245
246         optional<int> common_frame_rate;
247         BOOST_FOREACH (AudioStreamPtr i, streams) {
248                 if (i->frame_rate() != resampled_audio_frame_rate()) {
249                         resampled = true;
250                 } else {
251                         not_resampled = true;
252                 }
253
254                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
255                         same = false;
256                 }
257                 common_frame_rate = i->frame_rate ();
258         }
259
260         if (not_resampled && !resampled) {
261                 return _("Audio will not be resampled");
262         }
263
264         if (not_resampled && resampled) {
265                 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
266         }
267
268         if (!not_resampled && resampled) {
269                 if (same) {
270                         return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
271                 } else {
272                         return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
273                 }
274         }
275
276         return "";
277 }
278
279 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
280 bool
281 AudioContent::has_rate_above_48k () const
282 {
283         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
284                 if (i->frame_rate() > 48000) {
285                         return true;
286                 }
287         }
288
289         return false;
290 }
291
292 /** @return User-visible names of each of our audio channels */
293 vector<string>
294 AudioContent::audio_channel_names () const
295 {
296         vector<string> n;
297
298         int t = 1;
299         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
300                 for (int j = 0; j < i->channels(); ++j) {
301                         n.push_back (String::compose ("%1:%2", t, j + 1));
302                 }
303                 ++t;
304         }
305
306         return n;
307 }
308
309 void
310 AudioContent::add_properties (list<UserProperty>& p) const
311 {
312         shared_ptr<const AudioStream> stream;
313         if (audio_streams().size() == 1) {
314                 stream = audio_streams().front ();
315         }
316
317         if (stream) {
318                 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
319                 p.push_back (UserProperty (_("Audio"), _("Content audio frame rate"), stream->frame_rate(), _("Hz")));
320         }
321
322         FrameRateChange const frc (audio_video_frame_rate(), film()->video_frame_rate());
323         ContentTime const c (full_length(), frc);
324
325         p.push_back (
326                 UserProperty (_("Length"), _("Full length in video frames at content rate"), c.frames_round(frc.source))
327                 );
328
329         if (stream) {
330                 p.push_back (
331                         UserProperty (
332                                 _("Length"),
333                                 _("Full length in audio frames at content rate"),
334                                 c.frames_round (stream->frame_rate ())
335                                 )
336                         );
337         }
338
339         p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_audio_frame_rate (), _("Hz")));
340         p.push_back (UserProperty (_("Length"), _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
341
342         if (stream) {
343                 p.push_back (
344                         UserProperty (
345                                 _("Length"),
346                                 _("Full length in audio frames at DCP rate"),
347                                 c.frames_round (resampled_audio_frame_rate ())
348                                 )
349                         );
350         }
351 }
352
353 void
354 AudioContent::set_audio_video_frame_rate (double r)
355 {
356         {
357                 boost::mutex::scoped_lock lm (_mutex);
358                 _audio_video_frame_rate = r;
359         }
360
361         signal_changed (AudioContentProperty::AUDIO_VIDEO_FRAME_RATE);
362 }
363
364 double
365 AudioContent::audio_video_frame_rate () const
366 {
367         {
368                 boost::mutex::scoped_lock lm (_mutex);
369                 if (_audio_video_frame_rate) {
370                         return _audio_video_frame_rate.get ();
371                 }
372         }
373
374         /* No frame rate specified, so assume this content has been
375            prepared for any concurrent video content.
376         */
377         return film()->active_frame_rate_change(position()).source;
378 }