Skip to content

Commit

Permalink
Merge pull request #13 from ratzrattillo/agc
Browse files Browse the repository at this point in the history
Add AGC block
  • Loading branch information
ratzrattillo authored Jul 28, 2024
2 parents 0d86666 + 4517f0e commit 79b6499
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.idea
.vscode
config.toml
/target
/Cargo.lock
target
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ members = [
[dependencies]
futuresdr = { git = "https://github.com/FutureSDR/FutureSDR", branch = "main" }
#futuresdr = { path = "../FutureSDR" }
async-channel = { version = "2.2.0", optional = true }
async-trait = "0.1.78"
crossbeam-channel = { version = "0.5.12", optional = true }
async-channel = { version = "2.3.1", optional = true }
async-trait = "0.1.81"
crossbeam-channel = { version = "0.5.13", optional = true }
bimap = { version = "0.6.3", optional = true }
sigmf = { version = "0.1.0", path = "crates/sigmf" }
async-fs = "2.1.1"
serde = "1.0.197"
async-fs = "2.1.2"
serde = "1.0.204"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
tokio-test = "0.4.4"
rand = { version = "0.8.5" }
quickcheck_macros = "1"
serde_json = "1.0.114"
serde_json = "1.0.120"

[features]
default = []
Expand Down
7 changes: 5 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ SCRIPTPATH=`dirname $SCRIPT`
# FMT
###########################################################
cd ${SCRIPTPATH} && cargo fmt --check
#cd ${SCRIPTPATH}/examples/agc && cargo fmt --check

###########################################################
# CLIPPY
###########################################################
cd ${SCRIPTPATH} && cargo clippy --all-targets --all-features --workspace
cd ${SCRIPTPATH} && cargo clippy --all-targets --all-features --workspace
# cd ${SCRIPTPATH}/examples/agc && cargo clippy --all-targets -- -D warnings

###########################################################
# Test
###########################################################
cd ${SCRIPTPATH} && cargo test --all-targets --all-features --workspace
cd ${SCRIPTPATH} && cargo test --all-targets --all-features --workspace
# cd ${SCRIPTPATH}/examples/agc && cargo test --all-targets
13 changes: 13 additions & 0 deletions examples/agc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "agc"
version = "0.0.1"
edition = "2021"

[workspace]

[[bin]]
name = "audio-agc"
path = "audio_agc.rs"

[dependencies]
futuresdr = { path = "../..", features = ["audio"] }
75 changes: 75 additions & 0 deletions examples/agc/audio_agc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use futuresdr::anyhow::Result;
use futuresdr::async_io;
use futuresdr::blocks::audio::AudioSink;
use futuresdr::blocks::{AgcBuilder, Combine, SignalSourceBuilder};
use futuresdr::macros::connect;
use futuresdr::runtime::Flowgraph;
use futuresdr::runtime::Pmt;
use futuresdr::runtime::Runtime;
use std::thread::sleep;
use std::time::Duration;

fn main() -> Result<()> {
let mut fg = Flowgraph::new();

// Generate 220Hz tone
let src = SignalSourceBuilder::<f32>::sin(220.0, 48_000.0)
.amplitude(0.4)
.build();
// Modulation Wave for the 220Hz tone, changing from loud to silent every second
let gain_change = SignalSourceBuilder::<f32>::sin(0.5, 48_000.0)
.amplitude(0.5)
.build();
// Modulate Tone with the modulation wave
let combine = Combine::new(|a: &f32, b: &f32| a * b);
// Set the Automatic Gain Control settings
let agc = AgcBuilder::<f32>::new()
.squelch(0.0)
.max_gain(65536.0)
.adjustment_rate(0.1)
.reference_power(1.0)
.build();
let gain_locked_handler_id = agc.message_input_name_to_id("gain_locked").unwrap();
let max_gain_handler_id = agc.message_input_name_to_id("max_gain").unwrap();
let _adjustment_rate_handler_id = agc.message_input_name_to_id("adjustment_rate").unwrap();
let reference_power_handler_id = agc.message_input_name_to_id("reference_power").unwrap();

// Audiosink to output the modulated tone
let audio_snk = AudioSink::new(48_000, 1);

connect!(fg,
src > combine.in0;
gain_change.out > combine.in1;
combine > agc > audio_snk;
);

// Start the flowgraph and save the handle
let rt = Runtime::new();
let (_res, mut handle) = rt.start_sync(fg);

// Keep changing gain and gain lock.
loop {
// Reference power of 1.0 is the power level we want to achieve
println!("Setting reference power to 1.0");
async_io::block_on(handle.call(agc, reference_power_handler_id, Pmt::F32(1.0)))?;

// A high max gain allows to amplify a signal stronger
println!("Setting Max Gain to 65536.0");
async_io::block_on(handle.call(agc, max_gain_handler_id, Pmt::F32(65536.0)))?;
sleep(Duration::from_secs(5));

// Setting a gain lock prevents gain changes from happening
println!("Setting gain lock for 5s");
async_io::block_on(handle.call(agc, gain_locked_handler_id, Pmt::Bool(true)))?;

// Audio should get quiet faster, but gain is still locked here. it will be released after 5 seconds
println!("Setting reference power to 0.2");
async_io::block_on(handle.call(agc, reference_power_handler_id, Pmt::F32(0.2)))?;
sleep(Duration::from_secs(5));

// Gain lock released! Audio should get more quiet here for 10 seconds
println!("Releasing gain lock");
async_io::block_on(handle.call(agc, gain_locked_handler_id, Pmt::Bool(false)))?;
sleep(Duration::from_secs(10));
}
}
Loading

0 comments on commit 79b6499

Please sign in to comment.