Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
logging and flac encoding interruption
  • Loading branch information
dheijl committed Aug 18, 2024
1 parent 5ecce89 commit 34b315d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# swyh-rs Changelog

- 1.11.2 (unreleased)
- clean up log initializing mess
- handle flac encoding interruption better

- 1.11.1 (Aug 16 2024 dheijl)
- Rust 1.80.1
Expand Down
28 changes: 14 additions & 14 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 @@ -81,7 +81,7 @@ winres = "0.1.12"
[target.'cfg(unix)'.dependencies]
fltk = { version = "1.4.33", features = ["use-ninja"], optional = true }
fltk-theme = { version = "0.7.3", optional = true }
libc = "0.2.156"
libc = "0.2.157"

[patch.crates-io]
#fltk = { git = "https://github.com/fltk-rs/fltk-rs" }
Expand Down
14 changes: 7 additions & 7 deletions src/bin/swyh-rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ fn main() {
let config_changed: Rc<Cell<bool>> = Rc::new(Cell::new(false));

// configure simplelogger
let loglevel = config.log_level;
let config_id = config.config_id.clone().unwrap();
let logfilename = "log{}.txt".replace("{}", &config_id);
let logfile = Path::new(&config.log_dir()).join(logfilename);
if cfg!(debug_assertions) {
config.log_level = LevelFilter::Debug;
}
let loglevel = config.log_level;
// disable TermLogger on susbsystem Windows because it panics now with Rust edition 2021
if cfg!(debug_assertions) || cfg!(target_os = "linux") {
let _ = CombinedLogger::init(vec![
Expand All @@ -125,20 +128,17 @@ fn main() {
std::env::consts::FAMILY,
std::env::consts::OS
);
if cfg!(debug_assertions) {
ui_log("*W*W*>Running DEBUG build => log level set to DEBUG!");
}

if let Some(config_id) = &config.config_id {
if !config_id.is_empty() {
ui_log(&format!("Loaded configuration -c {config_id}"));
}
}
ui_log(&format!("{config:?}"));
if cfg!(debug_assertions) {
config.log_level = LevelFilter::Debug;
}

if cfg!(debug_assertions) {
ui_log("*W*W*>Running DEBUG build => log level set to DEBUG!");
}
info!("Config: {:?}", config);

// get the output device from the config and get all available audio source names
Expand Down
6 changes: 3 additions & 3 deletions src/ui/mainform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,14 +689,14 @@ impl MainForm {
let local_addr = self.local_addr;
let wd = self.wd;
move |b| {
debug!(
info!(
"Pushed renderer #{} {} {}, state = {}",
bi,
newr_c.dev_model,
newr_c.dev_name,
if b.is_set() { "ON" } else { "OFF" }
if b.is_on() { "ON" } else { "OFF" },
);
if b.is_set() {
if b.is_on() {
{
let mut conf = CONFIG.write();
conf.last_renderer = Some(b.label());
Expand Down
23 changes: 13 additions & 10 deletions src/utils/flacstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crossbeam_channel::{unbounded, Receiver, Sender};
use dasp_sample::Sample;
use fastrand::Rng;
use flac_bound::{FlacEncoder, WriteWrapper};
use log::info;
use std::{
io::Write,
sync::{
Expand All @@ -11,8 +12,6 @@ use std::{
time::Duration,
};

use crate::utils::ui_logger::ui_log;

const NOISE_PERIOD_MS: u64 = 250; // milliseconds

// the flacwriter receives the data from the encoder
Expand Down Expand Up @@ -122,8 +121,13 @@ impl FlacChannel {
.iter()
.map(|s| s.to_sample::<i32>() >> shift)
.collect::<Vec<i32>>();
enc.process_interleaved(samples.as_slice(), (samples.len() / 2) as u32)
.unwrap();
if enc
.process_interleaved(samples.as_slice(), (samples.len() / 2) as u32)
.is_err()
{
info!("Flac encoding interrupted.");
break;
}
} else {
time_out = Duration::from_millis(NOISE_PERIOD_MS * 2);
// if no samples for a certain time: send very faint near silence bursts
Expand All @@ -133,12 +137,11 @@ impl FlacChannel {
.iter()
.map(|s| (s.to_sample::<i32>() >> shift) & 0x3)
.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 inject near silence: end {e:?}"));
if enc
.process_interleaved(samples.as_slice(), (samples.len() / 2) as u32)
.is_err()
{
info!("Flac inject near silence interrupted.");
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/rwstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl ChannelStream {
// called by the wave_reader to write the f32 samples to the input channel
pub fn write(&self, samples: &[f32]) {
// don't blow up memory if streaming stalls for some reason
// 10_000 messages (capture buffers, not samples) is a quite a lot
if self.s.len() < 10_000 {
self.s.send(samples.to_vec()).unwrap();
}
Expand Down

0 comments on commit 34b315d

Please sign in to comment.