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