cleanup and fix MuLaw encoding code;

82-dvmbridge---implement-notch-filter-for-2175hz-trc-guard-tone
Bryan Biedenkapp 1 year ago
parent 6e327eb2b2
commit 29d37126ef

@ -143,3 +143,8 @@ system:
# Enable local audio over speakers. # Enable local audio over speakers.
localAudio: true localAudio: true
# Flag indicating whether or not trace logging is enabled.
trace: false
# Flag indicating whether or not debug logging is enabled.
debug: false

@ -165,17 +165,17 @@ static short search(short val, short* table, short size)
/* Helper to convert PCM into G.711 aLaw. */ /* Helper to convert PCM into G.711 aLaw. */
uint8_t pcmToaLaw(short pcm) uint8_t encodeALaw(short pcm)
{ {
short mask; short mask;
unsigned char aval; uint8_t aval;
pcm = pcm >> 3; pcm = pcm >> 3;
if (pcm >= 0) { if (pcm >= 0) {
mask = 0xD5; // sign (7th) bit = 1 mask = 0xD5U; // sign (7th) bit = 1
} else { } else {
mask = 0x55; // sign bit = 0 mask = 0x55U; // sign bit = 0
pcm = -pcm - 1; pcm = -pcm - 1;
} }
@ -200,9 +200,9 @@ uint8_t pcmToaLaw(short pcm)
/* Helper to convert G.711 aLaw into PCM. */ /* Helper to convert G.711 aLaw into PCM. */
short aLawToPCM(uint8_t alaw) short decodeALaw(uint8_t alaw)
{ {
alaw ^= 0x55; alaw ^= 0x55U;
short t = (alaw & QUANT_MASK) << 4; short t = (alaw & QUANT_MASK) << 4;
short seg = ((unsigned)alaw & SEG_MASK) >> SEG_SHIFT; short seg = ((unsigned)alaw & SEG_MASK) >> SEG_SHIFT;
@ -211,19 +211,19 @@ short aLawToPCM(uint8_t alaw)
t += 8; t += 8;
break; break;
case 1: case 1:
t += 0x108; t += 0x108U;
break; break;
default: default:
t += 0x108; t += 0x108U;
t <<= seg - 1; t <<= seg - 1;
} }
return ((alaw & SIGN_BIT) ? t : -t); return ((alaw & SIGN_BIT) ? t : -t);
} }
/* Helper to convert PCM into G.711 uLaw. */ /* Helper to convert PCM into G.711 MuLaw. */
uint8_t pcmTouLaw(short pcm) uint8_t encodeMuLaw(short pcm)
{ {
short mask; short mask;
@ -256,9 +256,9 @@ uint8_t pcmTouLaw(short pcm)
} }
} }
/* Helper to convert G.711 uLaw into PCM. */ /* Helper to convert G.711 MuLaw into PCM. */
short uLawToPCM(uint8_t ulaw) short decodeMuLaw(uint8_t ulaw)
{ {
// complement to obtain normal u-law value // complement to obtain normal u-law value
ulaw = ~ulaw; ulaw = ~ulaw;
@ -350,6 +350,7 @@ HostBridge::HostBridge(const std::string& confFile) :
m_detectedSampleCnt(0U), m_detectedSampleCnt(0U),
m_dumpSampleLevel(false), m_dumpSampleLevel(false),
m_running(false), m_running(false),
m_trace(false),
m_debug(false), m_debug(false),
m_rtpSeqNo(0U), m_rtpSeqNo(0U),
m_rtpTimestamp(INVALID_TS) m_rtpTimestamp(INVALID_TS)
@ -969,6 +970,9 @@ bool HostBridge::readParams()
yaml::Node networkConf = m_conf["network"]; yaml::Node networkConf = m_conf["network"];
m_udpAudio = networkConf["udpAudio"].as<bool>(false); m_udpAudio = networkConf["udpAudio"].as<bool>(false);
m_trace = systemConf["trace"].as<bool>(false);
m_debug = systemConf["debug"].as<bool>(false);
LogInfo("General Parameters"); LogInfo("General Parameters");
LogInfo(" Rx Audio Gain: %.1f", m_rxAudioGain); LogInfo(" Rx Audio Gain: %.1f", m_rxAudioGain);
LogInfo(" Vocoder Decoder Audio Gain: %.1f", m_vocoderDecoderAudioGain); LogInfo(" Vocoder Decoder Audio Gain: %.1f", m_vocoderDecoderAudioGain);
@ -987,6 +991,10 @@ bool HostBridge::readParams()
LogInfo(" Local Audio: %s", m_localAudio ? "yes" : "no"); LogInfo(" Local Audio: %s", m_localAudio ? "yes" : "no");
LogInfo(" UDP Audio: %s", m_udpAudio ? "yes" : "no"); LogInfo(" UDP Audio: %s", m_udpAudio ? "yes" : "no");
if (m_debug) {
LogInfo(" Debug: yes");
}
return true; return true;
} }
@ -1160,8 +1168,8 @@ void HostBridge::processUDPAudio()
} }
if (length > 0) { if (length > 0) {
if (m_debug) if (m_trace)
Utils::dump(1U, "UDP Audio Network Packet", buffer, length); Utils::dump(1U, "HostBridge()::processUDPAudio() Audio Network Packet", buffer, length);
uint32_t pcmLength = 0; uint32_t pcmLength = 0;
if (m_udpNoIncludeLength) { if (m_udpNoIncludeLength) {
@ -1171,7 +1179,7 @@ void HostBridge::processUDPAudio()
} }
if (m_udpRTPFrames) if (m_udpRTPFrames)
pcmLength = MBE_SAMPLES_LENGTH; pcmLength = MBE_SAMPLES_LENGTH * 2U;
UInt8Array __pcm = std::make_unique<uint8_t[]>(pcmLength); UInt8Array __pcm = std::make_unique<uint8_t[]>(pcmLength);
uint8_t* pcm = __pcm.get(); uint8_t* pcm = __pcm.get();
@ -1185,7 +1193,7 @@ void HostBridge::processUDPAudio()
return; return;
} }
::memcpy(pcm, buffer + RTP_HEADER_LENGTH_BYTES, MBE_SAMPLES_LENGTH); ::memcpy(pcm, buffer + RTP_HEADER_LENGTH_BYTES, MBE_SAMPLES_LENGTH * 2U);
} }
else { else {
if (m_udpNoIncludeLength) { if (m_udpNoIncludeLength) {
@ -1211,10 +1219,20 @@ void HostBridge::processUDPAudio()
int smpIdx = 0; int smpIdx = 0;
short samples[MBE_SAMPLES_LENGTH]; short samples[MBE_SAMPLES_LENGTH];
if (m_udpUseULaw) { if (m_udpUseULaw) {
if (m_trace)
Utils::dump(1U, "HostBridge()::processUDPAudio() uLaw Audio", pcm, MBE_SAMPLES_LENGTH * 2U);
for (uint32_t pcmIdx = 0; pcmIdx < pcmLength; pcmIdx++) { for (uint32_t pcmIdx = 0; pcmIdx < pcmLength; pcmIdx++) {
samples[smpIdx] = uLawToPCM(pcm[pcmIdx]); samples[smpIdx] = decodeMuLaw(pcm[pcmIdx]);
smpIdx++; smpIdx++;
} }
int pcmIdx = 0;
for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) {
pcm[pcmIdx + 0] = (uint8_t)(samples[smpIdx] & 0xFF);
pcm[pcmIdx + 1] = (uint8_t)((samples[smpIdx] >> 8) & 0xFF);
pcmIdx += 2;
}
} }
else { else {
for (uint32_t pcmIdx = 0; pcmIdx < pcmLength; pcmIdx += 2) { for (uint32_t pcmIdx = 0; pcmIdx < pcmLength; pcmIdx += 2) {
@ -1505,8 +1523,11 @@ void HostBridge::decodeDMRAudioFrame(uint8_t* ambe, uint32_t srcId, uint32_t dst
uint8_t pcm[MBE_SAMPLES_LENGTH * 2U]; uint8_t pcm[MBE_SAMPLES_LENGTH * 2U];
if (m_udpUseULaw) { if (m_udpUseULaw) {
for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) { for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) {
pcm[smpIdx] = pcmTouLaw(samples[smpIdx]); pcm[smpIdx] = encodeMuLaw(samples[smpIdx]);
} }
if (m_trace)
Utils::dump(1U, "HostBridge()::decodeDMRAudioFrame() Encoded uLaw Audio", pcm, MBE_SAMPLES_LENGTH);
} }
else { else {
for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) { for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) {
@ -2090,8 +2111,11 @@ void HostBridge::decodeP25AudioFrame(uint8_t* ldu, uint32_t srcId, uint32_t dstI
uint8_t pcm[MBE_SAMPLES_LENGTH * 2U]; uint8_t pcm[MBE_SAMPLES_LENGTH * 2U];
if (m_udpUseULaw) { if (m_udpUseULaw) {
for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) { for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) {
pcm[smpIdx] = pcmTouLaw(samples[smpIdx]); pcm[smpIdx] = encodeMuLaw(samples[smpIdx]);
} }
if (m_trace)
Utils::dump(1U, "HostBridge()::decodeP25AudioFrame() Encoded uLaw Audio", pcm, MBE_SAMPLES_LENGTH);
} }
else { else {
for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) { for (uint32_t smpIdx = 0; smpIdx < MBE_SAMPLES_LENGTH; smpIdx++) {

@ -96,25 +96,25 @@ void mdcPacketDetected(int frameCount, mdc_u8_t op, mdc_u8_t arg, mdc_u16_t unit
* @param pcm PCM value. * @param pcm PCM value.
* @return uint8_t aLaw value. * @return uint8_t aLaw value.
*/ */
uint8_t pcmToaLaw(short pcm); uint8_t encodeALaw(short pcm);
/** /**
* @brief Helper to convert G.711 aLaw into PCM. * @brief Helper to convert G.711 aLaw into PCM.
* @param alaw aLaw value. * @param alaw aLaw value.
* @return short PCM value. * @return short PCM value.
*/ */
short aLawToPCM(uint8_t alaw); short decodeALaw(uint8_t alaw);
/** /**
* @brief Helper to convert PCM into G.711 uLaw. * @brief Helper to convert PCM into G.711 MuLaw.
* @param pcm PCM value. * @param pcm PCM value.
* @return uint8_t uLaw value. * @return uint8_t MuLaw value.
*/ */
uint8_t pcmTouLaw(short pcm); uint8_t encodeMuLaw(short pcm);
/** /**
* @brief Helper to convert G.711 uLaw into PCM. * @brief Helper to convert G.711 MuLaw into PCM.
* @param ulaw uLaw value. * @param ulaw MuLaw value.
* @return short PCM value. * @return short PCM value.
*/ */
short uLawToPCM(uint8_t ulaw); short decodeMuLaw(uint8_t ulaw);
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Class Declaration // Class Declaration
@ -239,6 +239,7 @@ private:
bool m_dumpSampleLevel; bool m_dumpSampleLevel;
bool m_running; bool m_running;
bool m_trace;
bool m_debug; bool m_debug;
uint16_t m_rtpSeqNo; uint16_t m_rtpSeqNo;

Loading…
Cancel
Save

Powered by TurnKey Linux.