9611adc05adb6c53a6341df6d242d110d606c02d
[asdcplib.git] / src / WavFileWriter.h
1 /*
2 Copyright (c) 2005-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27 /*! \file    WavFileWriter.h
28     \version $Id$
29     \brief   demux and write PCM data to WAV file(s)
30 */
31
32 #include <KM_fileio.h>
33 #include <KM_log.h>
34 #include <Wav.h>
35 #include <list>
36
37 #ifndef _WAVFILEWRITER_H_
38 #define _WAVFILEWRITER_H_
39
40
41 //
42 class WavFileElement : public Kumu::FileWriter
43 {
44   ASDCP::PCM::FrameBuffer m_Buf;
45   byte_t* m_p;
46
47   WavFileElement();
48   KM_NO_COPY_CONSTRUCT(WavFileElement);
49
50 public:
51   WavFileElement(ui32_t s) : m_Buf(s), m_p(0)
52   {
53     m_p = m_Buf.Data();
54   }
55
56   ~WavFileElement() {}
57
58   void WriteSample(const byte_t* sample, ui32_t sample_size)
59   {
60     memcpy(m_p, sample, sample_size);
61     m_p += sample_size;
62   }
63
64   ASDCP::Result_t Flush()
65   {
66     ui32_t write_count = 0;
67
68     if ( m_p == m_Buf.Data() )
69       return ASDCP::RESULT_EMPTY_FB;
70
71     ui32_t write_size = m_p - m_Buf.Data();
72     m_p = m_Buf.Data();
73     return Write(m_Buf.RoData(), write_size, &write_count);
74   }
75 };
76
77
78 //
79 class WavFileWriter
80 {
81   ASDCP::PCM::AudioDescriptor m_ADesc;
82   std::list<WavFileElement*>  m_OutFile;
83   ui32_t                      m_ChannelCount;
84   ASDCP_NO_COPY_CONSTRUCT(WavFileWriter);
85
86  public:
87   WavFileWriter() : m_ChannelCount(0) {}
88   ~WavFileWriter()
89     {
90       while ( ! m_OutFile.empty() )
91         {
92           delete m_OutFile.back();
93           m_OutFile.pop_back();
94         }
95     }
96
97   //
98   enum SplitType_t {
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
102   };
103
104   ASDCP::Result_t
105     OpenWrite(ASDCP::PCM::AudioDescriptor &ADesc, const char* file_root, SplitType_t split = ST_NONE)
106     {
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;
111       m_ADesc = ADesc;
112
113       switch ( split )
114         {
115         case ST_NONE:
116           file_count = 1;
117           m_ChannelCount = m_ADesc.ChannelCount;
118           break;
119
120         case ST_MONO:
121           file_count = m_ADesc.ChannelCount;
122           m_ChannelCount = 1;
123           break;
124
125         case ST_STEREO:
126           if ( m_ADesc.ChannelCount % 2 != 0 )
127             {
128               Kumu::DefaultLogSink().Error("Unable to create 2-channel splits with odd number of input channels.\n");
129               return ASDCP::RESULT_PARAM;
130             }
131
132           file_count = m_ADesc.ChannelCount / 2;
133           m_ChannelCount = 2;
134           break;
135         }
136       assert(file_count && m_ChannelCount);
137
138       ui32_t element_size = ASDCP::PCM::CalcFrameBufferSize(m_ADesc) / file_count;
139       if (split == ST_NONE)
140       {
141           snprintf(filename, Kumu::MaxFilePath, "%s", file_root);
142           m_OutFile.push_back(new WavFileElement(element_size));
143           result = m_OutFile.back()->OpenWrite(filename);
144           if ( ASDCP_SUCCESS(result) )
145           {
146               ASDCP::PCM::AudioDescriptor tmpDesc = m_ADesc;
147               tmpDesc.ChannelCount = m_ChannelCount;
148               ASDCP::RF64::SimpleRF64Header Wav(tmpDesc);
149               result = Wav.WriteToFile(*(m_OutFile.back()));
150           }
151       }
152       else
153       {
154           for ( ui32_t i = 0; i < file_count && ASDCP_SUCCESS(result); i++ )
155           {
156               snprintf(filename, Kumu::MaxFilePath, "%s_%u.wav", file_root, (i + 1));
157               m_OutFile.push_back(new WavFileElement(element_size));
158               result = m_OutFile.back()->OpenWrite(filename);
159
160               if ( ASDCP_SUCCESS(result) )
161               {
162                   ASDCP::PCM::AudioDescriptor tmpDesc = m_ADesc;
163                   tmpDesc.ChannelCount = m_ChannelCount;
164                   ASDCP::RF64::SimpleRF64Header Wav(tmpDesc);
165                   result = Wav.WriteToFile(*(m_OutFile.back()));
166               }
167           }
168       }
169       return result;
170     }
171
172   ASDCP::Result_t
173     WriteFrame(ASDCP::PCM::FrameBuffer& FB)
174     {
175       if ( m_OutFile.empty() )
176         return ASDCP::RESULT_STATE;
177
178       if ( m_OutFile.size() == 1 ) // no de-interleave needed, just write out the frame
179         return m_OutFile.back()->Write(FB.RoData(), FB.Size(), 0);
180  
181       std::list<WavFileElement*>::iterator fi;
182       ui32_t sample_size = m_ADesc.QuantizationBits / 8;
183       const byte_t* p = FB.RoData();
184       const byte_t* end_p = p + FB.Size();
185
186       while ( p < end_p )
187         {
188           for ( fi = m_OutFile.begin(); fi != m_OutFile.end(); fi++ )
189             {
190               for ( ui32_t c = 0; c < m_ChannelCount; c++ )
191                 {
192                   (*fi)->WriteSample(p, sample_size);
193                   p += sample_size;
194                 }
195             }
196         }
197
198       ASDCP::Result_t result = ASDCP::RESULT_OK;
199
200       for ( fi = m_OutFile.begin(); fi != m_OutFile.end() && ASDCP_SUCCESS(result); fi++ )
201         result = (*fi)->Flush();
202
203       return result;
204     }
205 };
206
207
208 #endif // _WAVFILEWRITER_H_
209
210 //
211 // end WavFileWriter.h
212 //