Allow incremental writing of picture MXFs.
[libdcp.git] / src / sound_asset.cc
index 500a72e5c09e73219aa01b82a6474d4add87fb46..04ca88da3a7a22cf2a6c36598d7a9066d2e340f6 100644 (file)
@@ -42,12 +42,19 @@ using boost::lexical_cast;
 using namespace libdcp;
 
 SoundAsset::SoundAsset (
-       vector<string> const & files, string directory, string mxf_name, boost::signals2::signal<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 intrinsic_duration, int start_frame
        )
-       : MXFAsset (directory, mxf_name, progress, fps, 0, length)
+       : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
        , _channels (files.size ())
        , _sampling_rate (0)
+       , _start_frame (start_frame)
 {
+       assert (_channels);
+       
        construct (boost::bind (&SoundAsset::path_from_channel, this, _1, files));
 }
 
@@ -56,18 +63,22 @@ SoundAsset::SoundAsset (
        string directory,
        string mxf_name,
        boost::signals2::signal<void (float)>* progress,
-       int fps, int length, int channels
+       int fps, int intrinsic_duration, int start_frame, int channels
        )
-       : MXFAsset (directory, mxf_name, progress, fps, 0, length)
+       : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
        , _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)
+SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int intrinsic_duration)
+       : MXFAsset (directory, mxf_name, 0, fps, intrinsic_duration)
        , _channels (0)
+       , _start_frame (0)
 {
        ASDCP::PCM::MXFReader reader;
        if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
@@ -96,7 +107,7 @@ void
 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));
@@ -146,34 +157,36 @@ SoundAsset::construct (boost::function<string (Channel)> get_path)
        frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
 
        ASDCP::WriterInfo writer_info;
-       fill_writer_info (&writer_info);
+       fill_writer_info (&writer_info, _uuid);
 
        ASDCP::PCM::MXFWriter mxf_writer;
        if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
                throw FileError ("could not open audio MXF for writing", path().string());
        }
 
-       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;
+       /* 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 < _intrinsic_duration; ++i) {
 
                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()) {
-                               stringstream s;
-                               s << "short audio frame; " << _channels << " channels, "
-                                 << frame_buffer_channel[j].Size() << " vs " << frame_buffer_channel[j].Capacity();
-                               throw MiscError (s.str ());
-                       }
                }
 
+               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;
@@ -188,7 +201,7 @@ SoundAsset::construct (boost::function<string (Channel)> get_path)
                }
 
                if (_progress) {
-                       (*_progress) (0.5 * float (i) / _length);
+                       (*_progress) (0.5 * float (i) / _intrinsic_duration);
                }
        }
 
@@ -204,9 +217,9 @@ SoundAsset::write_to_cpl (ostream& s) const
          << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
          << "          <AnnotationText>" << _file_name << "</AnnotationText>\n"
          << "          <EditRate>" << _fps << " 1</EditRate>\n"
-         << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
-         << "          <EntryPoint>0</EntryPoint>\n"
-         << "          <Duration>" << _length << "</Duration>\n"
+         << "          <IntrinsicDuration>" << _intrinsic_duration << "</IntrinsicDuration>\n"
+         << "          <EntryPoint>" << _entry_point << "</EntryPoint>\n"
+         << "          <Duration>" << _duration << "</Duration>\n"
          << "        </MainSound>\n";
 }
 
@@ -256,7 +269,7 @@ SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<str
        ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
        ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
        
-       for (int i = 0; i < _length; ++i) {
+       for (int i = 0; i < _intrinsic_duration; ++i) {
                if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
                        throw DCPReadError ("could not read audio frame");
                }