Various fixes to make audio analysis sort-of work.
[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
26 using std::string;
27 using boost::shared_ptr;
28 using boost::lexical_cast;
29 using boost::dynamic_pointer_cast;
30
31 int const AudioContentProperty::AUDIO_CHANNELS = 200;
32 int const AudioContentProperty::AUDIO_LENGTH = 201;
33 int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
34 int const AudioContentProperty::AUDIO_GAIN = 203;
35 int const AudioContentProperty::AUDIO_DELAY = 204;
36 int const AudioContentProperty::AUDIO_MAPPING = 205;
37
38 AudioContent::AudioContent (shared_ptr<const Film> f, Time s)
39         : Content (f, s)
40         , _audio_gain (0)
41         , _audio_delay (0)
42 {
43
44 }
45
46 AudioContent::AudioContent (shared_ptr<const Film> f, boost::filesystem::path p)
47         : Content (f, p)
48         , _audio_gain (0)
49         , _audio_delay (0)
50 {
51
52 }
53
54 AudioContent::AudioContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
55         : Content (f, node)
56 {
57         _audio_gain = node->number_child<float> ("AudioGain");
58         _audio_delay = node->number_child<int> ("AudioDelay");
59 }
60
61 AudioContent::AudioContent (AudioContent const & o)
62         : Content (o)
63         , _audio_gain (o._audio_gain)
64         , _audio_delay (o._audio_delay)
65 {
66
67 }
68
69 void
70 AudioContent::as_xml (xmlpp::Node* node) const
71 {
72         boost::mutex::scoped_lock lm (_mutex);
73         node->add_child("AudioGain")->add_child_text (lexical_cast<string> (_audio_gain));
74         node->add_child("AudioDelay")->add_child_text (lexical_cast<string> (_audio_delay));
75 }
76
77
78 void
79 AudioContent::set_audio_gain (float g)
80 {
81         {
82                 boost::mutex::scoped_lock lm (_mutex);
83                 _audio_gain = g;
84         }
85         
86         signal_changed (AudioContentProperty::AUDIO_GAIN);
87 }
88
89 void
90 AudioContent::set_audio_delay (int d)
91 {
92         {
93                 boost::mutex::scoped_lock lm (_mutex);
94                 _audio_delay = d;
95         }
96         
97         signal_changed (AudioContentProperty::AUDIO_DELAY);
98 }
99
100 void
101 AudioContent::analyse_audio (boost::function<void()> finished)
102 {
103         shared_ptr<const Film> film = _film.lock ();
104         if (!film) {
105                 return;
106         }
107         
108         shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, dynamic_pointer_cast<AudioContent> (shared_from_this())));
109         job->Finished.connect (finished);
110         JobManager::instance()->add (job);
111 }
112
113 boost::filesystem::path
114 AudioContent::audio_analysis_path () const
115 {
116         shared_ptr<const Film> film = _film.lock ();
117         if (!film) {
118                 return boost::filesystem::path ();
119         }
120
121         return film->audio_analysis_path (dynamic_pointer_cast<const AudioContent> (shared_from_this ()));
122 }