d8435613d6123a7a3bfa74a6448e4bada940aebf
[dcpomatic.git] / src / lib / sndfile_content.cc
1 /*
2     Copyright (C) 2013-2016 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 #include <libxml++/libxml++.h>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::cout;
37 using std::vector;
38 using boost::shared_ptr;
39
40 SndfileContent::SndfileContent (shared_ptr<const Film> film, boost::filesystem::path p)
41         : Content (film, p)
42         , AudioContent (film, p)
43 {
44
45 }
46
47 SndfileContent::SndfileContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
48         : Content (film, node)
49         , AudioContent (film, node)
50         , _audio_length (node->number_child<Frame> ("AudioLength"))
51         , _audio_stream (new AudioStream (node->number_child<int> ("AudioFrameRate"), AudioMapping (node->node_child ("AudioMapping"), version)))
52 {
53
54 }
55
56 void
57 SndfileContent::as_xml (xmlpp::Node* node) const
58 {
59         node->add_child("Type")->add_child_text ("Sndfile");
60         Content::as_xml (node);
61         AudioContent::as_xml (node);
62         node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio_stream()->frame_rate ()));
63         audio_stream()->mapping().as_xml (node->add_child("AudioMapping"));
64         node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio_length ()));
65 }
66
67
68 string
69 SndfileContent::summary () const
70 {
71         /* Get the string() here so that the name does not have quotes around it */
72         return String::compose (_("%1 [audio]"), path_summary ());
73 }
74
75 string
76 SndfileContent::technical_summary () const
77 {
78         return Content::technical_summary() + " - "
79                 + AudioContent::technical_summary ()
80                 + " - sndfile";
81 }
82
83 bool
84 SndfileContent::valid_file (boost::filesystem::path f)
85 {
86         /* XXX: more extensions */
87         string ext = f.extension().string();
88         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
89         return (ext == ".wav" || ext == ".w64" || ext == ".flac" || ext == ".aif" || ext == ".aiff");
90 }
91
92 void
93 SndfileContent::examine (shared_ptr<Job> job)
94 {
95         job->set_progress_unknown ();
96         Content::examine (job);
97         shared_ptr<AudioExaminer> dec (new SndfileExaminer (shared_from_this ()));
98         take_from_audio_examiner (dec);
99 }
100
101 void
102 SndfileContent::take_from_audio_examiner (shared_ptr<AudioExaminer> examiner)
103 {
104         {
105                 boost::mutex::scoped_lock lm (_mutex);
106                 _audio_stream.reset (new AudioStream (examiner->audio_frame_rate(), examiner->audio_channels ()));
107                 AudioMapping m = _audio_stream->mapping ();
108                 film()->make_audio_mapping_default (m);
109                 _audio_stream->set_mapping (m);
110                 _audio_length = examiner->audio_length ();
111         }
112
113         signal_changed (AudioContentProperty::AUDIO_STREAMS);
114 }
115
116 DCPTime
117 SndfileContent::full_length () const
118 {
119         FrameRateChange const frc (audio_video_frame_rate(), film()->video_frame_rate());
120         return DCPTime::from_frames (audio_length() / frc.speed_up, audio_stream()->frame_rate ());
121 }
122
123 vector<AudioStreamPtr>
124 SndfileContent::audio_streams () const
125 {
126         vector<AudioStreamPtr> s;
127         s.push_back (_audio_stream);
128         return s;
129 }