8c85f9dfcac5632984c5a6a9bcff2128a56b3a53
[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
31 using namespace std;
32 using namespace boost;
33 using namespace libdcp;
34
35 SoundAsset::SoundAsset (list<string> const & files, string mxf_path, sigc::signal1<void, float>* progress, int fps, int length)
36         : Asset (mxf_path, progress, fps, length)
37 {
38         ASDCP::Rational asdcp_fps (_fps, 1);
39         
40         ASDCP::PCM::WAVParser pcm_parser_channel[files.size()];
41         if (pcm_parser_channel[0].OpenRead (files.front().c_str(), asdcp_fps)) {
42                 throw runtime_error ("could not open WAV file for reading");
43         }
44         
45         ASDCP::PCM::AudioDescriptor audio_desc;
46         pcm_parser_channel[0].FillAudioDescriptor (audio_desc);
47         audio_desc.ChannelCount = 0;
48         audio_desc.BlockAlign = 0;
49         audio_desc.EditRate = asdcp_fps;
50         audio_desc.AvgBps = audio_desc.AvgBps * files.size ();
51
52         ASDCP::PCM::FrameBuffer frame_buffer_channel[files.size()];
53         ASDCP::PCM::AudioDescriptor audio_desc_channel[files.size()];
54         
55         int j = 0;
56         for (list<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
57                 
58                 if (ASDCP_FAILURE (pcm_parser_channel[j].OpenRead (i->c_str(), asdcp_fps))) {
59                         throw runtime_error ("could not open WAV file for reading");
60                 }
61
62                 pcm_parser_channel[j].FillAudioDescriptor (audio_desc_channel[j]);
63                 frame_buffer_channel[j].Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc_channel[j]));
64
65                 audio_desc.ChannelCount += audio_desc_channel[j].ChannelCount;
66                 audio_desc.BlockAlign += audio_desc_channel[j].BlockAlign;
67                 ++j;
68         }
69
70         ASDCP::PCM::FrameBuffer frame_buffer;
71         frame_buffer.Capacity (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
72         frame_buffer.Size (ASDCP::PCM::CalcFrameBufferSize (audio_desc));
73
74         ASDCP::WriterInfo writer_info;
75         fill_writer_info (&writer_info);
76
77         ASDCP::PCM::MXFWriter mxf_writer;
78         if (ASDCP_FAILURE (mxf_writer.OpenWrite (_mxf_path.c_str(), writer_info, audio_desc))) {
79                 throw runtime_error ("could not open audio MXF for writing");
80         }
81
82         for (int i = 0; i < _length; ++i) {
83
84                 byte_t *data_s = frame_buffer.Data();
85                 byte_t *data_e = data_s + frame_buffer.Capacity();
86                 byte_t sample_size = ASDCP::PCM::CalcSampleSize (audio_desc_channel[0]);
87                 int offset = 0;
88
89                 for (list<string>::size_type j = 0; j < files.size(); ++j) {
90                         memset (frame_buffer_channel[j].Data(), 0, frame_buffer_channel[j].Capacity());
91                         if (ASDCP_FAILURE (pcm_parser_channel[j].ReadFrame (frame_buffer_channel[j]))) {
92                                 throw runtime_error ("could not read audio frame");
93                         }
94                         
95                         if (frame_buffer_channel[j].Size() != frame_buffer_channel[j].Capacity()) {
96                                 throw runtime_error ("short audio frame");
97                         }
98                 }
99
100                 while (data_s < data_e) {
101                         for (list<string>::size_type j = 0; j < files.size(); ++j) {
102                                 byte_t* frame = frame_buffer_channel[j].Data() + offset;
103                                 memcpy (data_s, frame, sample_size);
104                                 data_s += sample_size;
105                         }
106                         offset += sample_size;
107                 }
108
109                 if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) {
110                         throw runtime_error ("could not write audio MXF frame");
111                 }
112
113                 (*_progress) (0.5 * float (i) / _length);
114         }
115
116         if (ASDCP_FAILURE (mxf_writer.Finalize())) {
117                 throw runtime_error ("could not finalise audio MXF");
118         }
119
120         _digest = make_digest (_mxf_path, _progress);
121 }
122
123 void
124 SoundAsset::write_to_cpl (ostream& s) const
125 {
126         s << "        <MainSound>\n"
127           << "          <Id>urn:uuid:" << _uuid << "</Id>\n"
128           << "          <AnnotationText>" << filesystem::path(_mxf_path).filename() << "</AnnotationText>\n"
129           << "          <EditRate>" << _fps << " 1</EditRate>\n"
130           << "          <IntrinsicDuration>" << _length << "</IntrinsicDuration>\n"
131           << "          <EntryPoint>0</EntryPoint>\n"
132           << "          <Duration>" << _length << "</Duration>\n"
133           << "        </MainSound>\n";
134 }
135