audiosample.h
Go to the documentation of this file.
#ifndef AUDIOSAMPLE_H
#define AUDIOSAMPLE_H
#include <climits>
#include <itpp/base/ittypes.h>
#include <itpp/srccode/g711.h>
namespace itpp
{
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};
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);
}
template<typename T, T down_scaling> double audio_sample_to_double(T s)
{
return (1.0/down_scaling) * s;
}
template<Audio_Encoding> class Audio_Sample;
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)));
}
};
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);
}
};
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);
}
};
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;
};
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;
}
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;
}
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());
}
};
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);
}
};
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;}
};
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;}
};
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)));
}
};
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
itpp::Array
General array class.
Definition array.h:105
itpp::Audio_Sample
Generic template class for Audio samples.
Definition audiosample.h:99
itpp::Sample_24
Small class to represent 24-bit PCM samples.
Definition audiosample.h:161
g711.h
Implementation of G.711 logarithmic codecs.
itpp::Audio_Encoding
Audio_Encoding
Supported encoding types for audio samples.
Definition audiosample.h:58
itpp::alaw_expand
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
itpp::ulaw_compress
uint8_t ulaw_compress(int16_t s)
G.711 u-Law compression function. Returns encoded value for sample s.
Definition g711.h:143
itpp::ulaw_expand
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
itpp::alaw_compress
uint8_t alaw_compress(int16_t s)
G.711 a-Law compression function. Returns encoded value for sample s.
Definition g711.h:192
itpp::limit_audio_sample
T limit_audio_sample(double s)
Helper function for scaling and limiting of audio samples.
Definition audiosample.h:71
itpp::audio_sample_to_double
double audio_sample_to_double(T s)
Helper function for scaling and limiting of audio samples.
Definition audiosample.h:85
ittypes.h
IT++ type definitions.
itpp
itpp namespace
Definition itmex.h:37
itpp::operator<<
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition binary.cpp:36
itpp::operator>>
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition binary.cpp:42
itpp::encoded_sample_size
std::size_t encoded_sample_size(Audio_Encoding e)
Size of encoded sample based on the encoding type e.
Definition audiosample.h:302

Generated on Mon Jun 10 2024 11:49:25 for IT++ by Doxygen 1.9.8