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