Skip to content

Commit

Permalink
Merge pull request #2 from paulh002/Dev
Browse files Browse the repository at this point in the history
add if gain
  • Loading branch information
paulh002 authored Mar 19, 2022
2 parents 3e116b8 + a2850f8 commit 1146486
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 50 deletions.
2 changes: 2 additions & 0 deletions AMDemodulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void AMDemodulator::operator()()
usleep(500);
continue;
}
adjust_gain(iqsamples, gbar.get_if());
perform_fft(iqsamples);
Fft_calc.set_signal_strength(get_if_level());
process(iqsamples, audiosamples);
Expand Down Expand Up @@ -170,6 +171,7 @@ void AMDemodulator::operator()()
const auto timePassed = std::chrono::duration_cast<std::chrono::microseconds>(now - startTime);
printf("Queued Audio Samples %d droppedframes %d underrun %d\n", audio_output->queued_samples() / 2, dropped_frames, audio_output->get_underrun());
printf("peak %f db gain %f db threshold %f ratio %f atack %f release %f\n", Agc.getPeak(), Agc.getGain(), Agc.getThreshold(), Agc.getRatio(), Agc.getAtack(),Agc.getRelease());
printf("mean %f rms %f \n", m_audio_mean, m_audio_rms);
pr_time = 0;
if (rcount > 10 && audio_output->get_underrun() == 0 && dropped_frames > 15)
{
Expand Down
7 changes: 7 additions & 0 deletions AudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ bool AudioOutput::open(std::string device)
return true;
}

void AudioOutput::set_volume(int vol)
{
// log volume
m_volume = exp(((double)vol * 6.908) / 100.0) / 1000;
//fprintf(stderr,"vol %f\n", (float)m_volume);
}

void AudioOutput::adjust_gain(SampleVector& samples)
{
for (unsigned int i = 0, n = samples.size(); i < n; i++) {
Expand Down
2 changes: 1 addition & 1 deletion AudioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AudioOutput :
void close();
~AudioOutput();
double get_volume() { return m_volume; }
void set_volume(int vol) { m_volume = exp(((double)vol * 6.908) / 100.0) / 1000.0; } // log volume
void set_volume(int vol);
unsigned int get_framesize() { return bufferFrames; }
int queued_samples();
void listDevices(std::vector<std::string> &devices);
Expand Down
1 change: 1 addition & 0 deletions FMDemodulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void FMDemodulator::operator()()
usleep(500);
continue;
}
adjust_gain(iqsamples, gbar.get_if());
perform_fft(iqsamples);
Fft_calc.set_signal_strength(get_if_level());
process(iqsamples, audiosamples);
Expand Down
1 change: 1 addition & 0 deletions FT8Demodulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ void FT8Demodulator::operator()()
usleep(500);
continue;
}
adjust_gain(iqsamples, gbar.get_if());
perform_fft(iqsamples);
Fft_calc.set_signal_strength(get_if_level());
process(iqsamples, audiosamples);
Expand Down
11 changes: 10 additions & 1 deletion FmDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ void FmDecoder::stereo_to_left_right(const SampleVector& samples_mono,
}
}

void FmDecoder::adjust_gain(IQSampleVector &samples_in, float vol)
{
for (auto &col : samples_in)
{
col.real(col.real() * vol);
col.imag(col.imag() * vol);
}
}

pthread_t fm_thread;

void* rx_fm_thread(void* fm_ptr)
Expand Down Expand Up @@ -496,7 +505,7 @@ void* rx_fm_thread(void* fm_ptr)
usleep(5000);
continue;
}

pfm->adjust_gain(iqsamples, gbar.get_if());
buf_mix.clear();
for (auto& col : iqsamples)
{
Expand Down
3 changes: 2 additions & 1 deletion FmDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ class FmDecoder
*/
void process(const IQSampleVector& samples_in,
SampleVector& audio);


void adjust_gain(IQSampleVector &samples_in, float vol);
/** Return true if a stereo signal is detected. */
bool stereo_detected() const
{
Expand Down
2 changes: 1 addition & 1 deletion Gui_band.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void Gui_band::init_button_gui(lv_obj_t *o_tab, lv_coord_t w, SoapySDR::RangeLis

button_width_margin = ((w - tab_margin) / x_number_buttons);
button_width = ((w - tab_margin) / x_number_buttons) - x_margin;
button_height = 50;
button_height = 40;
button_height_margin = button_height + y_margin;

lv_coord_t pos_x = x_margin, pos_y = y_margin;
Expand Down
11 changes: 11 additions & 0 deletions Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ int Settings::volume()
return 0;
}

int Settings::if_gain()
{
if (radio.find("if-gain") != radio.end())
{
auto s = radio.find("if-gain");
return atoi((const char *)s->second.c_str());
}
else
return 0;
}

int Settings::gain()
{
if (radio.find("gain") != radio.end())
Expand Down
3 changes: 2 additions & 1 deletion Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class Settings
int convert_mode(string s);
long long get_ft8(int band);
void getagc_preset(std::string key, int &atack, int &release);

int if_gain();

vector<int> meters;
vector<string> labels;
vector<long> f_low;
Expand Down
4 changes: 2 additions & 2 deletions Waterfall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void Waterfall::init(lv_obj_t* scr, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv
chart = lv_chart_create(scr);
lv_obj_add_style(chart, &waterfall_style, 0);
lv_obj_set_pos(chart, x, y);
lv_obj_set_size(chart, w, (h - 150));
lv_obj_set_size(chart, w, (h - 170));
lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
lv_obj_set_style_pad_hor(scr, 0, LV_PART_MAIN);
lv_obj_set_style_pad_ver(scr, 0, LV_PART_MAIN);
Expand Down Expand Up @@ -234,7 +234,7 @@ void Fft_calculator::upload_fft(std::vector<lv_coord_t>& data_set)
void Fft_calculator::set_signal_strength(double strength)
{
std::unique_lock<std::mutex> lock(m_mutex);
signal_strength = 20* log10(strength);
signal_strength = 20* log10(strength) + 120;
//printf(" signal_strength %f \n", signal_strength);
}

Expand Down
2 changes: 1 addition & 1 deletion gui_agc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Gui_agc::init(lv_obj_t* o_tab, lv_coord_t w)

int button_width_margin = ((w - tab_margin) / x_number_buttons);
int button_width = ((w - tab_margin) / x_number_buttons) - x_margin;
int button_height = 50;
int button_height = 40;
int button_height_margin = button_height + y_margin;
int ibutton_x = 0, ibutton_y = 0;
int i = 0;
Expand Down
84 changes: 52 additions & 32 deletions gui_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,24 @@ static void bar_button_handler(lv_event_t * e)

static void vol_slider_event_cb(lv_event_t * e)
{
char buf[20];

lv_obj_t * slider = lv_event_get_target(e);
sprintf(buf, "vol %d", lv_slider_get_value(slider));
lv_label_set_text(gbar.get_vol_slider_label(), buf);
lv_label_set_text_fmt(gbar.get_vol_slider_label(), "vol %d", lv_slider_get_value(slider));
audio_output->set_volume(lv_slider_get_value(slider));
catinterface.SetAG(lv_slider_get_value(slider));
}

static void if_slider_event_cb(lv_event_t *e)
{
lv_obj_t *slider = lv_event_get_target(e);
lv_label_set_text_fmt(gbar.get_if_slider_label(), "if %d db", lv_slider_get_value(slider));
gbar.m_if = 10 * lv_slider_get_value(slider);
}

static void gain_slider_event_cb(lv_event_t * e)
{
char buf[20];

lv_obj_t * slider = lv_event_get_target(e);
sprintf(buf, "gain %ddb", lv_slider_get_value(slider));
lv_label_set_text(gbar.get_gain_slider_label(), buf);

lv_label_set_text_fmt(gbar.get_gain_slider_label(), "rf %d db", lv_slider_get_value(slider));
try
{
SdrDevices.SdrDevices.at(default_radio)->setGain(SOAPY_SDR_RX, default_rx_channel, lv_slider_get_value(slider));
Expand All @@ -171,10 +173,7 @@ static void gain_slider_event_cb(lv_event_t * e)

void gui_bar::update_gain_slider(int gain)
{
char buf[30];

sprintf(buf, "gain %d db", gain);
lv_label_set_text(gain_slider_label, buf);
lv_label_set_text_fmt(gain_slider_label, "rf %d db", gain);
lv_slider_set_value(gain_slider, gain, LV_ANIM_ON);
}

Expand All @@ -183,6 +182,12 @@ void gui_bar::step_gain_slider(int step)
set_gain_slider(lv_slider_get_value(gain_slider) + step);
}

gui_bar::gui_bar()
: m_if{1000}
{

}

gui_bar::~gui_bar()
{
for (int i = 0; i < ibuttons; i++)
Expand All @@ -198,7 +203,6 @@ gui_bar::~gui_bar()

void gui_bar::set_gain_slider(int gain)
{
char buf[20];
double max_gain {0.0};
double min_gain{0.0};

Expand All @@ -217,8 +221,8 @@ void gui_bar::set_gain_slider(int gain)
gain = max_gain;
if (gain < min_gain)
gain = min_gain;
sprintf(buf, "gain %ddb", gain);
lv_label_set_text(gain_slider_label, buf);

lv_label_set_text_fmt(gain_slider_label, "rf %d db", gain);
lv_slider_set_value(gain_slider, gain, LV_ANIM_ON);
try
{
Expand Down Expand Up @@ -262,12 +266,12 @@ void gui_bar::set_gain_range()

void gui_bar::init(lv_obj_t *o_parent, lv_group_t *button_group, int mode, lv_coord_t w, lv_coord_t h)
{
const lv_coord_t x_margin_dropdown = 20;
const lv_coord_t x_margin_dropdown = 0;
const lv_coord_t x_margin = 2;
const lv_coord_t y_margin = 5;
const int x_number_buttons = 6;
const lv_coord_t y_margin = 2; //5;
const int x_number_buttons = 4;
const int y_number_buttons = 4;
const int max_rows = 2;
const int max_rows = 3;
const lv_coord_t tab_margin = w / 3;
const int cw_margin = 20;

Expand Down Expand Up @@ -420,7 +424,7 @@ void gui_bar::init(lv_obj_t *o_parent, lv_group_t *button_group, int mode, lv_co
}

int vol_x = x_number_buttons * button_width_margin + 10 + x_margin_dropdown;
int vol_width = (w / 3) - 30;
int vol_width = (w / 3); // -30;
vol_slider_label = lv_label_create(o_parent);
lv_label_set_text(vol_slider_label, "vol");
lv_obj_align(vol_slider_label, LV_ALIGN_TOP_LEFT, vol_x + vol_width + 5, 15);
Expand All @@ -429,8 +433,19 @@ void gui_bar::init(lv_obj_t *o_parent, lv_group_t *button_group, int mode, lv_co
lv_obj_set_width(vol_slider, vol_width);
lv_obj_align(vol_slider, LV_ALIGN_TOP_LEFT, vol_x , 15);
lv_obj_add_event_cb(vol_slider, vol_slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

int gain_y = 15 + button_height_margin;
if_slider_label = lv_label_create(o_parent);
lv_label_set_text(if_slider_label, "if 60 db");
lv_obj_align(if_slider_label, LV_ALIGN_TOP_LEFT, vol_x + vol_width + 5, gain_y);
if_slider = lv_slider_create(o_parent);
lv_slider_set_range(if_slider, 0, 100);
lv_obj_set_width(if_slider, vol_width);
lv_obj_align(if_slider, LV_ALIGN_TOP_LEFT, vol_x, gain_y);
lv_obj_add_event_cb(if_slider, if_slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_slider_set_value(if_slider, 60, LV_ANIM_OFF);

int gain_y = 15 + y_margin + button_height_margin;
gain_y += (button_height_margin);
gain_slider_label = lv_label_create(o_parent);
lv_label_set_text(gain_slider_label, "gain");
lv_obj_align(gain_slider_label, LV_ALIGN_TOP_LEFT, vol_x + vol_width + 5, gain_y);
Expand All @@ -457,7 +472,7 @@ void gui_bar::init(lv_obj_t *o_parent, lv_group_t *button_group, int mode, lv_co
std::cout << e.what();
}

int cw_y = y_margin + 2 * button_height_margin;
int cw_y = y_margin + max_rows * button_height_margin;

lv_style_init(&cw_style);
lv_style_set_radius(&cw_style, 0);
Expand Down Expand Up @@ -568,8 +583,6 @@ void gui_bar::set_cw_message(std::string message)

void gui_bar::set_cw_wpm(int wpm)
{
char str[30];

unique_lock<mutex> gui_lock(gui_mutex, std::defer_lock);
gui_lock.try_lock();
if (!gui_lock.owns_lock())
Expand All @@ -579,8 +592,7 @@ void gui_bar::set_cw_wpm(int wpm)
if (!gui_lock.owns_lock())
return;
}
sprintf(str, "wpm: %d", wpm);
lv_label_set_text(cw_wpm, str);
lv_label_set_text_fmt(cw_wpm, "wpm: %d", wpm);
}

void gui_bar::set_led(bool status)
Expand Down Expand Up @@ -611,12 +623,8 @@ void gui_bar::set_vol_slider(int volume)
volume = 0;
if (volume > max_volume)
volume = max_volume;
lv_slider_set_value(vol_slider, volume, LV_ANIM_ON);

char buf[20];

sprintf(buf, "vol %d", volume);
lv_label_set_text(vol_slider_label, buf);
lv_slider_set_value(vol_slider, volume, LV_ANIM_ON);
lv_label_set_text_fmt(vol_slider_label, "vol %d", volume);
audio_output->set_volume(volume);
}

Expand All @@ -625,6 +633,18 @@ int gui_bar::get_vol_range()
return max_volume;
}

float gui_bar::get_if()
{
return m_if.load();
}

void gui_bar::set_if(int rf)
{
lv_slider_set_value(if_slider, rf, LV_ANIM_ON);
lv_label_set_text_fmt(if_slider_label, "if %d db", rf);
m_if = std::pow(10.0,(float)rf / 20.0);
}

void gui_bar::get_filter_range(vector<string> &filters)
{
filters.push_back("0.5 Khz");
Expand Down
16 changes: 12 additions & 4 deletions gui_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern const int tunerHeight;
class gui_bar
{
public:
gui_bar();
~gui_bar();
void init(lv_obj_t *o_parent, lv_group_t *button_group, int mode, lv_coord_t w, lv_coord_t h);
void set_vol_slider(int volume);
Expand All @@ -26,7 +27,10 @@ class gui_bar
void select_option(int option);
lv_obj_t *get_vol_slider_label() { return vol_slider_label; }
lv_obj_t *get_gain_slider_label() { return gain_slider_label; }
lv_obj_t *get_if_slider_label() { return if_slider_label; }
int get_vol_range();
float get_if();
void set_if(int rf);
void set_gain_range();
void update_gain_slider(int gain);
void set_gain_slider(int gain);
Expand All @@ -51,6 +55,8 @@ class gui_bar
{
return ifilters[sel];
}

atomic<float> m_if;

private:
lv_style_t style_btn;
Expand All @@ -59,11 +65,13 @@ class gui_bar
int filter;
const int number_of_buttons {12};
lv_obj_t* vol_slider, *vol_slider_label, *gain_slider, *gain_slider_label;
const int max_volume {100};
lv_obj_t *if_slider_label, *if_slider;
const int max_volume {100};
vector<int> ifilters;
lv_obj_t *cw_wpm, *cw_message, *cw_box, *cw_led;
lv_style_t cw_style, style_selected_color;
lv_group_t *m_button_group{nullptr};
lv_obj_t *cw_wpm, *cw_message, *cw_box, *cw_led;
lv_style_t cw_style, style_selected_color;
lv_group_t *m_button_group{nullptr};

};

extern gui_bar gbar;
Loading

0 comments on commit 1146486

Please sign in to comment.