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