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