Merge.
[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 <libdcp/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 libdcp::raw_convert;
36
37 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
38         : Content (f, p)
39         , AudioContent (f, p)
40         , _audio_channels (0)
41         , _audio_length (0)
42         , _audio_frame_rate (0)
43 {
44
45 }
46
47 SndfileContent::SndfileContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
48         : Content (f, node)
49         , AudioContent (f, node)
50         , _audio_mapping (node->node_child ("AudioMapping"), version)
51 {
52         _audio_channels = node->number_child<int> ("AudioChannels");
53         _audio_length = node->number_child<AudioContent::Frame> ("AudioLength");
54         _audio_frame_rate = node->number_child<int> ("AudioFrameRate");
55 }
56
57 string
58 SndfileContent::summary () const
59 {
60         /* Get the string() here so that the name does not have quotes around it */
61         return String::compose (_("%1 [audio]"), path_summary ());
62 }
63
64 string
65 SndfileContent::technical_summary () const
66 {
67         return Content::technical_summary() + " - "
68                 + AudioContent::technical_summary ()
69                 + " - sndfile";
70 }
71
72 string
73 SndfileContent::information () const
74 {
75         if (_audio_frame_rate == 0) {
76                 return "";
77         }
78         
79         stringstream s;
80
81         s << String::compose (
82                 _("%1 channels, %2kHz, %3 samples"),
83                 audio_channels(),
84                 content_audio_frame_rate() / 1000.0,
85                 audio_length()
86                 );
87         
88         return s.str ();
89 }
90
91 bool
92 SndfileContent::valid_file (boost::filesystem::path f)
93 {
94         /* XXX: more extensions */
95         string ext = f.extension().string();
96         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
97         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
98 }
99
100 void
101 SndfileContent::examine (shared_ptr<Job> job)
102 {
103         job->set_progress_unknown ();
104         Content::examine (job);
105
106         shared_ptr<const Film> film = _film.lock ();
107         assert (film);
108
109         SndfileDecoder dec (film, shared_from_this());
110
111         {
112                 boost::mutex::scoped_lock lm (_mutex);
113                 _audio_channels = dec.audio_channels ();
114                 _audio_length = dec.audio_length ();
115                 _audio_frame_rate = dec.audio_frame_rate ();
116         }
117
118         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
119         signal_changed (AudioContentProperty::AUDIO_LENGTH);
120         signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
121
122         {
123                 boost::mutex::scoped_lock lm (_mutex);
124                 /* XXX: do this in signal_changed...? */
125                 _audio_mapping = AudioMapping (_audio_channels);
126                 _audio_mapping.make_default ();
127         }
128         
129         signal_changed (AudioContentProperty::AUDIO_MAPPING);
130 }
131
132 void
133 SndfileContent::as_xml (xmlpp::Node* node) const
134 {
135         node->add_child("Type")->add_child_text ("Sndfile");
136         Content::as_xml (node);
137         AudioContent::as_xml (node);
138
139         node->add_child("AudioChannels")->add_child_text (raw_convert<string> (audio_channels ()));
140         node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio_length ()));
141         node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (content_audio_frame_rate ()));
142         _audio_mapping.as_xml (node->add_child("AudioMapping"));
143 }
144
145 Time
146 SndfileContent::full_length () const
147 {
148         shared_ptr<const Film> film = _film.lock ();
149         assert (film);
150
151         FrameRateChange frc = film->active_frame_rate_change (position ());
152
153         OutputAudioFrame const len = divide_with_round (
154                 audio_length() * output_audio_frame_rate() * frc.source,
155                 content_audio_frame_rate() * film->video_frame_rate()
156                 );
157         
158         return film->audio_frames_to_time (len);
159 }
160
161 void
162 SndfileContent::set_audio_mapping (AudioMapping m)
163 {
164         {
165                 boost::mutex::scoped_lock lm (_mutex);
166                 _audio_mapping = m;
167         }
168
169         signal_changed (AudioContentProperty::AUDIO_MAPPING);
170 }