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