diff --git a/Controller.cpp b/Controller.cpp index 94a2370..9fae4e8 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -56,7 +56,8 @@ bool CController::Start() reflectorFuture = std::async(std::launch::async, &CController::ReadReflectorThread, this); c2Future = std::async(std::launch::async, &CController::ProcessC2Thread, this); swambe2Future = std::async(std::launch::async, &CController::ProcessSWAMBE2Thread,this); - imbeFuture = std::async(std::launch::async, &CController::ProcessIMBEThread,this); + imbeFuture = std::async(std::launch::async, &CController::ProcessIMBEThread, this); + usrpFuture = std::async(std::launch::async, &CController::ProcessUSRPThread, this); return false; } @@ -266,6 +267,9 @@ void CController::ReadReflectorThread() case ECodecType::p25: imbe_queue.push(packet); break; + case ECodecType::usrp: + usrp_queue.push(packet); + break; case ECodecType::c2_1600: case ECodecType::c2_3200: codec2_queue.push(packet); @@ -303,6 +307,7 @@ void CController::AudiotoCodec2(std::shared_ptr packet) // set the m17_is_set flag if this is the last packet packet->SetM17Data(m17data); } + // we might be all done... send_mux.lock(); if (packet->AllCodecsAreSet() && packet->HasNotBeenSent()) SendToReflector(packet); @@ -363,6 +368,7 @@ void CController::Codec2toAudio(std::shared_ptr packet) packet->SetDMRData(ambe2); p25vocoder.encode_4400((int16_t*)packet->GetAudioSamples(), imbe); packet->SetP25Data(imbe); + packet->SetUSRPData((int16_t*)packet->GetAudioSamples()); } void CController::ProcessC2Thread() @@ -383,6 +389,7 @@ void CController::ProcessC2Thread() case ECodecType::dstar: case ECodecType::dmr: case ECodecType::p25: + case ECodecType::usrp: // codec_in was AMBE, so we need to calculate the the M17 data AudiotoCodec2(packet); break; @@ -424,6 +431,7 @@ void CController::SWAMBE2toAudio(std::shared_ptr packet) dstar_device->AddPacket(packet); codec2_queue.push(packet); imbe_queue.push(packet); + usrp_queue.push(packet); } void CController::ProcessSWAMBE2Thread() @@ -438,6 +446,7 @@ void CController::ProcessSWAMBE2Thread() case ECodecType::c2_3200: case ECodecType::dstar: case ECodecType::p25: + case ECodecType::usrp: AudiotoSWAMBE2(packet); break; @@ -465,9 +474,8 @@ void CController::AudiotoIMBE(std::shared_ptr packet) } } - p25vocoder.encode_4400(tmp, imbe); + p25vocoder.encode_4400((int16_t*)p, imbe); packet->SetP25Data(imbe); - // we might be all done... send_mux.lock(); if (packet->AllCodecsAreSet() && packet->HasNotBeenSent()) SendToReflector(packet); @@ -482,6 +490,7 @@ void CController::IMBEtoAudio(std::shared_ptr packet) dstar_device->AddPacket(packet); codec2_queue.push(packet); swambe2_queue.push(packet); + usrp_queue.push(packet); } void CController::ProcessIMBEThread() @@ -496,6 +505,7 @@ void CController::ProcessIMBEThread() case ECodecType::c2_3200: case ECodecType::dstar: case ECodecType::dmr: + case ECodecType::usrp: AudiotoIMBE(packet); break; @@ -506,6 +516,62 @@ void CController::ProcessIMBEThread() } } +void CController::AudiotoUSRP(std::shared_ptr packet) +{ + const auto m = packet->GetModule(); + int16_t tmp[160]; + const int16_t *p = packet->GetAudioSamples(); + const uint32_t g = abs(gain); + + for(int i = 0; i < 160; ++i){ + if(gain < 0){ + tmp[i] = p[i] / g; + } + else{ + tmp[i] = p[i] * g; + } + } + + packet->SetUSRPData(p); + + // we might be all done... + send_mux.lock(); + if (packet->AllCodecsAreSet() && packet->HasNotBeenSent()) SendToReflector(packet); + send_mux.unlock(); +} + +void CController::USRPtoAudio(std::shared_ptr packet) +{ + packet->SetAudioSamples(packet->GetUSRPData(), false); + dstar_device->AddPacket(packet); + codec2_queue.push(packet); + swambe2_queue.push(packet); + imbe_queue.push(packet); +} + +void CController::ProcessUSRPThread() +{ + while (keep_running) + { + auto packet = usrp_queue.pop(); + + switch (packet->GetCodecIn()) + { + case ECodecType::c2_1600: + case ECodecType::c2_3200: + case ECodecType::dstar: + case ECodecType::dmr: + case ECodecType::p25: + AudiotoUSRP(packet); + break; + + case ECodecType::usrp: + USRPtoAudio(packet); + break; + } + } +} + void CController::SendToReflector(std::shared_ptr packet) { // open a socket to the reflector channel @@ -526,6 +592,7 @@ void CController::RouteDstPacket(std::shared_ptr packet) // codec_in is dstar, the audio has just completed, so now calc the M17 and DMR codec2_queue.push(packet); imbe_queue.push(packet); + usrp_queue.push(packet); if(swambe2) swambe2_queue.push(packet); else @@ -545,6 +612,7 @@ void CController::RouteDmrPacket(std::shared_ptr packet) { codec2_queue.push(packet); imbe_queue.push(packet); + usrp_queue.push(packet); dstar_device->AddPacket(packet); } else diff --git a/Controller.h b/Controller.h index 265af22..0e68751 100644 --- a/Controller.h +++ b/Controller.h @@ -46,7 +46,7 @@ public: protected: std::atomic keep_running; - std::future reflectorFuture, c2Future, swambe2Future, imbeFuture; + std::future reflectorFuture, c2Future, swambe2Future, imbeFuture, usrpFuture; std::unordered_map audio_store; std::unordered_map data_store; CUnixDgramReader reader; @@ -57,6 +57,7 @@ protected: CPacketQueue codec2_queue; CPacketQueue swambe2_queue; CPacketQueue imbe_queue; + CPacketQueue usrp_queue; std::mutex send_mux; int16_t gain; bool swambe2; @@ -69,11 +70,14 @@ protected: void ProcessC2Thread(); void ProcessSWAMBE2Thread(); void ProcessIMBEThread(); + void ProcessUSRPThread(); void Codec2toAudio(std::shared_ptr packet); void AudiotoCodec2(std::shared_ptr packet); void SWAMBE2toAudio(std::shared_ptr packet); void AudiotoSWAMBE2(std::shared_ptr packet); void IMBEtoAudio(std::shared_ptr packet); void AudiotoIMBE(std::shared_ptr packet); + void USRPtoAudio(std::shared_ptr packet); + void AudiotoUSRP(std::shared_ptr packet); void SendToReflector(std::shared_ptr packet); }; diff --git a/TranscoderPacket.cpp b/TranscoderPacket.cpp index c9f46ad..004333a 100644 --- a/TranscoderPacket.cpp +++ b/TranscoderPacket.cpp @@ -19,7 +19,7 @@ #include "TranscoderPacket.h" -CTranscoderPacket::CTranscoderPacket(const STCPacket &tcp) : dstar_set(false), dmr_set(false), p25_set(false), m17_set(false), not_sent(true) +CTranscoderPacket::CTranscoderPacket(const STCPacket &tcp) : dstar_set(false), dmr_set(false), p25_set(false), m17_set(false), usrp_set(false), not_sent(true) { tcpacket.module = tcp.module; tcpacket.is_last = tcp.is_last; @@ -37,6 +37,9 @@ CTranscoderPacket::CTranscoderPacket(const STCPacket &tcp) : dstar_set(false), d case ECodecType::p25: SetP25Data(tcp.p25); break; + case ECodecType::usrp: + SetUSRPData(tcp.usrp); + break; case ECodecType::c2_1600: case ECodecType::c2_3200: SetM17Data(tcp.m17); @@ -67,6 +70,11 @@ const uint8_t *CTranscoderPacket::GetP25Data() const return tcpacket.p25; } +const int16_t *CTranscoderPacket::GetUSRPData() const +{ + return tcpacket.usrp; +} + const uint8_t *CTranscoderPacket::GetM17Data() const { return tcpacket.m17; @@ -101,6 +109,14 @@ void CTranscoderPacket::SetP25Data(const uint8_t *p25) p25_set = true; } +void CTranscoderPacket::SetUSRPData(const int16_t *usrp) +{ + for(int i = 0; i < 160; ++i){ + tcpacket.usrp[i] = usrp[i]; + } + usrp_set = true; +} + void CTranscoderPacket::SetAudioSamples(const int16_t *sample, bool swap) { for (unsigned int i=0; i<160; i++) @@ -157,6 +173,11 @@ bool CTranscoderPacket::P25IsSet() const return p25_set; } +bool CTranscoderPacket::USRPIsSet() const +{ + return usrp_set; +} + bool CTranscoderPacket::M17IsSet() const { return m17_set; @@ -164,7 +185,7 @@ bool CTranscoderPacket::M17IsSet() const bool CTranscoderPacket::AllCodecsAreSet() const { - return (dstar_set && dmr_set && m17_set && p25_set); + return (dstar_set && dmr_set && m17_set && p25_set && usrp_set); } void CTranscoderPacket::Sent() diff --git a/TranscoderPacket.h b/TranscoderPacket.h index b76162f..039f6f0 100644 --- a/TranscoderPacket.h +++ b/TranscoderPacket.h @@ -37,10 +37,12 @@ public: const uint8_t *GetDMRData() const; const uint8_t *GetP25Data() const; const uint8_t *GetM17Data() const; + const int16_t *GetUSRPData() const; void SetDStarData(const uint8_t *dstar); void SetDMRData(const uint8_t *dmr); void SetP25Data(const uint8_t *p25); void SetM17Data(const uint8_t *m17); + void SetUSRPData(const int16_t *usrp); void SetAudioSamples(const int16_t *samples, bool swap); // audio @@ -57,6 +59,7 @@ public: bool DMRIsSet() const; bool P25IsSet() const; bool M17IsSet() const; + bool USRPIsSet() const; bool AllCodecsAreSet() const; void Sent(); bool HasNotBeenSent() const; @@ -67,5 +70,5 @@ public: private: STCPacket tcpacket; int16_t audio[160]; - std::atomic_bool dstar_set, dmr_set, p25_set, m17_set, not_sent; + std::atomic_bool dstar_set, dmr_set, p25_set, m17_set, usrp_set, not_sent; };