Add sliding-frequency audio receiver tuning

This commit is contained in:
Belousov Oleg
2026-07-27 21:07:48 +03:00
parent c4f32ac436
commit ccf093b5d5
20 changed files with 790 additions and 69 deletions
+74 -4
View File
@@ -280,6 +280,12 @@ AnalogAudioView::AnalogAudioView(
field_frequency.on_show_options = [this]() {
this->on_show_options_frequency();
};
field_frequency.changing = [this](rf::Frequency frequency) {
return this->on_frequency_changed(frequency);
};
field_frequency.entered = [this](rf::Frequency frequency) {
this->set_frequency_absolute(frequency);
};
field_lna.on_show_options = [this]() {
this->on_show_options_rf_gain();
@@ -308,8 +314,10 @@ AnalogAudioView::AnalogAudioView(
};
waterfall.on_select = [this](int32_t offset) {
field_frequency.set_value(receiver_model.target_frequency() + offset);
field_frequency.set_value(
field_frequency.value() + offset * receiver_model.frequency_step());
};
waterfall.set_live_tuning(true);
#ifdef PRALINE
button_pro.on_select = [this](Button&) { this->on_show_options_praline(); };
@@ -319,6 +327,7 @@ AnalogAudioView::AnalogAudioView(
// This call starts the correct baseband image to run
// and sets the radio up as necessary for the given modulation.
sliding_center_frequency = receiver_model.target_frequency();
on_modulation_changed(modulation);
}
@@ -328,7 +337,7 @@ AnalogAudioView::AnalogAudioView(
: AnalogAudioView(nav) {
// Settings to override when launched from another app (versus from AppSettings .ini file)
// TODO: Which other settings make sense to override?
field_frequency.set_value(override.frequency_app_override);
set_frequency_absolute(override.frequency_app_override);
on_frequency_step_changed(override.frequency_step);
options_modulation.set_by_value(toUType(override.mode));
}
@@ -573,10 +582,14 @@ void AnalogAudioView::update_modulation(ReceiverModel::Mode modulation) {
receiver_model.set_sampling_rate(is_wideband_spectrum_mode ? spec_bw : 3072000);
receiver_model.set_baseband_bandwidth(is_wideband_spectrum_mode ? spec_bw / 2 : 1750000);
receiver_model.set_hidden_offset(modulation == ReceiverModel::Mode::AMAudioFMApt ? -2200 : 0); // wefax needs to be shifted, see wefax rx app.
reset_sliding_frequency(modulation);
receiver_model.enable();
if (sliding_enabled) {
baseband::set_audio_ddc_frequency(0);
}
// TODO: This doesn't belong here! There's a better way.
size_t sampling_rate = 0;
switch (modulation) {
@@ -610,7 +623,64 @@ void AnalogAudioView::handle_coded_squelch(uint32_t value) {
}
void AnalogAudioView::on_freqchg(int64_t freq) {
field_frequency.set_value(freq);
set_frequency_absolute(freq);
}
void AnalogAudioView::set_frequency_absolute(rf::Frequency frequency) {
if (!sliding_enabled) {
field_frequency.set_value(frequency);
return;
}
sliding_center_frequency = frequency;
/* set_value() does not call on_change when the displayed frequency is
* already equal, so reset the hardware and DDC explicitly in that case. */
if (field_frequency.value() == frequency) {
receiver_model.set_target_frequency_with_hidden_offset(frequency, 0);
baseband::set_audio_ddc_frequency(0);
} else {
field_frequency.set_value(frequency);
}
}
int32_t AnalogAudioView::sliding_limit() const {
const bool zoom_x2 =
receiver_model.modulation() == ReceiverModel::Mode::AMAudio &&
previous_zoom != 0;
return zoom_x2 ? sliding_limit_zoom_x2 : sliding_limit_zoom_x1;
}
void AnalogAudioView::reset_sliding_frequency(ReceiverModel::Mode modulation) {
sliding_enabled =
modulation == ReceiverModel::Mode::AMAudio ||
modulation == ReceiverModel::Mode::NarrowbandFMAudio;
sliding_center_frequency = receiver_model.target_frequency();
/* AMFM keeps its existing Wefax offset; sliding applies to AM and NFM. */
receiver_model.set_hidden_offset(
modulation == ReceiverModel::Mode::AMAudioFMApt ? -2200 : 0);
}
bool AnalogAudioView::on_frequency_changed(rf::Frequency frequency) {
if (!sliding_enabled)
return false;
const auto limit = sliding_limit();
int64_t offset = frequency - sliding_center_frequency;
if (offset > limit) {
sliding_center_frequency = frequency - limit;
offset = limit;
} else if (offset < -limit) {
sliding_center_frequency = frequency + limit;
offset = -limit;
}
/* Store the displayed frequency and retune the hardware centre atomically. */
receiver_model.set_target_frequency_with_hidden_offset(
frequency, sliding_center_frequency - frequency);
baseband::set_audio_ddc_frequency(static_cast<int32_t>(offset));
return true;
}
#ifdef PRALINE
@@ -269,6 +269,10 @@ class AnalogAudioView : public View {
uint8_t zoom_factor_amfm{0}; // initial zoom factor in AMFM mode
uint8_t previous_AM_mode_option{0}; // GUI 5 AM modes : (0..4 ) (DSB9K, DSB6K, USB,LSB, CW). Used to select proper FIR filter (0..11) AM mode + offset 0 (zoom+1) or +6 (if zoom+2)
uint8_t previous_zoom{0}; // GUI ZOOM+1, ZOOM+2 , equivalent to two values offset 0 (zoom+1) or +6 (if zoom+2)
static constexpr int32_t sliding_limit_zoom_x1 = 50000;
static constexpr int32_t sliding_limit_zoom_x2 = 30000;
rf::Frequency sliding_center_frequency{0};
bool sliding_enabled{false};
app_settings::SettingsManager settings_{
"rx_audio",
@@ -354,6 +358,10 @@ class AnalogAudioView : public View {
void handle_coded_squelch(uint32_t value);
void on_freqchg(int64_t freq);
bool on_frequency_changed(rf::Frequency frequency);
void set_frequency_absolute(rf::Frequency frequency);
int32_t sliding_limit() const;
void reset_sliding_frequency(ReceiverModel::Mode modulation);
MessageHandlerRegistration message_handler_coded_squelch{
Message::ID::CodedSquelch,
+5
View File
@@ -343,6 +343,11 @@ void set_spectrum(
send_message(&message);
}
void set_audio_ddc_frequency(int32_t frequency) {
const AudioDDCConfigMessage message{frequency};
send_message(&message);
}
void set_time_sink(
const size_t sampling_rate,
const size_t trigger) {
+1
View File
@@ -100,6 +100,7 @@ void set_rds_data(const uint16_t message_length);
void set_spectrum(
const size_t sampling_rate,
const size_t trigger);
void set_audio_ddc_frequency(int32_t frequency);
void set_time_sink(
const size_t sampling_rate,
const size_t trigger);
+9
View File
@@ -88,6 +88,15 @@ void ReceiverModel::set_target_frequency(rf::Frequency f) {
update_tuning_frequency();
}
void ReceiverModel::set_target_frequency_with_hidden_offset(
rf::Frequency f,
rf::Frequency offset) {
persistent_memory::set_target_frequency(f);
settings_.frequency_app_override = f;
hidden_offset = offset;
update_tuning_frequency();
}
uint32_t ReceiverModel::baseband_bandwidth() const {
return settings_.baseband_bandwidth;
}
+1
View File
@@ -66,6 +66,7 @@ class ReceiverModel {
/* The frequency to receive (no offset). */
rf::Frequency target_frequency() const;
void set_target_frequency(rf::Frequency f);
void set_target_frequency_with_hidden_offset(rf::Frequency f, rf::Frequency offset);
uint32_t baseband_bandwidth() const;
void set_baseband_bandwidth(uint32_t v);
+9 -3
View File
@@ -39,6 +39,8 @@ class BoundFrequencyField : public FrequencyField {
public:
decltype(FrequencyField::on_change) updated{};
std::function<bool(rf::Frequency)> changing{};
std::function<void(rf::Frequency)> entered{};
BoundFrequencyField(Point parent_pos, NavigationView& nav)
: FrequencyField(parent_pos) {
@@ -47,7 +49,8 @@ class BoundFrequencyField : public FrequencyField {
set_value(model->target_frequency());
on_change = [this](rf::Frequency f) {
model->set_target_frequency(f);
if (!changing || !changing(f))
model->set_target_frequency(f);
if (updated)
updated(f);
};
@@ -57,7 +60,10 @@ class BoundFrequencyField : public FrequencyField {
on_edit_shown();
auto freq_view = nav.push<FrequencyKeypadView>(model->target_frequency());
freq_view->on_changed = [this](rf::Frequency f) {
set_value(f);
if (entered)
entered(f);
else
set_value(f);
};
nav.set_on_pop([this]() {
if (on_edit_hidden)
@@ -76,4 +82,4 @@ using TxFrequencyField = BoundFrequencyField<TransmitterModel, &portapack::trans
} // namespace ui
#endif // __UI_FREQ_FIELD_H__
#endif // __UI_FREQ_FIELD_H__
+105 -12
View File
@@ -88,16 +88,29 @@ void FrequencyScale::set_spectrum_sampling_rate(const int new_sampling_rate) {
}
void FrequencyScale::set_channel_filter(
const int offset,
const int low_frequency,
const int high_frequency,
const int transition) {
if ((channel_filter_low_frequency != low_frequency) ||
const bool shape_changed =
(channel_filter_low_frequency != low_frequency) ||
(channel_filter_high_frequency != high_frequency) ||
(channel_filter_transition != transition)) {
(channel_filter_transition != transition);
const bool offset_changed = channel_filter_offset != offset;
if (shape_changed) {
channel_filter_offset = offset;
channel_filter_low_frequency = low_frequency;
channel_filter_high_frequency = high_frequency;
channel_filter_transition = transition;
set_dirty();
} else if (offset_changed) {
const auto old_offset = channel_filter_offset;
channel_filter_offset = offset;
if (live_tuning && spectrum_sampling_rate && drawn())
redraw_filter_cursor(old_offset);
else
set_dirty();
}
}
@@ -123,12 +136,14 @@ void FrequencyScale::paint(Painter& painter) {
draw_filter_ranges(painter, r);
draw_frequency_ticks(painter, r);
const Rect r_cursor{
(screen_width / 2 - 2) + cursor_position, r.bottom() - filter_band_height,
5, filter_band_height};
painter.fill_rectangle(
r_cursor,
Color::red());
if (!live_tuning) {
const Rect r_cursor{
(screen_width / 2 - 2) + cursor_position, r.bottom() - filter_band_height,
5, filter_band_height};
painter.fill_rectangle(
r_cursor,
Color::red());
}
}
void FrequencyScale::clear() {
@@ -184,9 +199,79 @@ void FrequencyScale::draw_frequency_ticks(Painter& painter, const Rect r) {
}
}
void FrequencyScale::redraw_filter_cursor(const int old_offset) {
const auto r = screen_rect();
const auto x_center = r.width() / 2;
const auto trans =
channel_filter_transition * spectrum_bins / spectrum_sampling_rate;
const auto cursor_left = [&](const int offset) {
return r.left() + x_center +
(offset + channel_filter_low_frequency) *
spectrum_bins / spectrum_sampling_rate -
trans;
};
const auto cursor_right = [&](const int offset) {
return r.left() + x_center +
(offset + channel_filter_high_frequency) *
spectrum_bins / spectrum_sampling_rate +
trans;
};
const auto dirty_left =
std::min(cursor_left(old_offset), cursor_left(channel_filter_offset)) - 1;
const auto dirty_right =
std::max(cursor_right(old_offset), cursor_right(channel_filter_offset)) + 1;
const Rect dirty{
dirty_left,
r.bottom() - filter_band_height,
dirty_right - dirty_left,
filter_band_height};
Painter painter;
painter.fill_rectangle(dirty, Theme::getInstance()->bg_darkest->background);
draw_filter_ranges(painter, r);
restore_tick_lines(painter, r, dirty);
}
void FrequencyScale::restore_tick_lines(
Painter& painter,
const Rect r,
const Rect dirty) {
const auto draw_if_dirty = [&](const Coord x) {
if (x >= dirty.left() && x < dirty.right()) {
painter.fill_rectangle(
{x, dirty.top(), 1, dirty.height()},
Theme::getInstance()->bg_darkest->foreground);
}
};
const auto x_center = r.left() + r.width() / 2;
draw_if_dirty(x_center);
constexpr int tick_count_max = 4;
float rough_tick_interval = float(spectrum_sampling_rate) / tick_count_max;
int magnitude = 1;
while (rough_tick_interval >= 10.0f) {
rough_tick_interval /= 10;
magnitude *= 10;
}
const int tick_interval = std::ceil(rough_tick_interval);
auto tick_offset = tick_interval;
while ((tick_offset * magnitude) < spectrum_sampling_rate / 2) {
const Dim pixel_offset =
tick_offset * magnitude * spectrum_bins / spectrum_sampling_rate;
draw_if_dirty(x_center - pixel_offset);
draw_if_dirty(x_center + pixel_offset);
tick_offset += tick_interval;
}
}
void FrequencyScale::draw_filter_ranges(Painter& painter, const Rect r) {
if (channel_filter_low_frequency != channel_filter_high_frequency) {
const auto x_center = r.width() / 2;
const auto x_center = r.width() / 2 +
channel_filter_offset * spectrum_bins / spectrum_sampling_rate;
const auto x_low = x_center + channel_filter_low_frequency * spectrum_bins / spectrum_sampling_rate;
const auto x_high = x_center + channel_filter_high_frequency * spectrum_bins / spectrum_sampling_rate;
@@ -220,6 +305,11 @@ void FrequencyScale::on_blur() {
}
bool FrequencyScale::on_encoder(const EncoderEvent delta) {
if (live_tuning) {
if (on_select) on_select(delta);
return true;
}
cursor_position += delta;
cursor_position = std::min<int32_t>(cursor_position, screen_width / 2 - 1);
@@ -326,9 +416,11 @@ WaterfallView::WaterfallView(const bool cursor) {
frequency_scale.focus(); // focus on frequency scale to show cursor
if (sampling_rate) {
// screen x to frequency scale x, NB we need two widgets align
int32_t cursor_position = x - (screen_width / 2);
frequency_scale.set_cursor_position(cursor_position);
const int32_t cursor_position = x - (screen_width / 2);
if (!frequency_scale.is_live_tuning()) {
// screen x to frequency scale x, NB we need two widgets align
frequency_scale.set_cursor_position(cursor_position);
}
}
};
@@ -405,6 +497,7 @@ void WaterfallView::on_channel_spectrum(const ChannelSpectrum& spectrum) {
sampling_rate = spectrum.sampling_rate;
frequency_scale.set_spectrum_sampling_rate(sampling_rate);
frequency_scale.set_channel_filter(
spectrum.channel_filter_offset,
spectrum.channel_filter_low_frequency,
spectrum.channel_filter_high_frequency,
spectrum.channel_filter_transition);
+8 -1
View File
@@ -83,8 +83,10 @@ class FrequencyScale : public Widget {
bool on_touch(const TouchEvent touch) override;
void set_spectrum_sampling_rate(const int new_sampling_rate);
void set_channel_filter(const int low_frequency, const int high_frequency, const int transition);
void set_channel_filter(const int offset, const int low_frequency, const int high_frequency, const int transition);
void set_cursor_position(const int32_t position);
void set_live_tuning(const bool enabled) { live_tuning = enabled; }
bool is_live_tuning() const { return live_tuning; }
void paint(Painter& painter) override;
@@ -94,15 +96,19 @@ class FrequencyScale : public Widget {
int32_t cursor_position{0};
int spectrum_sampling_rate{0};
const int spectrum_bins = std::tuple_size<decltype(ChannelSpectrum::db)>::value;
int channel_filter_offset{0};
int channel_filter_low_frequency{0};
int channel_filter_high_frequency{0};
int channel_filter_transition{0};
bool live_tuning{false};
void clear();
void clear_background(Painter& painter, const Rect r);
void draw_frequency_ticks(Painter& painter, const Rect r);
void draw_filter_ranges(Painter& painter, const Rect r);
void redraw_filter_cursor(const int old_offset);
void restore_tick_lines(Painter& painter, const Rect r, const Rect dirty);
};
/* NB: These visualizations rely on having a baseband image running.
@@ -147,6 +153,7 @@ class WaterfallView : public View {
void set_parent_rect(const Rect new_parent_rect) override;
void show_audio_spectrum_view(const bool show);
void load_gradient();
void set_live_tuning(const bool enabled) { frequency_scale.set_live_tuning(enabled); }
private:
void update_widgets_rect();