Some comments.
[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 #include <iostream>
21 #include <stdexcept>
22 #include <boost/filesystem.hpp>
23 #include "AS_DCP.h"
24 #include "sound_asset.h"
25 #include "util.h"
26
27 using namespace std;
28 using namespace boost;
29 using namespace libdcp;
30
31 /** Construct a SoundAsset, generating the MXF from the WAV files.
32  *  This may take some time; progress is indicated by emission of the Progress signal.
33  *  @param files Pathnames of sound files, in the order Left, Right, Centre, Lfe (sub), Left surround, Right surround.
34  *  @param p Pathname of MXF file to create.
35  *  @param fps Frames per second.
36  *  @param len Length in frames.
37  */
38
39 SoundAsset::SoundAsset (list<string> const & files, string p, int fps, int len)
40         : Asset (p, fps, len)
41 {
42         ASDCP::Rational asdcp_fps (_fps, 1);
43         
44         ASDCP::PCM::WAVParser pcm_parser_channel[files.size()];
45         if (pcm_parser_channel[0].OpenRead (files.front().c_str(), asdcp_fps)) {
46                 throw runtime_error ("could not open WAV file for reading");
47         }
48         
49         ASDCP::PCM::AudioDescriptor audio_desc;
50         pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
51         audio_desc.ChannelCount = 0;
52         audio_desc.BlockAlign = 0;
53         audio_desc.EditRate = asdcp_fps;
54         audio_desc.AvgBps = audio_desc.AvgBps * files.size ();
55
56         ASDCP::PCM::FrameBuffer frame_buffer_channel[files.size()];
57         ASDCP::PCM::AudioDescriptor audio_desc_channel[files.size()];
58         
59         int j = 0;
60         for (list<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
61                 
62                 if (ASDCP_FAILURE (pcm_parser_channel[j].OpenRead (i->c_str(), asdcp_fps))) {
63                         throw runtime_error ("could not open WAV file for reading");
64                 }
65
66                 pcm_parser_channel[j].FillAudioDescriptor (audio_desc_channel[j]);
67                 frame_buffer_channel[j].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[j]));
68
69                 audio_desc.ChannelCount += audio_desc_channel[j].ChannelCount;
70                 audio_desc.BlockAlign += audio_desc_channel[j].BlockAlign;
71                 ++j;
72         }
73
74         ASDCP::PCM::FrameBuffer frame_buffer;
75         frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
76         frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
77
78         ASDCP::WriterInfo writer_info;
79         fill_writer_info (&writer_info);
80
81         ASDCP::PCM::MXFWriter mxf_writer;
82         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, audio_desc))) {
83                 throw runtime_error ("could not open audio MXF for writing");
84         }
85
86         for (int i = 0; i < _length; ++i) {
87
88                 byte_t *data_s = frame_buffer.Data();
89                 byte_t *data_e = data_s + frame_buffer.Capacity();
90                 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
91                 int offset = 0;
92
93                 for (list<string>::size_type j = 0; j < files.size(); ++j) {
94                         memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
95                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
96                                 throw runtime_error ("could not read audio frame");
97                         }
98                         
99                         if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
100                                 throw runtime_error ("short audio frame");
101                         }
102                 }
103
104                 while (data_s < data_e) {
105                         for (list<string>::size_type j = 0; j < files.size(); ++j) {
106                                 byte_t* frame = frame_buffer_channel[j].Data() + offset;
107                                 memcpy (data_s, frame, sample_size);
108                                 data_s += sample_size;
109                         }
110                         offset += sample_size;
111                 }
112
113                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
114                         throw runtime_error ("could not write audio MXF frame");
115                 }
116
117                 Progress (float (i) / _length);
118         }
119
120         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
121                 throw runtime_error ("could not finalise audio MXF");
122         }
123
124         _digest = make_digest (_mxf_path);
125 }
126
127 /** Write details of this asset to a CPL stream.
128  *  @param s Stream.
129  */
130
131 void
132 SoundAsset::write_to_cpl (ostream& s) const
133 {
134         s << "        <MainSound>\n"
135           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
136           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
137           << "          <EditRate>" << _fps << " 1</EditRate>\n"
138           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
139           << "          <EntryPoint>0</EntryPoint>\n"
140           << "          <Duration>" << _length << "</Duration>\n"
141           << "        </MainSound>\n";
142 }
143