2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
34 #include "sound_asset_writer.h"
35 #include "sound_asset.h"
36 #include "exceptions.h"
37 #include "dcp_assert.h"
38 #include "compose.hpp"
45 struct SoundAssetWriter::ASDCPState
47 ASDCP::PCM::MXFWriter mxf_writer;
48 ASDCP::PCM::FrameBuffer frame_buffer;
49 ASDCP::WriterInfo writer_info;
50 ASDCP::PCM::AudioDescriptor audio_desc;
53 SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, Standard standard)
54 : AssetWriter (asset, file, standard)
55 , _state (new SoundAssetWriter::ASDCPState)
56 , _sound_asset (asset)
57 , _frame_buffer_offset (0)
59 /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
60 _state->audio_desc.EditRate = ASDCP::Rational (_sound_asset->edit_rate().numerator, _sound_asset->edit_rate().denominator);
61 _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_sound_asset->sampling_rate(), 1);
62 _state->audio_desc.Locked = 0;
63 _state->audio_desc.ChannelCount = _sound_asset->channels ();
64 _state->audio_desc.QuantizationBits = 24;
65 _state->audio_desc.BlockAlign = 3 * _sound_asset->channels();
66 _state->audio_desc.AvgBps = _sound_asset->sampling_rate() * _state->audio_desc.BlockAlign;
67 _state->audio_desc.LinkedTrackID = 0;
68 _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
70 _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
71 _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
72 memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
74 _sound_asset->fill_writer_info (&_state->writer_info, _sound_asset->id(), standard);
78 SoundAssetWriter::write (float const * const * data, int frames)
80 DCP_ASSERT (!_finalized);
82 static float const clip = 1.0f - (1.0f / pow (2, 23));
85 Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc);
86 if (ASDCP_FAILURE (r)) {
87 boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
90 _sound_asset->set_file (_file);
94 int const ch = _sound_asset->channels ();
96 for (int i = 0; i < frames; ++i) {
98 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
100 /* Write one sample per channel */
101 for (int j = 0; j < ch; ++j) {
102 /* Convert sample to 24-bit int, clipping if necessary. */
103 float x = data[j][i];
106 } else if (x < -clip) {
109 int32_t const s = x * (1 << 23);
111 *out++ = (s & 0xff00) >> 8;
112 *out++ = (s & 0xff0000) >> 16;
114 _frame_buffer_offset += 3 * ch;
116 DCP_ASSERT (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
118 /* Finish the MXF frame if required */
119 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
120 write_current_frame ();
121 _frame_buffer_offset = 0;
122 memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
128 SoundAssetWriter::write_current_frame ()
130 ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, _hmac_context);
131 if (ASDCP_FAILURE (r)) {
132 boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
139 SoundAssetWriter::finalize ()
141 if (_frame_buffer_offset > 0) {
142 write_current_frame ();
145 if (_started && ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
146 boost::throw_exception (MiscError ("could not finalise audio MXF"));
149 _sound_asset->_intrinsic_duration = _frames_written;
150 return AssetWriter::finalize ();