SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WavDef.hpp
Go to the documentation of this file.
1 
9 #ifndef WAVDEF_HPP_
10 #define WAVDEF_HPP_
11 
16 #include <istream>
17 #include <ostream>
18 #include <limits>
19 #include <algorithm>
20 /*
21  * \todo find a portable solution for int32 and int16 definitions
22  */
23 extern "C"{
24 #include <stdint.h>
25 }
26 
27 #include "error.hpp"
28 
29 typedef char char8;
30 typedef int32_t int32;
31 typedef int16_t int16;
32 
33 // REVERSE_ENDIANISM must be defined if the endianism of the host platform is not Intel
34 // (Intel is little-endian)
35 #define SWAP_32(in) ( \
36  (((int32) in) << 24) | \
37  ((((int32) in) & 0x0000FF00L) << 8) | \
38  ((((int32) in) & 0x00FF0000L) >> 8) | \
39  (((int32) in) >> 24))
40 
41 #define SWAP_16(in) ( \
42  (((int16) in) << 8) | \
43  (((int16) in) >> 8))
44 
45 
46 
47 
54 struct RIFF_CHUNK_HEADER
55 {
57  char8 chunk_id[5];
59  int32 chunk_size;
61  char8 format[5];
62 
64  friend std::ostream & operator<<(std::ostream & os, const RIFF_CHUNK_HEADER & s);
66  friend std::istream & operator>>(std::istream & is, RIFF_CHUNK_HEADER & s);
67 };
68 
69 std::ostream & operator<<(std::ostream & os, const RIFF_CHUNK_HEADER & s){
70  os << "RiffChunkId= " << s.chunk_id << " | ";
71  os << "RiffChunkSize= " << s.chunk_size << " | ";
72  os << "RiffFormat= " << s.format << '\n';
73  return os;
74 }
75 
76 std::istream & operator>>(std::istream & is, RIFF_CHUNK_HEADER & s){
77  std::string buf;
78  is >> buf >> s.chunk_id >> buf;
79  is >> buf >> s.chunk_size >> buf;
80  is >> buf >> s.format;
81  return is;
82 }
83 
90 struct FORMAT_CHUNK_HEADER
91 {
93  char8 chunk_id[5];
95  int32 chunk_size;
97  int16 format;
99  int16 num_channels;
101  int32 sample_rate;
103  int32 byte_rate;
105  int16 align;
107  int16 bits_per_sample;
109  friend std::ostream & operator<<(std::ostream & os, const FORMAT_CHUNK_HEADER & s);
111  friend std::istream & operator>>(std::istream & is, FORMAT_CHUNK_HEADER & s);
112 };
113 
114 std::ostream & operator<<(std::ostream & os, const FORMAT_CHUNK_HEADER & s){
115  os << "FmtChunkId= " << s.chunk_id << " | ";
116  os << "FmtChunkSize= " << s.chunk_size << " | ";
117  os << "FmtFormat= " << s.format << " | ";
118  os << "FmtNumChannels= " << s.num_channels << " | ";
119  os << "FmtSampleRate= " << s.sample_rate << " | ";
120  os << "FmtByteRate= " << s.byte_rate << " | ";
121  os << "FmtAlign= " << s.align << " | ";
122  os << "FmtBitsPerSample= " << s.bits_per_sample << '\n';
123  return os;
124 }
125 
126 std::istream & operator>>(std::istream & is, FORMAT_CHUNK_HEADER & s){
127  std::string buf;
128  is >> buf >> s.chunk_id >> buf;
129  is >> buf >> s.chunk_size >> buf;
130  is >> buf >> s.format >> buf;
131  is >> buf >> s.num_channels >> buf;
132  is >> buf >> s.sample_rate >> buf;
133  is >> buf >> s.byte_rate >> buf;
134  is >> buf >> s.align >> buf;
135  is >> buf >> s.bits_per_sample;
136  return is;
137 }
138 
145 struct DATA_CHUNK_HEADER
146 {
148  char8 chunk_id[5];
150  int32 chunk_size;
152  friend std::ostream & operator<<(std::ostream & os, const DATA_CHUNK_HEADER & s);
154  friend std::istream & operator>>(std::istream & is, DATA_CHUNK_HEADER & s);
155 };
156 
157 std::ostream & operator<<(std::ostream & os, const DATA_CHUNK_HEADER & s){
158  os << "DataChunkId= " << s.chunk_id << " | ";
159  os << "DataChunkSize= " << s.chunk_size << '\n';
160  return os;
161 }
162 
163 std::istream & operator>>(std::istream & is, DATA_CHUNK_HEADER & s){
164  std::string buf;
165  is >> buf >> s.chunk_id >> buf;
166  is >> buf >> s.chunk_size;
167  return is;
168 }
169 
179 struct WAVE_HEADER
180 {
181  RIFF_CHUNK_HEADER riff_header;
182  FORMAT_CHUNK_HEADER format_header;
183  DATA_CHUNK_HEADER data_header;
185  friend std::ostream & operator<<(std::ostream & os, const WAVE_HEADER & s);
187  friend std::istream & operator>>(std::istream & is, WAVE_HEADER & s);
188 };
189 
190 std::ostream & operator<<(std::ostream & os, const WAVE_HEADER & s){
191  os << s.riff_header << s.format_header << s.data_header;
192  return os;
193 }
194 
195 std::istream & operator>>(std::istream & is, WAVE_HEADER & s){
196  is >> s.riff_header;
197  is >> s.format_header;
198  is >> s.data_header;
199  return is;
200 }
201 
206 #endif /* WAVDEF_HPP_ */
207 
Provides SLIP error messages.
std::ostream & operator<<(std::ostream &out, const Array< T > &a)
Definition: Array.hpp:1282