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