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