Some basics of AudioMapping.
[dcpomatic.git] / src / lib / sndfile_content.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <libcxml/cxml.h>
23 #include "sndfile_content.h"
24 #include "sndfile_decoder.h"
25 #include "compose.hpp"
26 #include "job.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 SndfileContent::SndfileContent (boost::filesystem::path f)
37         : Content (f)
38         , AudioContent (f)
39         , _audio_channels (0)
40         , _audio_length (0)
41         , _audio_frame_rate (0)
42 {
43
44 }
45
46 SndfileContent::SndfileContent (shared_ptr<const cxml::Node> node)
47         : Content (node)
48         , AudioContent (node)
49 {
50         _audio_channels = node->number_child<int> ("AudioChannels");
51         _audio_length = node->number_child<ContentAudioFrame> ("AudioLength");
52         _audio_frame_rate = node->number_child<int> ("AudioFrameRate");
53         _mapping = AudioMapping (node->node_child ("Mapping"));
54 }
55
56 string
57 SndfileContent::summary () const
58 {
59         return String::compose (_("Sound file: %1"), file().filename().string());
60 }
61
62 string
63 SndfileContent::information () const
64 {
65         if (_audio_frame_rate == 0) {
66                 return "";
67         }
68         
69         stringstream s;
70
71         s << String::compose (
72                 _("%1 channels, %2kHz, %3 samples"),
73                 audio_channels(),
74                 content_audio_frame_rate() / 1000.0,
75                 audio_length()
76                 );
77         
78         return s.str ();
79 }
80
81 bool
82 SndfileContent::valid_file (boost::filesystem::path f)
83 {
84         /* XXX: more extensions */
85         string ext = f.extension().string();
86         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
87         return (ext == ".wav" || ext == ".aif" || ext == ".aiff");
88 }
89
90 shared_ptr<Content>
91 SndfileContent::clone () const
92 {
93         return shared_ptr<Content> (new SndfileContent (*this));
94 }
95
96 void
97 SndfileContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
98 {
99         job->set_progress_unknown ();
100         Content::examine (film, job, quick);
101
102         SndfileDecoder dec (film, shared_from_this());
103
104         {
105                 boost::mutex::scoped_lock lm (_mutex);
106                 _audio_channels = dec.audio_channels ();
107                 _audio_length = dec.audio_length ();
108                 _audio_frame_rate = dec.audio_frame_rate ();
109                 _mapping = AudioMapping (_audio_channels);
110         }
111
112         signal_changed (AudioContentProperty::AUDIO_CHANNELS);
113         signal_changed (AudioContentProperty::AUDIO_LENGTH);
114         signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
115 }
116
117 void
118 SndfileContent::as_xml (xmlpp::Node* node) const
119 {
120         node->add_child("Type")->add_child_text ("Sndfile");
121         Content::as_xml (node);
122         node->add_child("AudioChannels")->add_child_text (lexical_cast<string> (_audio_channels));
123         node->add_child("AudioLength")->add_child_text (lexical_cast<string> (_audio_length));
124         node->add_child("AudioFrameRate")->add_child_text (lexical_cast<string> (_audio_frame_rate));
125         _mapping.as_xml (node->add_child("Mapping"));
126 }
127
128 int
129 SndfileContent::output_audio_frame_rate (shared_ptr<const Film>) const
130 {
131         /* Resample to a DCI-approved sample rate */
132         return dcp_audio_frame_rate (content_audio_frame_rate ());
133 }
134
135 Time
136 SndfileContent::length (shared_ptr<const Film> film) const
137 {
138         return film->audio_frames_to_time (audio_length ());
139 }