diff --git a/Cargo.lock b/Cargo.lock index 0d19b3f..bc930f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -796,9 +796,9 @@ checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" [[package]] name = "proc-macro-crate" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d50bfb8c23f23915855a00d98b5a35ef2e0b871bb52937bacadb798fbb66c8" +checksum = "eda0fc3b0fb7c975631757e14d9049da17374063edb6ebbcbc54d880d4fe94e9" dependencies = [ "once_cell", "thiserror", @@ -948,18 +948,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.142" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2" +checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.142" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34b5b8d809babe02f538c2cfec6f2c1ed10804c0e5a6a041a049a4f5588ccc2e" +checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391" dependencies = [ "proc-macro2", "quote", @@ -1091,12 +1091,11 @@ dependencies = [ [[package]] name = "time" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74b7cc93fc23ba97fde84f7eea56c55d1ba183f495c6715defdfc7b9cb8c870f" +checksum = "db76ff9fa4b1458b3c7f077f3ff9887394058460d21e634355b273aaf11eea45" dependencies = [ "itoa", - "js-sys", "libc", "num_threads", "time-macros", diff --git a/Cargo.toml b/Cargo.toml index 51ba392..d66c7db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/utils/flacstream.rs b/src/utils/flacstream.rs index 71789f3..49fc0da 100644 --- a/src/utils/flacstream.rs +++ b/src/utils/flacstream.rs @@ -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, @@ -10,6 +11,7 @@ use std::{ time::Duration, }; +#[cfg(feature = "NOISE")] use crate::ui_log; const NOISE_PERIOD: u64 = 250; @@ -103,23 +105,33 @@ 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 = 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 = 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) @@ -127,21 +139,27 @@ impl FlacChannel { 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::>(); - 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::>(); + 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; + } } } } @@ -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) ///