/usr/include/itpp/srccode/audiosample.h is in libitpp-dev 4.3.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | /*!
* \file
* \brief Encoding and decoding of audio samples
* \author Andy Panov
*
* -------------------------------------------------------------------------
*
* Copyright (C) 2013 (see AUTHORS file for a list of contributors)
*
* This file is part of IT++ - a C++ library of mathematical, signal
* processing, speech processing, and communications classes and functions.
*
* IT++ is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with IT++. If not, see <http://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#ifndef AUDIOSAMPLE_H
#define AUDIOSAMPLE_H
#include <climits>
#include <itpp/base/ittypes.h>
#include <itpp/srccode/g711.h>
/*!
\addtogroup audio
\section audiorepresentation Representaion of Audio samples.
ITPP supports various types of representation for audio samples:
- 8/16/32-bit PCM encoding formats
- IEEE 754 Floating point formats with single and double precision
- G.711 u-law and A-law encoded samples
*/
namespace itpp
{
/*!
\ingroup audio
\brief Supported encoding types for audio samples.
Numerical values of these constants correspond to the encoding
type identifier in snd file format, introduced by Sun Microsystems.
*/
enum Audio_Encoding {enc_unknown = 0, enc_mulaw8 = 1, enc_alaw8 = 27,
enc_linear8 = 2,enc_linear16 = 3,enc_linear24 = 4,
enc_linear32 = 5,enc_float = 6,enc_double = 7};
/*!
\ingroup audio
\brief Helper function for scaling and limiting of audio samples.
This function maps [-1.0, 1.0] doubles to [-max_abs, max_abs] values of type T.
Input values are limited before mapping.
*/
template<typename T, T max_abs> T limit_audio_sample(double s)
{
//ternary operators allow optimizer to deploy SIMD floating-point instructions
s < -1.0 ? -1.0 : s > 1.0 ? 1.0 : s;
return (T)(s*max_abs);
}
/*!
\ingroup audio
\brief Helper function for scaling and limiting of audio samples.
This function maps [-max_abs, max_abs] values of type T to doubles in [-1.0,1.0] interval
*/
template<typename T, T down_scaling> double audio_sample_to_double(T s)
{
return (1.0/down_scaling) * s;
}
/*!
\ingroup audio
\brief Generic template class for Audio samples.
Specializations of this class provide encoding and decoding facilities for various
representations of audio samples. Encoding inputs are limited to [-1.0,1.0] range.
Decoding outputs are scaled to [-1.0,1.0] range.
*/
template<Audio_Encoding> class Audio_Sample;
/*!
\ingroup audio
\brief uLaw-encoded Audio samples.
*/
template<> class Audio_Sample<enc_mulaw8>
{
public:
typedef uint8_t enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s)
{
int16_t l = limit_audio_sample<int16_t, SHRT_MAX>(s);
return ulaw_compress(l);
}
static double decode(const enc_sample_type& s)
{
return audio_sample_to_double<int16_t, SHRT_MAX>((ulaw_expand(s)));
}
};
/*!
\ingroup audio
\brief 8-bit PCM encoded audio samples.
*/
template<> class Audio_Sample<enc_linear8>
{
public:
typedef int8_t enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s)
{
return limit_audio_sample<enc_sample_type, SCHAR_MAX>(s);
}
static double decode(const enc_sample_type& s)
{
return audio_sample_to_double<enc_sample_type, SCHAR_MAX>(s);
}
};
/*!
\ingroup audio
\brief 16-bit PCM encoded audio samples
*/
template<> class Audio_Sample<enc_linear16>
{
public:
typedef int16_t enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s)
{
return limit_audio_sample<enc_sample_type, SHRT_MAX>(s);
}
static double decode(const enc_sample_type& s)
{
return audio_sample_to_double<enc_sample_type, SHRT_MAX>(s);
}
};
//! Small class to represent 24-bit PCM samples.
class Sample_24
{
public:
static const int32_t max_abs_value = (1<<23) - 1;
explicit Sample_24(uint32_t v = 0):_value(v){}
uint32_t value() const {return _value;}
void value(uint32_t v){_value = v;}
private:
uint32_t _value;
};
//! insertion operator for 24-bit PCM sample
template<typename Binary_Out_Stream>
Binary_Out_Stream& operator<<(Binary_Out_Stream& s, Sample_24 v)
{
uint32_t sample = v.value();
char *c = reinterpret_cast<char *>(&sample);
if(s.get_endianity() == s.get_native_endianity()){
//stream endian matches machine endian
s.write(c,3);
}
else{
//stream endian differs from machine endian - reverse order of bytes
s.put(c[2]); s.put(c[1]); s.put(c[0]);
}
return s;
}
//! extraction operator for 24-bit PCM sample
template<typename Binary_In_Stream>
Binary_In_Stream& operator>>(Binary_In_Stream& s, Sample_24& v)
{
uint32_t sample;
char *c = reinterpret_cast<char *>(&sample);
if(s.get_endianity() == s.get_native_endianity()){
//stream endian matches machine endian
s.read(c,3);
}
else{
//stream endian differs from machine endian - reverse order of bytes
s.get(c[2]); s.get(c[1]); s.get(c[0]);
}
if(s) v.value(sample);
return s;
}
/*!
\ingroup audio
\brief 24-bit PCM encoded audio samples.
*/
template<> class Audio_Sample<enc_linear24>
{
public:
typedef Sample_24 enc_sample_type;
static const std::size_t enc_sample_size = 3; //3 bytes per sample
static enc_sample_type encode(const double& s)
{
return Sample_24(limit_audio_sample<int32_t, Sample_24::max_abs_value>(s));
}
static double decode(const enc_sample_type& s)
{
return audio_sample_to_double<int32_t, Sample_24::max_abs_value>(s.value());
}
};
/*!
\ingroup audio
\brief 32-bit PCM encoded audio samples.
*/
template<> class Audio_Sample<enc_linear32>
{
public:
typedef int32_t enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s)
{
return limit_audio_sample<enc_sample_type, INT_MAX>(s);
}
static double decode(const enc_sample_type& s)
{
return audio_sample_to_double<enc_sample_type, INT_MAX>(s);
}
};
/*!
\ingroup audio
\brief Audio samples encoded as floats.
Samples are NOT saturated to +/- 1.0 during conversion to this format.
Encoded values are limited to [INT_MIN,INT_MAX] to avoid overflow on conversion.
*/
template<> class Audio_Sample<enc_float>
{
public:
typedef float enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s)
{//saturate here to avoid Infinity values
return (enc_sample_type)(s < -INT_MAX ? -INT_MAX : s > INT_MAX ? INT_MAX : s);
}
static double decode(const enc_sample_type& s){return s;}
};
/*!
\ingroup audio
\brief Audio samples encoded as doubles.
Samples are NOT saturated to +/- 1.0 during conversion to this format.
*/
template<> class Audio_Sample<enc_double>
{
public:
typedef double enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s) {return s;}
static double decode(const enc_sample_type& s){return s;}
};
/*!
\brief aLaw-encoded Audio samples.
\ingroup audio
*/
template<> class Audio_Sample<enc_alaw8>
{
public:
typedef uint8_t enc_sample_type;
static const std::size_t enc_sample_size = sizeof(enc_sample_type);
static enc_sample_type encode(const double& s)
{
int16_t l = limit_audio_sample<int16_t, SHRT_MAX>(s);
return alaw_compress(l);
}
static double decode(const enc_sample_type& s)
{
return audio_sample_to_double<int16_t, SHRT_MAX>((alaw_expand(s)));
}
};
//! Size of encoded sample based on the encoding type \a e.
inline std::size_t encoded_sample_size(Audio_Encoding e)
{
switch(e) {
case enc_mulaw8:
return Audio_Sample<enc_mulaw8>::enc_sample_size;
case enc_linear8:
return Audio_Sample<enc_linear8>::enc_sample_size;
case enc_linear16:
return Audio_Sample<enc_linear16>::enc_sample_size;
case enc_linear24:
return Audio_Sample<enc_linear24>::enc_sample_size;
case enc_linear32:
return Audio_Sample<enc_linear32>::enc_sample_size;
case enc_float:
return Audio_Sample<enc_float>::enc_sample_size;
case enc_double:
return Audio_Sample<enc_double>::enc_sample_size;
case enc_alaw8:
return Audio_Sample<enc_alaw8>::enc_sample_size;
case enc_unknown:
default:
return 0;
}
}
} // namespace itpp
#endif // #ifndef AUDIOFILE_H
|