Actually turn on HMAC use.
[libdcp.git] / src / sound_asset_writer.cc
index 4a27639e31738fe8eaa89e1e34c45835b6ff6119..a716b654ba738cbb33bd7e800df0777199cef219 100644 (file)
@@ -24,6 +24,8 @@
 #include "compose.hpp"
 #include "AS_DCP.h"
 
+using std::min;
+using std::max;
 using namespace dcp;
 
 struct SoundAssetWriter::ASDCPState
@@ -35,7 +37,7 @@ struct SoundAssetWriter::ASDCPState
 };
 
 SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, Standard standard)
-       : AssetWriter (asset, file)
+       : AssetWriter (asset, file, standard)
        , _state (new SoundAssetWriter::ASDCPState)
        , _sound_asset (asset)
        , _frame_buffer_offset (0)
@@ -56,13 +58,6 @@ SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path f
        memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
 
        _sound_asset->fill_writer_info (&_state->writer_info, _sound_asset->id(), standard);
-
-       Kumu::Result_t r = _state->mxf_writer.OpenWrite (file.string().c_str(), _state->writer_info, _state->audio_desc);
-       if (ASDCP_FAILURE (r)) {
-               boost::throw_exception (FileError ("could not open audio MXF for writing", file.string(), r));
-       }
-
-       _sound_asset->set_file (file);
 }
 
 void
@@ -70,13 +65,26 @@ SoundAssetWriter::write (float const * const * data, int frames)
 {
        DCP_ASSERT (!_finalized);
 
+       static float const clip = 1.0f - (1.0f / pow (2, 23));
+
+       if (!_started) {
+               Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc);
+               if (ASDCP_FAILURE (r)) {
+                       boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
+               }
+
+               _sound_asset->set_file (_file);
+               _started = true;
+       }
+
        for (int i = 0; i < frames; ++i) {
 
                byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
 
                /* Write one sample per channel */
                for (int j = 0; j < _sound_asset->channels(); ++j) {
-                       int32_t const s = data[j][i] * (1 << 23);
+                       /* Convert sample to 24-bit int, clipping if necessary. */
+                       int32_t const s = min (clip, max (-clip, data[j][i])) * (1 << 23);
                        *out++ = (s & 0xff);
                        *out++ = (s & 0xff00) >> 8;
                        *out++ = (s & 0xff0000) >> 16;
@@ -97,7 +105,7 @@ SoundAssetWriter::write (float const * const * data, int frames)
 void
 SoundAssetWriter::write_current_frame ()
 {
-       ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, 0);
+       ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, _hmac_context);
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
        }
@@ -105,17 +113,17 @@ SoundAssetWriter::write_current_frame ()
        ++_frames_written;
 }
 
-void
+bool
 SoundAssetWriter::finalize ()
 {
        if (_frame_buffer_offset > 0) {
                write_current_frame ();
        }
 
-       if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
+       if (_started && ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
                boost::throw_exception (MiscError ("could not finalise audio MXF"));
        }
 
        _sound_asset->_intrinsic_duration = _frames_written;
-       AssetWriter::finalize ();
+       return AssetWriter::finalize ();
 }