2 Copyright (C) 2012-2016 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_mxf.cc
21 * @brief SoundAsset class.
24 #include "sound_asset.h"
26 #include "exceptions.h"
27 #include "sound_frame.h"
28 #include "sound_asset_writer.h"
29 #include "compose.hpp"
30 #include "KM_fileio.h"
32 #include "dcp_assert.h"
33 #include <libxml++/nodes/element.h>
34 #include <boost/filesystem.hpp>
39 using std::stringstream;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
47 SoundAsset::SoundAsset (boost::filesystem::path file)
50 ASDCP::PCM::MXFReader reader;
51 Kumu::Result_t r = reader.OpenRead (file.string().c_str());
52 if (ASDCP_FAILURE (r)) {
53 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file.string(), r));
56 ASDCP::PCM::AudioDescriptor desc;
57 if (ASDCP_FAILURE (reader.FillAudioDescriptor (desc))) {
58 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
61 _sampling_rate = desc.AudioSamplingRate.Numerator / desc.AudioSamplingRate.Denominator;
62 _channels = desc.ChannelCount;
63 _edit_rate = Fraction (desc.EditRate.Numerator, desc.EditRate.Denominator);
65 _intrinsic_duration = desc.ContainerDuration;
67 ASDCP::WriterInfo info;
68 if (ASDCP_FAILURE (reader.FillWriterInfo (info))) {
69 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
72 _id = read_writer_info (info);
75 SoundAsset::SoundAsset (Fraction edit_rate, int sampling_rate, int channels)
76 : _edit_rate (edit_rate)
77 , _intrinsic_duration (0)
78 , _channels (channels)
79 , _sampling_rate (sampling_rate)
85 SoundAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
87 ASDCP::PCM::MXFReader reader_A;
88 Kumu::Result_t r = reader_A.OpenRead (file().string().c_str());
89 if (ASDCP_FAILURE (r)) {
90 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
93 ASDCP::PCM::MXFReader reader_B;
94 r = reader_B.OpenRead (other->file().string().c_str());
95 if (ASDCP_FAILURE (r)) {
96 boost::throw_exception (MXFFileError ("could not open MXF file for reading", file().string(), r));
99 ASDCP::PCM::AudioDescriptor desc_A;
100 if (ASDCP_FAILURE (reader_A.FillAudioDescriptor (desc_A))) {
101 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
103 ASDCP::PCM::AudioDescriptor desc_B;
104 if (ASDCP_FAILURE (reader_B.FillAudioDescriptor (desc_B))) {
105 boost::throw_exception (DCPReadError ("could not read audio MXF information"));
109 desc_A.EditRate != desc_B.EditRate ||
110 desc_A.AudioSamplingRate != desc_B.AudioSamplingRate ||
111 desc_A.Locked != desc_B.Locked ||
112 desc_A.ChannelCount != desc_B.ChannelCount ||
113 desc_A.QuantizationBits != desc_B.QuantizationBits ||
114 desc_A.BlockAlign != desc_B.BlockAlign ||
115 desc_A.AvgBps != desc_B.AvgBps ||
116 desc_A.LinkedTrackID != desc_B.LinkedTrackID ||
117 desc_A.ContainerDuration != desc_B.ContainerDuration
118 // desc_A.ChannelFormat != desc_B.ChannelFormat ||
121 note (DCP_ERROR, "audio MXF picture descriptors differ");
125 shared_ptr<const SoundAsset> other_sound = dynamic_pointer_cast<const SoundAsset> (other);
127 for (int i = 0; i < _intrinsic_duration; ++i) {
129 shared_ptr<const SoundFrame> frame_A = get_frame (i);
130 shared_ptr<const SoundFrame> frame_B = other_sound->get_frame (i);
132 if (frame_A->size() != frame_B->size()) {
133 note (DCP_ERROR, String::compose ("sizes of audio data for frame %1 differ", i));
137 if (memcmp (frame_A->data(), frame_B->data(), frame_A->size()) != 0) {
138 for (int i = 0; i < frame_A->size(); ++i) {
139 int const d = abs (frame_A->data()[i] - frame_B->data()[i]);
140 if (d > opt.max_audio_sample_error) {
141 note (DCP_ERROR, String::compose ("PCM data difference of %1", d));
151 shared_ptr<const SoundFrame>
152 SoundAsset::get_frame (int n) const
154 /* XXX: should add on entry point here? */
155 return shared_ptr<const SoundFrame> (new SoundFrame (file(), n, _decryption_context));
158 shared_ptr<SoundAssetWriter>
159 SoundAsset::start_write (boost::filesystem::path file, Standard standard)
161 /* XXX: can't we use a shared_ptr here? */
162 return shared_ptr<SoundAssetWriter> (new SoundAssetWriter (this, file, standard));
166 SoundAsset::pkl_type (Standard standard) const
170 return "application/x-smpte-mxf;asdcpKind=Sound";
172 return "application/mxf";
179 SoundAsset::valid_mxf (boost::filesystem::path file)
181 ASDCP::PCM::MXFReader reader;
182 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
183 return !ASDCP_FAILURE (r);