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