Move video frame rate ('prepared-for') into Content.
[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::STREAMS = 200;
48 int const AudioContentProperty::GAIN = 201;
49 int const AudioContentProperty::DELAY = 202;
50
51 AudioContent::AudioContent (Content* parent, shared_ptr<const Film> film)
52         : ContentPart (parent, film)
53         , _gain (0)
54         , _delay (Config::instance()->default_audio_delay ())
55 {
56
57 }
58
59 AudioContent::AudioContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node)
60         : ContentPart (parent, film)
61 {
62         _gain = node->number_child<double> ("AudioGain");
63         _delay = node->number_child<int> ("AudioDelay");
64
65         /* Backwards compatibility */
66         optional<double> r = node->optional_number_child<double> ("AudioVideoFrameRate");
67         if (r) {
68                 _parent->set_video_frame_rate (r.get ());
69         }
70 }
71
72 AudioContent::AudioContent (Content* parent, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
73         : ContentPart (parent, film)
74 {
75         shared_ptr<AudioContent> ref = c[0]->audio;
76         DCPOMATIC_ASSERT (ref);
77
78         for (size_t i = 1; i < c.size(); ++i) {
79                 if (c[i]->audio->gain() != ref->gain()) {
80                         throw JoinError (_("Content to be joined must have the same audio gain."));
81                 }
82
83                 if (c[i]->audio->delay() != ref->delay()) {
84                         throw JoinError (_("Content to be joined must have the same audio delay."));
85                 }
86         }
87
88         _gain = ref->gain ();
89         _delay = ref->delay ();
90         _streams = ref->streams ();
91 }
92
93 void
94 AudioContent::as_xml (xmlpp::Node* node) const
95 {
96         boost::mutex::scoped_lock lm (_mutex);
97         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_gain));
98         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_delay));
99 }
100
101 void
102 AudioContent::set_gain (double g)
103 {
104         maybe_set (_gain, g, AudioContentProperty::GAIN);
105 }
106
107 void
108 AudioContent::set_delay (int d)
109 {
110         maybe_set (_delay, d, AudioContentProperty::DELAY);
111 }
112
113 string
114 AudioContent::technical_summary () const
115 {
116         string s = "audio :";
117         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
118                 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
119         }
120
121         return s;
122 }
123
124 void
125 AudioContent::set_mapping (AudioMapping mapping)
126 {
127         int c = 0;
128         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
129                 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
130                 for (int j = 0; j < i->channels(); ++j) {
131                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
132                                 stream_mapping.set (j, k, mapping.get (c, k));
133                         }
134                         ++c;
135                 }
136                 i->set_mapping (stream_mapping);
137         }
138
139         _parent->signal_changed (AudioContentProperty::STREAMS);
140 }
141
142 AudioMapping
143 AudioContent::mapping () const
144 {
145         int channels = 0;
146         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
147                 channels += i->channels ();
148         }
149
150         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
151         merged.make_zero ();
152
153         int c = 0;
154         int s = 0;
155         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
156                 AudioMapping mapping = i->mapping ();
157                 for (int j = 0; j < mapping.input_channels(); ++j) {
158                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
159                                 if (k < mapping.output_channels()) {
160                                         merged.set (c, k, mapping.get (j, k));
161                                 }
162                         }
163                         ++c;
164                 }
165                 ++s;
166         }
167
168         return merged;
169 }
170
171 /** @return the frame rate that this content should be resampled to in order
172  *  that it is in sync with the active video content at its start time.
173  */
174 int
175 AudioContent::resampled_frame_rate () const
176 {
177         /* Resample to a DCI-approved sample rate */
178         double t = has_rate_above_48k() ? 96000 : 48000;
179
180         shared_ptr<const Film> film = _film.lock ();
181         DCPOMATIC_ASSERT (film);
182         FrameRateChange frc (_parent->active_video_frame_rate(), film->video_frame_rate());
183
184         /* Compensate if the DCP is being run at a different frame rate
185            to the source; that is, if the video is run such that it will
186            look different in the DCP compared to the source (slower or faster).
187         */
188
189         if (frc.change_speed) {
190                 t /= frc.speed_up;
191         }
192
193         return lrint (t);
194 }
195
196 string
197 AudioContent::processing_description () const
198 {
199         if (streams().empty ()) {
200                 return "";
201         }
202
203         /* Possible answers are:
204            1. all audio will be resampled from x to y.
205            2. all audio will be resampled to y (from a variety of rates)
206            3. some audio will be resampled to y (from a variety of rates)
207            4. nothing will be resampled.
208         */
209
210         bool not_resampled = false;
211         bool resampled = false;
212         bool same = true;
213
214         optional<int> common_frame_rate;
215         BOOST_FOREACH (AudioStreamPtr i, streams()) {
216                 if (i->frame_rate() != resampled_frame_rate()) {
217                         resampled = true;
218                 } else {
219                         not_resampled = true;
220                 }
221
222                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
223                         same = false;
224                 }
225                 common_frame_rate = i->frame_rate ();
226         }
227
228         if (not_resampled && !resampled) {
229                 return _("Audio will not be resampled");
230         }
231
232         if (not_resampled && resampled) {
233                 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_frame_rate ());
234         }
235
236         if (!not_resampled && resampled) {
237                 if (same) {
238                         return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_frame_rate ());
239                 } else {
240                         return String::compose (_("Audio will be resampled to %1kHz"), resampled_frame_rate ());
241                 }
242         }
243
244         return "";
245 }
246
247 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
248 bool
249 AudioContent::has_rate_above_48k () const
250 {
251         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
252                 if (i->frame_rate() > 48000) {
253                         return true;
254                 }
255         }
256
257         return false;
258 }
259
260 /** @return User-visible names of each of our audio channels */
261 vector<string>
262 AudioContent::channel_names () const
263 {
264         vector<string> n;
265
266         int t = 1;
267         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
268                 for (int j = 0; j < i->channels(); ++j) {
269                         n.push_back (String::compose ("%1:%2", t, j + 1));
270                 }
271                 ++t;
272         }
273
274         return n;
275 }
276
277 void
278 AudioContent::add_properties (list<UserProperty>& p) const
279 {
280         shared_ptr<const AudioStream> stream;
281         if (streams().size() == 1) {
282                 stream = streams().front ();
283         }
284
285         if (stream) {
286                 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
287                 p.push_back (UserProperty (_("Audio"), _("Content audio frame rate"), stream->frame_rate(), _("Hz")));
288         }
289
290         shared_ptr<const Film> film = _film.lock ();
291         DCPOMATIC_ASSERT (film);
292
293         FrameRateChange const frc (_parent->active_video_frame_rate(), film->video_frame_rate());
294         ContentTime const c (_parent->full_length(), frc);
295
296         p.push_back (
297                 UserProperty (_("Length"), _("Full length in video frames at content rate"), c.frames_round(frc.source))
298                 );
299
300         if (stream) {
301                 p.push_back (
302                         UserProperty (
303                                 _("Length"),
304                                 _("Full length in audio frames at content rate"),
305                                 c.frames_round (stream->frame_rate ())
306                                 )
307                         );
308         }
309
310         p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_frame_rate (), _("Hz")));
311         p.push_back (UserProperty (_("Length"), _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
312
313         if (stream) {
314                 p.push_back (
315                         UserProperty (
316                                 _("Length"),
317                                 _("Full length in audio frames at DCP rate"),
318                                 c.frames_round (resampled_frame_rate ())
319                                 )
320                         );
321         }
322 }
323
324 void
325 AudioContent::set_streams (vector<AudioStreamPtr> streams)
326 {
327         {
328                 boost::mutex::scoped_lock lm (_mutex);
329                 _streams = streams;
330         }
331
332         _parent->signal_changed (AudioContentProperty::STREAMS);
333 }
334
335 AudioStreamPtr
336 AudioContent::stream () const
337 {
338         boost::mutex::scoped_lock lm (_mutex);
339         DCPOMATIC_ASSERT (_streams.size() == 1);
340         return _streams.front ();
341 }
342
343 void
344 AudioContent::add_stream (AudioStreamPtr stream)
345 {
346         {
347                 boost::mutex::scoped_lock lm (_mutex);
348                 _streams.push_back (stream);
349         }
350
351         _parent->signal_changed (AudioContentProperty::STREAMS);
352 }
353
354 void
355 AudioContent::set_stream (AudioStreamPtr stream)
356 {
357         {
358                 boost::mutex::scoped_lock lm (_mutex);
359                 _streams.clear ();
360                 _streams.push_back (stream);
361         }
362
363         _parent->signal_changed (AudioContentProperty::STREAMS);
364 }