[EXPERIMENTAL] improve IMBE and AMBE vocoder quality; isolate per-stream synthesis state, use the fixed-point 88-bit IMBE decoder, extend pitch tracking to the 20-sample boundary, refine submultiple and voiced/unvoiced decisions, correct encoder gain timing and silent-frame gain, remove excessive raw AMBE decoder scaling, and add focused vocoder regression tests;

pull/138/head
Bryan Biedenkapp 3 days ago
parent 5ca2b1c967
commit 7a49192d4e

@ -24,6 +24,8 @@ using namespace vocoder;
// Constants
// ---------------------------------------------------------------------------
static const uint32_t IMBE_WORD_WIDTHS[8U] = {12U, 12U, 12U, 12U, 11U, 11U, 11U, 7U};
const int MBEDecoder::dW[72] = { 0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,2,1,1,0,0,3,2,0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,2,1,1,0,0,3,2,0,0,3,2,1,1,0,0,1,1,0,0,3,2,1,1,3,3,2,1,0,0,3,3, };
const int MBEDecoder::dX[72] = { 10,22,11,9,10,22,11,23,8,20,9,21,10,8,9,21,8,6,7,19,8,20,9,7,6,18,7,5,6,18,7,19,4,16,5,17,6,4,5,17,4,2,3,15,4,16,5,3,2,14,3,1,2,14,3,15,0,12,1,13,2,0,1,13,0,12,10,11,0,12,1,13, };
@ -72,7 +74,8 @@ const int MBEDecoder::rZ[36] = {
/* Initializes a new instance of the MBEDecoder class. */
MBEDecoder::MBEDecoder(MBE_DECODER_MODE mode) :
m_mbelibParms(NULL),
m_mbelibParms(nullptr),
m_imbeVocoder(nullptr),
m_mbeMode(mode),
m_gainAdjust(1.0f),
m_autoGain(false)
@ -80,6 +83,9 @@ MBEDecoder::MBEDecoder(MBE_DECODER_MODE mode) :
m_mbelibParms = new mbelibParms();
mbe_initMbeParms(m_mbelibParms->m_cur_mp, m_mbelibParms->m_prev_mp, m_mbelibParms->m_prev_mp_enhanced);
if (m_mbeMode == DECODE_88BIT_IMBE)
m_imbeVocoder = new imbe_vocoder();
::memset(gainMaxBuf, 0, sizeof(float) * 200);
gainMaxBufPtr = gainMaxBuf;
gainMaxIdx = 0;
@ -89,6 +95,7 @@ MBEDecoder::MBEDecoder(MBE_DECODER_MODE mode) :
MBEDecoder::~MBEDecoder()
{
delete m_imbeVocoder;
delete m_mbelibParms;
}
@ -169,14 +176,25 @@ int32_t MBEDecoder::decodeBits(const uint8_t* mbeBits, int16_t samples[])
for (uint32_t i = 0U; i < 49U; i++)
ambe[i] = mbeBits[i] != 0U ? 1 : 0;
float decoded[160U];
int errs = 0;
int errs2 = 0;
char errStr[64U];
::memset(errStr, 0x20U, sizeof(errStr));
mbe_processAmbe2450Data(samples, &errs, &errs2, errStr, ambe,
mbe_processAmbe2450DataF(decoded, &errs, &errs2, errStr, ambe,
m_mbelibParms->m_cur_mp, m_mbelibParms->m_prev_mp,
m_mbelibParms->m_prev_mp_enhanced, 3);
for (uint32_t i = 0U; i < 160U; i++) {
float sample = decoded[i] * m_gainAdjust;
if (sample > 32760.0f)
sample = 32760.0f;
else if (sample < -32760.0f)
sample = -32760.0f;
samples[i] = static_cast<int16_t>(sample);
}
return errs2;
}
@ -222,20 +240,22 @@ int32_t MBEDecoder::decodeF(uint8_t* codeword, float samples[])
case DECODE_88BIT_IMBE:
{
char imbe_d[88U];
::memset(imbe_d, 0x00U, 88U);
for (int i = 0; i < 11; ++i) {
for (int j = 0; j < 8; j++) {
imbe_d[j + (8 * i)] = (1 & (codeword[i] >> (7 - j)));
int16_t frameVector[8U] = {0};
int16_t decoded[160U] = {0};
uint32_t offset = 0U;
for (uint32_t word = 0U; word < 8U; word++) {
for (uint32_t bit = 0U; bit < IMBE_WORD_WIDTHS[word]; bit++, offset++) {
frameVector[word] = static_cast<int16_t>((frameVector[word] << 1U) |
((codeword[offset / 8U] >> (7U - offset % 8U)) & 0x01U));
}
}
int ambeErrs;
char ambeErrStr[64U];
::memset(ambeErrStr, 0x20U, 64U);
assert(m_imbeVocoder != nullptr);
m_imbeVocoder->imbe_decode(frameVector, decoded);
mbe_processImbe4400DataF(samples, &ambeErrs, &errs, ambeErrStr, imbe_d, m_mbelibParms->m_cur_mp, m_mbelibParms->m_prev_mp, m_mbelibParms->m_prev_mp_enhanced, 3);
for (uint32_t sample = 0U; sample < 160U; sample++)
samples[sample] = static_cast<float>(decoded[sample]);
}
break;
}

@ -22,6 +22,7 @@ extern "C" {
}
#include "common/Defines.h"
#include "vocoder/imbe/imbe_vocoder.h"
#include <stdlib.h>
#include <queue>
@ -120,6 +121,7 @@ namespace vocoder
private:
mbelibParms* m_mbelibParms;
imbe_vocoder* m_imbeVocoder;
MBE_DECODER_MODE m_mbeMode;

@ -181,7 +181,7 @@ static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp,
// ref_pitch is Q8_8 in range 19.875 - 123.125
int b0_i = (imbe_param->ref_pitch >> 5) - 159;
if (b0_i < 0 || b0_i > b0_lmax) {
if (b0_i < 0 || b0_i >= b0_lmax) {
fprintf(stderr, "encode error b0_i %d\n", b0_i);
return;
}
@ -195,7 +195,7 @@ static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp,
b0_i++;
else if (L > imbe_param->num_harms)
b0_i--;
if (b0_i < 0 || b0_i > b0_lmax) {
if (b0_i < 0 || b0_i >= b0_lmax) {
fprintf(stderr, "encode error2 b0_i %d\n", b0_i);
return;
}
@ -238,9 +238,11 @@ static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp,
float log_l_w0 = 0.5 * log2f(num_harms_f * AmbeW0table[b[0]] * 2.0 * M_PI) + 2.289;
float lsa[NUM_HARMS_MAX];
float lsa_sum = 0.0;
bool hasSpectralEnergy = false;
for (int i1 = 0; i1 < imbe_param->num_harms; i1++) {
float sa = (float)imbe_param->sa[i1];
hasSpectralEnergy |= sa >= 1.0f;
if (sa < 1) sa = 1.0;
if (imbe_param->v_uv_dsn[i1])
lsa[i1] = log_l_2 + log2f(sa);
@ -255,13 +257,17 @@ static void encodeAMBE(const IMBE_PARAM* imbe_param, int b[], mbe_parms* cur_mp,
diff_gain -= gainAdjust;
float error;
int error_index;
int max_dg = 32;
for (int i1 = 0; i1 < max_dg; i1++) {
float diff = fabsf(diff_gain - AmbeDg[i1]);
if ((i1 == 0) || (diff < error)) {
error = diff;
error_index = i1;
int error_index = 0;
// The logarithm floor must not create transmitted gain when analysis
// produced no harmonic energy at all.
if (hasSpectralEnergy) {
int max_dg = 32;
for (int i1 = 0; i1 < max_dg; i1++) {
float diff = fabsf(diff_gain - AmbeDg[i1]);
if ((i1 == 0) || (diff < error)) {
error = diff;
error_index = i1;
}
}
}
@ -644,13 +650,14 @@ void MBEEncoder::encode(int16_t* samples, uint8_t* codeword)
int16_t frame_vector[8]; // result ignored
// Apply IMBE analysis gain before encoding the current frame. AMBE applies
// the same property directly when selecting its current gain index below.
if (m_mbeMode == ENCODE_88BIT_IMBE)
m_vocoder.set_gain_adjust(m_gainAdjust);
// first do speech analysis to generate mbe model parameters
m_vocoder.imbe_encode(frame_vector, samples);
if (m_mbeMode == ENCODE_88BIT_IMBE) {
if (m_gainAdjust >= 1.0f) {
m_vocoder.set_gain_adjust(m_gainAdjust);
}
uint32_t offset = 0U;
int16_t mask = 0x0800;

@ -57,6 +57,7 @@ void imbe_vocoder::decode(IMBE_PARAM* imbe_param, Word16* frame_vector, Word16*
v_uv_decode(imbe_param);
sa_decode(imbe_param);
sa_enh(imbe_param);
prepareRandomFrame();
v_synt(imbe_param, snd);
uv_synt(imbe_param, snd_tmp);

@ -61,3 +61,31 @@ imbe_vocoder::imbe_vocoder(void) :
decode_init(&my_imbe_param);
encode_init();
}
/* Generates the next stream-local pseudo-random sample. */
Word16 imbe_vocoder::randGen(void)
{
UWord32 lo = 16807U * (seed & 0xFFFFU);
const UWord32 hi = 16807U * (seed >> 16);
lo += (hi & 0x7FFFU) << 16;
lo += hi >> 15;
if (lo > 0x7FFFFFFFU)
lo -= 0x7FFFFFFFU;
seed = lo;
return static_cast<Word16>(lo);
}
/* Prepares stream-local random excitation without data-dependent advancement. */
void imbe_vocoder::prepareRandomFrame(void)
{
for (Word16 i = 0; i < NUM_HARMS_MAX; i++)
phaseRandom[i] = randGen();
for (Word16 i = 0; i < FFTLENGTH; i++)
unvoicedRandom[i] = randGen();
}

@ -66,6 +66,8 @@ private:
/* data items originally static (moved from individual c++ sources) */
Word16 prev_pitch, prev_prev_pitch, prev_e_p, prev_prev_e_p;
UWord32 seed;
Word16 phaseRandom[NUM_HARMS_MAX];
Word16 unvoicedRandom[FFTLENGTH];
Word16 num_harms_prev1;
Word32 sa_prev1[NUM_HARMS_MAX + 2];
Word16 num_harms_prev2;
@ -92,6 +94,15 @@ private:
void dct(Word16 *in, Word16 m_lim, Word16 i_lim, Word16 *out);
void fft_init(void);
void fft(Word16 *datam1, Word16 nn, Word16 isign);
/**
* @brief Generates the next pseudo-random sample for this vocoder stream.
* @returns Word16 Signed Q1.16 pseudo-random sample.
*/
Word16 randGen(void);
/**
* @brief Prepares a fixed pseudo-random excitation schedule for one frame.
*/
void prepareRandomFrame(void);
void encode(IMBE_PARAM *imbe_param, Word16 *frame_vector, Word16 *snd);
void pitch_est_init(void);
Word32 autocorr(Word16 *sigin, Word16 shift, Word16 scale_shift);

@ -37,30 +37,31 @@
// Constants
// ---------------------------------------------------------------------------
static const UWord16 min_max_tbl[203] =
/** Number of half-sample pitch candidates from 20 through 122 samples. */
static const Word16 PITCH_CANDIDATE_COUNT = 205;
/** Twice the minimum supported pitch, used to convert candidate indices. */
static const Word16 PITCH_INDEX_OFFSET = 40;
/** Initial prediction-error denominator for the period-20 candidate. */
static const Word16 PITCH_DEN_START_Q1_15 = 0x705C;
/** Maximum three-frame error for accepting an unambiguous pitch submultiple. */
static const Word16 SUBMULTIPLE_ERROR_THRESHOLD_Q4_12 = 0x019A;
/* Returns the lower index of the allowed 20-percent pitch-tracking range. */
static Word16 pitchMinIndex(Word16 index)
{
0x0008, 0x0009, 0x000a, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0012, 0x0013,
0x0014, 0x0115, 0x0216, 0x0218, 0x0319, 0x041a, 0x051b, 0x061c, 0x061e, 0x071f,
0x0820, 0x0921, 0x0a22, 0x0a24, 0x0b25, 0x0c26, 0x0d27, 0x0e28, 0x0e2a, 0x0f2b,
0x102c, 0x112d, 0x122e, 0x1230, 0x1331, 0x1432, 0x1533, 0x1634, 0x1636, 0x1737,
0x1838, 0x1939, 0x1a3a, 0x1a3c, 0x1b3d, 0x1c3e, 0x1d3f, 0x1e40, 0x1e42, 0x1f43,
0x2044, 0x2145, 0x2246, 0x2248, 0x2349, 0x244a, 0x254b, 0x264c, 0x264e, 0x274f,
0x2850, 0x2951, 0x2a52, 0x2a54, 0x2b55, 0x2c56, 0x2d57, 0x2e58, 0x2e5a, 0x2f5b,
0x305c, 0x315d, 0x325e, 0x3260, 0x3361, 0x3462, 0x3563, 0x3664, 0x3666, 0x3767,
0x3868, 0x3969, 0x3a6a, 0x3a6c, 0x3b6d, 0x3c6e, 0x3d6f, 0x3e70, 0x3e72, 0x3f73,
0x4074, 0x4175, 0x4276, 0x4278, 0x4379, 0x447a, 0x457b, 0x467c, 0x467e, 0x477f,
0x4880, 0x4981, 0x4a82, 0x4a84, 0x4b85, 0x4c86, 0x4d87, 0x4e88, 0x4e8a, 0x4f8b,
0x508c, 0x518d, 0x528e, 0x5290, 0x5391, 0x5492, 0x5593, 0x5694, 0x5696, 0x5797,
0x5898, 0x5999, 0x5a9a, 0x5a9c, 0x5b9d, 0x5c9e, 0x5d9f, 0x5ea0, 0x5ea2, 0x5fa3,
0x60a4, 0x61a5, 0x62a6, 0x62a8, 0x63a9, 0x64aa, 0x65ab, 0x66ac, 0x66ae, 0x67af,
0x68b0, 0x69b1, 0x6ab2, 0x6ab4, 0x6bb5, 0x6cb6, 0x6db7, 0x6eb8, 0x6eba, 0x6fbb,
0x70bc, 0x71bd, 0x72be, 0x72c0, 0x73c1, 0x74c2, 0x75c3, 0x76c4, 0x76c6, 0x77c7,
0x78c8, 0x79c9, 0x7aca, 0x7aca, 0x7bca, 0x7cca, 0x7dca, 0x7eca, 0x7eca, 0x7fca,
0x80ca, 0x81ca, 0x82ca, 0x82ca, 0x83ca, 0x84ca, 0x85ca, 0x86ca, 0x86ca, 0x87ca,
0x88ca, 0x89ca, 0x8aca, 0x8aca, 0x8bca, 0x8cca, 0x8dca, 0x8eca, 0x8eca, 0x8fca,
0x90ca, 0x91ca, 0x92ca, 0x92ca, 0x93ca, 0x94ca, 0x95ca, 0x96ca, 0x96ca, 0x97ca,
0x98ca, 0x99ca, 0x9aca
};
const Word16 pitch = index + PITCH_INDEX_OFFSET;
const Word16 minimum = ((4 * pitch + 4) / 5) - PITCH_INDEX_OFFSET;
return minimum > 0 ? minimum : 0;
}
/* Returns the upper index of the allowed 20-percent pitch-tracking range. */
static Word16 pitchMaxIndex(Word16 index)
{
const Word16 pitch = index + PITCH_INDEX_OFFSET;
const Word16 maximum = ((6 * pitch) / 5) - PITCH_INDEX_OFFSET;
return maximum < PITCH_CANDIDATE_COUNT ? maximum : PITCH_CANDIDATE_COUNT - 1;
}
// ---------------------------------------------------------------------------
// Private Class Members
@ -68,7 +69,7 @@ static const UWord16 min_max_tbl[203] =
void imbe_vocoder::pitch_est_init(void)
{
prev_pitch = prev_prev_pitch = 158; // 100
prev_pitch = prev_prev_pitch = 160; // 100
prev_e_p = prev_prev_e_p = 0;
}
@ -89,7 +90,7 @@ void imbe_vocoder::e_p(Word16* sigin, Word16* res_buf)
Word16 i, j, den_part_acc, tmp;
Word32 L_sum, L_num, L_den, L_e0, L_tmp;
Word16 sig_wndwed[PITCH_EST_FRAME];
Word32 corr[259];
Word32 corr[261];
Word16 index_beg, index_step;
Word16 scale_shift;
@ -117,28 +118,28 @@ void imbe_vocoder::e_p(Word16* sigin, Word16* res_buf)
for (i = 0; i < PITCH_EST_FRAME; i++)
L_e0 = L_add(L_e0, L_shr(L_mult(sig_wndwed[i], sig_wndwed[i]), scale_shift)); // sum(s^2 * wi^4)
// Calculate correlation for time shift in range 21...150 with step 0.5
// Calculate correlation for time shift in range 20...150 with step 0.5
// For integer shifts
for (tmp = 21, i = 0; tmp <= 150; tmp++, i += 2)
for (tmp = 20, i = 0; tmp <= 150; tmp++, i += 2)
corr[i] = autocorr(sig_wndwed, tmp, scale_shift);
// For intermediate shifts
for (i = 1; i < 258; i += 2)
for (i = 1; i < 260; i += 2)
corr[i] = L_shr(L_add(corr[i - 1], corr[i + 1]), 1);
// variable to calculate 1 - P * sum(wi ^4) in denominator
den_part_acc = CNST_0_8717_Q1_15;
den_part_acc = PITCH_DEN_START_Q1_15;
index_step = 42; // Note: 42 = 21 in Q15.1 format, so index_step will be used also as p in Q15.1 format
index_step = PITCH_INDEX_OFFSET; // also represents the pitch in Q15.1 format
index_beg = 0;
L_e0 = L_shr(L_e0, 7); // divide by 64 to compensate wi scaling
// p = 21...122 by step 0.5
for (i = 0; i < 203; i++) {
// p = 20...122 by step 0.5
for (i = 0; i < PITCH_CANDIDATE_COUNT; i++) {
// Calculate sum( corr ( n * p) )
L_tmp = 0;
j = index_beg;
while (j <= 258) {
while (j <= 260) {
L_tmp = L_add(L_tmp, corr[j]);
j += index_step;
}
@ -177,20 +178,21 @@ void imbe_vocoder::e_p(Word16* sigin, Word16* res_buf)
void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
{
Word16 e_p_arr0[203], e_p_arr1[203], e_p_arr2[203], e1p1_e2p2_est_save[203];
Word16 e_p_arr0[PITCH_CANDIDATE_COUNT], e_p_arr1[PITCH_CANDIDATE_COUNT];
Word16 e_p_arr2[PITCH_CANDIDATE_COUNT], e1p1_e2p2_est_save[PITCH_CANDIDATE_COUNT];
Word16 min_index, max_index, p, i, p_index;
UWord16 tmp = 0, p_fp;
UWord32 UL_tmp;
Word16 e_p_cur, pb, pf, ceb, s_tmp;
Word16 cef_est, cef, p0_est, p0, p1, p2, p1_max_index, p2_max_index, e1p1_e2p2_est, e1p1_e2p2;
Word16 e_p_arr2_min[203];
Word16 e_p_arr2_min[PITCH_CANDIDATE_COUNT];
// Calculate E(p) function for current and two future frames
e_p(&frames_buf[0], e_p_arr0);
// Look-Back Pitch Tracking
min_index = HI_BYTE(min_max_tbl[prev_pitch]);
max_index = LO_BYTE(min_max_tbl[prev_pitch]);
min_index = pitchMinIndex(prev_pitch);
max_index = pitchMaxIndex(prev_pitch);
p = pb = min_index;
e_p_cur = e_p_arr0[min_index];
@ -208,7 +210,7 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
prev_prev_e_p = prev_e_p;
prev_e_p = e_p_arr0[pb];
imbe_param->pitch = pb + 42; // Result in Q15.1 format
imbe_param->pitch = pb + PITCH_INDEX_OFFSET; // Result in Q15.1 format
imbe_param->e_p = prev_e_p;
return;
}
@ -223,9 +225,9 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
e1p1_e2p2 = 1;
p1 = 0;
while (p1 < 203) {
p2 = HI_BYTE(min_max_tbl[p1]);
p2_max_index = LO_BYTE(min_max_tbl[p1]);
while (p1 < PITCH_CANDIDATE_COUNT) {
p2 = pitchMinIndex(p1);
p2_max_index = pitchMaxIndex(p1);
s_tmp = e_p_arr2[p1];
while (p2 <= p2_max_index) {
if (e_p_arr2[p2] < s_tmp)
@ -235,10 +237,10 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
e_p_arr2_min[p1] = s_tmp;
p1++;
}
while (p0 < 203) {
while (p0 < PITCH_CANDIDATE_COUNT) {
e1p1_e2p2_est = e_p_arr1[p0] + e_p_arr2_min[p0];
p1 = HI_BYTE(min_max_tbl[p0]);
p1_max_index = LO_BYTE(min_max_tbl[p0]);
p1 = pitchMinIndex(p0);
p1_max_index = pitchMaxIndex(p0);
while (p1 <= p1_max_index) {
if (add(e_p_arr1[p1], e_p_arr2_min[p1]) < e1p1_e2p2_est)
e1p1_e2p2_est = add(e_p_arr1[p1], e_p_arr2_min[p1]);
@ -255,18 +257,18 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
pf = p0_est;
// Sub-multiples analysis
if (pf >= 42) // Check if Sub-multiples are possible
if (pf >= 44) // Check if Sub-multiples are possible
{
if (pf < 84)
if (pf < 86)
i = 1;
else if (pf < 126)
else if (pf < 128)
i = 2;
else if (pf < 168)
else if (pf < 170)
i = 3;
else
i = 4;
p_fp = (pf + 42) << 8; // Convert pitch estimation from array index to unsigned Q7.19 format
p_fp = (pf + PITCH_INDEX_OFFSET) << 8; // Convert pitch estimation from array index to unsigned Q7.19 format
while (i--) {
switch (i) {
@ -289,8 +291,7 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
break;
}
p_index = ((tmp + 0x0080) >> 8) - 42; // Convert fixed-point pitch value to integer array index with rounding
p_index = ((tmp + 0x0080) >> 8) - PITCH_INDEX_OFFSET; // Convert fixed-point pitch to an array index
cef = add(e_p_arr0[p_index], e1p1_e2p2_est_save[p_index]);
if (cef <= CNST_0_85_Q4_12 && mult_r(cef, CNST_0_5882_Q1_15) <= cef_est) // 1/1.7 = 0.5882
@ -305,7 +306,7 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
break;
}
if (cef <= CNST_0_05_Q4_12) {
if (cef <= SUBMULTIPLE_ERROR_THRESHOLD_Q4_12) {
pf = p_index;
break;
}
@ -325,6 +326,6 @@ void imbe_vocoder::pitch_est(IMBE_PARAM* imbe_param, Word16* frames_buf)
prev_e_p = e_p_arr0[p];
imbe_param->pitch = p + 42; // Result in Q15.1 format
imbe_param->pitch = p + PITCH_INDEX_OFFSET; // Result in Q15.1 format
imbe_param->e_p = prev_e_p;
}

@ -37,7 +37,9 @@
// ---------------------------------------------------------------------------
#define PITCH_REF_FRAME 221
#define MIN_INDEX 50
#define MIN_INDEX 50
/** Minimum encodable refined pitch, 19.875 samples in Q8.8 format. */
#define MIN_PITCH_Q8_8 0x13E0
// ---------------------------------------------------------------------------
// Global Functions
@ -45,113 +47,119 @@
void pitch_ref(IMBE_PARAM* imbe_param, Cmplx16* fft_buf)
{
Word16 i, j, index_a_save, pitch_est, tmp, shift, index_wr, up_lim;
Word16 i, j, index_a_save, pitch_est, pitch_center, pitch_passes, pass, tmp, shift, index_wr, up_lim;
Cmplx16 sp_rec[FFTLENGTH / 2];
Word32 fund_freq, fund_freq_2, fund_freq_acc_a, fund_freq_acc_b, fund_freq_acc, L_tmp, amp_re_acc, amp_im_acc, L_sum, L_diff_min;
Word16 ha, hb, index_a, index_b, index_tbl[20], it_ind, re_tmp, im_tmp, pitch_cand = 0;
Word32 fund_freq_cand = 0;
pitch_est = shl(imbe_param->pitch, 7); // Convert to Q8.8
pitch_est = sub(pitch_est, CNST_1_125_Q8_8); // Sub 1.125 = 9/8
pitch_center = shl(imbe_param->pitch, 7); // Convert to Q8.8
pitch_passes = imbe_param->pitch >= 76 && imbe_param->pitch <= 84 ? 2 : 1;
L_diff_min = MAX_32;
for (i = 0; i < 19; i++)
{
shift = norm_s(pitch_est);
tmp = shl(pitch_est, shift);
tmp = div_s(0x4000, tmp);
fund_freq = L_shl(tmp, shift + 11);
fund_freq_acc = fund_freq;
fund_freq_2 = L_shr(fund_freq, 1);
fund_freq_acc_a = L_sub(fund_freq, fund_freq_2);
fund_freq_acc_b = L_add(fund_freq, fund_freq_2);
// Calculate upper limit for spectrum reconstruction
up_lim = extract_h(L_shr((UWord32)CNST_0_9254_Q0_16 * pitch_est, 1)); // 0.9254/fund_freq
up_lim = sub(up_lim, CNST_0_5_Q8_8); // sub 0.5
up_lim = up_lim & 0xFF00; // extract fixed part
up_lim = mult(up_lim, extract_h(fund_freq));
up_lim = shr(up_lim, 1);
index_b = 0;
while (index_b <= up_lim)
{
ha = extract_h(fund_freq_acc_a);
hb = extract_h(fund_freq_acc_b);
index_a = (ha >> 8) + ((ha & 0xFF) ? 1 : 0);
index_b = (hb >> 8) + ((hb & 0xFF) ? 1 : 0);
if (index_b >= MIN_INDEX)
for (pass = 0; pass < pitch_passes; pass++) {
pitch_est = pass == 0 ? pitch_center : shr(pitch_center, 1);
pitch_est = sub(pitch_est, CNST_1_125_Q8_8); // Sub 1.125 = 9/8
if (pitch_est < MIN_PITCH_Q8_8)
pitch_est = MIN_PITCH_Q8_8;
for (i = 0; i < 19; i++) {
shift = norm_s(pitch_est);
tmp = shl(pitch_est, shift);
tmp = div_s(0x4000, tmp);
fund_freq = L_shl(tmp, shift + 11);
fund_freq_acc = fund_freq;
fund_freq_2 = L_shr(fund_freq, 1);
fund_freq_acc_a = L_sub(fund_freq, fund_freq_2);
fund_freq_acc_b = L_add(fund_freq, fund_freq_2);
// Calculate upper limit for spectrum reconstruction
up_lim = extract_h(L_shr((UWord32)CNST_0_9254_Q0_16 * pitch_est, 1)); // 0.9254/fund_freq
up_lim = sub(up_lim, CNST_0_5_Q8_8); // sub 0.5
up_lim = up_lim & 0xFF00; // extract fixed part
up_lim = mult(up_lim, extract_h(fund_freq));
up_lim = shr(up_lim, 1);
index_b = 0;
while (index_b <= up_lim)
{
L_tmp = L_shl(L_deposit_h(index_a), 8);
L_tmp = L_sub(L_tmp, fund_freq_acc);
L_tmp = L_add(L_tmp, 0x00020000); // for rounding purpose
L_tmp = L_shr(L_tmp, 2);
ha = extract_h(fund_freq_acc_a);
hb = extract_h(fund_freq_acc_b);
index_a = (ha >> 8) + ((ha & 0xFF) ? 1 : 0);
index_b = (hb >> 8) + ((hb & 0xFF) ? 1 : 0);
index_a_save = index_a;
it_ind = 0;
amp_re_acc = amp_im_acc = 0;
while (index_a < index_b)
if (index_b >= MIN_INDEX)
{
index_wr = extract_h(L_tmp);
if (index_wr < 0 && (L_tmp & 0xFFFF)) // truncating for negative number
index_wr = add(index_wr, 1);
index_wr = add(index_wr, 160);
index_tbl[it_ind++] = index_wr;
if (index_wr >= 0 && index_wr <= 320)
{
amp_re_acc = L_mac(amp_re_acc, fft_buf[index_a].re, wr_sp[index_wr]);
amp_im_acc = L_mac(amp_im_acc, fft_buf[index_a].im, wr_sp[index_wr]);
}
L_tmp = L_shl(L_deposit_h(index_a), 8);
L_tmp = L_sub(L_tmp, fund_freq_acc);
L_tmp = L_add(L_tmp, 0x00020000); // for rounding purpose
L_tmp = L_shr(L_tmp, 2);
index_a++;
L_tmp = L_add(L_tmp, 0x400000);
}
index_a_save = index_a;
it_ind = 0;
it_ind = 0;
index_a = index_a_save;
while (index_a < index_b)
{
index_wr = index_tbl[it_ind++];
if (index_wr < 0 || index_wr > 320)
amp_re_acc = amp_im_acc = 0;
while (index_a < index_b)
{
sp_rec[index_a].im = sp_rec[index_a].re = 0;
index_wr = extract_h(L_tmp);
if (index_wr < 0 && (L_tmp & 0xFFFF)) // truncating for negative number
index_wr = add(index_wr, 1);
index_wr = add(index_wr, 160);
index_tbl[it_ind++] = index_wr;
if (index_wr >= 0 && index_wr <= 320)
{
amp_re_acc = L_mac(amp_re_acc, fft_buf[index_a].re, wr_sp[index_wr]);
amp_im_acc = L_mac(amp_im_acc, fft_buf[index_a].im, wr_sp[index_wr]);
}
index_a++;
L_tmp = L_add(L_tmp, 0x400000);
}
else
it_ind = 0;
index_a = index_a_save;
while (index_a < index_b)
{
sp_rec[index_a].im = mult(mult(extract_h(amp_im_acc), wr_sp[index_wr]), 0x6666);
sp_rec[index_a].re = mult(mult(extract_h(amp_re_acc), wr_sp[index_wr]), 0x6666);
index_wr = index_tbl[it_ind++];
if (index_wr < 0 || index_wr > 320)
{
sp_rec[index_a].im = sp_rec[index_a].re = 0;
}
else
{
sp_rec[index_a].im = mult(mult(extract_h(amp_im_acc), wr_sp[index_wr]), 0x6666);
sp_rec[index_a].re = mult(mult(extract_h(amp_re_acc), wr_sp[index_wr]), 0x6666);
}
index_a++;
}
index_a++;
}
fund_freq_acc_a = L_add(fund_freq_acc_a, fund_freq);
fund_freq_acc_b = L_add(fund_freq_acc_b, fund_freq);
fund_freq_acc = L_add(fund_freq_acc, fund_freq);
}
fund_freq_acc_a = L_add(fund_freq_acc_a, fund_freq);
fund_freq_acc_b = L_add(fund_freq_acc_b, fund_freq);
fund_freq_acc = L_add(fund_freq_acc, fund_freq);
}
L_sum = 0;
for (j = MIN_INDEX; j <= up_lim; j++)
{
re_tmp = sub(fft_buf[j].re, sp_rec[j].re);
im_tmp = sub(fft_buf[j].im, sp_rec[j].im);
L_sum = L_mac(L_sum, re_tmp, re_tmp);
L_sum = L_mac(L_sum, im_tmp, im_tmp);
}
L_sum = 0;
for (j = MIN_INDEX; j <= up_lim; j++)
{
re_tmp = sub(fft_buf[j].re, sp_rec[j].re);
im_tmp = sub(fft_buf[j].im, sp_rec[j].im);
L_sum = L_mac(L_sum, re_tmp, re_tmp);
L_sum = L_mac(L_sum, im_tmp, im_tmp);
}
if (L_sum < L_diff_min)
{
L_diff_min = L_sum;
pitch_cand = pitch_est;
fund_freq_cand = fund_freq;
}
if (L_sum < L_diff_min)
{
L_diff_min = L_sum;
pitch_cand = pitch_est;
fund_freq_cand = fund_freq;
pitch_est = add(pitch_est, CNST_0_125_Q8_8); // Add 0.125 = 1/8
}
pitch_est = add(pitch_est, CNST_0_125_Q8_8); // Add 0.125 = 1/8
}
imbe_param->ref_pitch = pitch_cand;

@ -24,7 +24,6 @@
#include "vocoder/imbe/imbe.h"
#include "vocoder/imbe/aux_sub.h"
#include "vocoder/imbe/math_sub.h"
#include "vocoder/imbe/rand_gen.h"
#include "vocoder/imbe/tbls.h"
#include "vocoder/imbe/imbe_vocoder.h"
@ -74,8 +73,8 @@ void imbe_vocoder::uv_synt(IMBE_PARAM* imbe_param, Word16* snd)
}
else {
while (index_a < index_b) {
Uw[index_a].re = mult(sa, rand_gen());
Uw[index_a].im = mult(sa, rand_gen());
Uw[index_a].re = mult(sa, unvoicedRandom[2 * index_a]);
Uw[index_a].im = mult(sa, unvoicedRandom[2 * index_a + 1]);
//Uw[index_a].re = sa;
//Uw[index_a].im = sa;

@ -24,7 +24,6 @@
#include "vocoder/imbe/imbe.h"
#include "vocoder/imbe/aux_sub.h"
#include "vocoder/imbe/math_sub.h"
#include "vocoder/imbe/rand_gen.h"
#include "vocoder/imbe/tbls.h"
#include "vocoder/imbe/imbe_vocoder.h"
@ -43,7 +42,7 @@ void imbe_vocoder::v_synt_init(void)
Word16 i;
for (i = 0; i < NUM_HARMS_MAX; i++) {
ph_mem[i] = L_deposit_h(rand_gen());
ph_mem[i] = L_deposit_h(randGen());
vu_dsn_prev[i] = 0;
}
@ -99,10 +98,10 @@ void imbe_vocoder::v_synt(IMBE_PARAM* imbe_param, Word16* snd)
if (i > num_harms_max_4) {
if (num_uv == num_harms) {
dph[i] = L_deposit_h(rand_gen());
dph[i] = L_deposit_h(phaseRandom[i]);
}
else {
L_tmp = L_mult(rand_gen(), num_harms_inv);
L_tmp = L_mult(phaseRandom[i], num_harms_inv);
dph[i] = L_shr(L_tmp, 15 - num_harms_sh) * num_uv;
}
ph_mem[i] += dph[i];

@ -49,7 +49,8 @@
#define CNST_0_0025_Q1_15 0x0051
#define CNST_0_25_Q1_15 0x2000
#define CNST_PI_4_Q1_15 0x6488
#define CNST_0_55_Q4_12 0x08CD
/** Pitch-error threshold above which only the lowest band may remain voiced. */
#define PITCH_ERROR_UNVOICED_Q4_12 0x0800
// ---------------------------------------------------------------------------
// Globals
@ -209,7 +210,7 @@ void imbe_vocoder::v_uv_det(IMBE_PARAM* imbe_param, Cmplx16* fft_buf)
// =========== v/uv determination threshold function ==
if (num_harms_cnt == 0) // calculate one time per band
{
if (imbe_param->e_p > CNST_0_55_Q4_12 && band_cnt >= 1)
if (imbe_param->e_p > PITCH_ERROR_UNVOICED_Q4_12 && band_cnt >= 1)
dsn_thr = 0;
else if (v_uv_dsn[band_cnt] == 1)
dsn_thr = mult(M_fcn, sub(CNST_0_5625_Q1_15, mult(CNST_0_1741_Q1_15, extract_h(fund_fr_acc))));

@ -23,7 +23,6 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
@ -41,16 +40,21 @@
/* A pseudo - random float between[0.0, 1.0]. */
static float mbe_rand()
static float mbe_rand(mbe_parms* cur_mp)
{
return ((float)rand() / (float)RAND_MAX);
uint32_t value = cur_mp->randomSeed;
value ^= value << 13;
value ^= value >> 17;
value ^= value << 5;
cur_mp->randomSeed = value;
return (float)(value & 0x00FFFFFFU) / (float)0x01000000U;
}
/* A pseudo-random float between [-pi, +pi]. */
static float mbe_rand_phase()
static float mbe_rand_phase(mbe_parms* cur_mp)
{
return mbe_rand() * (((float)M_PI) * 2.0F) - ((float)M_PI);
return mbe_rand(cur_mp) * (((float)M_PI) * 2.0F) - ((float)M_PI);
}
/* */
@ -65,6 +69,7 @@ void mbe_moveMbeParms(mbe_parms* cur_mp, mbe_parms* prev_mp)
prev_mp->Ml[0] = (float)0;
prev_mp->gamma = cur_mp->gamma;
prev_mp->repeat = cur_mp->repeat;
prev_mp->randomSeed = cur_mp->randomSeed;
for (l = 0; l <= 56; l++) {
prev_mp->Ml[l] = cur_mp->Ml[l];
@ -87,6 +92,7 @@ void mbe_useLastMbeParms(mbe_parms* cur_mp, mbe_parms* prev_mp)
cur_mp->Ml[0] = (float)0;
cur_mp->gamma = prev_mp->gamma;
cur_mp->repeat = prev_mp->repeat;
cur_mp->randomSeed = prev_mp->randomSeed;
for (l = 0; l <= 56; l++) {
cur_mp->Ml[l] = prev_mp->Ml[l];
@ -117,6 +123,7 @@ void mbe_initMbeParms(mbe_parms* cur_mp, mbe_parms* prev_mp, mbe_parms* prev_mp_
}
prev_mp->repeat = 0;
prev_mp->randomSeed = 1U;
mbe_moveMbeParms(prev_mp, cur_mp);
mbe_moveMbeParms(prev_mp, prev_mp_enhanced);
}
@ -295,7 +302,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
cur_mp->PHIl[l] = cur_mp->PSIl[l];
}
else {
cur_mp->PHIl[l] = cur_mp->PSIl[l] + ((numUv * mbe_rand_phase()) / cur_mp->L);
cur_mp->PHIl[l] = cur_mp->PSIl[l] + ((numUv * mbe_rand_phase(cur_mp)) / cur_mp->L);
}
}
@ -306,7 +313,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
Ss = aout_buf;
// init random phase
for (i = 0; i < uvquality; i++) {
rphase[i] = mbe_rand_phase();
rphase[i] = mbe_rand_phase(cur_mp);
}
for (n = 0; n < N; n++) {
@ -321,7 +328,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
C3 = C3 + cosf((cw0 * (float)n * ((float)l + ((float)i * uvstep) - uvoffset)) + rphase[i]);
if (cw0l > uvthreshold)
{
C3 = C3 + ((cw0l - uvthreshold) * uvrand * mbe_rand());
C3 = C3 + ((cw0l - uvthreshold) * uvrand * mbe_rand(cur_mp));
}
}
C3 = C3 * uvsine * Ws[n] * cur_mp->Ml[l] * qfactor;
@ -333,7 +340,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
Ss = aout_buf;
// init random phase
for (i = 0; i < uvquality; i++) {
rphase[i] = mbe_rand_phase();
rphase[i] = mbe_rand_phase(cur_mp);
}
for (n = 0; n < N; n++) {
@ -346,7 +353,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
for (i = 0; i < uvquality; i++) {
C3 = C3 + cosf((pw0 * (float)n * ((float)l + ((float)i * uvstep) - uvoffset)) + rphase[i]);
if (pw0l > uvthreshold) {
C3 = C3 + ((pw0l - uvthreshold) * uvrand * mbe_rand());
C3 = C3 + ((pw0l - uvthreshold) * uvrand * mbe_rand(cur_mp));
}
}
C3 = C3 * uvsine * Ws[n + N] * prev_mp->Ml[l] * qfactor;
@ -393,12 +400,12 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
Ss = aout_buf;
// init random phase
for (i = 0; i < uvquality; i++) {
rphase[i] = mbe_rand_phase();
rphase[i] = mbe_rand_phase(cur_mp);
}
// init random phase
for (i = 0; i < uvquality; i++) {
rphase2[i] = mbe_rand_phase();
rphase2[i] = mbe_rand_phase(cur_mp);
}
for (n = 0; n < N; n++) {
@ -408,7 +415,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
for (i = 0; i < uvquality; i++) {
C3 = C3 + cosf((pw0 * (float)n * ((float)l + ((float)i * uvstep) - uvoffset)) + rphase[i]);
if (pw0l > uvthreshold) {
C3 = C3 + ((pw0l - uvthreshold) * uvrand * mbe_rand());
C3 = C3 + ((pw0l - uvthreshold) * uvrand * mbe_rand(cur_mp));
}
}
@ -419,7 +426,7 @@ void mbe_synthesizeSpeechF(float* aout_buf, mbe_parms* cur_mp, mbe_parms* prev_m
for (i = 0; i < uvquality; i++) {
C4 = C4 + cosf((cw0 * (float)n * ((float)l + ((float)i * uvstep) - uvoffset)) + rphase2[i]);
if (cw0l > uvthreshold) {
C4 = C4 + ((cw0l - uvthreshold) * uvrand * mbe_rand());
C4 = C4 + ((cw0l - uvthreshold) * uvrand * mbe_rand(cur_mp));
}
}

@ -22,6 +22,8 @@
#if !defined(__MBE_H__)
#define __MBE_H__
#include <stdint.h>
/*
* Copyright (C) 2010 mbelib Author
* GPG Key ID: 0xEA5EFE2C (9E7A 5527 9CDC EBF7 BF1B D772 4F98 E863 EA5E FE2C)
@ -71,6 +73,8 @@ struct mbe_parameters
float gamma;
int un;
int repeat;
/** Stream-local pseudo-random excitation state. */
uint32_t randomSeed;
};
typedef struct mbe_parameters mbe_parms;

@ -10,6 +10,7 @@
#include "vocoder/MBEDecoder.h"
#include <catch2/catch_test_macros.hpp>
#include <array>
#include <cstring>
#include <memory>
@ -48,3 +49,109 @@ TEST_CASE("Half-rate FEC consumes exactly 49 source bits", "[dmr][vocoder][bound
REQUIRE(::memcmp(output + 1U, repeated, 9U) == 0);
}
}
TEST_CASE("AMBE decoder pseudo-random state is isolated per stream", "[dmr][vocoder][ambe]") {
vocoder::MBEEncoder encoder(vocoder::ENCODE_DMR_AMBE);
vocoder::MBEDecoder decoderA(vocoder::DECODE_DMR_AMBE);
vocoder::MBEDecoder decoderB(vocoder::DECODE_DMR_AMBE);
std::array<int16_t, 160U> pcm{};
std::array<uint8_t, 49U> bits{};
std::array<int16_t, 160U> outputA{};
std::array<int16_t, 160U> outputB{};
uint32_t noise = 0x1A2B3C4DU;
for (uint32_t frame = 0U; frame < 8U; frame++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++) {
noise ^= noise << 13;
noise ^= noise >> 17;
noise ^= noise << 5;
pcm[sample] = static_cast<int16_t>(noise >> 16);
}
encoder.encodeBits(pcm.data(), bits.data());
REQUIRE(decoderA.decodeBits(bits.data(), outputA.data()) == 0);
REQUIRE(decoderB.decodeBits(bits.data(), outputB.data()) == 0);
REQUIRE(::memcmp(outputA.data(), outputB.data(), sizeof(outputA)) == 0);
}
}
TEST_CASE("AMBE encoder applies gain adjustment to analyzed speech", "[dmr][vocoder][ambe]") {
vocoder::MBEEncoder reference(vocoder::ENCODE_DMR_AMBE);
vocoder::MBEEncoder adjusted(vocoder::ENCODE_DMR_AMBE);
std::array<int16_t, 160U> pcm{};
std::array<uint8_t, 49U> referenceBits{};
std::array<uint8_t, 49U> adjustedBits{};
for (uint32_t sample = 0U; sample < pcm.size(); sample++)
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>((sample * 211U) & 0x3FFFU) - 0x2000);
adjusted.setGainAdjust(2.0f);
bool differs = false;
for (uint32_t frame = 0U; frame < 8U; frame++) {
reference.encodeBits(pcm.data(), referenceBits.data());
adjusted.encodeBits(pcm.data(), adjustedBits.data());
differs |= referenceBits != adjustedBits;
}
REQUIRE(differs);
}
TEST_CASE("AMBE encoder uses minimum gain for zero spectral energy", "[dmr][vocoder][ambe]") {
vocoder::MBEEncoder encoder(vocoder::ENCODE_DMR_AMBE);
std::array<int16_t, 160U> pcm{};
std::array<uint8_t, 49U> bits{};
for (uint32_t frame = 0U; frame < 4U; frame++) {
encoder.encodeBits(pcm.data(), bits.data());
const uint32_t gainIndex = (bits[8U] << 4U) | (bits[9U] << 3U) |
(bits[10U] << 2U) | (bits[11U] << 1U) | bits[36U];
REQUIRE(gainIndex == 0U);
}
}
TEST_CASE("AMBE encoder resolves a period-20 fundamental at its lower pitch boundary", "[dmr][vocoder][ambe]") {
vocoder::MBEEncoder encoder(vocoder::ENCODE_DMR_AMBE);
std::array<int16_t, 160U> pcm{};
std::array<uint8_t, 49U> bits{};
for (uint32_t frame = 0U; frame < 12U; frame++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++) {
const uint32_t phase = (frame * pcm.size() + sample) % 20U;
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>(phase * 12000U / 20U) - 6000);
}
encoder.encodeBits(pcm.data(), bits.data());
const uint32_t pitchIndex = (bits[0U] << 6U) | (bits[1U] << 5U) |
(bits[2U] << 4U) | (bits[3U] << 3U) | (bits[37U] << 2U) |
(bits[38U] << 1U) | bits[39U];
REQUIRE(pitchIndex < 120U);
if (frame >= 8U)
REQUIRE(pitchIndex == 0U);
}
}
TEST_CASE("Raw AMBE decoding uses the float decoder output scale", "[dmr][vocoder][ambe]") {
vocoder::MBEEncoder encoder(vocoder::ENCODE_DMR_AMBE);
vocoder::MBEDecoder decoder(vocoder::DECODE_DMR_AMBE);
std::array<int16_t, 160U> pcm{};
std::array<int16_t, 160U> decoded{};
std::array<uint8_t, 49U> bits{};
for (uint32_t frame = 0U; frame < 12U; frame++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++) {
const uint32_t phase = (frame * pcm.size() + sample) % 40U;
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>(phase * 12000U / 40U) - 6000);
}
encoder.encodeBits(pcm.data(), bits.data());
REQUIRE(decoder.decodeBits(bits.data(), decoded.data()) == 0);
if (frame >= 8U) {
uint32_t clippedSamples = 0U;
for (const int16_t output : decoded)
clippedSamples += output == 32760 || output == -32760;
REQUIRE(clippedSamples == 0U);
}
}
}

@ -0,0 +1,147 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Test Suite
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*/
#include "vocoder/imbe/imbe_vocoder.h"
#include "vocoder/MBEDecoder.h"
#include "vocoder/MBEEncoder.h"
#include <catch2/catch_test_macros.hpp>
#include <array>
#include <cstdint>
#include <cstring>
static void packFrameVector(const std::array<int16_t, 8U>& frame, std::array<uint8_t, 11U>& packed)
{
static const std::array<uint32_t, 8U> WORD_WIDTHS = {12U, 12U, 12U, 12U, 11U, 11U, 11U, 7U};
uint32_t offset = 0U;
packed.fill(0U);
for (uint32_t word = 0U; word < frame.size(); word++) {
uint16_t mask = static_cast<uint16_t>(1U << (WORD_WIDTHS[word] - 1U));
for (uint32_t bit = 0U; bit < WORD_WIDTHS[word]; bit++, offset++, mask >>= 1U) {
if ((static_cast<uint16_t>(frame[word]) & mask) != 0U)
packed[offset / 8U] |= static_cast<uint8_t>(1U << (7U - offset % 8U));
}
}
}
TEST_CASE("IMBE decoder pseudo-random state is isolated per stream", "[p25][vocoder][imbe]") {
imbe_vocoder encoder;
imbe_vocoder decoderA;
imbe_vocoder decoderB;
std::array<int16_t, 160U> pcm{};
std::array<int16_t, 8U> frame{};
std::array<int16_t, 160U> outputA{};
std::array<int16_t, 160U> outputB{};
for (uint32_t frameIndex = 0U; frameIndex < 8U; frameIndex++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++)
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>((sample * 97U + frameIndex * 313U) & 0x3FFFU) - 0x2000);
encoder.imbe_encode(frame.data(), pcm.data());
decoderA.imbe_decode(frame.data(), outputA.data());
decoderB.imbe_decode(frame.data(), outputB.data());
REQUIRE(::memcmp(outputA.data(), outputB.data(), sizeof(outputA)) == 0);
}
}
TEST_CASE("IMBE encoder selects the fundamental of stable harmonic signals", "[p25][vocoder][imbe]") {
static const std::array<uint32_t, 9U> PERIODS = {20U, 21U, 25U, 30U, 40U, 50U, 60U, 80U, 100U};
for (const uint32_t period : PERIODS) {
imbe_vocoder encoder;
std::array<int16_t, 160U> pcm{};
std::array<int16_t, 8U> frame{};
for (uint32_t frameIndex = 0U; frameIndex < 12U; frameIndex++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++) {
const uint32_t phase = (frameIndex * pcm.size() + sample) % period;
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>(phase * 12000U / period) - 6000);
}
encoder.imbe_encode(frame.data(), pcm.data());
if (frameIndex >= 8U) {
INFO("period=" << period << " frame=" << frameIndex);
REQUIRE(encoder.param()->b_vec[0] == static_cast<int16_t>(2U * period - 39U));
uint32_t voicedHarmonics = 0U;
for (int16_t harmonic = 0; harmonic < encoder.param()->num_harms; harmonic++)
voicedHarmonics += encoder.param()->v_uv_dsn[harmonic] != 0;
REQUIRE(voicedHarmonics == static_cast<uint32_t>(encoder.param()->num_harms));
}
}
}
}
TEST_CASE("MBEDecoder uses the fixed-point decoder for 88-bit IMBE", "[p25][vocoder][imbe]") {
imbe_vocoder encoder;
imbe_vocoder fixedDecoder;
vocoder::MBEDecoder decoder(vocoder::DECODE_88BIT_IMBE);
std::array<int16_t, 160U> pcm{};
std::array<int16_t, 8U> frame{};
std::array<uint8_t, 11U> packed{};
std::array<int16_t, 160U> fixedOutput{};
std::array<int16_t, 160U> decoderOutput{};
for (uint32_t frameIndex = 0U; frameIndex < 8U; frameIndex++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++)
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>((sample * 83U + frameIndex * 271U) & 0x3FFFU) - 0x2000);
encoder.imbe_encode(frame.data(), pcm.data());
packFrameVector(frame, packed);
fixedDecoder.imbe_decode(frame.data(), fixedOutput.data());
REQUIRE(decoder.decode(packed.data(), decoderOutput.data()) == 0);
REQUIRE(::memcmp(fixedOutput.data(), decoderOutput.data(), sizeof(fixedOutput)) == 0);
}
}
TEST_CASE("IMBE analysis does not classify broadband noise as harmonic speech", "[p25][vocoder][imbe]") {
imbe_vocoder encoder;
std::array<int16_t, 160U> pcm{};
std::array<int16_t, 8U> frame{};
uint32_t noise = 0x12345678U;
for (uint32_t frameIndex = 0U; frameIndex < 16U; frameIndex++) {
for (uint32_t sample = 0U; sample < pcm.size(); sample++) {
noise ^= noise << 13;
noise ^= noise >> 17;
noise ^= noise << 5;
pcm[sample] = static_cast<int16_t>(noise >> 16);
}
encoder.imbe_encode(frame.data(), pcm.data());
if (frameIndex >= 5U) {
const IMBE_PARAM* parameters = encoder.param();
uint32_t voicedHarmonics = 0U;
for (int16_t harmonic = 0; harmonic < parameters->num_harms; harmonic++)
voicedHarmonics += parameters->v_uv_dsn[harmonic] != 0;
INFO("frame=" << frameIndex << " pitchError=" << parameters->e_p);
REQUIRE(voicedHarmonics <= 3U);
}
}
}
TEST_CASE("IMBE encoder applies gain adjustment to the first frame", "[p25][vocoder][imbe]") {
vocoder::MBEEncoder reference(vocoder::ENCODE_88BIT_IMBE);
vocoder::MBEEncoder adjusted(vocoder::ENCODE_88BIT_IMBE);
std::array<int16_t, 160U> pcm{};
std::array<uint8_t, 11U> referenceBits{};
std::array<uint8_t, 11U> adjustedBits{};
for (uint32_t sample = 0U; sample < pcm.size(); sample++)
pcm[sample] = static_cast<int16_t>(static_cast<int32_t>((sample * 211U) & 0x3FFFU) - 0x2000);
adjusted.setGainAdjust(2.0f);
reference.encode(pcm.data(), referenceBits.data());
adjusted.encode(pcm.data(), adjustedBits.data());
REQUIRE(referenceBits != adjustedBits);
}
Loading…
Cancel
Save

Powered by TurnKey Linux.