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