Merge master.
[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 "audio_content.h"
22 #include "analyse_audio_job.h"
23 #include "job_manager.h"
24 #include "film.h"
25 #include "exceptions.h"
26 #include "config.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::vector;
32 using boost::shared_ptr;
33 using boost::lexical_cast;
34 using boost::dynamic_pointer_cast;
35
36 int const AudioContentProperty::AUDIO_CHANNELS = 200;
37 int const AudioContentProperty::AUDIO_LENGTH = 201;
38 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
39 int const AudioContentProperty::AUDIO_GAIN = 203;
40 int const AudioContentProperty::AUDIO_DELAY = 204;
41 int const AudioContentProperty::AUDIO_MAPPING = 205;
42
43 AudioContent::AudioContent (shared_ptr<const Film> f, DCPTime s)
44         : Content (f, s)
45         , _audio_gain (0)
46         , _audio_delay (Config::instance()->default_audio_delay ())
47 {
48
49 }
50
51 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
52         : Content (f, p)
53         , _audio_gain (0)
54         , _audio_delay (Config::instance()->default_audio_delay ())
55 {
56
57 }
58
59 AudioContent::AudioContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
60         : Content (f, node)
61 {
62         LocaleGuard lg;
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         LocaleGuard lg;
94         
95         boost::mutex::scoped_lock lm (_mutex);
96         node->add_child("AudioGain")->add_child_text (lexical_cast<string> (_audio_gain));
97         node->add_child("AudioDelay")->add_child_text (lexical_cast<string> (_audio_delay));
98 }
99
100
101 void
102 AudioContent::set_audio_gain (float g)
103 {
104         {
105                 boost::mutex::scoped_lock lm (_mutex);
106                 _audio_gain = g;
107         }
108         
109         signal_changed (AudioContentProperty::AUDIO_GAIN);
110 }
111
112 void
113 AudioContent::set_audio_delay (int d)
114 {
115         {
116                 boost::mutex::scoped_lock lm (_mutex);
117                 _audio_delay = d;
118         }
119         
120         signal_changed (AudioContentProperty::AUDIO_DELAY);
121 }
122
123 boost::signals2::connection
124 AudioContent::analyse_audio (boost::function<void()> finished)
125 {
126         shared_ptr<const Film> film = _film.lock ();
127         assert (film);
128         
129         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
130         boost::signals2::connection c = job->Finished.connect (finished);
131         JobManager::instance()->add (job);
132
133         return c;
134 }
135
136 boost::filesystem::path
137 AudioContent::audio_analysis_path () const
138 {
139         shared_ptr<const Film> film = _film.lock ();
140         if (!film) {
141                 return boost::filesystem::path ();
142         }
143
144         boost::filesystem::path p = film->audio_analysis_dir ();
145         p /= digest ();
146         return p;
147 }
148
149 string
150 AudioContent::technical_summary () const
151 {
152         return String::compose (
153                 "audio: channels %1, length %2, raw rate %3, out rate %4",
154                 audio_channels(),
155                 audio_length().seconds(),
156                 content_audio_frame_rate(),
157                 output_audio_frame_rate()
158                 );
159 }