// // cxombeinterface.cpp // ambed // // Software vocoder interface powered by xombe (AMBE+ / AMBE2+) // #include "main.h" #include #include #include "cambepacket.h" #include "cvoicepacket.h" #include "cxombeinterface.h" //////////////////////////////////////////////////////////////////////////////////////// // constructor & destructor CXombeInterface::CXombeInterface(int iIndex, int iNbPairs) : m_iIndex(iIndex), m_iNbPairs(iNbPairs) { std::snprintf(m_szName, sizeof(m_szName), "XOMBE-SW-%d", m_iIndex); std::snprintf(m_szSerial, sizeof(m_szSerial), "XOMBE%04d", m_iIndex); m_Slots.resize(m_iNbPairs * 2); } CXombeInterface::~CXombeInterface() { m_bStopThread = true; if ( m_pThread != NULL ) { m_pThread->join(); delete m_pThread; m_pThread = NULL; } } //////////////////////////////////////////////////////////////////////////////////////// // initialization bool CXombeInterface::Init(void) { if ( xombe_version() != XOMBE_VERSION ) { std::cerr << m_szName << ": xombe version mismatch (lib=" << xombe_version() << ", header=" << XOMBE_VERSION << ")" << std::endl; return false; } // Start the worker thread via base class return CVocodecInterface::Init(); } //////////////////////////////////////////////////////////////////////////////////////// // channel mapping // Even logical indices -> CODEC_AMBEPLUS (D-STAR) // Odd logical indices -> CODEC_AMBE2PLUS (DMR / YSF-DN / NXDN) uint8 CXombeInterface::GetChannelCodec(int iCh) const { return ((iCh % 2) == 0) ? CODEC_AMBEPLUS : CODEC_AMBE2PLUS; } CVocodecChannel *CXombeInterface::GetChannelWithChannelIn(int iCh) { for ( size_t i = 0; i < m_Channels.size(); i++ ) { if ( m_Channels[i]->GetChannelIn() == iCh ) return m_Channels[i]; } return NULL; } CVocodecChannel *CXombeInterface::GetChannelWithChannelOut(int iCh) { for ( size_t i = 0; i < m_Channels.size(); i++ ) { if ( m_Channels[i]->GetChannelOut() == iCh ) return m_Channels[i]; } return NULL; } //////////////////////////////////////////////////////////////////////////////////////// // xombe state reset (runs exclusively on this interface's worker thread) void CXombeInterface::ResetSlot(XombeSlot &slot, uint8 uiCodecIn, uint8 uiCodecOut) { enum xombe_codec fromCodec = (uiCodecIn == CODEC_AMBEPLUS) ? XOMBE_DSTAR : XOMBE_DMR; // 1. Direct parameter-domain transcoder (shares m_QwsMem on this thread) slot.xc = xombe_xc_init_shared(&slot.xcMem, fromCodec, &m_QwsMem); // 2. Decoder (AMBE -> PCM, used when M17/P25 output is enabled) slot.dec = xombe_dec_init(&slot.decMem, fromCodec); // 3. Encoder (PCM -> AMBE, used when OpenEncoderChannel receives PCM on VoiceQueue) xombe_enc_cfg encCfg; xombe_enc_cfg_default(&encCfg); encCfg.outputs = (uiCodecOut == CODEC_AMBEPLUS) ? XOMBE_ENC_DSTAR : XOMBE_ENC_DMR; slot.enc = xombe_enc_init(&slot.encMem, &encCfg); } //////////////////////////////////////////////////////////////////////////////////////// // worker thread loop void CXombeInterface::Task(void) { bool bDidWork = false; for ( size_t i = 0; i < m_Channels.size(); i++ ) { CVocodecChannel *ch = m_Channels[i]; XombeSlot &slot = m_Slots[i]; bool bOpen = ch->IsOpen(); if ( !bOpen ) { slot.wasOpen = false; continue; } if ( !slot.wasOpen ) { ResetSlot(slot, ch->GetCodecIn(), ch->GetCodecOut()); slot.wasOpen = true; } // --- PATH A: AMBE input (D-STAR <-> DMR, and optionally -> Codec2/IMBE) --- CAmbePacket *pktIn = NULL; CPacketQueue *qIn = ch->GetPacketQueueIn(); if ( !qIn->empty() ) { pktIn = (CAmbePacket *)qIn->front(); qIn->pop(); } ch->ReleasePacketQueueIn(); if ( pktIn != NULL ) { bDidWork = true; uint8 outAmbe[XOMBE_FRAME_BYTES]; // Direct parameter-domain transcoding (0-frame delay) if ( slot.xc != NULL && xombe_xc_frame(slot.xc, pktIn->GetAmbe(), 0, outAmbe) == 1 ) { CAmbePacket *pktOut = new CAmbePacket(); pktOut->SetPid(pktIn->GetPid()); pktOut->SetCodec(ch->GetCodecOut()); pktOut->SetAmbe(outAmbe); CPacketQueue *qOut = ch->GetPacketQueueOut(); qOut->push(pktOut); ch->ReleasePacketQueueOut(); } // If Codec2 (M17) or IMBE (P25) output is active, also decode to PCM // Convert xombe native int16_t PCM -> big-endian PCM expected by CSignalProcessor/OutputPcm if ( (ch->IsCodec2Enabled() || ch->IsImbeEnabled()) && slot.dec != NULL ) { int16_t pcm[160]; if ( xombe_dec_frame(slot.dec, pktIn->GetAmbe(), 0, pcm) == 1 ) { uint8 pcmBe[320]; for ( int s = 0; s < 160; s++ ) { pcmBe[s * 2] = (uint8)((pcm[s] >> 8) & 0xFF); pcmBe[s * 2 + 1] = (uint8)(pcm[s] & 0xFF); } CVoicePacket voicePkt(pcmBe, sizeof(pcmBe)); ch->ProcessSignal(voicePkt); } } delete pktIn; } // --- PATH B: PCM input on VoiceQueue (M17/P25 -> AMBE via OpenEncoderChannel) --- CVoicePacket *voiceIn = NULL; CPacketQueue *qVoice = ch->GetVoiceQueue(); if ( !qVoice->empty() ) { voiceIn = (CVoicePacket *)qVoice->front(); qVoice->pop(); } ch->ReleaseVoiceQueue(); if ( voiceIn != NULL ) { bDidWork = true; if ( slot.enc != NULL && voiceIn->GetVoiceSize() >= 320 ) { uint8 dmrOut[XOMBE_FRAME_BYTES]; uint8 dstarOut[XOMBE_FRAME_BYTES]; const uint8 *rawBe = voiceIn->GetVoice(); int16_t pcm[160]; // Convert big-endian PCM from VoiceQueue -> native int16_t for xombe for ( int s = 0; s < 160; s++ ) { pcm[s] = (int16_t)((rawBe[s * 2] << 8) | rawBe[s * 2 + 1]); } xombe_enc_frame(slot.enc, pcm, NULL, dmrOut, dstarOut); CAmbePacket *pktOut = new CAmbePacket(); pktOut->SetPid(ch->PopPid()); pktOut->SetCodec(ch->GetCodecOut()); pktOut->SetAmbe((ch->GetCodecOut() == CODEC_AMBEPLUS) ? dstarOut : dmrOut); CPacketQueue *qOut = ch->GetPacketQueueOut(); qOut->push(pktOut); ch->ReleasePacketQueueOut(); } delete voiceIn; } } // Avoid busy-spinning when all channels on this thread are idle if ( !bDidWork ) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } }