IT++ Logo
audiosample.h
Go to the documentation of this file.
1
29#ifndef AUDIOSAMPLE_H
30#define AUDIOSAMPLE_H
31
32#include <climits>
33#include <itpp/base/ittypes.h>
34#include <itpp/srccode/g711.h>
35
48namespace itpp
49{
50
58enum Audio_Encoding {enc_unknown = 0, enc_mulaw8 = 1, enc_alaw8 = 27,
59 enc_linear8 = 2,enc_linear16 = 3,enc_linear24 = 4,
60 enc_linear32 = 5,enc_float = 6,enc_double = 7};
61
62
71template<typename T, T max_abs> T limit_audio_sample(double s)
72{
73 //ternary operators allow optimizer to deploy SIMD floating-point instructions
74 s < -1.0 ? -1.0 : s > 1.0 ? 1.0 : s;
75 return (T)(s*max_abs);
76}
77
85template<typename T, T down_scaling> double audio_sample_to_double(T s)
86{
87 return (1.0/down_scaling) * s;
88}
89
99template<Audio_Encoding> class Audio_Sample;
100
105template<> class Audio_Sample<enc_mulaw8>
106{
107public:
108 typedef uint8_t enc_sample_type;
109 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
110 static enc_sample_type encode(const double& s)
111 {
112 int16_t l = limit_audio_sample<int16_t, SHRT_MAX>(s);
113 return ulaw_compress(l);
114 }
115 static double decode(const enc_sample_type& s)
116 {
117 return audio_sample_to_double<int16_t, SHRT_MAX>((ulaw_expand(s)));
118 }
119};
120
125template<> class Audio_Sample<enc_linear8>
126{
127public:
128 typedef int8_t enc_sample_type;
129 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
130 static enc_sample_type encode(const double& s)
131 {
132 return limit_audio_sample<enc_sample_type, SCHAR_MAX>(s);
133 }
134 static double decode(const enc_sample_type& s)
135 {
136 return audio_sample_to_double<enc_sample_type, SCHAR_MAX>(s);
137 }
138};
139
144template<> class Audio_Sample<enc_linear16>
145{
146public:
147 typedef int16_t enc_sample_type;
148 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
149 static enc_sample_type encode(const double& s)
150 {
151 return limit_audio_sample<enc_sample_type, SHRT_MAX>(s);
152 }
153 static double decode(const enc_sample_type& s)
154 {
155 return audio_sample_to_double<enc_sample_type, SHRT_MAX>(s);
156 }
157};
158
161{
162public:
163 static const int32_t max_abs_value = (1<<23) - 1;
164 explicit Sample_24(uint32_t v = 0):_value(v){}
165 uint32_t value() const {return _value;}
166 void value(uint32_t v){_value = v;}
167private:
168 uint32_t _value;
169};
170
171
173template<typename Binary_Out_Stream>
174Binary_Out_Stream& operator<<(Binary_Out_Stream& s, Sample_24 v)
175{
176 uint32_t sample = v.value();
177 char *c = reinterpret_cast<char *>(&sample);
178 if(s.get_endianity() == s.get_native_endianity()){
179 //stream endian matches machine endian
180 s.write(c,3);
181 }
182 else{
183 //stream endian differs from machine endian - reverse order of bytes
184 s.put(c[2]); s.put(c[1]); s.put(c[0]);
185 }
186 return s;
187}
188
190template<typename Binary_In_Stream>
191Binary_In_Stream& operator>>(Binary_In_Stream& s, Sample_24& v)
192{
193 uint32_t sample;
194 char *c = reinterpret_cast<char *>(&sample);
195 if(s.get_endianity() == s.get_native_endianity()){
196 //stream endian matches machine endian
197 s.read(c,3);
198 }
199 else{
200 //stream endian differs from machine endian - reverse order of bytes
201 s.get(c[2]); s.get(c[1]); s.get(c[0]);
202 }
203 if(s) v.value(sample);
204 return s;
205}
206
211template<> class Audio_Sample<enc_linear24>
212{
213public:
215 static const std::size_t enc_sample_size = 3; //3 bytes per sample
216 static enc_sample_type encode(const double& s)
217 {
218 return Sample_24(limit_audio_sample<int32_t, Sample_24::max_abs_value>(s));
219 }
220 static double decode(const enc_sample_type& s)
221 {
222 return audio_sample_to_double<int32_t, Sample_24::max_abs_value>(s.value());
223 }
224};
225
226
231template<> class Audio_Sample<enc_linear32>
232{
233public:
234 typedef int32_t enc_sample_type;
235 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
236 static enc_sample_type encode(const double& s)
237 {
238 return limit_audio_sample<enc_sample_type, INT_MAX>(s);
239 }
240 static double decode(const enc_sample_type& s)
241 {
242 return audio_sample_to_double<enc_sample_type, INT_MAX>(s);
243 }
244};
245
246
254template<> class Audio_Sample<enc_float>
255{
256public:
257 typedef float enc_sample_type;
258 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
259 static enc_sample_type encode(const double& s)
260 {//saturate here to avoid Infinity values
261 return (enc_sample_type)(s < -INT_MAX ? -INT_MAX : s > INT_MAX ? INT_MAX : s);
262 }
263 static double decode(const enc_sample_type& s){return s;}
264};
265
272template<> class Audio_Sample<enc_double>
273{
274public:
275 typedef double enc_sample_type;
276 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
277 static enc_sample_type encode(const double& s) {return s;}
278 static double decode(const enc_sample_type& s){return s;}
279};
280
285template<> class Audio_Sample<enc_alaw8>
286{
287public:
288 typedef uint8_t enc_sample_type;
289 static const std::size_t enc_sample_size = sizeof(enc_sample_type);
290 static enc_sample_type encode(const double& s)
291 {
292 int16_t l = limit_audio_sample<int16_t, SHRT_MAX>(s);
293 return alaw_compress(l);
294 }
295 static double decode(const enc_sample_type& s)
296 {
297 return audio_sample_to_double<int16_t, SHRT_MAX>((alaw_expand(s)));
298 }
299};
300
303{
304 switch(e) {
305 case enc_mulaw8:
307 case enc_linear8:
309 case enc_linear16:
311 case enc_linear24:
313 case enc_linear32:
315 case enc_float:
317 case enc_double:
319 case enc_alaw8:
321 case enc_unknown:
322 default:
323 return 0;
324 }
325}
326
327} // namespace itpp
328
329#endif // #ifndef AUDIOFILE_H
Generic template class for Audio samples.
Definition: audiosample.h:99
Small class to represent 24-bit PCM samples.
Definition: audiosample.h:161
Implementation of G.711 logarithmic codecs.
Audio_Encoding
Supported encoding types for audio samples.
Definition: audiosample.h:58
int16_t alaw_expand(uint8_t s)
G.711 u-Law expansion function. Returns decoded value for previously compressed sample s....
Definition: g711.h:226
uint8_t ulaw_compress(int16_t s)
G.711 u-Law compression function. Returns encoded value for sample s.
Definition: g711.h:143
int16_t ulaw_expand(uint8_t s)
G.711 u-Law expansion function. Returns decoded value for previously compressed sample s....
Definition: g711.h:176
uint8_t alaw_compress(int16_t s)
G.711 a-Law compression function. Returns encoded value for sample s.
Definition: g711.h:192
T limit_audio_sample(double s)
Helper function for scaling and limiting of audio samples.
Definition: audiosample.h:71
double audio_sample_to_double(T s)
Helper function for scaling and limiting of audio samples.
Definition: audiosample.h:85
IT++ type definitions.
itpp namespace
Definition: itmex.h:37
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition: binary.cpp:42
std::size_t encoded_sample_size(Audio_Encoding e)
Size of encoded sample based on the encoding type e.
Definition: audiosample.h:302
SourceForge Logo

Generated on Tue Jan 24 2023 00:00:00 for IT++ by Doxygen 1.9.6