mirror of https://github.com/g4klx/MMDVMHost
Merge pull request #580 from F4FXL/FM_Ext
Pack and unpack using shifting and masking
This commit is contained in:
commit
9a2dd6a5e6
|
|
@ -49,21 +49,17 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length)
|
||||||
for (unsigned int i = 0U; i < length; i += 3U) {
|
for (unsigned int i = 0U; i < length; i += 3U) {
|
||||||
unsigned short sample1 = 0U;
|
unsigned short sample1 = 0U;
|
||||||
unsigned short sample2 = 0U;
|
unsigned short sample2 = 0U;
|
||||||
unsigned short MASK = 0x0001U;
|
unsigned int MASK = 0x00000FFFU;
|
||||||
|
|
||||||
const unsigned char* base = data + i;
|
unsigned int pack = 0U;
|
||||||
for (unsigned int j = 0U; j < 12U; j++, MASK <<= 1) {
|
unsigned char* packPointer = (unsigned char*)&pack;
|
||||||
unsigned int pos1 = j;
|
|
||||||
unsigned int pos2 = j + 12U;
|
|
||||||
|
|
||||||
bool b1 = READ_BIT(base, pos1) != 0U;
|
packPointer[1U] = data[i];
|
||||||
bool b2 = READ_BIT(base, pos2) != 0U;
|
packPointer[2U] = data[i + 1U];
|
||||||
|
packPointer[3U] = data[i + 2U];
|
||||||
|
|
||||||
if (b1)
|
sample2 = (short)(pack & MASK);
|
||||||
sample1 |= MASK;
|
sample1 = (short)(pack >> 12);
|
||||||
if (b2)
|
|
||||||
sample2 |= MASK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert from unsigned short (0 - +4095) to float (-1.0 - +1.0)
|
// Convert from unsigned short (0 - +4095) to float (-1.0 - +1.0)
|
||||||
samples[nSamples++] = (float(sample1) - 2048.0F) / 2048.0F;
|
samples[nSamples++] = (float(sample1) - 2048.0F) / 2048.0F;
|
||||||
|
|
@ -108,18 +104,24 @@ unsigned int CFMControl::readModem(unsigned char* data, unsigned int space)
|
||||||
// Pre-emphasise the data and other stuff.
|
// Pre-emphasise the data and other stuff.
|
||||||
|
|
||||||
// Pack the floating point data (+1.0 to -1.0) to packed 12-bit samples (+2047 - -2048)
|
// Pack the floating point data (+1.0 to -1.0) to packed 12-bit samples (+2047 - -2048)
|
||||||
unsigned int offset = 0U;
|
unsigned int pack = 0U;
|
||||||
for (unsigned int i = 0U; i < nSamples; i++) {
|
unsigned char* packPointer = (unsigned char*)&pack;
|
||||||
unsigned short sample = (unsigned short)((samples[i] + 1.0F) * 2048.0F + 0.5F);
|
unsigned int j = 0U;
|
||||||
unsigned short MASK = 0x0001U;
|
unsigned int i = 0U;
|
||||||
for (unsigned int j = 0U; j < 12U; j++, MASK <<= 1) {
|
for (; i < nSamples && j < space; i += 2U, j += 3U) {
|
||||||
bool b = (sample & MASK) != 0U;
|
unsigned short sample1 = (unsigned short)((samples[i] + 1.0F) * 2048.0F + 0.5F);
|
||||||
WRITE_BIT(data, offset, b);
|
unsigned short sample2 = (unsigned short)((samples[i + 1] + 1.0F) * 2048.0F + 0.5F);
|
||||||
offset++;
|
|
||||||
}
|
pack = 0;
|
||||||
|
pack = ((unsigned int)sample1) << 12;
|
||||||
|
pack |= sample2;
|
||||||
|
|
||||||
|
data[j] = packPointer[1U];
|
||||||
|
data[j + 1U] = packPointer[2U];
|
||||||
|
data[j + 2U] = packPointer[3U];
|
||||||
}
|
}
|
||||||
|
|
||||||
return nSamples;
|
return j;//return the number of bytes written
|
||||||
}
|
}
|
||||||
|
|
||||||
void CFMControl::clock(unsigned int ms)
|
void CFMControl::clock(unsigned int ms)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue