Skip to content

Commit

Permalink
disable white noise for Flac in absence of sound
Browse files Browse the repository at this point in the history
  • Loading branch information
dheijl committed Aug 9, 2022
1 parent 746cbb2 commit 64318a9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
17 changes: 8 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ log = { version = "0.4.17", features = [
] }
once_cell = "1.13.0"
parking_lot = "0.12.1"
serde = { version = "1.0.142", features = ["derive"] }
serde = { version = "1.0.143", features = ["derive"] }
simplelog = "0.12.0"
rand = "0.8.5"
strfmt = "0.2.1"
Expand Down
77 changes: 48 additions & 29 deletions src/utils/flacstream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crossbeam_channel::{unbounded, Receiver, Sender};
use flac_bound::{FlacEncoder, WriteWrapper};
#[cfg(feature = "NOISE")]
use rand::{distributions::Uniform, rngs::StdRng, Rng, SeedableRng};
use std::{
io::Write,
Expand All @@ -10,6 +11,7 @@ use std::{
time::Duration,
};

#[cfg(feature = "NOISE")]
use crate::ui_log;

const NOISE_PERIOD: u64 = 250;
Expand Down Expand Up @@ -103,45 +105,61 @@ impl FlacChannel {
.unwrap();
// read captured samples and encode
let shift = if bps == 24 { 8u8 } else { 16u8 };
let mut sending_silence = false;
// create the random generatir for the white noise
let mut rng = StdRng::seed_from_u64(79);
// preallocate the noise buffer
const DIVISOR: u64 = 1000 / NOISE_PERIOD;
let size = ((sr * 2) / DIVISOR as u32) as usize;
let mut noise: Vec<f32> = Vec::with_capacity(size);
noise.resize(size, 0.0);
#[cfg(feature = "NOISE")]
{
// create the random generator for the white noise
let mut rng = StdRng::seed_from_u64(79);
// preallocate the noise buffer
const DIVISOR: u64 = 1000 / NOISE_PERIOD;
let size = ((sr * 2) / DIVISOR as u32) as usize;
let mut noise: Vec<f32> = Vec::with_capacity(size);
noise.resize(size, 0.0);
let mut sending_silence = false;
}
// read and FLAC encode samples
let time_out = Duration::from_millis(NOISE_PERIOD);
while l_active.load(Relaxed) {
let time_out = if sending_silence {
Duration::from_millis(NOISE_PERIOD)
} else {
Duration::from_millis(NOISE_PERIOD * 4)
};
#[cfg(feature = "NOISE")]
{
let time_out = if sending_silence {
Duration::from_millis(NOISE_PERIOD)
} else {
Duration::from_millis(NOISE_PERIOD * 4)
};
}
if let Ok(f32_samples) = samples_in.recv_timeout(time_out) {
sending_silence = false;
#[cfg(feature = "NOISE")]
{
sending_silence = false;
}
let samples = f32_samples
.iter()
.map(|s| to_i32_sample(*s) >> shift)
.collect::<Vec<i32>>();
enc.process_interleaved(samples.as_slice(), (samples.len() / 2) as u32)
.unwrap();
} else {
// if no samples for a certain time: send a faint white noise
sending_silence = true;
if l_active.load(Relaxed) {
fill_noise_buffer(&mut rng, &mut noise);
let samples = noise
.iter()
.map(|s| to_i32_sample(*s) >> shift)
.collect::<Vec<i32>>();
let res = enc.process_interleaved(
samples.as_slice(),
(samples.len() / 2) as u32,
);
if let Err(e) = res {
ui_log(format!("Flac encoding error caused by silence {:?}", e));
break;
#[cfg(feature = "NOISE")]
{
// if no samples for a certain time: send a faint white noise
sending_silence = true;
if l_active.load(Relaxed) {
fill_noise_buffer(&mut rng, &mut noise);
let samples = noise
.iter()
.map(|s| to_i32_sample(*s) >> shift)
.collect::<Vec<i32>>();
let res = enc.process_interleaved(
samples.as_slice(),
(samples.len() / 2) as u32,
);
if let Err(e) = res {
ui_log(format!(
"Flac encoding error caused by silence {:?}",
e
));
break;
}
}
}
}
Expand Down Expand Up @@ -169,6 +187,7 @@ fn to_i32_sample(mut f32_sample: f32) -> i32 {
}
}

#[cfg(feature = "NOISE")]
///
/// fille the pre-allocated noise buffer with a very faint white noise (-60db)
///
Expand Down

0 comments on commit 64318a9

Please sign in to comment.