From 8434c8518d8d5c1e1cc8898a79f2d59af477fae4 Mon Sep 17 00:00:00 2001 From: Doug McLain Date: Sun, 13 Feb 2022 14:07:59 -0500 Subject: [PATCH] Add gain adjust values --- Controller.cpp | 7 +++++-- DV3000.cpp | 17 +++++++++++++---- DV3000.h | 2 +- DV3003.cpp | 14 +++++++++++--- DV3003.h | 2 +- DVSIDevice.cpp | 17 +++++++++++++++-- DVSIDevice.h | 5 +++-- 7 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Controller.cpp b/Controller.cpp index 26060bf..1b42e1c 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -25,6 +25,9 @@ #include "TranscoderPacket.h" #include "Controller.h" +#define AMBE_GAIN 0 //dB I use -6 here +#define AMBE2_GAIN 0 //dB I use 6 here + CController::CController() : keep_running(true) {} bool CController::Start() @@ -168,10 +171,10 @@ bool CController::InitVocoders() } if (dstar_device && dmrsf_device) { - if (dstar_device->OpenDevice(deviceset.front().first, deviceset.front().second, dvtype)) + if (dstar_device->OpenDevice(deviceset.front().first, deviceset.front().second, dvtype, AMBE_GAIN)) return true; deviceset.pop_front(); - if (dmrsf_device->OpenDevice(deviceset.front().first, deviceset.front().second, dvtype)) + if (dmrsf_device->OpenDevice(deviceset.front().first, deviceset.front().second, dvtype, AMBE2_GAIN)) return true; deviceset.pop_front(); } diff --git a/DV3000.cpp b/DV3000.cpp index 9e57375..cd67fb0 100644 --- a/DV3000.cpp +++ b/DV3000.cpp @@ -52,7 +52,7 @@ std::shared_ptr CDV3000::PopWaitingPacket(unsigned int /* cha return waiting_packet.pop(); } -bool CDV3000::SendAudio(const uint8_t /*channel*/, const int16_t *audio) const +bool CDV3000::SendAudio(const uint8_t /*channel*/, const int16_t *audio, const int gain) const { // Create Audio packet based on input int8_ts SDV_Packet p; @@ -61,9 +61,18 @@ bool CDV3000::SendAudio(const uint8_t /*channel*/, const int16_t *audio) const p.header.packet_type = PKT_SPEECH; p.field_id = PKT_SPEECHD; p.payload.audio3k.num_samples = 160U; - for (int i=0; i<160; i++) - p.payload.audio3k.samples[i] = htons(audio[i]); - + const uint32_t g = abs(gain); + + std::cerr << "SendAudio() gain == " << gain << std::endl; + + for (int i=0; i<160; i++){ + if(gain < 0){ + p.payload.audio3k.samples[i] = htons(audio[i] / g); + } + else{ + p.payload.audio3k.samples[i] = htons(audio[i] * g); + } + } // send audio packet to DV3000 const DWORD size = packet_size(p); DWORD written; diff --git a/DV3000.h b/DV3000.h index 51ab591..58b8567 100644 --- a/DV3000.h +++ b/DV3000.h @@ -29,7 +29,7 @@ protected: void PushWaitingPacket(unsigned int channel, std::shared_ptr packet); std::shared_ptr PopWaitingPacket(unsigned int channel); void ProcessPacket(const SDV_Packet &p); - bool SendAudio(const uint8_t channel, const int16_t *audio) const; + bool SendAudio(const uint8_t channel, const int16_t *audio, const int gain) const; bool SendData(const uint8_t channel, const uint8_t *data) const; private: diff --git a/DV3003.cpp b/DV3003.cpp index 7638b2d..7994ff6 100644 --- a/DV3003.cpp +++ b/DV3003.cpp @@ -52,7 +52,7 @@ std::shared_ptr CDV3003::PopWaitingPacket(unsigned int channe return waiting_packet[channel].pop(); } -bool CDV3003::SendAudio(const uint8_t channel, const int16_t *audio) const +bool CDV3003::SendAudio(const uint8_t channel, const int16_t *audio, const int gain) const { // Create Audio packet based on input int8_ts SDV_Packet p; @@ -62,8 +62,16 @@ bool CDV3003::SendAudio(const uint8_t channel, const int16_t *audio) const p.field_id = channel + PKT_CHANNEL0; p.payload.audio.speechd = PKT_SPEECHD; p.payload.audio.num_samples = 160U; - for (int i=0; i<160; i++) - p.payload.audio.samples[i] = htons(audio[i]); + const uint32_t g = abs(gain); + + for (int i=0; i<160; i++){ + if(gain < 0){ + p.payload.audio3k.samples[i] = htons(audio[i]) / g; + } + else{ + p.payload.audio3k.samples[i] = htons(audio[i]) * g; + } + } // send audio packet to DV3000 const DWORD size = packet_size(p); diff --git a/DV3003.h b/DV3003.h index d19bb37..78b05fd 100644 --- a/DV3003.h +++ b/DV3003.h @@ -29,7 +29,7 @@ protected: void PushWaitingPacket(unsigned int channel, std::shared_ptr packet); std::shared_ptr PopWaitingPacket(unsigned int channel); void ProcessPacket(const SDV_Packet &p); - bool SendAudio(const uint8_t channel, const int16_t *audio) const; + bool SendAudio(const uint8_t channel, const int16_t *audio, const int gain) const; bool SendData(const uint8_t channel, const uint8_t *data) const; private: diff --git a/DVSIDevice.cpp b/DVSIDevice.cpp index a9fdf0c..245b2dd 100644 --- a/DVSIDevice.cpp +++ b/DVSIDevice.cpp @@ -36,6 +36,17 @@ #include "configure.h" #include "Controller.h" +int16_t calcGainVal(float db) +{ + float ratio = powf(10.0, (db/10.0)); + + if(db < 0){ + ratio = (1/ratio) * (-1); + } + + return (int16_t)roundf(ratio); +} + extern CController Controller; CDVDevice::CDVDevice(Encoding t) : type(t), ftHandle(nullptr), buffer_depth(0), keep_running(true) @@ -140,7 +151,7 @@ bool CDVDevice::checkResponse(SDV_Packet &p, uint8_t response) const return false; } -bool CDVDevice::OpenDevice(const std::string &serialno, const std::string &desc, Edvtype dvtype) +bool CDVDevice::OpenDevice(const std::string &serialno, const std::string &desc, Edvtype dvtype, float dbgain) { auto status = FT_OpenEx((PVOID)serialno.c_str(), FT_OPEN_BY_SERIAL_NUMBER, &ftHandle); if (FT_OK != status) @@ -149,6 +160,7 @@ bool CDVDevice::OpenDevice(const std::string &serialno, const std::string &desc, return true; } + gain = calcGainVal(dbgain); std::this_thread::sleep_for(std::chrono::milliseconds(50)); FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX ); std::this_thread::sleep_for(std::chrono::milliseconds(50)); @@ -593,7 +605,8 @@ void CDVDevice::FeedDevice() } else { - SendAudio(index, packet->GetAudioSamples()); + //SendAudio(index, packet->GetAudioSamples(), (Encoding::dstar==type) ? 4 : -4); + SendAudio(index, packet->GetAudioSamples(), gain); } buffer_depth++; } diff --git a/DVSIDevice.h b/DVSIDevice.h index 8875840..676a5a6 100644 --- a/DVSIDevice.h +++ b/DVSIDevice.h @@ -32,7 +32,7 @@ public: CDVDevice(Encoding t); virtual ~CDVDevice(); - bool OpenDevice(const std::string &serialno, const std::string &desc, Edvtype dvtype); + bool OpenDevice(const std::string &serialno, const std::string &desc, Edvtype dvtype, float dbgain); void Start(); void CloseDevice(); void AddPacket(const std::shared_ptr packet); @@ -46,6 +46,7 @@ protected: CPacketQueue input_queue; std::future feedFuture, readFuture; std::string description, productid; + int16_t gain; bool DiscoverFtdiDevices(); bool ConfigureVocoder(uint8_t pkt_ch, Encoding type); @@ -61,6 +62,6 @@ protected: virtual void PushWaitingPacket(unsigned int channel, std::shared_ptr packet) = 0; virtual std::shared_ptr PopWaitingPacket(unsigned int channel) = 0; virtual void ProcessPacket(const SDV_Packet &p) = 0; - virtual bool SendAudio(const uint8_t channel, const int16_t *audio) const = 0; + virtual bool SendAudio(const uint8_t channel, const int16_t *audio, const int gain) const = 0; virtual bool SendData(const uint8_t channel, const uint8_t *data) const = 0; };