No-op; fix GPL address and use the explicit-program-name version.
[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::stringstream;
39 using std::fixed;
40 using std::list;
41 using std::pair;
42 using std::setprecision;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::optional;
46
47 /** Something stream-related has changed */
48 int const AudioContentProperty::STREAMS = 200;
49 int const AudioContentProperty::GAIN = 201;
50 int const AudioContentProperty::DELAY = 202;
51
52 AudioContent::AudioContent (Content* parent)
53         : ContentPart (parent)
54         , _gain (0)
55         , _delay (Config::instance()->default_audio_delay ())
56 {
57
58 }
59
60 shared_ptr<AudioContent>
61 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node)
62 {
63         if (!node->optional_number_child<double> ("AudioGain")) {
64                 return shared_ptr<AudioContent> ();
65         }
66
67         return shared_ptr<AudioContent> (new AudioContent (parent, node));
68 }
69
70 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
71         : ContentPart (parent)
72 {
73         _gain = node->number_child<double> ("AudioGain");
74         _delay = node->number_child<int> ("AudioDelay");
75
76         /* Backwards compatibility */
77         optional<double> r = node->optional_number_child<double> ("AudioVideoFrameRate");
78         if (r) {
79                 _parent->set_video_frame_rate (r.get ());
80         }
81 }
82
83 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
84         : ContentPart (parent)
85 {
86         shared_ptr<AudioContent> ref = c[0]->audio;
87         DCPOMATIC_ASSERT (ref);
88
89         for (size_t i = 1; i < c.size(); ++i) {
90                 if (c[i]->audio->gain() != ref->gain()) {
91                         throw JoinError (_("Content to be joined must have the same audio gain."));
92                 }
93
94                 if (c[i]->audio->delay() != ref->delay()) {
95                         throw JoinError (_("Content to be joined must have the same audio delay."));
96                 }
97         }
98
99         _gain = ref->gain ();
100         _delay = ref->delay ();
101         _streams = ref->streams ();
102 }
103
104 void
105 AudioContent::as_xml (xmlpp::Node* node) const
106 {
107         boost::mutex::scoped_lock lm (_mutex);
108         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_gain));
109         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_delay));
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         FrameRateChange frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
192
193         /* Compensate if the DCP is being run at a different frame rate
194            to the source; that is, if the video is run such that it will
195            look different in the DCP compared to the source (slower or faster).
196         */
197
198         if (frc.change_speed) {
199                 t /= frc.speed_up;
200         }
201
202         return lrint (t);
203 }
204
205 string
206 AudioContent::processing_description () const
207 {
208         if (streams().empty ()) {
209                 return "";
210         }
211
212         /* Possible answers are:
213            1. all audio will be resampled from x to y.
214            2. all audio will be resampled to y (from a variety of rates)
215            3. some audio will be resampled to y (from a variety of rates)
216            4. nothing will be resampled.
217         */
218
219         bool not_resampled = false;
220         bool resampled = false;
221         bool same = true;
222
223         optional<int> common_frame_rate;
224         BOOST_FOREACH (AudioStreamPtr i, streams()) {
225                 if (i->frame_rate() != resampled_frame_rate()) {
226                         resampled = true;
227                 } else {
228                         not_resampled = true;
229                 }
230
231                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
232                         same = false;
233                 }
234                 common_frame_rate = i->frame_rate ();
235         }
236
237         if (not_resampled && !resampled) {
238                 return _("Audio will not be resampled");
239         }
240
241         if (not_resampled && resampled) {
242                 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_frame_rate ());
243         }
244
245         if (!not_resampled && resampled) {
246                 if (same) {
247                         return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_frame_rate ());
248                 } else {
249                         return String::compose (_("Audio will be resampled to %1kHz"), resampled_frame_rate ());
250                 }
251         }
252
253         return "";
254 }
255
256 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
257 bool
258 AudioContent::has_rate_above_48k () const
259 {
260         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
261                 if (i->frame_rate() > 48000) {
262                         return true;
263                 }
264         }
265
266         return false;
267 }
268
269 /** @return User-visible names of each of our audio channels */
270 vector<string>
271 AudioContent::channel_names () const
272 {
273         vector<string> n;
274
275         int t = 1;
276         BOOST_FOREACH (AudioStreamPtr i, streams ()) {
277                 for (int j = 0; j < i->channels(); ++j) {
278                         n.push_back (String::compose ("%1:%2", t, j + 1));
279                 }
280                 ++t;
281         }
282
283         return n;
284 }
285
286 void
287 AudioContent::add_properties (list<UserProperty>& p) const
288 {
289         shared_ptr<const AudioStream> stream;
290         if (streams().size() == 1) {
291                 stream = streams().front ();
292         }
293
294         if (stream) {
295                 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
296                 p.push_back (UserProperty (_("Audio"), _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
297         }
298
299         FrameRateChange const frc (_parent->active_video_frame_rate(), _parent->film()->video_frame_rate());
300         ContentTime const c (_parent->full_length(), frc);
301
302         p.push_back (
303                 UserProperty (_("Length"), _("Full length in video frames at content rate"), c.frames_round(frc.source))
304                 );
305
306         if (stream) {
307                 p.push_back (
308                         UserProperty (
309                                 _("Length"),
310                                 _("Full length in audio samples at content rate"),
311                                 c.frames_round (stream->frame_rate ())
312                                 )
313                         );
314         }
315
316         p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_frame_rate (), _("Hz")));
317         p.push_back (UserProperty (_("Length"), _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
318
319         if (stream) {
320                 p.push_back (
321                         UserProperty (
322                                 _("Length"),
323                                 _("Full length in audio samples at DCP rate"),
324                                 c.frames_round (resampled_frame_rate ())
325                                 )
326                         );
327         }
328 }
329
330 void
331 AudioContent::set_streams (vector<AudioStreamPtr> streams)
332 {
333         {
334                 boost::mutex::scoped_lock lm (_mutex);
335                 _streams = streams;
336         }
337
338         _parent->signal_changed (AudioContentProperty::STREAMS);
339 }
340
341 AudioStreamPtr
342 AudioContent::stream () const
343 {
344         boost::mutex::scoped_lock lm (_mutex);
345         DCPOMATIC_ASSERT (_streams.size() == 1);
346         return _streams.front ();
347 }
348
349 void
350 AudioContent::add_stream (AudioStreamPtr stream)
351 {
352         {
353                 boost::mutex::scoped_lock lm (_mutex);
354                 _streams.push_back (stream);
355         }
356
357         _parent->signal_changed (AudioContentProperty::STREAMS);
358 }
359
360 void
361 AudioContent::set_stream (AudioStreamPtr stream)
362 {
363         {
364                 boost::mutex::scoped_lock lm (_mutex);
365                 _streams.clear ();
366                 _streams.push_back (stream);
367         }
368
369         _parent->signal_changed (AudioContentProperty::STREAMS);
370 }