Still more licence fixups.
[libdcp.git] / src / sound_asset_writer.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18 */
19
20 #include "sound_asset_writer.h"
21 #include "sound_asset.h"
22 #include "exceptions.h"
23 #include "dcp_assert.h"
24 #include "compose.hpp"
25 #include "AS_DCP.h"
26
27 using std::min;
28 using std::max;
29 using namespace dcp;
30
31 struct SoundAssetWriter::ASDCPState
32 {
33         ASDCP::PCM::MXFWriter mxf_writer;
34         ASDCP::PCM::FrameBuffer frame_buffer;
35         ASDCP::WriterInfo writer_info;
36         ASDCP::PCM::AudioDescriptor audio_desc;
37 };
38
39 SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, Standard standard)
40         : AssetWriter (asset, file, standard)
41         , _state (new SoundAssetWriter::ASDCPState)
42         , _sound_asset (asset)
43         , _frame_buffer_offset (0)
44 {
45         /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
46         _state->audio_desc.EditRate = ASDCP::Rational (_sound_asset->edit_rate().numerator, _sound_asset->edit_rate().denominator);
47         _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_sound_asset->sampling_rate(), 1);
48         _state->audio_desc.Locked = 0;
49         _state->audio_desc.ChannelCount = _sound_asset->channels ();
50         _state->audio_desc.QuantizationBits = 24;
51         _state->audio_desc.BlockAlign = 3 * _sound_asset->channels();
52         _state->audio_desc.AvgBps = _sound_asset->sampling_rate() * _state->audio_desc.BlockAlign;
53         _state->audio_desc.LinkedTrackID = 0;
54         _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
55
56         _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
57         _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
58         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
59
60         _sound_asset->fill_writer_info (&_state->writer_info, _sound_asset->id(), standard);
61 }
62
63 void
64 SoundAssetWriter::write (float const * const * data, int frames)
65 {
66         DCP_ASSERT (!_finalized);
67
68         static float const clip = 1.0f - (1.0f / pow (2, 23));
69
70         if (!_started) {
71                 Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc);
72                 if (ASDCP_FAILURE (r)) {
73                         boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
74                 }
75
76                 _sound_asset->set_file (_file);
77                 _started = true;
78         }
79
80         for (int i = 0; i < frames; ++i) {
81
82                 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
83
84                 /* Write one sample per channel */
85                 for (int j = 0; j < _sound_asset->channels(); ++j) {
86                         /* Convert sample to 24-bit int, clipping if necessary. */
87                         int32_t const s = min (clip, max (-clip, data[j][i])) * (1 << 23);
88                         *out++ = (s & 0xff);
89                         *out++ = (s & 0xff00) >> 8;
90                         *out++ = (s & 0xff0000) >> 16;
91                 }
92                 _frame_buffer_offset += 3 * _sound_asset->channels();
93
94                 DCP_ASSERT (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
95
96                 /* Finish the MXF frame if required */
97                 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
98                         write_current_frame ();
99                         _frame_buffer_offset = 0;
100                         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
101                 }
102         }
103 }
104
105 void
106 SoundAssetWriter::write_current_frame ()
107 {
108         ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, _hmac_context);
109         if (ASDCP_FAILURE (r)) {
110                 boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
111         }
112
113         ++_frames_written;
114 }
115
116 bool
117 SoundAssetWriter::finalize ()
118 {
119         if (_frame_buffer_offset > 0) {
120                 write_current_frame ();
121         }
122
123         if (_started && ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
124                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
125         }
126
127         _sound_asset->_intrinsic_duration = _frames_written;
128         return AssetWriter::finalize ();
129 }