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