summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2013-04-06 14:42:08 +0100
committerCarl Hetherington <cth@carlh.net>2013-04-06 14:42:08 +0100
commit8750efb9e072cf3b42e6c3c29521c7031c0b5dfd (patch)
tree0f0dd00b0413763b678ed2388b849cfe45a88468 /src/lib
parent1bff0990433ab0ce588acaef7c589fa623bd998b (diff)
Basics of content dialogs.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/audio_content.cc4
-rw-r--r--src/lib/audio_content.h8
-rw-r--r--src/lib/audio_mapping.cc103
-rw-r--r--src/lib/audio_mapping.h53
-rw-r--r--src/lib/encoder.cc3
-rw-r--r--src/lib/sndfile_content.cc84
-rw-r--r--src/lib/sndfile_content.h32
-rw-r--r--src/lib/sndfile_decoder.cc65
-rw-r--r--src/lib/sndfile_decoder.h8
-rw-r--r--src/lib/util.cc52
-rw-r--r--src/lib/util.h13
-rw-r--r--src/lib/writer.cc3
-rw-r--r--src/lib/wscript1
13 files changed, 295 insertions, 134 deletions
diff --git a/src/lib/audio_content.cc b/src/lib/audio_content.cc
index 8ca1e83c9..9968f4725 100644
--- a/src/lib/audio_content.cc
+++ b/src/lib/audio_content.cc
@@ -22,6 +22,10 @@
using boost::shared_ptr;
+int const AudioContentProperty::AUDIO_CHANNELS = 200;
+int const AudioContentProperty::AUDIO_LENGTH = 201;
+int const AudioContentProperty::AUDIO_FRAME_RATE = 202;
+
AudioContent::AudioContent (boost::filesystem::path f)
: Content (f)
{
diff --git a/src/lib/audio_content.h b/src/lib/audio_content.h
index 2bfc20656..d5dbf266b 100644
--- a/src/lib/audio_content.h
+++ b/src/lib/audio_content.h
@@ -27,6 +27,14 @@ namespace cxml {
class Node;
}
+class AudioContentProperty
+{
+public:
+ static int const AUDIO_CHANNELS;
+ static int const AUDIO_LENGTH;
+ static int const AUDIO_FRAME_RATE;
+};
+
class AudioContent : public virtual Content
{
public:
diff --git a/src/lib/audio_mapping.cc b/src/lib/audio_mapping.cc
new file mode 100644
index 000000000..3fc423e10
--- /dev/null
+++ b/src/lib/audio_mapping.cc
@@ -0,0 +1,103 @@
+/*
+ Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "audio_mapping.h"
+
+using std::map;
+using boost::optional;
+
+AutomaticAudioMapping::AutomaticAudioMapping (int c)
+ : _source_channels (c)
+{
+
+}
+
+optional<libdcp::Channel>
+AutomaticAudioMapping::source_to_dcp (int c) const
+{
+ if (c >= _source_channels) {
+ return optional<libdcp::Channel> ();
+ }
+
+ if (_source_channels == 1) {
+ /* mono sources to centre */
+ return libdcp::CENTRE;
+ }
+
+ return static_cast<libdcp::Channel> (c);
+}
+
+optional<int>
+AutomaticAudioMapping::dcp_to_source (libdcp::Channel c) const
+{
+ if (_source_channels == 1) {
+ if (c == libdcp::CENTRE) {
+ return 0;
+ } else {
+ return optional<int> ();
+ }
+ }
+
+ if (static_cast<int> (c) >= _source_channels) {
+ return optional<int> ();
+ }
+
+ return static_cast<int> (c);
+}
+
+int
+AutomaticAudioMapping::dcp_channels () const
+{
+ if (_source_channels == 1) {
+ /* The source is mono, so to put the mono channel into
+ the centre we need to generate a 5.1 soundtrack.
+ */
+ return 6;
+ }
+
+ return _source_channels;
+}
+
+optional<int>
+ConfiguredAudioMapping::dcp_to_source (libdcp::Channel c) const
+{
+ map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.begin ();
+ while (i != _source_to_dcp.end() && i->second != c) {
+ ++i;
+ }
+
+ if (i == _source_to_dcp.end ()) {
+ return boost::none;
+ }
+
+ return i->first;
+}
+
+optional<libdcp::Channel>
+ConfiguredAudioMapping::source_to_dcp (int c) const
+{
+ map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.find (c);
+ if (i == _source_to_dcp.end ()) {
+ return boost::none;
+ }
+
+ return i->second;
+}
+
+
diff --git a/src/lib/audio_mapping.h b/src/lib/audio_mapping.h
new file mode 100644
index 000000000..8804bde06
--- /dev/null
+++ b/src/lib/audio_mapping.h
@@ -0,0 +1,53 @@
+/*
+ Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <vector>
+#include <map>
+#include <boost/optional.hpp>
+#include <libdcp/types.h>
+
+class AudioMapping
+{
+public:
+ virtual boost::optional<libdcp::Channel> source_to_dcp (int c) const = 0;
+ virtual boost::optional<int> dcp_to_source (libdcp::Channel c) const = 0;
+};
+
+class AutomaticAudioMapping : public AudioMapping
+{
+public:
+ AutomaticAudioMapping (int);
+
+ boost::optional<libdcp::Channel> source_to_dcp (int c) const;
+ boost::optional<int> dcp_to_source (libdcp::Channel c) const;
+ int dcp_channels () const;
+
+private:
+ int _source_channels;
+};
+
+class ConfiguredAudioMapping : public AudioMapping
+{
+public:
+ boost::optional<libdcp::Channel> source_to_dcp (int c) const;
+ boost::optional<int> dcp_to_source (libdcp::Channel c) const;
+
+private:
+ std::map<int, libdcp::Channel> _source_to_dcp;
+};
diff --git a/src/lib/encoder.cc b/src/lib/encoder.cc
index 568307462..a0b88e33e 100644
--- a/src/lib/encoder.cc
+++ b/src/lib/encoder.cc
@@ -38,6 +38,7 @@
#include "cross.h"
#include "writer.h"
#include "player.h"
+#include "audio_mapping.h"
#include "i18n.h"
@@ -426,7 +427,7 @@ Encoder::encoder_thread (ServerDescription* server)
void
Encoder::write_audio (shared_ptr<const AudioBuffers> data)
{
- AudioMapping m (_film->audio_channels ());
+ AutomaticAudioMapping m (_film->audio_channels ());
if (m.dcp_channels() != _film->audio_channels()) {
/* Remap (currently just for mono -> 5.1) */
diff --git a/src/lib/sndfile_content.cc b/src/lib/sndfile_content.cc
index 684405efb..ee4f21eef 100644
--- a/src/lib/sndfile_content.cc
+++ b/src/lib/sndfile_content.cc
@@ -17,13 +17,18 @@
*/
+#include <libcxml/cxml.h>
#include "sndfile_content.h"
+#include "sndfile_decoder.h"
#include "compose.hpp"
+#include "job.h"
#include "i18n.h"
using std::string;
+using std::stringstream;
using boost::shared_ptr;
+using boost::lexical_cast;
SndfileContent::SndfileContent (boost::filesystem::path f)
: Content (f)
@@ -35,9 +40,10 @@ SndfileContent::SndfileContent (boost::filesystem::path f)
SndfileContent::SndfileContent (shared_ptr<const cxml::Node> node)
: Content (node)
, AudioContent (node)
-
{
-
+ _audio_channels = node->number_child<int> ("AudioChannels");
+ _audio_length = node->number_child<ContentAudioFrame> ("AudioLength");
+ _audio_frame_rate = node->number_child<int> ("AudioFrameRate");
}
string
@@ -49,37 +55,21 @@ SndfileContent::summary () const
string
SndfileContent::information () const
{
- return "";
-}
-
-int
-SndfileContent::audio_channels () const
-{
- /* XXX */
- return 0;
-}
-
-ContentAudioFrame
-SndfileContent::audio_length () const
-{
- /* XXX */
- return 0;
-}
-
-int
-SndfileContent::audio_frame_rate () const
-{
- /* XXX */
- return 0;
-}
-
-int64_t
-SndfileContent::audio_channel_layout () const
-{
- /* XXX */
- return 0;
-}
+ if (_audio_frame_rate == 0) {
+ return "";
+ }
+
+ stringstream s;
+
+ s << String::compose (
+ _("%1 channels, %2kHz, %3 samples"),
+ audio_channels(),
+ audio_frame_rate() / 1000.0,
+ audio_length()
+ );
+ return s.str ();
+}
bool
SndfileContent::valid_file (boost::filesystem::path f)
@@ -95,3 +85,33 @@ SndfileContent::clone () const
{
return shared_ptr<Content> (new SndfileContent (*this));
}
+
+void
+SndfileContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
+{
+ job->set_progress_unknown ();
+ Content::examine (film, job, quick);
+
+ SndfileDecoder dec (film, shared_from_this());
+
+ {
+ boost::mutex::scoped_lock lm (_mutex);
+ _audio_channels = dec.audio_channels ();
+ _audio_length = dec.audio_length ();
+ _audio_frame_rate = dec.audio_frame_rate ();
+ }
+
+ signal_changed (AudioContentProperty::AUDIO_CHANNELS);
+ signal_changed (AudioContentProperty::AUDIO_LENGTH);
+ signal_changed (AudioContentProperty::AUDIO_FRAME_RATE);
+}
+
+void
+SndfileContent::as_xml (xmlpp::Node* node) const
+{
+ node->add_child("Type")->add_child_text ("Sndfile");
+ Content::as_xml (node);
+ node->add_child("AudioChannels")->add_child_text (lexical_cast<string> (_audio_channels));
+ node->add_child("AudioLength")->add_child_text (lexical_cast<string> (_audio_length));
+ node->add_child("AudioFrameRate")->add_child_text (lexical_cast<string> (_audio_frame_rate));
+}
diff --git a/src/lib/sndfile_content.h b/src/lib/sndfile_content.h
index b696b57a5..27c5f3615 100644
--- a/src/lib/sndfile_content.h
+++ b/src/lib/sndfile_content.h
@@ -17,6 +17,9 @@
*/
+extern "C" {
+#include <libavutil/audioconvert.h>
+}
#include "audio_content.h"
namespace cxml {
@@ -33,15 +36,36 @@ public:
return boost::dynamic_pointer_cast<SndfileContent> (Content::shared_from_this ());
}
+ void examine (boost::shared_ptr<Film>, boost::shared_ptr<Job>, bool);
std::string summary () const;
std::string information () const;
+ void as_xml (xmlpp::Node *) const;
boost::shared_ptr<Content> clone () const;
/* AudioContent */
- int audio_channels () const;
- ContentAudioFrame audio_length () const;
- int audio_frame_rate () const;
- int64_t audio_channel_layout () const;
+ int audio_channels () const {
+ boost::mutex::scoped_lock lm (_mutex);
+ return _audio_channels;
+ }
+
+ ContentAudioFrame audio_length () const {
+ boost::mutex::scoped_lock lm (_mutex);
+ return _audio_length;
+ }
+
+ int audio_frame_rate () const {
+ boost::mutex::scoped_lock lm (_mutex);
+ return _audio_frame_rate;
+ }
+
+ int64_t audio_channel_layout () const {
+ return av_get_default_channel_layout (audio_channels ());
+ }
static bool valid_file (boost::filesystem::path);
+
+private:
+ int _audio_channels;
+ ContentAudioFrame _audio_length;
+ int _audio_frame_rate;
};
diff --git a/src/lib/sndfile_decoder.cc b/src/lib/sndfile_decoder.cc
index 1f97e5c41..c7311112a 100644
--- a/src/lib/sndfile_decoder.cc
+++ b/src/lib/sndfile_decoder.cc
@@ -28,59 +28,62 @@
using std::vector;
using std::string;
-using std::stringstream;
using std::min;
using std::cout;
using boost::shared_ptr;
-using boost::optional;
-
-/* XXX */
SndfileDecoder::SndfileDecoder (shared_ptr<const Film> f, shared_ptr<const SndfileContent> c)
: Decoder (f)
, AudioDecoder (f)
+ , _sndfile_content (c)
{
- sf_count_t frames;
- SNDFILE* sf = open_file (frames);
- sf_close (sf);
+ _sndfile = sf_open (_sndfile_content->file().string().c_str(), SFM_READ, &_info);
+ if (!_sndfile) {
+ throw DecodeError (_("could not open audio file for reading"));
+ }
+
+ _remaining = _info.frames;
}
-SNDFILE*
-SndfileDecoder::open_file (sf_count_t & frames)
+SndfileDecoder::~SndfileDecoder ()
{
- frames = 0;
-
- SF_INFO info;
- SNDFILE* s = sf_open (_sndfile_content->file().string().c_str(), SFM_READ, &info);
- if (!s) {
- throw DecodeError (_("could not open external audio file for reading"));
+ if (_sndfile) {
+ sf_close (_sndfile);
}
-
- frames = info.frames;
- return s;
}
bool
SndfileDecoder::pass ()
{
- sf_count_t frames;
- SNDFILE* sndfile = open_file (frames);
-
/* Do things in half second blocks as I think there may be limits
to what FFmpeg (and in particular the resampler) can cope with.
*/
sf_count_t const block = _sndfile_content->audio_frame_rate() / 2;
+ sf_count_t const this_time = min (block, _remaining);
+
+ shared_ptr<AudioBuffers> audio (new AudioBuffers (_sndfile_content->audio_channels(), this_time));
+ sf_read_float (_sndfile, audio->data(0), this_time);
+ audio->set_frames (this_time);
+ Audio (audio);
+ _remaining -= this_time;
- shared_ptr<AudioBuffers> audio (new AudioBuffers (_sndfile_content->audio_channels(), block));
- while (frames > 0) {
- sf_count_t const this_time = min (block, frames);
- sf_read_float (sndfile, audio->data(0), this_time);
- audio->set_frames (this_time);
- Audio (audio);
- frames -= this_time;
- }
+ return (_remaining == 0);
+}
- sf_close (sndfile);
+int
+SndfileDecoder::audio_channels () const
+{
+ return _info.channels;
+}
- return true;
+ContentAudioFrame
+SndfileDecoder::audio_length () const
+{
+ return _info.frames;
+}
+
+int
+SndfileDecoder::audio_frame_rate () const
+{
+ return _info.samplerate;
}
diff --git a/src/lib/sndfile_decoder.h b/src/lib/sndfile_decoder.h
index 56fc3a9f0..2900afea0 100644
--- a/src/lib/sndfile_decoder.h
+++ b/src/lib/sndfile_decoder.h
@@ -27,12 +27,20 @@ class SndfileDecoder : public AudioDecoder
{
public:
SndfileDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const SndfileContent>);
+ ~SndfileDecoder ();
bool pass ();
+ int audio_channels () const;
+ ContentAudioFrame audio_length () const;
+ int audio_frame_rate () const;
+
private:
SNDFILE* open_file (sf_count_t &);
void close_file (SNDFILE*);
boost::shared_ptr<const SndfileContent> _sndfile_content;
+ SNDFILE* _sndfile;
+ SF_INFO _info;
+ ContentAudioFrame _remaining;
};
diff --git a/src/lib/util.cc b/src/lib/util.cc
index e0de82c64..1e60b43fc 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -914,58 +914,6 @@ audio_channel_name (int c)
return channels[c];
}
-AudioMapping::AudioMapping (int c)
- : _source_channels (c)
-{
-
-}
-
-optional<libdcp::Channel>
-AudioMapping::source_to_dcp (int c) const
-{
- if (c >= _source_channels) {
- return optional<libdcp::Channel> ();
- }
-
- if (_source_channels == 1) {
- /* mono sources to centre */
- return libdcp::CENTRE;
- }
-
- return static_cast<libdcp::Channel> (c);
-}
-
-optional<int>
-AudioMapping::dcp_to_source (libdcp::Channel c) const
-{
- if (_source_channels == 1) {
- if (c == libdcp::CENTRE) {
- return 0;
- } else {
- return optional<int> ();
- }
- }
-
- if (static_cast<int> (c) >= _source_channels) {
- return optional<int> ();
- }
-
- return static_cast<int> (c);
-}
-
-int
-AudioMapping::dcp_channels () const
-{
- if (_source_channels == 1) {
- /* The source is mono, so to put the mono channel into
- the centre we need to generate a 5.1 soundtrack.
- */
- return 6;
- }
-
- return _source_channels;
-}
-
FrameRateConversion::FrameRateConversion (float source, int dcp)
: skip (false)
, repeat (false)
diff --git a/src/lib/util.h b/src/lib/util.h
index 23ebd52bc..1fe6212e4 100644
--- a/src/lib/util.h
+++ b/src/lib/util.h
@@ -194,19 +194,6 @@ private:
float** _data;
};
-class AudioMapping
-{
-public:
- AudioMapping (int);
-
- boost::optional<libdcp::Channel> source_to_dcp (int c) const;
- boost::optional<int> dcp_to_source (libdcp::Channel c) const;
- int dcp_channels () const;
-
-private:
- int _source_channels;
-};
-
extern int64_t video_frames_to_audio_frames (ContentVideoFrame v, float audio_sample_rate, float frames_per_second);
extern std::pair<std::string, int> cpu_info ();
diff --git a/src/lib/writer.cc b/src/lib/writer.cc
index 5d38860e7..6df1a1f21 100644
--- a/src/lib/writer.cc
+++ b/src/lib/writer.cc
@@ -31,6 +31,7 @@
#include "dcp_video_frame.h"
#include "dcp_content_type.h"
#include "player.h"
+#include "audio_mapping.h"
#include "i18n.h"
@@ -77,7 +78,7 @@ Writer::Writer (shared_ptr<Film> f)
_picture_asset_writer = _picture_asset->start_write (_first_nonexistant_frame > 0);
- AudioMapping m (_film->audio_channels ());
+ AutomaticAudioMapping m (_film->audio_channels ());
if (m.dcp_channels() > 0) {
_sound_asset.reset (
diff --git a/src/lib/wscript b/src/lib/wscript
index 32a2cde23..896598a0e 100644
--- a/src/lib/wscript
+++ b/src/lib/wscript
@@ -8,6 +8,7 @@ sources = """
audio_analysis.cc
audio_content.cc
audio_decoder.cc
+ audio_mapping.cc
audio_source.cc
config.cc
combiner.cc