2d6330485569856d1454774bd9239c0a5b578302
[dcpomatic.git] / src / lib / sndfile_content.cc
1 /*
2     Copyright (C) 2013-2015 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 "sndfile_content.h"
21 #include "sndfile_decoder.h"
22 #include "sndfile_examiner.h"
23 #include "film.h"
24 #include "compose.hpp"
25 #include "job.h"
26 #include "util.h"
27 #include "safe_stringstream.h"
28 #include "raw_convert.h"
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::cout;
36 using boost::shared_ptr;
37
38 SndfileContent::SndfileContent (shared_ptr<const Film> film, boost::filesystem::path p)
39         : Content (film, p)
40         , SingleStreamAudioContent (film, p)
41 {
42
43 }
44
45 SndfileContent::SndfileContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
46         : Content (film, node)
47         , SingleStreamAudioContent (film, node, version)
48         , _audio_length (node->number_child<Frame> ("AudioLength"))
49 {
50
51 }
52
53 void
54 SndfileContent::as_xml (xmlpp::Node* node) const
55 {
56         node->add_child("Type")->add_child_text ("Sndfile");
57         Content::as_xml (node);
58         SingleStreamAudioContent::as_xml (node);
59         node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio_length ()));
60 }
61
62
63 string
64 SndfileContent::summary () const
65 {
66         /* Get the string() here so that the name does not have quotes around it */
67         return String::compose (_("%1 [audio]"), path_summary ());
68 }
69
70 string
71 SndfileContent::technical_summary () const
72 {
73         return Content::technical_summary() + " - "
74                 + AudioContent::technical_summary ()
75                 + " - sndfile";
76 }
77
78 bool
79 SndfileContent::valid_file (boost::filesystem::path f)
80 {
81         /* XXX: more extensions */
82         string ext = f.extension().string();
83         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
84         return (ext == ".wav" || ext == ".w64" || ext == ".flac" || ext == ".aif" || ext == ".aiff");
85 }
86
87 void
88 SndfileContent::examine (shared_ptr<Job> job)
89 {
90         job->set_progress_unknown ();
91         Content::examine (job);
92         shared_ptr<AudioExaminer> dec (new SndfileExaminer (shared_from_this ()));
93         take_from_audio_examiner (dec);
94 }
95
96 void
97 SndfileContent::take_from_audio_examiner (shared_ptr<AudioExaminer> examiner)
98 {
99         SingleStreamAudioContent::take_from_audio_examiner (examiner);
100
101         boost::mutex::scoped_lock lm (_mutex);
102         _audio_length = examiner->audio_length ();
103 }
104
105 DCPTime
106 SndfileContent::full_length () const
107 {
108         shared_ptr<const Film> film = _film.lock ();
109         DCPOMATIC_ASSERT (film);
110         FrameRateChange const frc = film->active_frame_rate_change (position ());
111         return DCPTime::from_frames (audio_length() / frc.speed_up, audio_stream()->frame_rate ());
112 }