Merge branch 'master' of ssh://main.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / lib / sndfile_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 <libdcp/raw_convert.h>
22 #include "sndfile_content.h"
23 #include "sndfile_decoder.h"
24 #include "film.h"
25 #include "compose.hpp"
26 #include "job.h"
27 #include "util.h"
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::stringstream;
33 using std::cout;
34 using boost::shared_ptr;
35 using libdcp::raw_convert;
36
37 int const SndfileContentProperty::VIDEO_FRAME_RATE = 600;
38
39 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
40         : Content (f, p)
41         , AudioContent (f, p)
42         , _audio_channels (0)
43         , _audio_length (0)
44         , _audio_frame_rate (0)
45 {
46
47 }
48
49 SndfileContent::SndfileContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
50         : Content (f, node)
51         , AudioContent (f, node)
52         , _audio_mapping (node->node_child ("AudioMapping"), version)
53 {
54         _audio_channels = node->number_child<int> ("AudioChannels");
55         _audio_length = node->number_child<AudioContent::Frame> ("AudioLength");
56         _audio_frame_rate = node->number_child<int> ("AudioFrameRate");
57 }
58
59 string
60 SndfileContent::summary () const
61 {
62         /* Get the string() here so that the name does not have quotes around it */
63         return String::compose (_("%1 [audio]"), path_summary ());
64 }
65
66 string
67 SndfileContent::technical_summary () const
68 {
69         return Content::technical_summary() + " - "
70                 + AudioContent::technical_summary ()
71                 + " - sndfile";
72 }
73
74 string
75 SndfileContent::information () const
76 {
77         if (_audio_frame_rate == 0) {
78                 return "";
79         }
80         
81         stringstream s;
82
83         s << String::compose (
84                 _("%1 channels, %2kHz, %3 samples"),
85                 audio_channels(),
86                 content_audio_frame_rate() / 1000.0,
87                 audio_length()
88                 );
89         
90         return s.str ();
91 }
92
93 bool
94 SndfileContent::valid_file (boost::filesystem::path f)
95 {
96         /* XXX: more extensions */
97         string ext = f.extension().string();
98         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
99         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
100 }
101
102 void
103 SndfileContent::examine (shared_ptr<Job> job)
104 {
105         job->set_progress_unknown ();
106         Content::examine (job);
107
108         shared_ptr<const Film> film = _film.lock ();
109         assert (film);
110
111         SndfileDecoder dec (film, shared_from_this());
112
113         {
114                 boost::mutex::scoped_lock lm (_mutex);
115                 _audio_channels = dec.audio_channels ();
116                 _audio_length = dec.audio_length ();
117                 _audio_frame_rate = dec.audio_frame_rate ();
118         }
119
120         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
121         signal_changed (AudioContentProperty::AUDIO_LENGTH);
122         signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
123
124         {
125                 boost::mutex::scoped_lock lm (_mutex);
126                 /* XXX: do this in signal_changed...? */
127                 _audio_mapping = AudioMapping (_audio_channels);
128                 _audio_mapping.make_default ();
129         }
130         
131         signal_changed (AudioContentProperty::AUDIO_MAPPING);
132 }
133
134 void
135 SndfileContent::as_xml (xmlpp::Node* node) const
136 {
137         node->add_child("Type")->add_child_text ("Sndfile");
138         Content::as_xml (node);
139         AudioContent::as_xml (node);
140
141         node->add_child("AudioChannels")->add_child_text (raw_convert<string> (audio_channels ()));
142         node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio_length ()));
143         node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (content_audio_frame_rate ()));
144         _audio_mapping.as_xml (node->add_child("AudioMapping"));
145 }
146
147 Time
148 SndfileContent::full_length () const
149 {
150         shared_ptr<const Film> film = _film.lock ();
151         assert (film);
152
153         float const rate = _video_frame_rate.get_value_or (film->video_frame_rate ());
154         OutputAudioFrame const len = divide_with_round (
155                 audio_length() * output_audio_frame_rate() * rate,
156                 content_audio_frame_rate() * film->video_frame_rate()
157                 );
158         
159         return film->audio_frames_to_time (len);
160 }
161
162 int
163 SndfileContent::output_audio_frame_rate () const
164 {
165         shared_ptr<const Film> film = _film.lock ();
166         assert (film);
167         
168         return film->audio_frame_rate ();
169 }
170
171 void
172 SndfileContent::set_audio_mapping (AudioMapping m)
173 {
174         {
175                 boost::mutex::scoped_lock lm (_mutex);
176                 _audio_mapping = m;
177         }
178
179         signal_changed (AudioContentProperty::AUDIO_MAPPING);
180 }
181
182 float
183 SndfileContent::video_frame_rate () const
184 {
185         {
186                 boost::mutex::scoped_lock lm (_mutex);
187                 if (_video_frame_rate) {
188                         return _video_frame_rate.get ();
189                 }
190         }
191
192         shared_ptr<const Film> film = _film.lock ();
193         assert (film);
194         return film->video_frame_rate ();
195 }