bugfix in indexing
[asdcplib.git] / src / WavFileWriter.h
1 /*
2 Copyright (c) 2005, 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 <FileIO.h>
33 #include <Wav.h>
34 #include <list>
35
36 #ifndef _WAVFILEWRITER_H_
37 #define _WAVFILEWRITER_H_
38
39 //
40 class WavFileWriter
41 {
42   ASDCP::PCM::AudioDescriptor m_ADesc;
43   std::list<ASDCP::FileWriter*> m_OutFile;
44   ASDCP_NO_COPY_CONSTRUCT(WavFileWriter);
45
46  public:
47   WavFileWriter() {}
48   ~WavFileWriter()
49     {
50       while ( ! m_OutFile.empty() )
51         {
52           delete m_OutFile.back();
53           m_OutFile.pop_back();
54         }
55     }
56
57   ASDCP::Result_t
58     OpenWrite(ASDCP::PCM::AudioDescriptor &ADesc, const char* file_root, bool split)
59     {
60       ASDCP_TEST_NULL_STR(file_root);
61       char filename[256];
62       ui32_t file_count = 1;
63       ASDCP::Result_t result = ASDCP::RESULT_OK;
64       m_ADesc = ADesc;
65
66       if ( split )
67         {
68           assert ( m_ADesc.ChannelCount % 2 == 0 ); // no support yet for stuffing odd files
69           file_count = m_ADesc.ChannelCount / 2;
70           m_ADesc.ChannelCount = 2;
71         }
72
73       for ( ui32_t i = 0; i < file_count && ASDCP_SUCCESS(result); i++ )
74         {
75           snprintf(filename, 256, "%s_%lu.wav", file_root, (i + 1));
76           m_OutFile.push_back(new ASDCP::FileWriter);
77           result = m_OutFile.back()->OpenWrite(filename);
78
79           if ( ASDCP_SUCCESS(result) )
80             {
81               ASDCP::Wav::SimpleWaveHeader Wav(m_ADesc);
82               result = Wav.WriteToFile(*(m_OutFile.back()));
83             }
84         }
85       
86       return result;
87     }
88
89   ASDCP::Result_t
90     WriteFrame(ASDCP::PCM::FrameBuffer& FB)
91     {
92       ui32_t write_count;
93       ASDCP::Result_t result = ASDCP::RESULT_OK;
94       std::list<ASDCP::FileWriter*>::iterator fi;
95       assert(! m_OutFile.empty());
96
97       ui32_t sample_size = ASDCP::PCM::CalcSampleSize(m_ADesc);
98       const byte_t* p = FB.RoData();
99       const byte_t* end_p = p + FB.Size();
100
101       while ( p < end_p )
102         {
103           for ( fi = m_OutFile.begin(); fi != m_OutFile.end() && ASDCP_SUCCESS(result); fi++ )
104             {
105               result = (*fi)->Write(p, sample_size, &write_count);
106               assert(write_count == sample_size);
107               p += sample_size;
108             }
109         }
110
111       return result;
112     }
113 };
114
115
116 #endif // _WAVFILEWRITER_H_
117
118 //
119 // end WavFileWriter.h
120 //