2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <libcxml/cxml.h>
21 #include <libdcp/raw_convert.h>
22 #include "sndfile_content.h"
23 #include "sndfile_decoder.h"
25 #include "compose.hpp"
32 using std::stringstream;
34 using boost::shared_ptr;
35 using libdcp::raw_convert;
37 int const SndfileContentProperty::VIDEO_FRAME_RATE = 600;
39 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
44 , _audio_frame_rate (0)
49 SndfileContent::SndfileContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
51 , AudioContent (f, node)
52 , _audio_mapping (node->node_child ("AudioMapping"), version)
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");
60 SndfileContent::summary () const
62 /* Get the string() here so that the name does not have quotes around it */
63 return String::compose (_("%1 [audio]"), path_summary ());
67 SndfileContent::technical_summary () const
69 return Content::technical_summary() + " - "
70 + AudioContent::technical_summary ()
75 SndfileContent::information () const
77 if (_audio_frame_rate == 0) {
83 s << String::compose (
84 _("%1 channels, %2kHz, %3 samples"),
86 content_audio_frame_rate() / 1000.0,
94 SndfileContent::valid_file (boost::filesystem::path f)
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");
103 SndfileContent::examine (shared_ptr<Job> job)
105 job->set_progress_unknown ();
106 Content::examine (job);
108 shared_ptr<const Film> film = _film.lock ();
111 SndfileDecoder dec (film, shared_from_this());
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 ();
120 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
121 signal_changed (AudioContentProperty::AUDIO_LENGTH);
122 signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
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 ();
131 signal_changed (AudioContentProperty::AUDIO_MAPPING);
135 SndfileContent::as_xml (xmlpp::Node* node) const
137 node->add_child("Type")->add_child_text ("Sndfile");
138 Content::as_xml (node);
139 AudioContent::as_xml (node);
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"));
148 SndfileContent::full_length () const
150 shared_ptr<const Film> film = _film.lock ();
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()
159 return film->audio_frames_to_time (len);
163 SndfileContent::output_audio_frame_rate () const
165 shared_ptr<const Film> film = _film.lock ();
168 return film->audio_frame_rate ();
172 SndfileContent::set_audio_mapping (AudioMapping m)
175 boost::mutex::scoped_lock lm (_mutex);
179 signal_changed (AudioContentProperty::AUDIO_MAPPING);
183 SndfileContent::video_frame_rate () const
186 boost::mutex::scoped_lock lm (_mutex);
187 if (_video_frame_rate) {
188 return _video_frame_rate.get ();
192 shared_ptr<const Film> film = _film.lock ();
194 return film->video_frame_rate ();