ginormo merge-back with Kumu, SMPTE MIC key and MPEG parser fix
[asdcplib.git] / src / Wav.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    Wav.h
28     \version $Id$
29     \brief   Wave file common elements
30 */
31
32 #ifndef _WAV_H_
33 #define _WAV_H_
34
35 #include <KM_fileio.h>
36 #include <AS_DCP.h>
37
38 namespace ASDCP
39 {
40   //
41   class fourcc
42     {
43     private:
44       byte_t data[4];
45
46     public:
47       inline fourcc() { memset( data, 0, 4 ); }
48       inline fourcc( const char* v )   { memcpy( this->data, v, 4 ); }
49       inline fourcc( const byte_t* v ) { memcpy( this->data, v, 4 ); }
50       inline fourcc& operator=(const fourcc & s) { memcpy( this->data, s.data, 4); return *this; }
51       inline bool operator==(const fourcc &rhs)  { return memcmp(data, rhs.data, 4) == 0 ? true : false; }
52       inline bool operator!=(const fourcc &rhs)  { return memcmp(data, rhs.data, 4) != 0 ? true : false; }
53     };
54
55   namespace AIFF
56     {
57       const fourcc FCC_FORM("FORM");
58       const fourcc FCC_AIFF("AIFF");
59       const fourcc FCC_COMM("COMM");
60       const fourcc FCC_SSND("SSND");
61
62       class SimpleAIFFHeader
63         {
64         public:
65           ui16_t  numChannels;
66           ui32_t  numSampleFrames;
67           ui16_t  sampleSize;
68           byte_t  sampleRate[10]; // 80-bit IEEE 754 float
69           ui32_t  data_len;
70
71           SimpleAIFFHeader() :
72             numChannels(0), numSampleFrames(0), sampleSize(0), data_len(0) {
73             memset(sampleRate, 0, 10);
74           }
75           
76           Result_t  ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start);
77           Result_t  ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start);
78           void      FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, Rational PictureRate) const;
79         };
80
81     } // namespace AIFF
82
83   namespace Wav
84     {
85       const ui32_t MaxWavHeader = 1024*32; // must find "data" within this space or no happy
86
87       const fourcc FCC_RIFF("RIFF");
88       const fourcc FCC_WAVE("WAVE");
89       const fourcc FCC_fmt_("fmt ");
90       const fourcc FCC_data("data");
91
92       //
93       class SimpleWaveHeader
94         {
95         public:
96           ui16_t        format;
97           ui16_t        nchannels;
98           ui32_t        samplespersec;
99           ui32_t        avgbps;
100           ui16_t        blockalign;
101           ui16_t        bitspersample;
102           ui16_t        cbsize;
103           ui32_t        data_len;
104
105           SimpleWaveHeader() :
106             format(0), nchannels(0), samplespersec(0), avgbps(0),
107             blockalign(0), bitspersample(0), cbsize(0), data_len(0) {}
108       
109           SimpleWaveHeader(ASDCP::PCM::AudioDescriptor& ADesc);
110           
111           Result_t  ReadFromBuffer(const byte_t* buf, ui32_t buf_len, ui32_t* data_start);
112           Result_t  ReadFromFile(const Kumu::FileReader& InFile, ui32_t* data_start);
113           Result_t  WriteToFile(Kumu::FileWriter& OutFile) const;
114           void      FillADesc(ASDCP::PCM::AudioDescriptor& ADesc, Rational PictureRate) const;
115         };
116
117     } // namespace Wav
118 } // namespace ASDCP
119
120 #endif // _WAV_H_
121
122 //
123 // end Wav.h
124 //