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.
20 /** @file src/sound_asset.cc
21 * @brief An asset made up of WAV files
26 #include <boost/filesystem.hpp>
28 #include "sound_asset.h"
30 #include "exceptions.h"
33 using namespace boost;
34 using namespace libdcp;
36 SoundAsset::SoundAsset (
37 vector<string> const & files, string mxf_path, sigc::signal1<void, float>* progress, int fps, int length
39 : Asset (mxf_path, progress, fps, length)
40 , _channels (files.size ())
42 construct (sigc::bind (sigc::mem_fun (*this, &SoundAsset::path_from_channel), files));
45 SoundAsset::SoundAsset (
46 sigc::slot<string, Channel> get_path, string mxf_path, sigc::signal1<void, float>* progress, int fps, int length, int channels
48 : Asset (mxf_path, progress, fps, length)
49 , _channels (channels)
54 SoundAsset::SoundAsset (string mxf_path, int fps, int length)
55 : Asset (mxf_path, 0, fps, length)
62 SoundAsset::path_from_channel (Channel channel, vector<string> const & files)
64 unsigned int const c = int (channel);
65 assert (c < files.size ());
70 SoundAsset::construct (sigc::slot<string, Channel> get_path)
72 ASDCP::Rational asdcp_fps (_fps, 1);
74 ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
75 if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_fps)) {
76 throw FileError ("could not open WAV file for reading", get_path(LEFT));
79 ASDCP::PCM::AudioDescriptor audio_desc;
80 pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
81 audio_desc.ChannelCount = 0;
82 audio_desc.BlockAlign = 0;
83 audio_desc.EditRate = asdcp_fps;
84 audio_desc.AvgBps = audio_desc.AvgBps * _channels;
86 Channel channels[] = {
95 ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
96 ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
98 for (int i = 0; i < _channels; ++i) {
100 string const path = get_path (channels[i]);
102 if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.c_str(), asdcp_fps))) {
103 throw FileError ("could not open WAV file for reading", path);
106 pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
107 frame_buffer_channel[i].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[i]));
109 audio_desc.ChannelCount += audio_desc_channel[i].ChannelCount;
110 audio_desc.BlockAlign += audio_desc_channel[i].BlockAlign;
113 ASDCP::PCM::FrameBuffer frame_buffer;
114 frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
115 frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
117 ASDCP::WriterInfo writer_info;
118 fill_writer_info (&writer_info);
120 ASDCP::PCM::MXFWriter mxf_writer;
121 if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, audio_desc))) {
122 throw FileError ("could not open audio MXF for writing", _mxf_path);
125 for (int i = 0; i < _length; ++i) {
127 byte_t *data_s = frame_buffer.Data();
128 byte_t *data_e = data_s + frame_buffer.Capacity();
129 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
132 for (int j = 0; j < _channels; ++j) {
133 memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
134 if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
135 throw MiscError ("could not read audio frame");
138 if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
139 throw MiscError ("short audio frame");
143 while (data_s < data_e) {
144 for (int j = 0; j < _channels; ++j) {
145 byte_t* frame = frame_buffer_channel[j].Data() + offset;
146 memcpy (data_s, frame, sample_size);
147 data_s += sample_size;
149 offset += sample_size;
152 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
153 throw MiscError ("could not write audio MXF frame");
156 (*_progress) (0.5 * float (i) / _length);
159 if (ASDCP_FAILURE (mxf_writer.Finalize())) {
160 throw MiscError ("could not finalise audio MXF");
163 _digest = make_digest (_mxf_path, _progress);
167 SoundAsset::write_to_cpl (ostream& s) const
169 s << " <MainSound>\n"
170 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
171 #if BOOST_FILESYSTEM_VERSION == 3
172 << " <AnnotationText>" << filesystem::path(_mxf_path).filename().string() << "</AnnotationText>\n"
174 << " <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
176 << " <EditRate>" << _fps << " 1</EditRate>\n"
177 << " <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
178 << " <EntryPoint>0</EntryPoint>\n"
179 << " <Duration>" << _length << "</Duration>\n"
180 << " </MainSound>\n";