Merge master.
[dcpomatic.git] / src / lib / audio_content.cc
1 /*
2     Copyright (C) 2013-2014 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 <libcxml/cxml.h>
21 #include <dcp/raw_convert.h>
22 #include "audio_content.h"
23 #include "analyse_audio_job.h"
24 #include "job_manager.h"
25 #include "film.h"
26 #include "exceptions.h"
27 #include "config.h"
28 #include "frame_rate_change.h"
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::cout;
34 using std::vector;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37 using dcp::raw_convert;
38
39 int const AudioContentProperty::AUDIO_CHANNELS = 200;
40 int const AudioContentProperty::AUDIO_LENGTH = 201;
41 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
42 int const AudioContentProperty::AUDIO_GAIN = 203;
43 int const AudioContentProperty::AUDIO_DELAY = 204;
44 int const AudioContentProperty::AUDIO_MAPPING = 205;
45
46 AudioContent::AudioContent (shared_ptr<const Film> f)
47         : Content (f)
48         , _audio_gain (0)
49         , _audio_delay (Config::instance()->default_audio_delay ())
50 {
51
52 }
53
54 AudioContent::AudioContent (shared_ptr<const Film> f, DCPTime s)
55         : Content (f, s)
56         , _audio_gain (0)
57         , _audio_delay (Config::instance()->default_audio_delay ())
58 {
59
60 }
61
62 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
63         : Content (f, p)
64         , _audio_gain (0)
65         , _audio_delay (Config::instance()->default_audio_delay ())
66 {
67
68 }
69
70 AudioContent::AudioContent (shared_ptr<const Film> f, cxml::ConstNodePtr node)
71         : Content (f, node)
72 {
73         _audio_gain = node->number_child<float> ("AudioGain");
74         _audio_delay = node->number_child<int> ("AudioDelay");
75 }
76
77 AudioContent::AudioContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
78         : Content (f, c)
79 {
80         shared_ptr<AudioContent> ref = dynamic_pointer_cast<AudioContent> (c[0]);
81         assert (ref);
82         
83         for (size_t i = 0; i < c.size(); ++i) {
84                 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (c[i]);
85
86                 if (ac->audio_gain() != ref->audio_gain()) {
87                         throw JoinError (_("Content to be joined must have the same audio gain."));
88                 }
89
90                 if (ac->audio_delay() != ref->audio_delay()) {
91                         throw JoinError (_("Content to be joined must have the same audio delay."));
92                 }
93         }
94
95         _audio_gain = ref->audio_gain ();
96         _audio_delay = ref->audio_delay ();
97 }
98
99 void
100 AudioContent::as_xml (xmlpp::Node* node) const
101 {
102         boost::mutex::scoped_lock lm (_mutex);
103         node->add_child("AudioGain")->add_child_text (raw_convert<string> (_audio_gain));
104         node->add_child("AudioDelay")->add_child_text (raw_convert<string> (_audio_delay));
105 }
106
107
108 void
109 AudioContent::set_audio_gain (double g)
110 {
111         {
112                 boost::mutex::scoped_lock lm (_mutex);
113                 _audio_gain = g;
114         }
115         
116         signal_changed (AudioContentProperty::AUDIO_GAIN);
117 }
118
119 void
120 AudioContent::set_audio_delay (int d)
121 {
122         {
123                 boost::mutex::scoped_lock lm (_mutex);
124                 _audio_delay = d;
125         }
126         
127         signal_changed (AudioContentProperty::AUDIO_DELAY);
128 }
129
130 boost::signals2::connection
131 AudioContent::analyse_audio (boost::function<void()> finished)
132 {
133         shared_ptr<const Film> film = _film.lock ();
134         assert (film);
135         
136         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
137         boost::signals2::connection c = job->Finished.connect (finished);
138         JobManager::instance()->add (job);
139
140         return c;
141 }
142
143 boost::filesystem::path
144 AudioContent::audio_analysis_path () const
145 {
146         shared_ptr<const Film> film = _film.lock ();
147         if (!film) {
148                 return boost::filesystem::path ();
149         }
150
151         boost::filesystem::path p = film->audio_analysis_dir ();
152         p /= digest() + "_" + audio_mapping().digest();
153         return p;
154 }
155
156 string
157 AudioContent::technical_summary () const
158 {
159         return String::compose (
160                 "audio: channels %1, length %2, content rate %3, resampled rate %4",
161                 audio_channels(),
162                 audio_length().seconds(),
163                 audio_frame_rate(),
164                 resampled_audio_frame_rate()
165                 );
166 }
167
168 void
169 AudioContent::set_audio_mapping (AudioMapping)
170 {
171         signal_changed (AudioContentProperty::AUDIO_MAPPING);
172 }
173
174 /** @return the frame rate that this content should be resampled to in order
175  *  that it is in sync with the active video content at its start time.
176  */
177 int
178 AudioContent::resampled_audio_frame_rate () const
179 {
180         shared_ptr<const Film> film = _film.lock ();
181         assert (film);
182         
183         /* Resample to a DCI-approved sample rate */
184         double t = dcp_audio_frame_rate (audio_frame_rate ());
185
186         FrameRateChange frc = film->active_frame_rate_change (position ());
187
188         /* Compensate if the DCP is being run at a different frame rate
189            to the source; that is, if the video is run such that it will
190            look different in the DCP compared to the source (slower or faster).
191         */
192
193         if (frc.change_speed) {
194                 t /= frc.speed_up;
195         }
196
197         return rint (t);
198 }