Access sound asset sampling rate, channels; access sound asset from DCP; fix bad...
[libdcp.git] / src / sound_asset.cc
index e22357fd1ed2833427a0e915d56049c17f59f7e8..a8b6372748c83069221fc95790bc5b8b36f6215e 100644 (file)
@@ -24,6 +24,8 @@
 #include <iostream>
 #include <stdexcept>
 #include <boost/filesystem.hpp>
+#include <boost/lexical_cast.hpp>
+#include "KM_fileio.h"
 #include "AS_DCP.h"
 #include "sound_asset.h"
 #include "util.h"
@@ -118,7 +120,7 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
        fill_writer_info (&writer_info);
 
        ASDCP::PCM::MXFWriter mxf_writer;
-       if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().c_str(), writer_info, audio_desc))) {
+       if (ASDCP_FAILURE (mxf_writer.OpenWrite (mxf_path().string().c_str(), writer_info, audio_desc))) {
                throw FileError ("could not open audio MXF for writing", mxf_path().string());
        }
 
@@ -159,8 +161,6 @@ SoundAsset::construct (sigc::slot<string, Channel> get_path)
        if (ASDCP_FAILURE (mxf_writer.Finalize())) {
                throw MiscError ("could not finalise audio MXF");
        }
-
-       _digest = make_digest (mxf_path().string(), _progress);
 }
 
 void
@@ -176,3 +176,91 @@ SoundAsset::write_to_cpl (ostream& s) const
          << "        </MainSound>\n";
 }
 
+list<string>
+SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt) const
+{
+       list<string> notes = Asset::equals (other, opt);
+                    
+       if (opt.flags & MXF_INSPECT) {
+               ASDCP::PCM::MXFReader reader_A;
+               if (ASDCP_FAILURE (reader_A.OpenRead (mxf_path().string().c_str()))) {
+                       cout << "failed " << mxf_path() << "\n";
+                       throw FileError ("could not open MXF file for reading", mxf_path().string());
+               }
+
+               ASDCP::PCM::MXFReader reader_B;
+               if (ASDCP_FAILURE (reader_B.OpenRead (other->mxf_path().string().c_str()))) {
+                       cout << "failed " << other->mxf_path() << "\n";
+                       throw FileError ("could not open MXF file for reading", mxf_path().string());
+               }
+
+               ASDCP::PCM::AudioDescriptor desc_A;
+               if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
+                       throw DCPReadError ("could not read audio MXF information");
+               }
+               ASDCP::PCM::AudioDescriptor desc_B;
+               if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
+                       throw DCPReadError ("could not read audio MXF information");
+               }
+
+               if (
+                       desc_A.EditRate != desc_B.EditRate ||
+                       desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
+                       desc_A.Locked != desc_B.Locked ||
+                       desc_A.ChannelCount != desc_B.ChannelCount ||
+                       desc_A.QuantizationBits != desc_B.QuantizationBits ||
+                       desc_A.BlockAlign != desc_B.BlockAlign ||
+                       desc_A.AvgBps != desc_B.AvgBps ||
+                       desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
+                       desc_A.ContainerDuration != desc_B.ContainerDuration
+//                     desc_A.ChannelFormat != desc_B.ChannelFormat ||
+                       ) {
+               
+                       notes.push_back ("audio MXF picture descriptors differ");
+               }
+
+               ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
+               ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
+
+               for (int i = 0; i < _length; ++i) {
+                       if (ASDCP_FAILURE (reader_A.ReadFrame (0, buffer_A))) {
+                               throw DCPReadError ("could not read audio frame");
+                       }
+
+                       if (ASDCP_FAILURE (reader_B.ReadFrame (0, buffer_B))) {
+                               throw DCPReadError ("could not read audio frame");
+                       }
+
+                       if (buffer_A.Size() != buffer_B.Size()) {
+                               notes.push_back ("sizes of audio data for frame " + lexical_cast<string>(i) + " differ");
+                               continue;
+                       }
+
+                       if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
+                               notes.push_back ("PCM data for frame " + lexical_cast<string>(i) + " differ");
+                               continue;
+                       }
+               }
+       }
+
+       return notes;
+}
+
+
+int
+SoundAsset::sampling_rate () const
+{
+       ASDCP::PCM::MXFReader reader;
+       if (ASDCP_FAILURE (reader.OpenRead (mxf_path().string().c_str()))) {
+               throw FileError ("could not open MXF file for reading", mxf_path().string());
+       }
+
+       
+       ASDCP::PCM::AudioDescriptor desc;
+       if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
+               throw DCPReadError ("could not read audio MXF information");
+       }
+
+       return desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
+}
+