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