4e9d5cd5cee5395b3a22058fd0cf6c08f57e1e57
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "audio_content.h"
22 #include "film.h"
23 #include "exceptions.h"
24 #include "config.h"
25 #include "frame_rate_change.h"
26 #include "raw_convert.h"
27 #include "compose.hpp"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <boost/foreach.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::cout;
37 using std::vector;
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)
52         : ContentPart (parent)
53         , _gain (0)
54         , _delay (Config::instance()->default_audio_delay ())
55 {
56
57 }
58
59 shared_ptr<AudioContent>
60 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
61 {
62         if (version < 34) {
63                 /* With old metadata FFmpeg content has the audio-related tags even with no
64                    audio streams, so check for that.
65                 */
66                 if (node->string_child("Type") == "FFmpeg" && node->node_children("AudioStream").empty()) {
67                         return shared_ptr<AudioContent> ();
68                 }
69
70                 /* Otherwise we can drop through to the newer logic */
71         }
72
73         if (!node->optional_number_child<double> ("AudioGain")) {
74                 return shared_ptr<AudioContent> ();
75         }
76
77         return shared_ptr<AudioContent> (new AudioContent (parent, node));
78 }
79
80 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
81         : ContentPart (parent)
82 {
83         _gain = node->number_child<double> ("AudioGain");
84         _delay = node->number_child<int> ("AudioDelay");
85
86         /* Backwards compatibility */
87         optional<double> r = node->optional_number_child<double> ("AudioVideoFrameRate");
88         if (r) {
89                 _parent->set_video_frame_rate (r.get ());
90         }
91 }
92
93 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
94         : ContentPart (parent)
95 {
96         shared_ptr<AudioContent> ref = c[0]->audio;
97         DCPOMATIC_ASSERT (ref);
98
99         for (size_t i = 1; i < c.size(); ++i) {
100                 if (c[i]->audio->gain() != ref->gain()) {
101                         throw JoinError (_("Content to be joined must have the same audio gain."));
102                 }
103
104                 if (c[i]->audio->delay() != ref->delay()) {
105                         throw JoinError (_("Content to be joined must have the same audio delay."));
106                 }
107         }
108
109         _gain = ref->gain ();
110         _delay = ref->delay ();
111         _streams = ref->streams ();
112 }
113
114 void
115 AudioContent::as_xml (xmlpp::Node* node) const
116 {
117         boost::mutex::scoped_lock lm (_mutex);
118         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_gain));
119         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_delay));
120 }
121
122 void
123 AudioContent::set_gain (double g)
124 {
125         maybe_set (_gain, g, AudioContentProperty::GAIN);
126 }
127
128 void
129 AudioContent::set_delay (int d)
130 {
131         maybe_set (_delay, d, AudioContentProperty::DELAY);
132 }
133
134 string
135 AudioContent::technical_summary () const
136 {
137         string s = "audio :";
138         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
139                 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
140         }
141
142         return s;
143 }
144
145 void
146 AudioContent::set_mapping (AudioMapping mapping)
147 {
148         int c = 0;
149         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
150                 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
151                 for (int j = 0; j < i->channels(); ++j) {
152                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
153                                 stream_mapping.set (j, k, mapping.get (c, k));
154                         }
155                         ++c;
156                 }
157                 i->set_mapping (stream_mapping);
158         }
159
160         _parent->signal_changed (AudioContentProperty::STREAMS);
161 }
162
163 AudioMapping
164 AudioContent::mapping () const
165 {
166         int channels = 0;
167         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
168                 channels += i->channels ();
169         }
170
171         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
172         merged.make_zero ();
173
174         int c = 0;
175         int s = 0;
176         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
177                 AudioMapping mapping = i->mapping ();
178                 for (int j = 0; j < mapping.input_channels(); ++j) {
179                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
180                                 if (k < mapping.output_channels()) {
181                                         merged.set (c, k, mapping.get (j, k));
182                                 }
183                         }
184                         ++c;
185                 }
186                 ++s;
187         }
188
189         return merged;
190 }
191
192 /** @return the frame rate that this content should be resampled to in order
193  *  that it is in sync with the active video content at its start time.
194  */
195 int
196 AudioContent::resampled_frame_rate () const
197 {
198         /* Resample to a DCI-approved sample rate */
199         double t = has_rate_above_48k() ? 96000 : 48000;
200
201         FrameRateChange frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
202
203         /* Compensate if the DCP is being run at a different frame rate
204            to the source; that is, if the video is run such that it will
205            look different in the DCP compared to the source (slower or faster).
206         */
207
208         if (frc.change_speed) {
209                 t /= frc.speed_up;
210         }
211
212         return lrint (t);
213 }
214
215 string
216 AudioContent::processing_description () const
217 {
218         if (streams().empty ()) {
219                 return "";
220         }
221
222         /* Possible answers are:
223            1. all audio will be resampled from x to y.
224            2. all audio will be resampled to y (from a variety of rates)
225            3. some audio will be resampled to y (from a variety of rates)
226            4. nothing will be resampled.
227         */
228
229         bool not_resampled = false;
230         bool resampled = false;
231         bool same = true;
232
233         optional<int> common_frame_rate;
234         BOOST_FOREACH (AudioStreamPtr i, streams()) {
235                 if (i->frame_rate() != resampled_frame_rate()) {
236                         resampled = true;
237                 } else {
238                         not_resampled = true;
239                 }
240
241                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
242                         same = false;
243                 }
244                 common_frame_rate = i->frame_rate ();
245         }
246
247         if (not_resampled && !resampled) {
248                 return _("Audio will not be resampled");
249         }
250
251         if (not_resampled && resampled) {
252                 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_frame_rate ());
253         }
254
255         if (!not_resampled && resampled) {
256                 if (same) {
257                         return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_frame_rate ());
258                 } else {
259                         return String::compose (_("Audio will be resampled to %1kHz"), resampled_frame_rate ());
260                 }
261         }
262
263         return "";
264 }
265
266 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
267 bool
268 AudioContent::has_rate_above_48k () const
269 {
270         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
271                 if (i->frame_rate() > 48000) {
272                         return true;
273                 }
274         }
275
276         return false;
277 }
278
279 /** @return User-visible names of each of our audio channels */
280 vector<string>
281 AudioContent::channel_names () const
282 {
283         vector<string> n;
284
285         int t = 1;
286         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
287                 for (int j = 0; j < i->channels(); ++j) {
288                         n.push_back (String::compose ("%1:%2", t, j + 1));
289                 }
290                 ++t;
291         }
292
293         return n;
294 }
295
296 void
297 AudioContent::add_properties (list<UserProperty>& p) const
298 {
299         shared_ptr<const AudioStream> stream;
300         if (streams().size() == 1) {
301                 stream = streams().front ();
302         }
303
304         if (stream) {
305                 p.push_back (UserProperty (UserProperty::AUDIO, _("Channels"), stream->channels ()));
306                 p.push_back (UserProperty (UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
307         }
308
309         FrameRateChange const frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
310         ContentTime const c (_parent->full_length(), frc);
311
312         p.push_back (
313                 UserProperty (UserProperty::LENGTH, _("Full length in video frames at content rate"), c.frames_round(frc.source))
314                 );
315
316         if (stream) {
317                 p.push_back (
318                         UserProperty (
319                                 UserProperty::LENGTH,
320                                 _("Full length in audio samples at content rate"),
321                                 c.frames_round (stream->frame_rate ())
322                                 )
323                         );
324         }
325
326         p.push_back (UserProperty (UserProperty::AUDIO, _("DCP sample rate"), resampled_frame_rate (), _("Hz")));
327         p.push_back (UserProperty (UserProperty::LENGTH, _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
328
329         if (stream) {
330                 p.push_back (
331                         UserProperty (
332                                 UserProperty::LENGTH,
333                                 _("Full length in audio samples at DCP rate"),
334                                 c.frames_round (resampled_frame_rate ())
335                                 )
336                         );
337         }
338 }
339
340 void
341 AudioContent::set_streams (vector<AudioStreamPtr> streams)
342 {
343         {
344                 boost::mutex::scoped_lock lm (_mutex);
345                 _streams = streams;
346         }
347
348         _parent->signal_changed (AudioContentProperty::STREAMS);
349 }
350
351 AudioStreamPtr
352 AudioContent::stream () const
353 {
354         boost::mutex::scoped_lock lm (_mutex);
355         DCPOMATIC_ASSERT (_streams.size() == 1);
356         return _streams.front ();
357 }
358
359 void
360 AudioContent::add_stream (AudioStreamPtr stream)
361 {
362         {
363                 boost::mutex::scoped_lock lm (_mutex);
364                 _streams.push_back (stream);
365         }
366
367         _parent->signal_changed (AudioContentProperty::STREAMS);
368 }
369
370 void
371 AudioContent::set_stream (AudioStreamPtr stream)
372 {
373         {
374                 boost::mutex::scoped_lock lm (_mutex);
375                 _streams.clear ();
376                 _streams.push_back (stream);
377         }
378
379         _parent->signal_changed (AudioContentProperty::STREAMS);
380 }