Code cleanup and optimisation$

pull/801/head
AlexandreRouma 2022-07-08 17:30:43 +02:00
rodzic 3ab8badb6a
commit 30b6c2b1da
2 zmienionych plików z 15 dodań i 96 usunięć

Wyświetl plik

@ -13,7 +13,6 @@ namespace dsp::audio {
void init(stream<stereo_t>* in, double volume, bool muted) {
_volume = powf(volume, 2);
_muted = muted;
level = { -150.0f, -150.0f };
base_type::init(in);
}
@ -35,41 +34,8 @@ namespace dsp::audio {
return _muted;
}
stereo_t getOutputLevel() {
assert(base_type::_block_init);
std::lock_guard<std::recursive_mutex> lck0(base_type::ctrlMtx);
std::lock_guard<std::mutex> lck1(lvlMtx);
stereo_t lvl = level;
level = { -150.0f, -150.0f };
return lvl;
}
void reset() {
assert(base_type::_block_init);
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);
level = { -150.0f, -150.0f };
}
inline int process(int count, const stereo_t* in, stereo_t* out) {
// Apply volume factor
volk_32f_s32f_multiply_32f((float*)out, (float*)in, _muted ? 0.0f : _volume, count * 2);
// Measure block level
stereo_t maxLvl = { 0, 0 };
for (int i = 0; i < count; i++) {
stereo_t lvl = { fabsf(out[i].l), fabsf(out[i].r) };
if (lvl.l > maxLvl.l) { maxLvl.l = lvl.l; }
if (lvl.r > maxLvl.r) { maxLvl.r = lvl.r; }
}
stereo_t maxLvlDB = { 20.0f * log10f(maxLvl.l), 20.0f * log10f(maxLvl.r) };
// Update max level
{
std::lock_guard<std::mutex> lck(lvlMtx);
if (maxLvlDB.l > level.l) { level.l = maxLvlDB.l; }
if (maxLvlDB.r > level.r) { level.r = maxLvlDB.r; }
}
return count;
}
@ -87,8 +53,6 @@ namespace dsp::audio {
private:
float _volume;
bool _muted;
std::mutex lvlMtx;
stereo_t level;
};
}

Wyświetl plik

@ -20,53 +20,21 @@ namespace rds {
{ BLOCK_TYPE_D, 0b0110110100 }
};
// This parity check matrix is given in annex B2.1 of the specificiation
const uint16_t PARITY_CHECK_MAT[] = {
0b1000000000,
0b0100000000,
0b0010000000,
0b0001000000,
0b0000100000,
0b0000010000,
0b0000001000,
0b0000000100,
0b0000000010,
0b0000000001,
0b1011011100,
0b0101101110,
0b0010110111,
0b1010000111,
0b1110011111,
0b1100010011,
0b1101010101,
0b1101110110,
0b0110111011,
0b1000000001,
0b1111011100,
0b0111101110,
0b0011110111,
0b1010100111,
0b1110001111,
0b1100011011
};
// 9876543210
const uint16_t LFSR_POLY = 0b0110111001;
const uint16_t IN_POLY = 0b1100011011;
const int BLOCK_LEN = 26;
const int DATA_LEN = 16;
const int POLY_LEN = 10;
// 9876543210
const uint16_t LFSR_POLY = 0b0110111001;
const uint16_t IN_POLY = 0b1100011011;
void RDSDecoder::process(uint8_t* symbols, int count) {
for (int i = 0; i < count; i++) {
// Shift in the bit
shiftReg = ((shiftReg << 1) & 0x3FFFFFF) | (symbols[i] & 1);
// Skip if we need to shift in new data
if (--skip > 0) {
continue;
}
if (--skip > 0) { continue; }
// Calculate the syndrome and update sync status
uint16_t syn = calcSyndrome(shiftReg);
@ -109,23 +77,9 @@ namespace rds {
}
uint16_t RDSDecoder::calcSyndrome(uint32_t block) {
// Perform vector/matrix dot product between block and parity matrix
uint16_t syn = 0;
for(int i = 0; i < BLOCK_LEN; i++) {
syn ^= PARITY_CHECK_MAT[BLOCK_LEN - 1 - i] * ((block >> i) & 1);
}
return syn;
}
uint32_t RDSDecoder::correctErrors(uint32_t block, BlockType type, bool& recovered) {
// Subtract the offset from block
block ^= (uint32_t)OFFSETS[type];
// Init the syndrome and output
uint16_t syn = 0;
uint32_t out = block;
// Feed in the data
// Calculate the syndrome using a LFSR
for (int i = BLOCK_LEN - 1; i >= 0; i--) {
// Shift the syndrome and keep the output
uint8_t outBit = (syn >> (POLY_LEN - 1)) & 1;
@ -138,7 +92,16 @@ namespace rds {
syn ^= IN_POLY * ((block >> i) & 1);
}
uint16_t firstSyn = syn;
return syn;
}
uint32_t RDSDecoder::correctErrors(uint32_t block, BlockType type, bool& recovered) {
// Subtract the offset from block
block ^= (uint32_t)OFFSETS[type];
uint32_t out = block;
// Calculate syndrome of corrected block
uint16_t syn = calcSyndrome(block);
// Use the syndrome register to do error correction if errors are present
uint8_t errorFound = 0;
@ -158,14 +121,6 @@ namespace rds {
}
recovered = !(syn & 0b11111);
// // One last check
// if (errorFound) {
// printf("Error found: %04X -> %04X, %08X -> %08X\n", firstSyn, syn, block, out);
// }
// else if (!recovered) {
// printf("Non recoverable error\n");
// }
return out;
}