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