2 Copyright (c) 2004-2012, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*! \file PCMParserList.cpp
29 \brief Read WAV file(s), multiplex multiple PCM frame buffers into one
32 #include <PCMParserList.h>
33 #include <KM_fileio.h>
37 using namespace ASDCP;
41 ASDCP::ParserInstance::ParserInstance() : m_p(0), m_SampleSize(0)
45 ASDCP::ParserInstance::~ParserInstance()
49 // PCM::CalcSampleSize(ADesc);
51 ASDCP::ParserInstance::OpenRead(const char* filename, const Rational& PictureRate)
53 ASDCP_TEST_NULL_STR(filename);
55 Result_t result = Parser.OpenRead(filename, PictureRate);
57 if ( ASDCP_SUCCESS(result) )
58 result = Parser.FillAudioDescriptor(ADesc);
60 if ( ASDCP_SUCCESS(result) )
62 ADesc.EditRate = PictureRate;
63 m_SampleSize = PCM::CalcSampleSize(ADesc);
64 result = FB.Capacity(PCM::CalcFrameBufferSize(ADesc));
71 // deposit the next available sample into the given framebuffer
73 ASDCP::ParserInstance::PutSample(byte_t* p)
77 memcpy(p, m_p, m_SampleSize);
85 ASDCP::ParserInstance::ReadFrame()
87 Result_t result = Parser.ReadFrame(FB);
88 m_p = ASDCP_SUCCESS(result) ? FB.RoData() : 0;
93 //------------------------------------------------------------------------------------------
98 ASDCP::PCMParserList::PCMParserList() : m_ChannelCount(0)
102 ASDCP::PCMParserList::~PCMParserList()
113 ASDCP::PCMParserList::OpenRead(ui32_t argc, const char** argv, const Rational& PictureRate)
115 ASDCP_TEST_NULL_STR(argv);
116 PathList_t TmpFileList;
118 for ( ui32_t i = 0; i < argc; ++i )
119 TmpFileList.push_back(argv[i]);
121 return OpenRead(TmpFileList, PictureRate);
126 ASDCP::PCMParserList::OpenRead(const Kumu::PathList_t& argv, const Rational& PictureRate)
128 Result_t result = RESULT_OK;
129 PathList_t::iterator fi;
130 Kumu::PathList_t file_list;
132 if ( argv.size() == 1 && PathIsDirectory(argv.front()) )
135 char name_buf[MaxFilePath];
136 result = Dir.Open(argv.front().c_str());
138 if ( KM_SUCCESS(result) )
139 result = Dir.GetNext(name_buf);
141 while ( KM_SUCCESS(result) )
143 if ( name_buf[0] != '.' ) // no hidden files
145 std::string tmp_path = argv.front() + "/" + name_buf;
146 file_list.push_back(tmp_path);
149 result = Dir.GetNext(name_buf);
152 if ( result == RESULT_ENDOFFILE )
163 for ( fi = file_list.begin(); KM_SUCCESS(result) && fi != file_list.end(); ++fi )
165 mem_ptr<ParserInstance> I = new ParserInstance;
166 result = I->OpenRead(fi->c_str(), PictureRate);
168 if ( ASDCP_SUCCESS(result) )
170 if ( fi == file_list.begin() )
176 if ( I->ADesc.AudioSamplingRate != m_ADesc.AudioSamplingRate )
178 DefaultLogSink().Error("AudioSamplingRate mismatch in PCM parser list.");
179 return RESULT_FORMAT;
182 if ( I->ADesc.QuantizationBits != m_ADesc.QuantizationBits )
184 DefaultLogSink().Error("QuantizationBits mismatch in PCM parser list.");
185 return RESULT_FORMAT;
188 if ( I->ADesc.ContainerDuration < m_ADesc.ContainerDuration )
189 m_ADesc.ContainerDuration = I->ADesc.ContainerDuration;
191 m_ADesc.BlockAlign += I->ADesc.BlockAlign;
194 m_ChannelCount += I->ADesc.ChannelCount;
197 if ( ASDCP_SUCCESS(result) )
198 result = I->FB.Capacity(PCM::CalcFrameBufferSize(m_ADesc));
200 if ( ASDCP_SUCCESS(result) )
207 if ( ASDCP_SUCCESS(result) )
209 m_ADesc.ChannelCount = m_ChannelCount;
210 m_ADesc.AvgBps = (ui32_t)(ceil(m_ADesc.AudioSamplingRate.Quotient()) * m_ADesc.BlockAlign);
222 ASDCP::PCMParserList::FillAudioDescriptor(PCM::AudioDescriptor& ADesc) const
230 ASDCP::PCMParserList::Reset()
232 Result_t result = RESULT_OK;
233 PCMParserList::iterator self_i;
235 for ( self_i = begin(); self_i != end() && ASDCP_SUCCESS(result) ; self_i++ )
236 result = (*self_i)->Parser.Reset();
244 ASDCP::PCMParserList::ReadFrame(PCM::FrameBuffer& OutFB)
246 Result_t result = RESULT_OK;
249 return front()->Parser.ReadFrame(OutFB);
251 PCMParserList::iterator self_i;
252 assert(PCM::CalcFrameBufferSize(m_ADesc) <= OutFB.Capacity());
254 for ( self_i = begin(); self_i != end() && ASDCP_SUCCESS(result) ; self_i++ )
255 result = (*self_i)->ReadFrame();
257 if ( ASDCP_SUCCESS(result) )
259 OutFB.Size(PCM::CalcFrameBufferSize(m_ADesc));
261 // ui32_t sample_size = (PCM::CalcSampleSize(m_ADesc));
262 byte_t* Out_p = OutFB.Data();
263 byte_t* End_p = Out_p + OutFB.Size();
265 while ( Out_p < End_p && ASDCP_SUCCESS(result) )
269 while ( self_i != end() && ASDCP_SUCCESS(result) )
271 result = (*self_i)->PutSample(Out_p);
272 Out_p += (*self_i)->SampleSize();
277 assert(Out_p == End_p);
284 // end PCMParserList.cpp