Missing install target.
[libdcp.git] / src / sound_asset_writer.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
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 namespace dcp;
28
29 struct SoundAssetWriter::ASDCPState
30 {
31         ASDCP::PCM::MXFWriter mxf_writer;
32         ASDCP::PCM::FrameBuffer frame_buffer;
33         ASDCP::WriterInfo writer_info;
34         ASDCP::PCM::AudioDescriptor audio_desc;
35 };
36
37 SoundAssetWriter::SoundAssetWriter (SoundAsset* asset, boost::filesystem::path file, Standard standard)
38         : AssetWriter (asset, file)
39         , _state (new SoundAssetWriter::ASDCPState)
40         , _sound_asset (asset)
41         , _frame_buffer_offset (0)
42 {
43         /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
44         _state->audio_desc.EditRate = ASDCP::Rational (_sound_asset->edit_rate().numerator, _sound_asset->edit_rate().denominator);
45         _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_sound_asset->sampling_rate(), 1);
46         _state->audio_desc.Locked = 0;
47         _state->audio_desc.ChannelCount = _sound_asset->channels ();
48         _state->audio_desc.QuantizationBits = 24;
49         _state->audio_desc.BlockAlign = 3 * _sound_asset->channels();
50         _state->audio_desc.AvgBps = _sound_asset->sampling_rate() * _state->audio_desc.BlockAlign;
51         _state->audio_desc.LinkedTrackID = 0;
52         _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
53
54         _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
55         _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
56         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
57
58         _sound_asset->fill_writer_info (&_state->writer_info, _sound_asset->id(), standard);
59 }
60
61 void
62 SoundAssetWriter::write (float const * const * data, int frames)
63 {
64         DCP_ASSERT (!_finalized);
65
66         if (!_started) {
67                 Kumu::Result_t r = _state->mxf_writer.OpenWrite (_file.string().c_str(), _state->writer_info, _state->audio_desc);
68                 if (ASDCP_FAILURE (r)) {
69                         boost::throw_exception (FileError ("could not open audio MXF for writing", _file.string(), r));
70                 }
71
72                 _sound_asset->set_file (_file);
73                 _started = true;
74         }
75
76         for (int i = 0; i < frames; ++i) {
77
78                 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
79
80                 /* Write one sample per channel */
81                 for (int j = 0; j < _sound_asset->channels(); ++j) {
82                         int32_t const s = data[j][i] * (1 << 23);
83                         *out++ = (s & 0xff);
84                         *out++ = (s & 0xff00) >> 8;
85                         *out++ = (s & 0xff0000) >> 16;
86                 }
87                 _frame_buffer_offset += 3 * _sound_asset->channels();
88
89                 DCP_ASSERT (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
90
91                 /* Finish the MXF frame if required */
92                 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
93                         write_current_frame ();
94                         _frame_buffer_offset = 0;
95                         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
96                 }
97         }
98 }
99
100 void
101 SoundAssetWriter::write_current_frame ()
102 {
103         ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _encryption_context, 0);
104         if (ASDCP_FAILURE (r)) {
105                 boost::throw_exception (MiscError (String::compose ("could not write audio MXF frame (%1)", int (r))));
106         }
107
108         ++_frames_written;
109 }
110
111 bool
112 SoundAssetWriter::finalize ()
113 {
114         if (_frame_buffer_offset > 0) {
115                 write_current_frame ();
116         }
117
118         if (_started && ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
119                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
120         }
121
122         _sound_asset->_intrinsic_duration = _frames_written;
123         return AssetWriter::finalize ();
124 }