2 Copyright (C) 2013 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 "sndfile_content.h"
22 #include "sndfile_decoder.h"
24 #include "compose.hpp"
30 using std::stringstream;
32 using boost::shared_ptr;
33 using boost::lexical_cast;
35 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
40 , _audio_frame_rate (0)
45 SndfileContent::SndfileContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
47 , AudioContent (f, node)
48 , _audio_mapping (node->node_child ("AudioMapping"))
50 _audio_channels = node->number_child<int> ("AudioChannels");
51 _audio_length = node->number_child<AudioContent::Frame> ("AudioLength");
52 _audio_frame_rate = node->number_child<int> ("AudioFrameRate");
56 SndfileContent::summary () const
58 /* Get the string() here so that the name does not have quotes around it */
59 return String::compose (_("%1 [audio]"), path().filename().string());
63 SndfileContent::technical_summary () const
65 return Content::technical_summary() + " - "
66 + AudioContent::technical_summary ()
71 SndfileContent::information () const
73 if (_audio_frame_rate == 0) {
79 s << String::compose (
80 _("%1 channels, %2kHz, %3 samples"),
82 content_audio_frame_rate() / 1000.0,
90 SndfileContent::valid_file (boost::filesystem::path f)
92 /* XXX: more extensions */
93 string ext = f.extension().string();
94 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
95 return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
99 SndfileContent::examine (shared_ptr<Job> job)
101 job->set_progress_unknown ();
102 Content::examine (job);
104 shared_ptr<const Film> film = _film.lock ();
107 SndfileDecoder dec (film, shared_from_this());
110 boost::mutex::scoped_lock lm (_mutex);
111 _audio_channels = dec.audio_channels ();
112 _audio_length = dec.audio_length ();
113 _audio_frame_rate = dec.audio_frame_rate ();
116 signal_changed (AudioContentProperty::AUDIO_CHANNELS);
117 signal_changed (AudioContentProperty::AUDIO_LENGTH);
118 signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
120 /* XXX: do this in signal_changed...? */
121 _audio_mapping = AudioMapping (_audio_channels);
122 _audio_mapping.make_default ();
123 signal_changed (AudioContentProperty::AUDIO_MAPPING);
127 SndfileContent::as_xml (xmlpp::Node* node) const
129 node->add_child("Type")->add_child_text ("Sndfile");
130 Content::as_xml (node);
131 AudioContent::as_xml (node);
132 node->add_child("AudioChannels")->add_child_text (lexical_cast<string> (_audio_channels));
133 node->add_child("AudioLength")->add_child_text (lexical_cast<string> (_audio_length));
134 node->add_child("AudioFrameRate")->add_child_text (lexical_cast<string> (_audio_frame_rate));
135 _audio_mapping.as_xml (node->add_child("AudioMapping"));
139 SndfileContent::length () const
141 shared_ptr<const Film> film = _film.lock ();
144 return film->audio_frames_to_time (audio_length ());
148 SndfileContent::output_audio_frame_rate () const
150 shared_ptr<const Film> film = _film.lock ();
153 return film->audio_frame_rate ();
157 SndfileContent::set_audio_mapping (AudioMapping m)
160 boost::mutex::scoped_lock lm (_mutex);
164 signal_changed (AudioContentProperty::AUDIO_MAPPING);