2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
22 #include <boost/filesystem.hpp>
24 #include "sound_asset.h"
28 using namespace boost;
29 using namespace libdcp;
31 SoundAsset::SoundAsset (list<string> const & files, string p, int fps, int len)
34 ASDCP::Rational asdcp_fps (_fps, 1);
36 ASDCP::PCM::WAVParser pcm_parser_channel[files.size()];
37 if (pcm_parser_channel[0].OpenRead (files.front().c_str(), asdcp_fps)) {
38 throw runtime_error ("could not open WAV file for reading");
41 ASDCP::PCM::AudioDescriptor audio_desc;
42 pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
43 audio_desc.ChannelCount = 0;
44 audio_desc.BlockAlign = 0;
45 audio_desc.EditRate = asdcp_fps;
46 audio_desc.AvgBps = audio_desc.AvgBps * files.size ();
48 ASDCP::PCM::FrameBuffer frame_buffer_channel[files.size()];
49 ASDCP::PCM::AudioDescriptor audio_desc_channel[files.size()];
52 for (list<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
54 if (ASDCP_FAILURE (pcm_parser_channel[j].OpenRead (i->c_str(), asdcp_fps))) {
55 throw runtime_error ("could not open WAV file for reading");
58 pcm_parser_channel[j].FillAudioDescriptor (audio_desc_channel[j]);
59 frame_buffer_channel[j].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[j]));
61 audio_desc.ChannelCount += audio_desc_channel[j].ChannelCount;
62 audio_desc.BlockAlign += audio_desc_channel[j].BlockAlign;
66 ASDCP::PCM::FrameBuffer frame_buffer;
67 frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
68 frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
70 ASDCP::WriterInfo writer_info;
71 fill_writer_info (&writer_info);
73 ASDCP::PCM::MXFWriter mxf_writer;
74 if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, audio_desc))) {
75 throw runtime_error ("could not open audio MXF for writing");
78 for (int i = 0; i < _length; ++i) {
80 byte_t *data_s = frame_buffer.Data();
81 byte_t *data_e = data_s + frame_buffer.Capacity();
82 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
85 for (list<string>::size_type j = 0; j < files.size(); ++j) {
86 memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
87 if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
88 throw runtime_error ("could not read audio frame");
91 if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
92 throw runtime_error ("short audio frame");
96 while (data_s < data_e) {
97 for (list<string>::size_type j = 0; j < files.size(); ++j) {
98 byte_t* frame = frame_buffer_channel[j].Data() + offset;
99 memcpy (data_s, frame, sample_size);
100 data_s += sample_size;
102 offset += sample_size;
105 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
106 throw runtime_error ("could not write audio MXF frame");
109 Progress (float (i) / _length);
112 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
113 throw runtime_error ("could not finalise audio MXF");
116 _digest = make_digest (_mxf_path);
120 SoundAsset::write_to_cpl (ostream& s) const
122 s << " <MainSound>\n"
123 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
124 << " <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
125 << " <EditRate>" << _fps << " 1</EditRate>\n"
126 << " <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
127 << " <EntryPoint>0</EntryPoint>\n"
128 << " <Duration>" << _length << "</Duration>\n"
129 << " </MainSound>\n";