Add simple support for generating audio MXFs from part of a WAV file (for multi-reel...
[libdcp.git] / src / sound_asset.cc
index 7a3d44152b4b27ff15844ae19c97866ef41dc1b5..98266b2805d486164f75545b6b5dd67b98ce2e8d 100644 (file)
 #include "exceptions.h"
 #include "sound_frame.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using std::stringstream;
+using std::ostream;
+using std::vector;
+using std::list;
+using boost::shared_ptr;
+using boost::lexical_cast;
 using namespace libdcp;
 
 SoundAsset::SoundAsset (
-       vector<string> const & files, string directory, string mxf_name, sigc::signal1<void, float>* progress, int fps, int length
+       vector<string> const & files, string directory, string mxf_name, boost::signals2::signal<void (float)>* progress, int fps, int length, int start_frame
        )
        : MXFAsset (directory, mxf_name, progress, fps, 0, length)
        , _channels (files.size ())
        , _sampling_rate (0)
+       , _start_frame (start_frame)
 {
-       construct (sigc::bind (sigc::mem_fun (*this, &SoundAsset::path_from_channel), files));
+       assert (_channels);
+       
+       construct (boost::bind (&SoundAsset::path_from_channel, this, _1, files));
 }
 
 SoundAsset::SoundAsset (
-       sigc::slot<string, Channel> get_path, string directory, string mxf_name, sigc::signal1<void, float>* progress, int fps, int length, int channels
+       boost::function<string (Channel)> get_path,
+       string directory,
+       string mxf_name,
+       boost::signals2::signal<void (float)>* progress,
+       int fps, int length, int start_frame, int channels
        )
        : MXFAsset (directory, mxf_name, progress, fps, 0, length)
        , _channels (channels)
        , _sampling_rate (0)
+       , _start_frame (start_frame)
 {
+       assert (_channels);
+       
        construct (get_path);
 }
 
 SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int entry_point, int length)
        : MXFAsset (directory, mxf_name, 0, fps, entry_point, length)
        , _channels (0)
+       , _start_frame (0)
 {
        ASDCP::PCM::MXFReader reader;
        if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
@@ -84,10 +100,10 @@ SoundAsset::path_from_channel (Channel channel, vector<string> const & files)
 }
 
 void
-SoundAsset::construct (sigc::slot<string, Channel> get_path)
+SoundAsset::construct (boost::function<string (Channel)> get_path)
 {
        ASDCP::Rational asdcp_fps (_fps, 1);
-       
+
        ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
        if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_fps)) {
                throw FileError ("could not open WAV file for reading", get_path(LEFT));
@@ -106,9 +122,14 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                CENTRE,
                LFE,
                LS,
-               RS
+               RS,
+               /* XXX: not quite sure what these should be yet */
+               CHANNEL_7,
+               CHANNEL_8
        };
 
+       assert (int(_channels) <= int(sizeof(channels) / sizeof(Channel)));
+
        ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
        ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
 
@@ -139,24 +160,29 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
                throw FileError ("could not open audio MXF for writing", path().string());
        }
 
+       /* Skip through up to our _start_frame; this is pretty inefficient... */
+       for (int i = 0; i < _start_frame; ++i) {
+               for (int j = 0; j < _channels; ++j) {
+                       if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
+                               throw MiscError ("could not read audio frame");
+                       }
+               }
+       }
+       
        for (int i = 0; i < _length; ++i) {
 
-               byte_t *data_s = frame_buffer.Data();
-               byte_t *data_e = data_s + frame_buffer.Capacity();
-               byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
-               int offset = 0;
-
                for (int j = 0; j < _channels; ++j) {
                        memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
                        if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
                                throw MiscError ("could not read audio frame");
                        }
-                       
-                       if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
-                               throw MiscError ("short audio frame");
-                       }
                }
 
+               byte_t *data_s = frame_buffer.Data();
+               byte_t *data_e = data_s + frame_buffer.Capacity();
+               byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
+               int offset = 0;
+
                while (data_s < data_e) {
                        for (int j = 0; j < _channels; ++j) {
                                byte_t* frame = frame_buffer_channel[j].Data() + offset;
@@ -254,8 +280,13 @@ SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<str
                }
                
                if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
-                       notes.push_back ("PCM data for MXF frame " + lexical_cast<string>(i) + " differ");
-                       return false;
+                       for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
+                               int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
+                               if (d > opt.max_audio_sample_error) {
+                                       notes.push_back ("PCM data difference of " + lexical_cast<string> (d));
+                                       return false;
+                               }
+                       }
                }
        }