kwave  18.07.70
SampleDecoderLinear.cpp File Reference
#include "config.h"
#include <stdio.h>
#include <QtGlobal>
#include "libkwave/Sample.h"
#include "libkwave/SampleFormat.h"
#include "libkwave/Utils.h"
#include "SampleDecoderLinear.h"
Include dependency graph for SampleDecoderLinear.cpp:

Go to the source code of this file.

Macros

#define MAKE_DECODER(bits)
 

Functions

static void decode_NULL (const quint8 *src, sample_t *dst, unsigned int count)
 
static quint32 shl (const quint32 v, const int s)
 
template<const unsigned int bits, const bool is_signed, const bool is_little_endian>
void decode_linear (const quint8 *src, sample_t *dst, unsigned int count)
 

Macro Definition Documentation

◆ MAKE_DECODER

#define MAKE_DECODER (   bits)
Value:
if (sample_format != Kwave::SampleFormat::Unsigned) { \
if (endianness != Kwave::BigEndian) { \
m_decoder = decode_linear<bits, true, true>; \
} else { \
m_decoder = decode_linear<bits, true, false>; \
} \
} else { \
if (endianness != Kwave::BigEndian) { \
m_decoder = decode_linear<bits, false, true>; \
} else { \
m_decoder = decode_linear<bits, false, false>; \
} \
}

Definition at line 102 of file SampleDecoderLinear.cpp.

Referenced by Kwave::SampleDecoderLinear::SampleDecoderLinear().

Function Documentation

◆ decode_linear()

template<const unsigned int bits, const bool is_signed, const bool is_little_endian>
void decode_linear ( const quint8 *  src,
sample_t dst,
unsigned int  count 
)

Template for decoding a buffer with linear samples. The tricky part is done in the compiler which optimizes away all unused parts of current variant and does nice loop optimizing!

Parameters
srcarray with raw data
dstarray that receives the samples in Kwave's format
countthe number of samples to be decoded

Definition at line 63 of file SampleDecoderLinear.cpp.

References SAMPLE_BITS, and shl().

64 {
65  const int shift = (SAMPLE_BITS - bits);
66  const quint32 sign = 1 << (SAMPLE_BITS-1);
67  const quint32 negative = ~(sign - 1);
68  const quint32 bytes = (bits+7) >> 3;
69 
70  while (count) {
71  count--;
72 
73  // read from source buffer
74  quint32 s = 0;
75  if (is_little_endian) {
76  // little endian
77  for (unsigned int byte = 0; byte < bytes; ++byte, ++src) {
78  s |= static_cast<quint8>(*src) << (byte << 3);
79  }
80  } else {
81  // big endian
82  for (int byte = bytes - 1; byte >= 0; --byte, ++src) {
83  s |= static_cast<quint8>(*src) << (byte << 3);
84  }
85  }
86 
87  // convert to signed
88  if (!is_signed) s -= shl(1, bits-1)-1;
89 
90  // shift up to Kwave's bit count
91  s = shl(s, shift);
92 
93  // sign correcture for negative values
94  if (is_signed && (s & sign)) s |= negative;
95 
96  // write to destination buffer
97  *(dst++) = static_cast<sample_t>(s);
98  }
99 }
static quint32 shl(const quint32 v, const int s)
#define SAMPLE_BITS
Definition: Sample.h:43
Here is the call graph for this function:

◆ decode_NULL()

static void decode_NULL ( const quint8 *  src,
sample_t dst,
unsigned int  count 
)
static

Definition at line 31 of file SampleDecoderLinear.cpp.

References SAMPLE_BITS, and Kwave::toInt().

32 {
33  while (count--) {
34  printf("%02X ", Kwave::toInt(*src));
35  *(dst++) = count % (1 << (SAMPLE_BITS-1));
36  }
37 }
int toInt(T x)
Definition: Utils.h:127
#define SAMPLE_BITS
Definition: Sample.h:43
Here is the call graph for this function:

◆ shl()

static quint32 shl ( const quint32  v,
const int  s 
)
inlinestatic

Definition at line 42 of file SampleDecoderLinear.cpp.

Referenced by decode_linear().

43 {
44  if (!s)
45  return v;
46  else if (s > 0)
47  return (v << s);
48  else
49  return (v >> (-s));
50 }
Here is the caller graph for this function: