o removed waywars #endif
[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     ST_MAX
103   };
104
105   ASDCP::Result_t
106     OpenWrite(ASDCP::PCM::AudioDescriptor &ADesc, const char* file_root, SplitType_t split = ST_NONE)
107     {
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;
112       m_ADesc = ADesc;
113
114       switch ( split )
115         {
116         case ST_NONE:
117           file_count = 1;
118           m_ChannelCount = m_ADesc.ChannelCount;
119           break;
120
121         case ST_MONO:
122           file_count = m_ADesc.ChannelCount;
123           m_ChannelCount = 1;
124           break;
125
126         case ST_STEREO:
127           if ( m_ADesc.ChannelCount % 2 != 0 )
128             {
129               Kumu::DefaultLogSink().Error("Unable to create 2-channel splits with odd number of input channels.\n");
130               return ASDCP::RESULT_PARAM;
131             }
132
133           file_count = m_ADesc.ChannelCount / 2;
134           m_ChannelCount = 2;
135           break;
136         }
137       assert(file_count && m_ChannelCount);
138
139       ui32_t element_size = ASDCP::PCM::CalcFrameBufferSize(m_ADesc) / file_count;
140       if (split == ST_NONE)
141       {
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) )
146           {
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()));
151           }
152       }
153       else
154       {
155           for ( ui32_t i = 0; i < file_count && ASDCP_SUCCESS(result); i++ )
156           {
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);
160
161               if ( ASDCP_SUCCESS(result) )
162               {
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()));
167               }
168           }
169       }
170       return result;
171     }
172
173   ASDCP::Result_t
174     WriteFrame(ASDCP::PCM::FrameBuffer& FB)
175     {
176       if ( m_OutFile.empty() )
177         return ASDCP::RESULT_STATE;
178
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);
181  
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();
186
187       while ( p < end_p )
188         {
189           for ( fi = m_OutFile.begin(); fi != m_OutFile.end(); fi++ )
190             {
191               for ( ui32_t c = 0; c < m_ChannelCount; c++ )
192                 {
193                   (*fi)->WriteSample(p, sample_size);
194                   p += sample_size;
195                 }
196             }
197         }
198
199       ASDCP::Result_t result = ASDCP::RESULT_OK;
200
201       for ( fi = m_OutFile.begin(); fi != m_OutFile.end() && ASDCP_SUCCESS(result); fi++ )
202         result = (*fi)->Flush();
203
204       return result;
205     }
206 };
207
208
209 #endif // _WAVFILEWRITER_H_
210
211 //
212 // end WavFileWriter.h
213 //