Rename Content::_file to path and support md5sums of directories.
[dcpomatic.git] / src / lib / sndfile_content.cc
1 /*
2     Copyright (C) 2013 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 "sndfile_content.h"
22 #include "sndfile_decoder.h"
23 #include "film.h"
24 #include "compose.hpp"
25 #include "job.h"
26
27 #include "i18n.h"
28
29 using std::string;
30 using std::stringstream;
31 using std::cout;
32 using boost::shared_ptr;
33 using boost::lexical_cast;
34
35 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
36         : Content (f, p)
37         , AudioContent (f, p)
38         , _audio_channels (0)
39         , _audio_length (0)
40         , _audio_frame_rate (0)
41 {
42
43 }
44
45 SndfileContent::SndfileContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
46         : Content (f, node)
47         , AudioContent (f, node)
48         , _audio_mapping (node->node_child ("AudioMapping"))
49 {
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");
53 }
54
55 string
56 SndfileContent::summary () const
57 {
58         return String::compose (_("%1 [audio]"), path().filename().string());
59 }
60
61 string
62 SndfileContent::technical_summary () const
63 {
64         return Content::technical_summary() + " - "
65                 + AudioContent::technical_summary ()
66                 + "sndfile";
67 }
68
69 string
70 SndfileContent::information () const
71 {
72         if (_audio_frame_rate == 0) {
73                 return "";
74         }
75         
76         stringstream s;
77
78         s << String::compose (
79                 _("%1 channels, %2kHz, %3 samples"),
80                 audio_channels(),
81                 content_audio_frame_rate() / 1000.0,
82                 audio_length()
83                 );
84         
85         return s.str ();
86 }
87
88 bool
89 SndfileContent::valid_file (boost::filesystem::path f)
90 {
91         /* XXX: more extensions */
92         string ext = f.extension().string();
93         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
94         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
95 }
96
97 void
98 SndfileContent::examine (shared_ptr<Job> job)
99 {
100         job->set_progress_unknown ();
101         Content::examine (job);
102
103         shared_ptr<const Film> film = _film.lock ();
104         assert (film);
105
106         SndfileDecoder dec (film, shared_from_this());
107
108         {
109                 boost::mutex::scoped_lock lm (_mutex);
110                 _audio_channels = dec.audio_channels ();
111                 _audio_length = dec.audio_length ();
112                 _audio_frame_rate = dec.audio_frame_rate ();
113         }
114
115         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
116         signal_changed (AudioContentProperty::AUDIO_LENGTH);
117         signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
118
119         /* XXX: do this in signal_changed...? */
120         _audio_mapping = AudioMapping (_audio_channels);
121         _audio_mapping.make_default ();
122         signal_changed (AudioContentProperty::AUDIO_MAPPING);
123 }
124
125 void
126 SndfileContent::as_xml (xmlpp::Node* node) const
127 {
128         node->add_child("Type")->add_child_text ("Sndfile");
129         Content::as_xml (node);
130         AudioContent::as_xml (node);
131         node->add_child("AudioChannels")->add_child_text (lexical_cast<string> (_audio_channels));
132         node->add_child("AudioLength")->add_child_text (lexical_cast<string> (_audio_length));
133         node->add_child("AudioFrameRate")->add_child_text (lexical_cast<string> (_audio_frame_rate));
134         _audio_mapping.as_xml (node->add_child("AudioMapping"));
135 }
136
137 Time
138 SndfileContent::length () const
139 {
140         shared_ptr<const Film> film = _film.lock ();
141         assert (film);
142         
143         return film->audio_frames_to_time (audio_length ());
144 }
145
146 int
147 SndfileContent::output_audio_frame_rate () const
148 {
149         shared_ptr<const Film> film = _film.lock ();
150         assert (film);
151         
152         return film->audio_frame_rate ();
153 }
154
155 void
156 SndfileContent::set_audio_mapping (AudioMapping m)
157 {
158         {
159                 boost::mutex::scoped_lock lm (_mutex);
160                 _audio_mapping = m;
161         }
162
163         signal_changed (AudioContentProperty::AUDIO_MAPPING);
164 }