Use boost::function for making notes during equals operations.
[libdcp.git] / src / sound_asset.cc
1 /*
2     Copyright (C) 2012 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 /** @file  src/sound_asset.cc
21  *  @brief An asset made up of WAV files
22  */
23
24 #include <iostream>
25 #include <stdexcept>
26 #include <boost/filesystem.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include "KM_fileio.h"
29 #include "AS_DCP.h"
30 #include "sound_asset.h"
31 #include "util.h"
32 #include "exceptions.h"
33 #include "sound_frame.h"
34
35 using std::string;
36 using std::stringstream;
37 using std::ostream;
38 using std::vector;
39 using std::list;
40 using boost::shared_ptr;
41 using boost::lexical_cast;
42 using namespace libdcp;
43
44 SoundAsset::SoundAsset (
45         vector<string> const & files,
46         string directory,
47         string mxf_name,
48         boost::signals2::signal<void (float)>* progress,
49         int fps, int intrinsic_duration
50         )
51         : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
52         , _channels (files.size ())
53         , _sampling_rate (0)
54 {
55         assert (_channels);
56         
57         construct (boost::bind (&SoundAsset::path_from_channel, this, _1, files));
58 }
59
60 SoundAsset::SoundAsset (
61         boost::function<string (Channel)> get_path,
62         string directory,
63         string mxf_name,
64         boost::signals2::signal<void (float)>* progress,
65         int fps, int intrinsic_duration, int channels
66         )
67         : MXFAsset (directory, mxf_name, progress, fps, intrinsic_duration)
68         , _channels (channels)
69         , _sampling_rate (0)
70 {
71         assert (_channels);
72         
73         construct (get_path);
74 }
75
76 SoundAsset::SoundAsset (string directory, string mxf_name)
77         : MXFAsset (directory, mxf_name)
78         , _channels (0)
79 {
80         ASDCP::PCM::MXFReader reader;
81         if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
82                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
83         }
84
85         ASDCP::PCM::AudioDescriptor desc;
86         if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
87                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
88         }
89
90         _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
91         _channels = desc.ChannelCount;
92         _edit_rate = desc.EditRate.Numerator;
93         assert (desc.EditRate.Denominator == 1);
94         _intrinsic_duration = desc.ContainerDuration;
95 }
96
97 SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int channels, int sampling_rate)
98         : MXFAsset (directory, mxf_name, 0, fps, 0)
99         , _channels (channels)
100         , _sampling_rate (sampling_rate)
101 {
102
103 }
104
105 string
106 SoundAsset::path_from_channel (Channel channel, vector<string> const & files)
107 {
108         unsigned int const c = int (channel);
109         assert (c < files.size ());
110         return files[c];
111 }
112
113 void
114 SoundAsset::construct (boost::function<string (Channel)> get_path)
115 {
116         ASDCP::Rational asdcp_edit_rate (_edit_rate, 1);
117
118         ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
119         if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_edit_rate)) {
120                 boost::throw_exception (FileError ("could not open WAV file for reading", get_path(LEFT)));
121         }
122         
123         ASDCP::PCM::AudioDescriptor audio_desc;
124         pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
125         audio_desc.ChannelCount = 0;
126         audio_desc.BlockAlign = 0;
127         audio_desc.EditRate = asdcp_edit_rate;
128         audio_desc.AvgBps = audio_desc.AvgBps * _channels;
129
130         Channel channels[] = {
131                 LEFT,
132                 RIGHT,
133                 CENTRE,
134                 LFE,
135                 LS,
136                 RS,
137                 /* XXX: not quite sure what these should be yet */
138                 CHANNEL_7,
139                 CHANNEL_8
140         };
141
142         assert (int(_channels) <= int(sizeof(channels) / sizeof(Channel)));
143
144         ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
145         ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
146
147         for (int i = 0; i < _channels; ++i) {
148
149                 string const path = get_path (channels[i]);
150                 
151                 if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.c_str(), asdcp_edit_rate))) {
152                         boost::throw_exception (FileError ("could not open WAV file for reading", path));
153                 }
154
155                 pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
156                 frame_buffer_channel[i].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[i]));
157
158                 audio_desc.ChannelCount += audio_desc_channel[i].ChannelCount;
159                 audio_desc.BlockAlign += audio_desc_channel[i].BlockAlign;
160         }
161
162         ASDCP::PCM::FrameBuffer frame_buffer;
163         frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
164         frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
165
166         ASDCP::WriterInfo writer_info;
167         MXFAsset::fill_writer_info (&writer_info, _uuid);
168
169         ASDCP::PCM::MXFWriter mxf_writer;
170         if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
171                 boost::throw_exception (FileError ("could not open audio MXF for writing", path().string()));
172         }
173         
174         for (int i = 0; i < _intrinsic_duration; ++i) {
175
176                 for (int j = 0; j < _channels; ++j) {
177                         memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
178                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
179                                 boost::throw_exception (MiscError ("could not read audio frame"));
180                         }
181                 }
182
183                 byte_t *data_s = frame_buffer.Data();
184                 byte_t *data_e = data_s + frame_buffer.Capacity();
185                 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
186                 int offset = 0;
187
188                 while (data_s < data_e) {
189                         for (int j = 0; j < _channels; ++j) {
190                                 byte_t* frame = frame_buffer_channel[j].Data() + offset;
191                                 memcpy (data_s, frame, sample_size);
192                                 data_s += sample_size;
193                         }
194                         offset += sample_size;
195                 }
196
197                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
198                         boost::throw_exception (MiscError ("could not write audio MXF frame"));
199                 }
200
201                 if (_progress) {
202                         (*_progress) (0.5 * float (i) / _intrinsic_duration);
203                 }
204         }
205
206         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
207                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
208         }
209 }
210
211 void
212 SoundAsset::write_to_cpl (ostream& s) const
213 {
214         s << "        <MainSound>\n"
215           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
216           << "          <AnnotationText>" << _file_name << "</AnnotationText>\n"
217           << "          <EditRate>" << _edit_rate << " 1</EditRate>\n"
218           << "          <IntrinsicDuration>" << _intrinsic_duration << "</IntrinsicDuration>\n"
219           << "          <EntryPoint>" << _entry_point << "</EntryPoint>\n"
220           << "          <Duration>" << _duration << "</Duration>\n"
221           << "        </MainSound>\n";
222 }
223
224 bool
225 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (string)> note) const
226 {
227         if (!MXFAsset::equals (other, opt, note)) {
228                 return false;
229         }
230                      
231         ASDCP::PCM::MXFReader reader_A;
232         if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
233                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
234         }
235
236         ASDCP::PCM::MXFReader reader_B;
237         if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
238                 boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
239         }
240
241         ASDCP::PCM::AudioDescriptor desc_A;
242         if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
243                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
244         }
245         ASDCP::PCM::AudioDescriptor desc_B;
246         if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
247                 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
248         }
249         
250         if (
251                 desc_A.EditRate != desc_B.EditRate ||
252                 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
253                 desc_A.Locked != desc_B.Locked ||
254                 desc_A.ChannelCount != desc_B.ChannelCount ||
255                 desc_A.QuantizationBits != desc_B.QuantizationBits ||
256                 desc_A.BlockAlign != desc_B.BlockAlign ||
257                 desc_A.AvgBps != desc_B.AvgBps ||
258                 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
259                 desc_A.ContainerDuration != desc_B.ContainerDuration
260 //              desc_A.ChannelFormat != desc_B.ChannelFormat ||
261                 ) {
262                 
263                 note ("audio MXF picture descriptors differ");
264                 return false;
265         }
266         
267         ASDCP::PCM::FrameBuffer buffer_A (1 * Kumu::Megabyte);
268         ASDCP::PCM::FrameBuffer buffer_B (1 * Kumu::Megabyte);
269         
270         for (int i = 0; i < _intrinsic_duration; ++i) {
271                 if (ASDCP_FAILURE (reader_A.ReadFrame (i, buffer_A))) {
272                         boost::throw_exception (DCPReadError ("could not read audio frame"));
273                 }
274                 
275                 if (ASDCP_FAILURE (reader_B.ReadFrame (i, buffer_B))) {
276                         boost::throw_exception (DCPReadError ("could not read audio frame"));
277                 }
278                 
279                 if (buffer_A.Size() != buffer_B.Size()) {
280                         note ("sizes of audio data for frame " + lexical_cast<string>(i) + " differ");
281                         return false;
282                 }
283                 
284                 if (memcmp (buffer_A.RoData(), buffer_B.RoData(), buffer_A.Size()) != 0) {
285                         for (uint32_t i = 0; i < buffer_A.Size(); ++i) {
286                                 int const d = abs (buffer_A.RoData()[i] - buffer_B.RoData()[i]);
287                                 if (d > opt.max_audio_sample_error) {
288                                         note ("PCM data difference of " + lexical_cast<string> (d));
289                                         return false;
290                                 }
291                         }
292                 }
293         }
294
295         return true;
296 }
297
298 shared_ptr<const SoundFrame>
299 SoundAsset::get_frame (int n) const
300 {
301         /* XXX: should add on entry point here? */
302         return shared_ptr<const SoundFrame> (new SoundFrame (path().string(), n));
303 }
304
305 shared_ptr<SoundAssetWriter>
306 SoundAsset::start_write ()
307 {
308         /* XXX: can't we use a shared_ptr here? */
309         return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this));
310 }
311
312 struct SoundAssetWriter::ASDCPState
313 {
314         ASDCP::PCM::MXFWriter mxf_writer;
315         ASDCP::PCM::FrameBuffer frame_buffer;
316         ASDCP::WriterInfo writer_info;
317         ASDCP::PCM::AudioDescriptor audio_desc;
318 };
319
320 SoundAssetWriter::SoundAssetWriter (SoundAsset* a)
321         : _state (new SoundAssetWriter::ASDCPState)
322         , _asset (a)
323         , _finalized (false)
324         , _frames_written (0)
325         , _frame_buffer_offset (0)
326 {
327         /* Derived from ASDCP::Wav::SimpleWaveHeader::FillADesc */
328         _state->audio_desc.EditRate = ASDCP::Rational (_asset->edit_rate(), 1);
329         _state->audio_desc.AudioSamplingRate = ASDCP::Rational (_asset->sampling_rate(), 1);
330         _state->audio_desc.Locked = 0;
331         _state->audio_desc.ChannelCount = _asset->channels ();
332         _state->audio_desc.QuantizationBits = 24;
333         _state->audio_desc.BlockAlign = 3 * _asset->channels();
334         _state->audio_desc.AvgBps = _asset->sampling_rate() * _state->audio_desc.BlockAlign;
335         _state->audio_desc.LinkedTrackID = 0;
336         _state->audio_desc.ChannelFormat = ASDCP::PCM::CF_NONE;
337         
338         _state->frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
339         _state->frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (_state->audio_desc));
340         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
341         
342         MXFAsset::fill_writer_info (&_state->writer_info, _asset->uuid ());
343         
344         if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (_asset->path().string().c_str(), _state->writer_info, _state->audio_desc))) {
345                 boost::throw_exception (FileError ("could not open audio MXF for writing", _asset->path().string()));
346         }
347 }
348
349 void
350 SoundAssetWriter::write (float const * const * data, int frames)
351 {
352         for (int i = 0; i < frames; ++i) {
353
354                 byte_t* out = _state->frame_buffer.Data() + _frame_buffer_offset;
355
356                 /* Write one sample per channel */
357                 for (int j = 0; j < _asset->channels(); ++j) {
358                         int32_t const s = data[j][i] * (1 << 23);
359                         *out++ = (s & 0xff);
360                         *out++ = (s & 0xff00) >> 8;
361                         *out++ = (s & 0xff0000) >> 16;
362                 }
363                 _frame_buffer_offset += 3 * _asset->channels();
364
365                 assert (_frame_buffer_offset <= int (_state->frame_buffer.Capacity()));
366
367                 /* Finish the MXF frame if required */
368                 if (_frame_buffer_offset == int (_state->frame_buffer.Capacity())) {
369                         write_current_frame ();
370                         _frame_buffer_offset = 0;
371                         memset (_state->frame_buffer.Data(), 0, _state->frame_buffer.Capacity());
372                 }
373         }
374 }
375
376 void
377 SoundAssetWriter::write_current_frame ()
378 {
379         if (ASDCP_FAILURE (_state->mxf_writer.WriteFrame (_state->frame_buffer, 0, 0))) {
380                 boost::throw_exception (MiscError ("could not write audio MXF frame"));
381         }
382
383         ++_frames_written;
384 }
385
386 void
387 SoundAssetWriter::finalize ()
388 {
389         if (_frame_buffer_offset > 0) {
390                 write_current_frame ();
391         }
392         
393         if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
394                 boost::throw_exception (MiscError ("could not finalise audio MXF"));
395         }
396
397         _finalized = true;
398         _asset->set_intrinsic_duration (_frames_written);
399         _asset->set_duration (_frames_written);
400 }
401
402 SoundAssetWriter::~SoundAssetWriter ()
403 {
404         assert (_finalized);
405 }