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