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
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
106 OpenWrite(ASDCP::PCM::AudioDescriptor &ADesc, const char* file_root, SplitType_t split = ST_NONE)
108 ASDCP_TEST_NULL_STR(file_root);
109 char filename[Kumu::MaxFilePath];
110 ui32_t file_count = 0;
111 ASDCP::Result_t result = ASDCP::RESULT_OK;
118 m_ChannelCount = m_ADesc.ChannelCount;
122 file_count = m_ADesc.ChannelCount;
127 if ( m_ADesc.ChannelCount % 2 != 0 )
129 Kumu::DefaultLogSink().Error("Unable to create 2-channel splits with odd number of input channels.\n");
130 return ASDCP::RESULT_PARAM;
133 file_count = m_ADesc.ChannelCount / 2;
137 assert(file_count && m_ChannelCount);
139 ui32_t element_size = ASDCP::PCM::CalcFrameBufferSize(m_ADesc) / file_count;
140 if (split == ST_NONE)
142 snprintf(filename, Kumu::MaxFilePath, "%s", file_root);
143 m_OutFile.push_back(new WavFileElement(element_size));
144 result = m_OutFile.back()->OpenWrite(filename);
145 if ( ASDCP_SUCCESS(result) )
147 ASDCP::PCM::AudioDescriptor tmpDesc = m_ADesc;
148 tmpDesc.ChannelCount = m_ChannelCount;
149 ASDCP::RF64::SimpleRF64Header Wav(tmpDesc);
150 result = Wav.WriteToFile(*(m_OutFile.back()));
155 for ( ui32_t i = 0; i < file_count && ASDCP_SUCCESS(result); i++ )
157 snprintf(filename, Kumu::MaxFilePath, "%s_%02u.wav", file_root, (i + 1));
158 m_OutFile.push_back(new WavFileElement(element_size));
159 result = m_OutFile.back()->OpenWrite(filename);
161 if ( ASDCP_SUCCESS(result) )
163 ASDCP::PCM::AudioDescriptor tmpDesc = m_ADesc;
164 tmpDesc.ChannelCount = m_ChannelCount;
165 ASDCP::RF64::SimpleRF64Header Wav(tmpDesc);
166 result = Wav.WriteToFile(*(m_OutFile.back()));
174 WriteFrame(ASDCP::PCM::FrameBuffer& FB)
176 if ( m_OutFile.empty() )
177 return ASDCP::RESULT_STATE;
179 if ( m_OutFile.size() == 1 ) // no de-interleave needed, just write out the frame
180 return m_OutFile.back()->Write(FB.RoData(), FB.Size(), 0);
182 std::list<WavFileElement*>::iterator fi;
183 ui32_t sample_size = m_ADesc.QuantizationBits / 8;
184 const byte_t* p = FB.RoData();
185 const byte_t* end_p = p + FB.Size();
189 for ( fi = m_OutFile.begin(); fi != m_OutFile.end(); fi++ )
191 for ( ui32_t c = 0; c < m_ChannelCount; c++ )
193 (*fi)->WriteSample(p, sample_size);
199 ASDCP::Result_t result = ASDCP::RESULT_OK;
201 for ( fi = m_OutFile.begin(); fi != m_OutFile.end() && ASDCP_SUCCESS(result); fi++ )
202 result = (*fi)->Flush();
209 #endif // _WAVFILEWRITER_H_
212 // end WavFileWriter.h