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