Express standard differences in FontNode more neatly.
[libdcp.git] / src / sound_asset_writer.cc
index 02029f18935902c35d5d1cdbb97e25ae6404a292..35c3dab0d4b6222bb17ca89d91bc00bc1f68a0df 100644 (file)
@@ -1,20 +1,34 @@
 /*
     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
+    libdcp is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
 */
 
 #include "sound_asset_writer.h"
@@ -24,6 +38,8 @@
 #include "compose.hpp"
 #include "AS_DCP.h"
 
+using std::min;
+using std::max;
 using namespace dcp;
 
 struct SoundAssetWriter::ASDCPState
@@ -35,7 +51,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)
@@ -63,6 +79,8 @@ 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)) {
@@ -79,7 +97,8 @@ SoundAssetWriter::write (float const * const * data, int frames)
 
                /* 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;
@@ -100,7 +119,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))));
        }