C++11 tidying.
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "audio_content.h"
23 #include "compose.hpp"
24 #include "config.h"
25 #include "exceptions.h"
26 #include "film.h"
27 #include "frame_rate_change.h"
28 #include <dcp/raw_convert.h>
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
31 #include <iostream>
32
33 #include "i18n.h"
34
35
36 using std::cout;
37 using std::dynamic_pointer_cast;
38 using std::fixed;
39 using std::list;
40 using std::make_shared;
41 using std::pair;
42 using std::setprecision;
43 using std::shared_ptr;
44 using std::string;
45 using std::vector;
46 using boost::optional;
47 using dcp::raw_convert;
48 using namespace dcpomatic;
49
50
51 /** Something stream-related has changed */
52 int const AudioContentProperty::STREAMS = 200;
53 int const AudioContentProperty::GAIN = 201;
54 int const AudioContentProperty::DELAY = 202;
55
56
57 AudioContent::AudioContent (Content* parent)
58         : ContentPart (parent)
59         , _delay (Config::instance()->default_audio_delay())
60 {
61
62 }
63
64
65 shared_ptr<AudioContent>
66 AudioContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
67 {
68         if (version < 34) {
69                 /* With old metadata FFmpeg content has the audio-related tags even with no
70                    audio streams, so check for that.
71                 */
72                 if (node->string_child("Type") == "FFmpeg" && node->node_children("AudioStream").empty()) {
73                         return {};
74                 }
75
76                 /* Otherwise we can drop through to the newer logic */
77         }
78
79         if (!node->optional_number_child<double> ("AudioGain")) {
80                 return {};
81         }
82
83         return make_shared<AudioContent>(parent, node);
84 }
85
86
87 AudioContent::AudioContent (Content* parent, cxml::ConstNodePtr node)
88         : ContentPart (parent)
89 {
90         _gain = node->number_child<double> ("AudioGain");
91         _delay = node->number_child<int> ("AudioDelay");
92
93         /* Backwards compatibility */
94         auto r = node->optional_number_child<double>("AudioVideoFrameRate");
95         if (r) {
96                 _parent->set_video_frame_rate (r.get());
97         }
98 }
99
100
101 AudioContent::AudioContent (Content* parent, vector<shared_ptr<Content> > c)
102         : ContentPart (parent)
103 {
104         auto ref = c[0]->audio;
105         DCPOMATIC_ASSERT (ref);
106
107         for (size_t i = 1; i < c.size(); ++i) {
108                 if (c[i]->audio->gain() != ref->gain()) {
109                         throw JoinError (_("Content to be joined must have the same audio gain."));
110                 }
111
112                 if (c[i]->audio->delay() != ref->delay()) {
113                         throw JoinError (_("Content to be joined must have the same audio delay."));
114                 }
115         }
116
117         _gain = ref->gain ();
118         _delay = ref->delay ();
119         _streams = ref->streams ();
120 }
121
122
123 void
124 AudioContent::as_xml (xmlpp::Node* node) const
125 {
126         boost::mutex::scoped_lock lm (_mutex);
127         node->add_child("AudioGain")->add_child_text(raw_convert<string>(_gain));
128         node->add_child("AudioDelay")->add_child_text(raw_convert<string>(_delay));
129 }
130
131
132 void
133 AudioContent::set_gain (double g)
134 {
135         maybe_set (_gain, g, AudioContentProperty::GAIN);
136 }
137
138
139 void
140 AudioContent::set_delay (int d)
141 {
142         maybe_set (_delay, d, AudioContentProperty::DELAY);
143 }
144
145
146 string
147 AudioContent::technical_summary () const
148 {
149         string s = "audio: ";
150         for (auto i: streams()) {
151                 s += String::compose ("stream channels %1 rate %2 ", i->channels(), i->frame_rate());
152         }
153
154         return s;
155 }
156
157
158 void
159 AudioContent::set_mapping (AudioMapping mapping)
160 {
161         ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
162
163         int c = 0;
164         for (auto i: streams()) {
165                 AudioMapping stream_mapping (i->channels(), MAX_DCP_AUDIO_CHANNELS);
166                 for (int j = 0; j < i->channels(); ++j) {
167                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
168                                 stream_mapping.set (j, k, mapping.get(c, k));
169                         }
170                         ++c;
171                 }
172                 i->set_mapping (stream_mapping);
173         }
174 }
175
176
177 AudioMapping
178 AudioContent::mapping () const
179 {
180         int channels = 0;
181         for (auto i: streams()) {
182                 channels += i->channels ();
183         }
184
185         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
186         merged.make_zero ();
187
188         int c = 0;
189         int s = 0;
190         for (auto i: streams()) {
191                 auto mapping = i->mapping ();
192                 for (int j = 0; j < mapping.input_channels(); ++j) {
193                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
194                                 if (k < mapping.output_channels()) {
195                                         merged.set (c, k, mapping.get(j, k));
196                                 }
197                         }
198                         ++c;
199                 }
200                 ++s;
201         }
202
203         return merged;
204 }
205
206
207 /** @return the frame rate that this content should be resampled to in order
208  *  that it is in sync with the active video content at its start time.
209  */
210 int
211 AudioContent::resampled_frame_rate (shared_ptr<const Film> film) const
212 {
213         double t = film->audio_frame_rate ();
214
215         FrameRateChange frc (film, _parent);
216
217         /* Compensate if the DCP is being run at a different frame rate
218            to the source; that is, if the video is run such that it will
219            look different in the DCP compared to the source (slower or faster).
220         */
221
222         if (frc.change_speed) {
223                 t /= frc.speed_up;
224         }
225
226         return lrint (t);
227 }
228
229 string
230 AudioContent::processing_description (shared_ptr<const Film> film) const
231 {
232         if (streams().empty()) {
233                 return "";
234         }
235
236         /* Possible answers are:
237            1. all audio will be resampled from x to y.
238            2. all audio will be resampled to y (from a variety of rates)
239            3. some audio will be resampled to y (from a variety of rates)
240            4. nothing will be resampled.
241         */
242
243         bool not_resampled = false;
244         bool resampled = false;
245         bool same = true;
246
247         optional<int> common_frame_rate;
248         for (auto i: streams()) {
249                 if (i->frame_rate() != resampled_frame_rate(film)) {
250                         resampled = true;
251                 } else {
252                         not_resampled = true;
253                 }
254
255                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
256                         same = false;
257                 }
258                 common_frame_rate = i->frame_rate ();
259         }
260
261         if (not_resampled && !resampled) {
262                 return _("Audio will not be resampled");
263         }
264
265         if (not_resampled && resampled) {
266                 return String::compose (_("Some audio will be resampled to %1Hz"), resampled_frame_rate(film));
267         }
268
269         if (!not_resampled && resampled) {
270                 if (same) {
271                         return String::compose (_("Audio will be resampled from %1Hz to %2Hz"), common_frame_rate.get(), resampled_frame_rate(film));
272                 } else {
273                         return String::compose (_("Audio will be resampled to %1Hz"), resampled_frame_rate(film));
274                 }
275         }
276
277         return "";
278 }
279
280
281 /** @return User-visible names of each of our audio channels */
282 vector<NamedChannel>
283 AudioContent::channel_names () const
284 {
285         vector<NamedChannel> n;
286
287         int index = 0;
288         int stream = 1;
289         for (auto i: streams()) {
290                 for (int j = 0; j < i->channels(); ++j) {
291                         n.push_back (NamedChannel(String::compose ("%1:%2", stream, j + 1), index++));
292                 }
293                 ++stream;
294         }
295
296         return n;
297 }
298
299
300 void
301 AudioContent::add_properties (shared_ptr<const Film> film, list<UserProperty>& p) const
302 {
303         shared_ptr<const AudioStream> stream;
304         if (streams().size() == 1) {
305                 stream = streams().front();
306         }
307
308         if (stream) {
309                 p.push_back (UserProperty(UserProperty::AUDIO, _("Channels"), stream->channels()));
310                 p.push_back (UserProperty(UserProperty::AUDIO, _("Content audio sample rate"), stream->frame_rate(), _("Hz")));
311         }
312
313         FrameRateChange const frc (_parent->active_video_frame_rate(film), film->video_frame_rate());
314         ContentTime const c (_parent->full_length(film), frc);
315
316         p.push_back (
317                 UserProperty (UserProperty::LENGTH, _("Full length in video frames at content rate"), c.frames_round(frc.source))
318                 );
319
320         if (stream) {
321                 p.push_back (
322                         UserProperty (
323                                 UserProperty::LENGTH,
324                                 _("Full length in audio samples at content rate"),
325                                 c.frames_round (stream->frame_rate ())
326                                 )
327                         );
328         }
329
330         p.push_back (UserProperty(UserProperty::AUDIO, _("DCP sample rate"), resampled_frame_rate(film), _("Hz")));
331         p.push_back (UserProperty(UserProperty::LENGTH, _("Full length in video frames at DCP rate"), c.frames_round (frc.dcp)));
332
333         if (stream) {
334                 p.push_back (
335                         UserProperty (
336                                 UserProperty::LENGTH,
337                                 _("Full length in audio samples at DCP rate"),
338                                 c.frames_round(resampled_frame_rate(film))
339                                 )
340                         );
341         }
342 }
343
344
345 void
346 AudioContent::set_streams (vector<AudioStreamPtr> streams)
347 {
348         ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
349
350         {
351                 boost::mutex::scoped_lock lm (_mutex);
352                 _streams = streams;
353         }
354 }
355
356
357 AudioStreamPtr
358 AudioContent::stream () const
359 {
360         boost::mutex::scoped_lock lm (_mutex);
361         DCPOMATIC_ASSERT (_streams.size() == 1);
362         return _streams.front ();
363 }
364
365
366 void
367 AudioContent::add_stream (AudioStreamPtr stream)
368 {
369         ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
370
371         {
372                 boost::mutex::scoped_lock lm (_mutex);
373                 _streams.push_back (stream);
374         }
375 }
376
377
378 void
379 AudioContent::set_stream (AudioStreamPtr stream)
380 {
381         ContentChangeSignaller cc (_parent, AudioContentProperty::STREAMS);
382
383         {
384                 boost::mutex::scoped_lock lm (_mutex);
385                 _streams.clear ();
386                 _streams.push_back (stream);
387         }
388 }
389
390
391 void
392 AudioContent::take_settings_from (shared_ptr<const AudioContent> c)
393 {
394         set_gain (c->_gain);
395         set_delay (c->_delay);
396
397         size_t i = 0;
398         size_t j = 0;
399
400         while (i < _streams.size() && j < c->_streams.size()) {
401                 _streams[i]->set_mapping (c->_streams[j]->mapping());
402                 ++i;
403                 ++j;
404         }
405 }
406
407
408 void
409 AudioContent::modify_position (shared_ptr<const Film> film, DCPTime& pos) const
410 {
411         pos = pos.round (film->audio_frame_rate());
412 }
413
414
415 void
416 AudioContent::modify_trim_start (ContentTime& trim) const
417 {
418         DCPOMATIC_ASSERT (!_streams.empty());
419         /* XXX: we're in trouble if streams have different rates */
420         trim = trim.round (_streams.front()->frame_rate());
421 }