Fix bizarre quotation mark insertion by newer boost filesystem library.
[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 "AS_DCP.h"
28 #include "sound_asset.h"
29 #include "util.h"
30 #include "exceptions.h"
31
32 using namespace std;
33 using namespace boost;
34 using namespace libdcp;
35
36 SoundAsset::SoundAsset (
37         vector<string> const & files, string mxf_path, sigc::signal1<void, float>* progress, int fps, int length
38         )
39         : Asset (mxf_path, progress, fps, length)
40         , _channels (files.size ())
41 {
42         construct (sigc::bind (sigc::mem_fun (*this, &SoundAsset::path_from_channel), files));
43 }
44
45 SoundAsset::SoundAsset (
46         sigc::slot<string, Channel> get_path, string mxf_path, sigc::signal1<void, float>* progress, int fps, int length, int channels
47         )
48         : Asset (mxf_path, progress, fps, length)
49         , _channels (channels)
50 {
51         construct (get_path);
52 }
53
54 string
55 SoundAsset::path_from_channel (Channel channel, vector<string> const & files)
56 {
57         unsigned int const c = int (channel);
58         assert (c < files.size ());
59         return files[c];
60 }
61
62 void
63 SoundAsset::construct (sigc::slot<string, Channel> get_path)
64 {
65         ASDCP::Rational asdcp_fps (_fps, 1);
66         
67         ASDCP::PCM::WAVParser pcm_parser_channel[_channels];
68         if (pcm_parser_channel[0].OpenRead (get_path(LEFT).c_str(), asdcp_fps)) {
69                 throw FileError ("could not open WAV file for reading", get_path(LEFT));
70         }
71         
72         ASDCP::PCM::AudioDescriptor audio_desc;
73         pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
74         audio_desc.ChannelCount = 0;
75         audio_desc.BlockAlign = 0;
76         audio_desc.EditRate = asdcp_fps;
77         audio_desc.AvgBps = audio_desc.AvgBps * _channels;
78
79         Channel channels[] = {
80                 LEFT,
81                 RIGHT,
82                 CENTRE,
83                 LFE,
84                 LS,
85                 RS
86         };
87
88         ASDCP::PCM::FrameBuffer frame_buffer_channel[_channels];
89         ASDCP::PCM::AudioDescriptor audio_desc_channel[_channels];
90
91         for (int i = 0; i < _channels; ++i) {
92
93                 string const path = get_path (channels[i]);
94                 
95                 if (ASDCP_FAILURE (pcm_parser_channel[i].OpenRead (path.c_str(), asdcp_fps))) {
96                         throw FileError ("could not open WAV file for reading", path);
97                 }
98
99                 pcm_parser_channel[i].FillAudioDescriptor (audio_desc_channel[i]);
100                 frame_buffer_channel[i].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[i]));
101
102                 audio_desc.ChannelCount += audio_desc_channel[i].ChannelCount;
103                 audio_desc.BlockAlign += audio_desc_channel[i].BlockAlign;
104         }
105
106         ASDCP::PCM::FrameBuffer frame_buffer;
107         frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
108         frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
109
110         ASDCP::WriterInfo writer_info;
111         fill_writer_info (&writer_info);
112
113         ASDCP::PCM::MXFWriter mxf_writer;
114         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, audio_desc))) {
115                 throw FileError ("could not open audio MXF for writing", _mxf_path);
116         }
117
118         for (int i = 0; i < _length; ++i) {
119
120                 byte_t *data_s = frame_buffer.Data();
121                 byte_t *data_e = data_s + frame_buffer.Capacity();
122                 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
123                 int offset = 0;
124
125                 for (int j = 0; j < _channels; ++j) {
126                         memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
127                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
128                                 throw MiscError ("could not read audio frame");
129                         }
130                         
131                         if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
132                                 throw MiscError ("short audio frame");
133                         }
134                 }
135
136                 while (data_s < data_e) {
137                         for (int j = 0; j < _channels; ++j) {
138                                 byte_t* frame = frame_buffer_channel[j].Data() + offset;
139                                 memcpy (data_s, frame, sample_size);
140                                 data_s += sample_size;
141                         }
142                         offset += sample_size;
143                 }
144
145                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
146                         throw MiscError ("could not write audio MXF frame");
147                 }
148
149                 (*_progress) (0.5 * float (i) / _length);
150         }
151
152         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
153                 throw MiscError ("could not finalise audio MXF");
154         }
155
156         _digest = make_digest (_mxf_path, _progress);
157 }
158
159 void
160 SoundAsset::write_to_cpl (ostream& s) const
161 {
162         s << "        <MainSound>\n"
163           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
164           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename().string() << "</AnnotationText>\n"
165           << "          <EditRate>" << _fps << " 1</EditRate>\n"
166           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
167           << "          <EntryPoint>0</EntryPoint>\n"
168           << "          <Duration>" << _length << "</Duration>\n"
169           << "        </MainSound>\n";
170 }
171