Handle multiple audio streams in a single piece of content
[dcpomatic.git] / src / lib / sndfile_content.cc
1 /*
2     Copyright (C) 2013-2015 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 "sndfile_content.h"
21 #include "sndfile_decoder.h"
22 #include "sndfile_examiner.h"
23 #include "film.h"
24 #include "compose.hpp"
25 #include "job.h"
26 #include "util.h"
27 #include "safe_stringstream.h"
28 #include "raw_convert.h"
29 #include <libcxml/cxml.h>
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::cout;
35 using boost::shared_ptr;
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         , _audio_length (node->number_child<int64_t> ("AudioLength"))
48 {
49
50 }
51
52 void
53 SndfileContent::as_xml (xmlpp::Node* node) const
54 {
55         node->add_child("Type")->add_child_text ("Sndfile");
56         Content::as_xml (node);
57         SingleStreamAudioContent::as_xml (node);
58         node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio_length ()));
59 }
60
61
62 string
63 SndfileContent::summary () const
64 {
65         /* Get the string() here so that the name does not have quotes around it */
66         return String::compose (_("%1 [audio]"), path_summary ());
67 }
68
69 string
70 SndfileContent::technical_summary () const
71 {
72         return Content::technical_summary() + " - "
73                 + AudioContent::technical_summary ()
74                 + " - sndfile";
75 }
76
77 bool
78 SndfileContent::valid_file (boost::filesystem::path f)
79 {
80         /* XXX: more extensions */
81         string ext = f.extension().string();
82         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
83         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
84 }
85
86 void
87 SndfileContent::examine (shared_ptr<Job> job)
88 {
89         job->set_progress_unknown ();
90         Content::examine (job);
91         shared_ptr<AudioExaminer> dec (new SndfileExaminer (shared_from_this ()));
92         take_from_audio_examiner (dec);
93 }
94
95 void
96 SndfileContent::take_from_audio_examiner (shared_ptr<AudioExaminer> examiner)
97 {
98         SingleStreamAudioContent::take_from_audio_examiner (examiner);
99
100         boost::mutex::scoped_lock lm (_mutex);
101         _audio_length = examiner->audio_length ();
102 }
103
104 DCPTime
105 SndfileContent::full_length () const
106 {
107         shared_ptr<const Film> film = _film.lock ();
108         DCPOMATIC_ASSERT (film);
109         FrameRateChange const frc = film->active_frame_rate_change (position ());
110         return DCPTime::from_frames (audio_length() / frc.speed_up, audio_stream()->frame_rate ());
111 }
112