Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2015 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::setprecision;
40 using boost::shared_ptr;
41 using boost::dynamic_pointer_cast;
42 using boost::optional;
43
44 /** Something stream-related has changed */
45 int const AudioContentProperty::AUDIO_STREAMS = 200;
46 int const AudioContentProperty::AUDIO_GAIN = 201;
47 int const AudioContentProperty::AUDIO_DELAY = 202;
48
49 AudioContent::AudioContent (shared_ptr<const Film> film)
50         : Content (film)
51         , _audio_gain (0)
52         , _audio_delay (Config::instance()->default_audio_delay ())
53 {
54
55 }
56
57 AudioContent::AudioContent (shared_ptr<const Film> film, DCPTime s)
58         : Content (film, s)
59         , _audio_gain (0)
60         , _audio_delay (Config::instance()->default_audio_delay ())
61 {
62
63 }
64
65 AudioContent::AudioContent (shared_ptr<const Film> film, boost::filesystem::path p)
66         : Content (film, p)
67         , _audio_gain (0)
68         , _audio_delay (Config::instance()->default_audio_delay ())
69 {
70
71 }
72
73 AudioContent::AudioContent (shared_ptr<const Film> film, cxml::ConstNodePtr node)
74         : Content (film, node)
75 {
76         _audio_gain = node->number_child<float> ("AudioGain");
77         _audio_delay = node->number_child<int> ("AudioDelay");
78 }
79
80 AudioContent::AudioContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
81         : Content (film, c)
82 {
83         shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
84         DCPOMATIC_ASSERT (ref);
85
86         for (size_t i = 0; i < c.size(); ++i) {
87                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
88
89                 if (ac->audio_gain() != ref->audio_gain()) {
90                         throw JoinError (_("Content to be joined must have the same audio gain."));
91                 }
92
93                 if (ac->audio_delay() != ref->audio_delay()) {
94                         throw JoinError (_("Content to be joined must have the same audio delay."));
95                 }
96         }
97
98         _audio_gain = ref->audio_gain ();
99         _audio_delay = ref->audio_delay ();
100 }
101
102 void
103 AudioContent::as_xml (xmlpp::Node* node) const
104 {
105         boost::mutex::scoped_lock lm (_mutex);
106         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
107         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
108 }
109
110
111 void
112 AudioContent::set_audio_gain (double g)
113 {
114         {
115                 boost::mutex::scoped_lock lm (_mutex);
116                 _audio_gain = g;
117         }
118
119         signal_changed (AudioContentProperty::AUDIO_GAIN);
120 }
121
122 void
123 AudioContent::set_audio_delay (int d)
124 {
125         {
126                 boost::mutex::scoped_lock lm (_mutex);
127                 _audio_delay = d;
128         }
129
130         signal_changed (AudioContentProperty::AUDIO_DELAY);
131 }
132
133 string
134 AudioContent::technical_summary () const
135 {
136         string s = "audio :";
137         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
138                 s += String::compose ("stream channels %1 rate %2", i->channels(), i->frame_rate());
139         }
140
141         return s;
142 }
143
144 void
145 AudioContent::set_audio_mapping (AudioMapping mapping)
146 {
147         int c = 0;
148         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
149                 AudioMapping stream_mapping (i->channels (), MAX_DCP_AUDIO_CHANNELS);
150                 for (int j = 0; j < i->channels(); ++j) {
151                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
152                                 stream_mapping.set (j, k, mapping.get (c, k));
153                         }
154                         ++c;
155                 }
156                 i->set_mapping (stream_mapping);
157         }
158
159         signal_changed (AudioContentProperty::AUDIO_STREAMS);
160 }
161
162 AudioMapping
163 AudioContent::audio_mapping () const
164 {
165         int channels = 0;
166         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
167                 channels += i->channels ();
168         }
169
170         AudioMapping merged (channels, MAX_DCP_AUDIO_CHANNELS);
171
172         int c = 0;
173         int s = 0;
174         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
175                 AudioMapping mapping = i->mapping ();
176                 for (int j = 0; j < mapping.input_channels(); ++j) {
177                         for (int k = 0; k < MAX_DCP_AUDIO_CHANNELS; ++k) {
178                                 merged.set (c, k, mapping.get (j, k));
179                         }
180                         ++c;
181                 }
182                 ++s;
183         }
184
185         return merged;
186 }
187
188 /** @return the frame rate that this content should be resampled to in order
189  *  that it is in sync with the active video content at its start time.
190  */
191 int
192 AudioContent::resampled_audio_frame_rate () const
193 {
194         shared_ptr<const Film> film = _film.lock ();
195         DCPOMATIC_ASSERT (film);
196
197         /* Resample to a DCI-approved sample rate */
198         double t = has_rate_above_48k() ? 96000 : 48000;
199
200         FrameRateChange frc = film->active_frame_rate_change (position ());
201
202         /* Compensate if the DCP is being run at a different frame rate
203            to the source; that is, if the video is run such that it will
204            look different in the DCP compared to the source (slower or faster).
205         */
206
207         if (frc.change_speed) {
208                 t /= frc.speed_up;
209         }
210
211         return lrint (t);
212 }
213
214 string
215 AudioContent::processing_description () const
216 {
217         vector<AudioStreamPtr> streams = audio_streams ();
218         if (streams.empty ()) {
219                 return "";
220         }
221
222         /* Possible answers are:
223            1. all audio will be resampled from x to y.
224            2. all audio will be resampled to y (from a variety of rates)
225            3. some audio will be resampled to y (from a variety of rates)
226            4. nothing will be resampled.
227         */
228
229         bool not_resampled = false;
230         bool resampled = false;
231         bool same = true;
232
233         optional<int> common_frame_rate;
234         BOOST_FOREACH (AudioStreamPtr i, streams) {
235                 if (i->frame_rate() != resampled_audio_frame_rate()) {
236                         resampled = true;
237                 } else {
238                         not_resampled = true;
239                 }
240
241                 if (common_frame_rate && common_frame_rate != i->frame_rate ()) {
242                         same = false;
243                 }
244                 common_frame_rate = i->frame_rate ();
245         }
246
247         if (not_resampled && !resampled) {
248                 return _("Audio will not be resampled");
249         }
250
251         if (not_resampled && resampled) {
252                 return String::compose (_("Some audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
253         }
254
255         if (!not_resampled && resampled) {
256                 if (same) {
257                         return String::compose (_("Audio will be resampled from %1kHz to %2kHz"), common_frame_rate.get(), resampled_audio_frame_rate ());
258                 } else {
259                         return String::compose (_("Audio will be resampled to %1kHz"), resampled_audio_frame_rate ());
260                 }
261         }
262
263         return "";
264 }
265
266 /** @return true if any stream in this content has a sampling rate of more than 48kHz */
267 bool
268 AudioContent::has_rate_above_48k () const
269 {
270         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
271                 if (i->frame_rate() > 48000) {
272                         return true;
273                 }
274         }
275
276         return false;
277 }
278
279 /** @return User-visible names of each of our audio channels */
280 vector<string>
281 AudioContent::audio_channel_names () const
282 {
283         vector<string> n;
284
285         int t = 1;
286         BOOST_FOREACH (AudioStreamPtr i, audio_streams ()) {
287                 for (int j = 0; j < i->channels(); ++j) {
288                         n.push_back (String::compose ("%1:%2", t, j + 1));
289                 }
290                 ++t;
291         }
292
293         return n;
294 }