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