Merge master.
[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 <dcp/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 dcp::raw_convert;
36
37 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
38         : Content (f, p)
39         , SingleStreamAudioContent (f, p)
40 {
41
42 }
43
44 SndfileContent::SndfileContent (shared_ptr<const Film> f, cxml::ConstNodePtr node, int version)
45         : Content (f, node)
46         , SingleStreamAudioContent (f, node, version)
47 {
48         
49 }
50
51 void
52 SndfileContent::as_xml (xmlpp::Node* node) const
53 {
54         node->add_child("Type")->add_child_text ("Sndfile");
55         Content::as_xml (node);
56         SingleStreamAudioContent::as_xml (node);
57 }
58
59
60 string
61 SndfileContent::summary () const
62 {
63         /* Get the string() here so that the name does not have quotes around it */
64         return String::compose (_("%1 [audio]"), path_summary ());
65 }
66
67 string
68 SndfileContent::technical_summary () const
69 {
70         return Content::technical_summary() + " - "
71                 + AudioContent::technical_summary ()
72                 + " - sndfile";
73 }
74
75 string
76 SndfileContent::information () const
77 {
78         if (_audio_frame_rate == 0) {
79                 return "";
80         }
81         
82         stringstream s;
83
84         s << String::compose (
85                 _("%1 channels, %2kHz, %3 samples"),
86                 audio_channels(),
87                 audio_frame_rate() / 1000.0,
88                 audio_length().frames (audio_frame_rate ())
89                 );
90         
91         return s.str ();
92 }
93
94 bool
95 SndfileContent::valid_file (boost::filesystem::path f)
96 {
97         /* XXX: more extensions */
98         string ext = f.extension().string();
99         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
100         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
101 }
102
103 void
104 SndfileContent::examine (shared_ptr<Job> job)
105 {
106         job->set_progress_unknown ();
107         Content::examine (job);
108         shared_ptr<AudioExaminer> dec (new SndfileDecoder (shared_from_this()));
109         take_from_audio_examiner (dec);
110 }
111
112 DCPTime
113 SndfileContent::full_length () const
114 {
115         shared_ptr<const Film> film = _film.lock ();
116         assert (film);
117         return DCPTime (audio_length(), film->active_frame_rate_change (position ()));
118 }
119