use std::fs;
use std::io::{BufRead, BufReader};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use crate::config;
pub struct Cava {
values: Arc<Mutex<Vec<f64>>>,
alive: Arc<Mutex<bool>>,
child: Option<Child>,
error: Option<String>,
}
fn input_blocks() -> &'static [&'static str] {
if cfg!(target_os = "linux") {
&[
"[input]\nmethod = pipewire\nsource = auto\n",
"[input]\nmethod = pulse\nsource = auto\n",
]
} else if cfg!(target_os = "macos") {
&[
"[input]\nmethod = coreaudio\nsource = tap\n",
"[input]\nmethod = coreaudio\nsource = auto\n",
]
} else {
&[""]
}
}
fn config_text(bars: u16, input: &str) -> String {
format!(
"[general]\nframerate = 60\nbars = {bars}\nautosens = 1\n\
lower_cutoff_freq = 50\nhigher_cutoff_freq = 12000\n\n\
{input}\
[smoothing]\nnoise_reduction = 20\n\n\
[output]\nmethod = raw\n\
data_format = ascii\nascii_max_range = 1000\n\
bar_delimiter = 59\nframe_delimiter = 10\n"
)
}
struct Spawned {
child: Child,
values: Arc<Mutex<Vec<f64>>>,
alive: Arc<Mutex<bool>>,
}
fn spawn_with(config_path: &std::path::Path, bars: u16, input: &str) -> Result<Spawned, String> {
let raw = config_text(bars, input);
fs::write(config_path, raw).map_err(|err| format!("could not write cava config: {err}"))?;
let mut command = Command::new("cava");
command
.arg("-p")
.arg(config_path)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null());
crate::ipc::die_with_parent(&mut command);
let mut child = command.spawn().map_err(|err| {
if err.kind() == std::io::ErrorKind::NotFound {
"install cava for the spectrum visualiser".to_string()
} else {
format!("cava failed to start: {err}")
}
})?;
let values = Arc::new(Mutex::new(vec![0.0; bars as usize]));
let alive = Arc::new(Mutex::new(true));
if let Some(stdout) = child.stdout.take() {
let shared = Arc::clone(&values);
let alive_reader = Arc::clone(&alive);
thread::Builder::new()
.name("cava-reader".to_string())
.spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
let Ok(line) = line else { break };
let parsed: Vec<f64> = line
.split(';')
.filter(|part| !part.trim().is_empty())
.filter_map(|part| part.trim().parse::<f64>().ok())
.map(|value| (value / 1000.0).clamp(0.0, 1.0))
.collect();
if parsed.is_empty() {
continue;
}
if let Ok(mut current) = shared.lock() {
if current.len() != parsed.len() {
*current = parsed;
} else {
for (slot, value) in current.iter_mut().zip(parsed) {
*slot = *slot * 0.25 + value * 0.75;
}
}
}
}
if let Ok(mut alive) = alive_reader.lock() {
*alive = false;
}
})
.ok();
}
Ok(Spawned {
child,
values,
alive,
})
}
impl Cava {
pub fn spawn(bars: u16) -> Self {
let config_path = config::runtime_dir().join("cava.conf");
if let Some(parent) = config_path.parent() {
let _ = fs::create_dir_all(parent);
}
let bars = bars.clamp(4, 96);
let mut last_error = None;
for input in input_blocks() {
let mut spawned = match spawn_with(&config_path, bars, input) {
Ok(spawned) => spawned,
Err(err) => {
last_error = Some(err);
continue;
}
};
thread::sleep(std::time::Duration::from_millis(160));
match spawned.child.try_wait() {
Ok(Some(_)) => {
last_error = Some("cava could not open an audio source".to_string());
continue;
}
_ => {
return Self {
values: spawned.values,
alive: spawned.alive,
child: Some(spawned.child),
error: None,
};
}
}
}
Self {
values: Arc::new(Mutex::new(vec![0.0; bars as usize])),
alive: Arc::new(Mutex::new(false)),
child: None,
error: Some(
last_error.unwrap_or_else(|| "cava could not open an audio source".to_string()),
),
}
}
pub fn values(&self) -> Vec<f64> {
self.values
.lock()
.map(|values| values.clone())
.unwrap_or_default()
}
pub fn error(&self) -> Option<&str> {
self.error.as_deref()
}
pub fn running(&self) -> bool {
self.child.is_some() && self.alive.lock().map(|alive| *alive).unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn config_never_hardcodes_pipewire_on_other_platforms() {
let inputs = input_blocks();
assert!(!inputs.is_empty());
if cfg!(target_os = "linux") {
assert!(inputs[0].contains("pipewire"));
assert!(inputs.iter().any(|block| block.contains("pulse")));
} else if cfg!(target_os = "macos") {
assert!(inputs[0].contains("coreaudio"));
assert!(inputs[0].contains("source = tap"));
} else if cfg!(windows) {
assert_eq!(inputs, &[""]);
}
}
#[test]
fn raw_output_does_not_depend_on_dev_stdout() {
let text = config_text(28, "");
assert!(text.contains("method = raw"));
assert!(!text.contains("/dev/stdout"));
assert!(text.contains("framerate = 60"));
}
}