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