Various improvements to the content properties dialogue (including #791).
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "audio_content.h"
21 #include "film.h"
22 #include "exceptions.h"
23 #include "config.h"
24 #include "frame_rate_change.h"
25 #include "raw_convert.h"
26 #include "compose.hpp"
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::cout;
36 using std::vector;
37 using std::stringstream;
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
46 /** Something stream-related has changed */
47 int const AudioContentProperty::AUDIO_STREAMS = 200;
48 int const AudioContentProperty::AUDIO_GAIN = 201;
49 int const AudioContentProperty::AUDIO_DELAY = 202;
50
51 AudioContent::AudioContent (shared_ptr<const Film> film)
52         : Content (film)
53         , _audio_gain (0)
54         , _audio_delay (Config::instance()->default_audio_delay ())
55 {
56
57 }
58
59 AudioContent::AudioContent (shared_ptr<const Film> film, DCPTime s)
60         : Content (film, s)
61         , _audio_gain (0)
62         , _audio_delay (Config::instance()->default_audio_delay ())
63 {
64
65 }
66
67 AudioContent::AudioContent (shared_ptr<const Film> film, boost::filesystem::path p)
68         : Content (film, p)
69         , _audio_gain (0)
70         , _audio_delay (Config::instance()->default_audio_delay ())
71 {
72
73 }
74
75 AudioContent::AudioContent (shared_ptr<const Film> film, cxml::ConstNodePtr node)
76         : Content (film, node)
77 {
78         _audio_gain = node->number_child<double> ("AudioGain");
79         _audio_delay = node->number_child<int> ("AudioDelay");
80 }
81
82 AudioContent::AudioContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
83         : Content (film, c)
84 {
85         shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
86         DCPOMATIC_ASSERT (ref);
87
88         for (size_t i = 0; i < c.size(); ++i) {
89                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
90
91                 if (ac->audio_gain() != ref->audio_gain()) {
92                         throw JoinError (_("Content to be joined must have the same audio gain."));
93                 }
94
95                 if (ac->audio_delay() != ref->audio_delay()) {
96                         throw JoinError (_("Content to be joined must have the same audio delay."));
97                 }
98         }
99
100         _audio_gain = ref->audio_gain ();
101         _audio_delay = ref->audio_delay ();
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> (_audio_gain));
109         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
110 }
111
112
113 void
114 AudioContent::set_audio_gain (double g)
115 {
116         {
117                 boost::mutex::scoped_lock lm (_mutex);
118                 _audio_gain = g;
119         }
120
121         signal_changed (AudioContentProperty::AUDIO_GAIN);
122 }
123
124 void
125 AudioContent::set_audio_delay (int d)
126 {
127         {
128                 boost::mutex::scoped_lock lm (_mutex);
129                 _audio_delay = d;
130         }
131
132         signal_changed (AudioContentProperty::AUDIO_DELAY);
133 }
134
135 string
136 AudioContent::technical_summary () const
137 {
138         string s = "audio :";
139         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
140                 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
141         }
142
143         return s;
144 }
145
146 void
147 AudioContent::set_audio_mapping (AudioMapping mapping)
148 {
149         int c = 0;
150         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
151                 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
152                 for (int j = 0; j < i->channels(); ++j) {
153                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
154                                 stream_mapping.set (j, k, mapping.get (c, k));
155                         }
156                         ++c;
157                 }
158                 i->set_mapping (stream_mapping);
159         }
160
161         signal_changed (AudioContentProperty::AUDIO_STREAMS);
162 }
163
164 AudioMapping
165 AudioContent::audio_mapping () const
166 {
167         int channels = 0;
168         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
169                 channels += i->channels ();
170         }
171
172         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
173         merged.make_zero ();
174
175         int c = 0;
176         int s = 0;
177         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
178                 AudioMapping mapping = i->mapping ();
179                 for (int j = 0; j < mapping.input_channels(); ++j) {
180                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
181                                 if (k < mapping.output_channels()) {
182                                         merged.set (c, k, mapping.get (j, k));
183                                 }
184                         }
185                         ++c;
186                 }
187                 ++s;
188         }
189
190         return merged;
191 }
192
193 /** @return the frame rate that this content should be resampled to in order
194  *  that it is in sync with the active video content at its start time.
195  */
196 int
197 AudioContent::resampled_audio_frame_rate () const
198 {
199         /* Resample to a DCI-approved sample rate */
200         double t = has_rate_above_48k() ? 96000 : 48000;
201
202         FrameRateChange frc = film()->active_frame_rate_change (position ());
203
204         /* Compensate if the DCP is being run at a different frame rate
205            to the source; that is, if the video is run such that it will
206            look different in the DCP compared to the source (slower or faster).
207         */
208
209         if (frc.change_speed) {
210                 t /= frc.speed_up;
211         }
212
213         return lrint (t);
214 }
215
216 string
217 AudioContent::processing_description () const
218 {
219         vector<AudioStreamPtr> streams = audio_streams ();
220         if (streams.empty ()) {
221                 return "";
222         }
223
224         /* Possible answers are:
225            1. all audio will be resampled from x to y.
226            2. all audio will be resampled to y (from a variety of rates)
227            3. some audio will be resampled to y (from a variety of rates)
228            4. nothing will be resampled.
229         */
230
231         bool not_resampled = false;
232         bool resampled = false;
233         bool same = true;
234
235         optional<int> common_frame_rate;
236         BOOST_FOREACH (AudioStreamPtr i, streams) {
237                 if (i->frame_rate() != resampled_audio_frame_rate()) {
238                         resampled = true;
239                 } else {
240                         not_resampled = true;
241                 }
242
243                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
244                         same = false;
245                 }
246                 common_frame_rate = i->frame_rate ();
247         }
248
249         if (not_resampled && !resampled) {
250                 return _("Audio will not be resampled");
251         }
252
253         if (not_resampled && resampled) {
254                 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
255         }
256
257         if (!not_resampled && resampled) {
258                 if (same) {
259                         return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
260                 } else {
261                         return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
262                 }
263         }
264
265         return "";
266 }
267
268 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
269 bool
270 AudioContent::has_rate_above_48k () const
271 {
272         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
273                 if (i->frame_rate() > 48000) {
274                         return true;
275                 }
276         }
277
278         return false;
279 }
280
281 /** @return User-visible names of each of our audio channels */
282 vector<string>
283 AudioContent::audio_channel_names () const
284 {
285         vector<string> n;
286
287         int t = 1;
288         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
289                 for (int j = 0; j < i->channels(); ++j) {
290                         n.push_back (String::compose ("%1:%2", t, j + 1));
291                 }
292                 ++t;
293         }
294
295         return n;
296 }
297
298 void
299 AudioContent::add_properties (list<UserProperty>& p) const
300 {
301         shared_ptr<const AudioStream> stream;
302         if (audio_streams().size() == 1) {
303                 stream = audio_streams().front ();
304         }
305
306         if (stream) {
307                 p.push_back (UserProperty (_("Audio"), _("Channels"), stream->channels ()));
308                 p.push_back (UserProperty (_("Audio"), _("Content audio frame rate"), stream->frame_rate(), _("Hz")));
309         }
310
311         shared_ptr<const Film> film = _film.lock ();
312         DCPOMATIC_ASSERT (film);
313
314         FrameRateChange const frc = film->active_frame_rate_change (position ());
315         ContentTime const c (full_length(), frc);
316
317         p.push_back (
318                 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                                 _("Length"),
325                                 _("Full length in audio frames at content rate"),
326                                 c.frames_round (stream->frame_rate ())
327                                 )
328                         );
329         }
330
331         p.push_back (UserProperty (_("Audio"), _("DCP frame rate"), resampled_audio_frame_rate ()));
332         p.push_back (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                                 _("Length"),
338                                 _("Full length in audio frames at DCP rate"),
339                                 c.frames_round (resampled_audio_frame_rate ())
340                                 )
341                         );
342         }
343 }