Don't overlap simultaneous video content in the timeline. Fix keep-aligned for separ...
[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 #include "util.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::stringstream;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::lexical_cast;
35
36 int const SndfileContentProperty::VIDEO_FRAME_RATE = 600;
37
38 SndfileContent::SndfileContent (shared_ptr<const Film> f, boost::filesystem::path p)
39         : Content (f, p)
40         , AudioContent (f, p)
41         , _audio_channels (0)
42         , _audio_length (0)
43         , _audio_frame_rate (0)
44 {
45
46 }
47
48 SndfileContent::SndfileContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
49         : Content (f, node)
50         , AudioContent (f, node)
51         , _audio_mapping (node->node_child ("AudioMapping"), version)
52 {
53         _audio_channels = node->number_child<int> ("AudioChannels");
54         _audio_length = node->number_child<AudioContent::Frame> ("AudioLength");
55         _audio_frame_rate = node->number_child<int> ("AudioFrameRate");
56 }
57
58 string
59 SndfileContent::summary () const
60 {
61         /* Get the string() here so that the name does not have quotes around it */
62         return String::compose (_("%1 [audio]"), path_summary ());
63 }
64
65 string
66 SndfileContent::technical_summary () const
67 {
68         return Content::technical_summary() + " - "
69                 + AudioContent::technical_summary ()
70                 + " - sndfile";
71 }
72
73 string
74 SndfileContent::information () const
75 {
76         if (_audio_frame_rate == 0) {
77                 return "";
78         }
79         
80         stringstream s;
81
82         s << String::compose (
83                 _("%1 channels, %2kHz, %3 samples"),
84                 audio_channels(),
85                 content_audio_frame_rate() / 1000.0,
86                 audio_length()
87                 );
88         
89         return s.str ();
90 }
91
92 bool
93 SndfileContent::valid_file (boost::filesystem::path f)
94 {
95         /* XXX: more extensions */
96         string ext = f.extension().string();
97         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
98         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
99 }
100
101 void
102 SndfileContent::examine (shared_ptr<Job> job)
103 {
104         job->set_progress_unknown ();
105         Content::examine (job);
106
107         shared_ptr<const Film> film = _film.lock ();
108         assert (film);
109
110         SndfileDecoder dec (film, shared_from_this());
111
112         {
113                 boost::mutex::scoped_lock lm (_mutex);
114                 _audio_channels = dec.audio_channels ();
115                 _audio_length = dec.audio_length ();
116                 _audio_frame_rate = dec.audio_frame_rate ();
117         }
118
119         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
120         signal_changed (AudioContentProperty::AUDIO_LENGTH);
121         signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
122
123         {
124                 boost::mutex::scoped_lock lm (_mutex);
125                 /* XXX: do this in signal_changed...? */
126                 _audio_mapping = AudioMapping (_audio_channels);
127                 _audio_mapping.make_default ();
128         }
129         
130         signal_changed (AudioContentProperty::AUDIO_MAPPING);
131 }
132
133 void
134 SndfileContent::as_xml (xmlpp::Node* node) const
135 {
136         node->add_child("Type")->add_child_text ("Sndfile");
137         Content::as_xml (node);
138         AudioContent::as_xml (node);
139
140         node->add_child("AudioChannels")->add_child_text (lexical_cast<string> (audio_channels ()));
141         node->add_child("AudioLength")->add_child_text (lexical_cast<string> (audio_length ()));
142         node->add_child("AudioFrameRate")->add_child_text (lexical_cast<string> (content_audio_frame_rate ()));
143         _audio_mapping.as_xml (node->add_child("AudioMapping"));
144 }
145
146 Time
147 SndfileContent::full_length () const
148 {
149         shared_ptr<const Film> film = _film.lock ();
150         assert (film);
151
152         float const rate = _video_frame_rate.get_value_or (film->video_frame_rate ());
153         OutputAudioFrame const len = divide_with_round (
154                 audio_length() * output_audio_frame_rate() * rate,
155                 content_audio_frame_rate() * film->video_frame_rate()
156                 );
157         
158         return film->audio_frames_to_time (len);
159 }
160
161 int
162 SndfileContent::output_audio_frame_rate () const
163 {
164         shared_ptr<const Film> film = _film.lock ();
165         assert (film);
166         
167         return film->audio_frame_rate ();
168 }
169
170 void
171 SndfileContent::set_audio_mapping (AudioMapping m)
172 {
173         {
174                 boost::mutex::scoped_lock lm (_mutex);
175                 _audio_mapping = m;
176         }
177
178         signal_changed (AudioContentProperty::AUDIO_MAPPING);
179 }
180
181 float
182 SndfileContent::video_frame_rate () const
183 {
184         {
185                 boost::mutex::scoped_lock lm (_mutex);
186                 if (_video_frame_rate) {
187                         return _video_frame_rate.get ();
188                 }
189         }
190
191         shared_ptr<const Film> film = _film.lock ();
192         assert (film);
193         return film->video_frame_rate ();
194 }