Use raw_convert instead of boost::lexical_cast as it seems
[libdcp.git] / src / sound_asset.cc
index 8f666ee60a1cb46ba8142726f59fa22d283b169c..54fbdb88b5727df4f9800404f0d0c14382f219e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2014 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
@@ -42,7 +42,7 @@ using boost::shared_ptr;
 using boost::lexical_cast;
 using namespace libdcp;
 
-SoundAsset::SoundAsset (boost::filesystem::path directory, string mxf_name)
+SoundAsset::SoundAsset (boost::filesystem::path directory, boost::filesystem::path mxf_name)
        : MXFAsset (directory, mxf_name)
        , _channels (0)
        , _sampling_rate (0)
@@ -60,8 +60,9 @@ void
 SoundAsset::read ()
 {
        ASDCP::PCM::MXFReader reader;
-       if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
-               boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+       Kumu::Result_t r = reader.OpenRead (path().string().c_str());
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
        }
 
        ASDCP::PCM::AudioDescriptor desc;
@@ -90,13 +91,18 @@ SoundAsset::create (boost::function<boost::filesystem::path (Channel)> get_path)
        ASDCP::Rational asdcp_edit_rate (_edit_rate, 1);
 
        assert (_channels > 0);
-       ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
-       if (pcm_parser_channel[0].OpenRead (get_path(LEFT).string().c_str(), asdcp_edit_rate)) {
-               boost::throw_exception (FileError ("could not open WAV file for reading", get_path(LEFT)));
+       ASDCP::PCM::WAVParser* pcm_parser_channel[_channels];
+       for (int i = 0; i < _channels; ++i) {
+               pcm_parser_channel[i] = new ASDCP::PCM::WAVParser ();
+       }
+
+       Kumu::Result_t r = pcm_parser_channel[0]->OpenRead (get_path(LEFT).string().c_str(), asdcp_edit_rate);
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (FileError ("could not open WAV file for reading", get_path(LEFT), r));
        }
        
        ASDCP::PCM::AudioDescriptor audio_desc;
-       pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
+       pcm_parser_channel[0]->FillAudioDescriptor (audio_desc);
        audio_desc.ChannelCount = 0;
        audio_desc.BlockAlign = 0;
        audio_desc.EditRate = asdcp_edit_rate;
@@ -116,22 +122,27 @@ SoundAsset::create (boost::function<boost::filesystem::path (Channel)> get_path)
 
        assert (int(_channels) <= int(sizeof(channels) / sizeof(Channel)));
 
-       ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
-       ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
+       ASDCP::PCM::FrameBuffer* frame_buffer_channel[_channels];
+       ASDCP::PCM::AudioDescriptor* audio_desc_channel[_channels];
+       for (int i = 0; i < _channels; ++i) {
+               frame_buffer_channel[i] = new ASDCP::PCM::FrameBuffer ();
+               audio_desc_channel[i] = new ASDCP::PCM::AudioDescriptor ();
+       }
 
        for (int i = 0; i < _channels; ++i) {
 
                boost::filesystem::path const path = get_path (channels[i]);
                
-               if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.string().c_str(), asdcp_edit_rate))) {
-                       boost::throw_exception (FileError ("could not open WAV file for reading", path));
+               Kumu::Result_t r = pcm_parser_channel[i]->OpenRead (path.string().c_str(), asdcp_edit_rate);
+               if (ASDCP_FAILURE (r)) {
+                       boost::throw_exception (FileError ("could not open WAV file for reading", path, r));
                }
 
-               pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
-               frame_buffer_channel[i].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[i]));
+               pcm_parser_channel[i]->FillAudioDescriptor (*audio_desc_channel[i]);
+               frame_buffer_channel[i]->Capacity (ASDCP::PCM::CalcFrameBufferSize (*audio_desc_channel[i]));
 
-               audio_desc.ChannelCount += audio_desc_channel[i].ChannelCount;
-               audio_desc.BlockAlign += audio_desc_channel[i].BlockAlign;
+               audio_desc.ChannelCount += audio_desc_channel[i]->ChannelCount;
+               audio_desc.BlockAlign += audio_desc_channel[i]->BlockAlign;
        }
 
        ASDCP::PCM::FrameBuffer frame_buffer;
@@ -139,30 +150,31 @@ SoundAsset::create (boost::function<boost::filesystem::path (Channel)> get_path)
        frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
 
        ASDCP::WriterInfo writer_info;
-       MXFAsset::fill_writer_info (&writer_info, _uuid, _interop, _metadata);
+       MXFAsset::fill_writer_info (&writer_info);
 
        ASDCP::PCM::MXFWriter mxf_writer;
-       if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
-               boost::throw_exception (FileError ("could not open audio MXF for writing", path().string()));
+       r = mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc);
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (FileError ("could not open audio MXF for writing", path().string(), r));
        }
        
        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]))) {
+                       memset (frame_buffer_channel[j]->Data(), 0, frame_buffer_channel[j]->Capacity());
+                       if (ASDCP_FAILURE (pcm_parser_channel[j]->ReadFrame (*frame_buffer_channel[j]))) {
                                boost::throw_exception (MiscError ("could not read 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]);
+               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;
+                               byte_t* frame = frame_buffer_channel[j]->Data() + offset;
                                memcpy (data_s, frame, sample_size);
                                data_s += sample_size;
                        }
@@ -178,7 +190,15 @@ SoundAsset::create (boost::function<boost::filesystem::path (Channel)> get_path)
                }
        }
 
-       if (ASDCP_FAILURE (mxf_writer.Finalize())) {
+       bool const failed = ASDCP_FAILURE (mxf_writer.Finalize());
+
+       for (int i = 0; i < _channels; ++i) {
+               delete pcm_parser_channel[i];
+               delete frame_buffer_channel[i];
+               delete audio_desc_channel[i];
+       }
+
+       if (failed) {
                boost::throw_exception (MiscError ("could not finalise audio MXF"));
        }
 }
@@ -197,13 +217,15 @@ SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::f
        }
                     
        ASDCP::PCM::MXFReader reader_A;
-       if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
-               boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+       Kumu::Result_t r = reader_A.OpenRead (path().string().c_str());
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
        }
 
        ASDCP::PCM::MXFReader reader_B;
-       if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
-               boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+       r = reader_B.OpenRead (other->path().string().c_str());
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
        }
 
        ASDCP::PCM::AudioDescriptor desc_A;
@@ -310,10 +332,11 @@ SoundAssetWriter::SoundAssetWriter (SoundAsset* a)
        _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
        memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
        
-       _asset->fill_writer_info (&_state->writer_info, _asset->uuid (), _asset->interop(), _asset->metadata());
+       _asset->fill_writer_info (&_state->writer_info);
        
-       if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (_asset->path().string().c_str(), _state->writer_info, _state->audio_desc))) {
-               boost::throw_exception (FileError ("could not open audio MXF for writing", _asset->path().string()));
+       Kumu::Result_t r = _state->mxf_writer.OpenWrite (_asset->path().string().c_str(), _state->writer_info, _state->audio_desc);
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (FileError ("could not open audio MXF for writing", _asset->path().string(), r));
        }
 }
 
@@ -347,8 +370,9 @@ SoundAssetWriter::write (float const * const * data, int frames)
 void
 SoundAssetWriter::write_current_frame ()
 {
-       if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0))) {
-               boost::throw_exception (MiscError ("could not write audio MXF frame"));
+       ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0);
+       if (ASDCP_FAILURE (r)) {
+               boost::throw_exception (MiscError ("could not write audio MXF frame (" + lexical_cast<string> (int (r)) + ")"));
        }
 
        ++_frames_written;