2 Copyright (c) 2005-2009, 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 WavFileWriter.h
28 \version $Id: WavFileWriter.h,v 1.6 2009/04/09 19:16:49 msheby Exp $
29 \brief demux and write PCM data to WAV file(s)
32 #include <KM_fileio.h>
37 #ifndef _WAVFILEWRITER_H_
38 #define _WAVFILEWRITER_H_
42 class WavFileElement : public Kumu::FileWriter
44 ASDCP::PCM::FrameBuffer m_Buf;
48 KM_NO_COPY_CONSTRUCT(WavFileElement);
51 WavFileElement(ui32_t s) : m_Buf(s), m_p(0)
58 void WriteSample(const byte_t* sample, ui32_t sample_size)
60 memcpy(m_p, sample, sample_size);
64 ASDCP::Result_t Flush()
66 ui32_t write_count = 0;
68 if ( m_p == m_Buf.Data() )
69 return ASDCP::RESULT_EMPTY_FB;
71 ui32_t write_size = m_p - m_Buf.Data();
73 return Write(m_Buf.RoData(), write_size, &write_count);
81 ASDCP::PCM::AudioDescriptor m_ADesc;
82 std::list<WavFileElement*> m_OutFile;
83 ui32_t m_ChannelCount;
84 ASDCP_NO_COPY_CONSTRUCT(WavFileWriter);
87 WavFileWriter() : m_ChannelCount(0) {}
90 while ( ! m_OutFile.empty() )
92 delete m_OutFile.back();
99 ST_NONE, // write all channels to a single WAV file
100 ST_MONO, // write each channel a separate WAV file
101 ST_STEREO // write channel pairs to separate WAV files
105 OpenWrite(ASDCP::PCM::AudioDescriptor &ADesc, const char* file_root, SplitType_t split = ST_NONE)
107 ASDCP_TEST_NULL_STR(file_root);
108 char filename[Kumu::MaxFilePath];
109 ui32_t file_count = 0;
110 ASDCP::Result_t result = ASDCP::RESULT_OK;
117 m_ChannelCount = m_ADesc.ChannelCount;
121 file_count = m_ADesc.ChannelCount;
126 if ( m_ADesc.ChannelCount % 2 != 0 )
128 Kumu::DefaultLogSink().Error("Unable to create 2-channel splits with odd number of input channels.\n");
129 return ASDCP::RESULT_PARAM;
132 file_count = m_ADesc.ChannelCount / 2;
136 assert(file_count && m_ChannelCount);
138 ui32_t element_size = ASDCP::PCM::CalcFrameBufferSize(m_ADesc) / file_count;
140 for ( ui32_t i = 0; i < file_count && ASDCP_SUCCESS(result); i++ )
142 snprintf(filename, Kumu::MaxFilePath, "%s_%u.wav", file_root, (i + 1));
143 m_OutFile.push_back(new WavFileElement(element_size));
144 result = m_OutFile.back()->OpenWrite(filename);
146 if ( ASDCP_SUCCESS(result) )
148 ASDCP::PCM::AudioDescriptor tmpDesc = m_ADesc;
149 tmpDesc.ChannelCount = m_ChannelCount;
150 ASDCP::Wav::SimpleWaveHeader Wav(tmpDesc);
151 result = Wav.WriteToFile(*(m_OutFile.back()));
159 WriteFrame(ASDCP::PCM::FrameBuffer& FB)
161 if ( m_OutFile.empty() )
162 return ASDCP::RESULT_STATE;
164 if ( m_OutFile.size() == 1 ) // no de-interleave needed, just write out the frame
165 return m_OutFile.back()->Write(FB.RoData(), FB.Size(), 0);
167 std::list<WavFileElement*>::iterator fi;
168 ui32_t sample_size = m_ADesc.QuantizationBits / 8;
169 const byte_t* p = FB.RoData();
170 const byte_t* end_p = p + FB.Size();
174 for ( fi = m_OutFile.begin(); fi != m_OutFile.end(); fi++ )
176 for ( ui32_t c = 0; c < m_ChannelCount; c++ )
178 (*fi)->WriteSample(p, sample_size);
184 ASDCP::Result_t result = ASDCP::RESULT_OK;
186 for ( fi = m_OutFile.begin(); fi != m_OutFile.end() && ASDCP_SUCCESS(result); fi++ )
187 result = (*fi)->Flush();
194 #endif // _WAVFILEWRITER_H_
197 // end WavFileWriter.h