mod cli {
pub mod inputs;
pub mod terminal;
}
use std::borrow::Cow;
use std::io::{self, IsTerminal, Write};
use std::num::NonZeroUsize;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError};
use std::thread;
use std::time::{Duration, Instant};
use choon::grid::{Evidence, Family, Scan};
use choon::{
CancelCheck, Claimed, Coverage, Error, Flag, HeAac, MeasureThresholds, Measured, OpusUse,
Reading, ReadingThresholds, Settings, Verdict,
};
use clap::{Args, ColorChoice, Parser, Subcommand};
use rayon::iter::{ParallelBridge, ParallelIterator};
use serde::{Deserialize, Serialize};
use crate::cli::inputs::{EXTENSIONS, Inputs};
use crate::cli::terminal::{self, Bars, Row};
const CLEAN: u8 = 0;
const LOSSY: u8 = 1;
const FAILED: u8 = 2;
#[derive(Parser)]
#[command(version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
#[command(after_help = "\
Exit status:
0 every file is consistent with its format, possibly lossy or inconclusive
1 at least one file reads as a likely transcode or mixed sources
2 a file could not be read, or an argument was wrong
A file the analyzer cannot open as audio is one that could not be read.")]
Check(Check),
}
#[derive(Args)]
struct Check {
#[arg(
required_unless_present = "defaults",
value_name = "PATH",
long_help = paths_help()
)]
paths: Vec<PathBuf>,
#[arg(long)]
json: bool,
#[arg(long, value_name = "FILE")]
profile: Vec<PathBuf>,
#[arg(long, exclusive = true)]
defaults: bool,
#[arg(
long,
value_name = "MINUTES",
requires = "sampled_windows",
value_parser = minutes
)]
max_full_minutes: Option<f64>,
#[arg(long, value_name = "COUNT", requires = "max_full_minutes")]
sampled_windows: Option<NonZeroUsize>,
#[arg(short, long, value_name = "N", value_parser = jobs)]
jobs: Option<NonZeroUsize>,
#[arg(short = '0', long)]
null: bool,
#[arg(short = 'L', long)]
follow: bool,
#[arg(short, long)]
verbose: bool,
#[arg(long, value_name = "WHEN", default_value_t = ColorChoice::Auto)]
color: ColorChoice,
}
fn paths_help() -> String {
format!(
"Audio files and directories to read, or - for the paths listed on stdin\n\n\
A directory is read with the directories under it. Hidden files and directories are left \
out. Of the others, files with one of these extensions are read, in upper or lower case: \
{}. A file named here or on stdin is read whatever its name. Stdin lists one path per line.",
EXTENSIONS.join(", ")
)
}
#[derive(Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct Thresholds {
measure: MeasureThresholds,
reading: ReadingThresholds,
}
struct Checked {
claimed: Claimed,
duration: f64,
bitrate_kbps: Option<u32>,
sampled: bool,
windows: usize,
grid: Option<Scan>,
opus: Vec<OpusUse>,
he_aac: Option<HeAac>,
dsd_rate: Option<u32>,
dsd_multiple: Option<u32>,
readings: Vec<Read>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Analyzed<'a> {
file: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
file_bytes: Option<&'a [u8]>,
claimed: &'a Claimed,
duration_seconds: f64,
bitrate_kbps: Option<u32>,
sampled: bool,
windows: usize,
grid: Option<&'a Scan>,
#[serde(skip_serializing_if = "<[OpusUse]>::is_empty")]
opus: &'a [OpusUse],
#[serde(skip_serializing_if = "Option::is_none")]
he_aac: Option<HeAac>,
#[serde(skip_serializing_if = "Option::is_none")]
dsd_rate: Option<u32>,
readings: &'a [Read],
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Read {
profile: String,
measure_seconds: f64,
read_seconds: f64,
#[serde(flatten)]
reading: Reading,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct NotAudio<'a> {
file: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
file_bytes: Option<&'a [u8]>,
error: String,
}
fn main() -> ExitCode {
let Command::Check(check) = Cli::parse().command;
match run(&check) {
Ok(code) => ExitCode::from(code),
Err(e) => {
eprintln!("choon: {e}");
ExitCode::from(FAILED)
}
}
}
enum Failed {
Profile(PathBuf, String),
Stdin(io::Error),
Threads(rayon::ThreadPoolBuildError),
Output(io::Error),
}
enum Event {
Started {
file: usize,
progress: Vec<Arc<AtomicU32>>,
},
Done {
file: usize,
result: Result<Box<Checked>, Unread>,
},
}
enum Unread {
Open(io::Error),
NotRead(String),
}
const TICK: Duration = Duration::from_millis(100);
impl std::fmt::Display for Failed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Profile(path, e) => write!(f, "{}: {e}", path.display()),
Self::Stdin(e) => write!(f, "reading the paths on stdin: {e}"),
Self::Threads(e) => write!(f, "starting the threads: {e}"),
Self::Output(e) => write!(f, "writing the output: {e}"),
}
}
}
fn run(check: &Check) -> Result<u8, Failed> {
if check.defaults {
let defaults =
serde_json::to_string_pretty(&Thresholds::default()).expect("thresholds serialize");
print(&mut io::stdout().lock(), &defaults)?;
return Ok(CLEAN);
}
let profiles = if check.profile.is_empty() {
vec![("default".to_string(), Thresholds::default())]
} else {
let profiles: Vec<(String, Thresholds)> = check
.profile
.iter()
.map(|path| profile(path))
.collect::<Result<_, _>>()?;
for (at, (name, _)) in profiles.iter().enumerate() {
if profiles[..at].iter().any(|(earlier, _)| earlier == name) {
let why = format!("another profile is named {name} too");
return Err(Failed::Profile(check.profile[at].clone(), why));
}
}
profiles
};
let coverage = match (check.max_full_minutes, check.sampled_windows) {
(Some(minutes), Some(windows)) => Coverage::Sampled {
over_seconds: minutes * 60.0,
windows,
},
_ => Coverage::Full,
};
let inputs = Inputs::gather(
&check.paths,
&mut io::stdin().lock(),
check.null,
check.follow,
)
.map_err(Failed::Stdin)?;
let mut code = CLEAN;
for problem in &inputs.problems {
eprintln!("{}", complaint(&problem.path, &problem.why));
if problem.unread {
code = FAILED;
}
}
let files = inputs.files;
if files.is_empty() {
eprintln!("choon: no audio files found");
return Ok(code);
}
let jobs = match check.jobs {
Some(jobs) => jobs.get(),
None => thread::available_parallelism().map_or(1, NonZeroUsize::get),
};
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(jobs)
.build()
.map_err(Failed::Threads)?;
let started = Instant::now();
let stop = Arc::new(AtomicBool::new(false));
let canceled: CancelCheck = {
let stop = stop.clone();
Arc::new(move || stop.load(Ordering::Relaxed))
};
let measurings = profiles
.iter()
.enumerate()
.filter(|(at, (_, thresholds))| {
profiles[..*at]
.iter()
.all(|(_, earlier)| earlier.measure != thresholds.measure)
})
.count();
let (events, received) = mpsc::sync_channel(jobs);
let read = {
let (stop, profiles) = (&*stop, &profiles);
move |(file, path): (usize, &PathBuf)| {
if stop.load(Ordering::Relaxed) {
return;
}
let progress: Vec<Arc<AtomicU32>> = (0..measurings).map(|_| Arc::default()).collect();
let event = Event::Started {
file,
progress: progress.clone(),
};
if events.send(event).is_err() {
return;
}
let run = AssertUnwindSafe(|| analyzed(path, profiles, coverage, &progress, &canceled));
let result = match catch_unwind(run) {
Ok(Ok(checked)) => Ok(Box::new(checked)),
Ok(Err(Error::Canceled)) => return,
Ok(Err(Error::Open(e))) => Err(Unread::Open(e)),
Ok(Err(Error::NotAudio(why) | Error::Unsupported(why))) => Err(Unread::NotRead(why)),
Ok(Err(Error::OutOfTime)) => unreachable!("no analysis has a deadline"),
Ok(Err(Error::Panicked)) | Err(_) => Err(Unread::NotRead(Error::Panicked.to_string())),
};
let _ = events.send(Event::Done { file, result });
}
};
thread::scope(|scope| {
let (pool, files) = (&pool, &files);
scope.spawn(move || pool.install(|| files.iter().enumerate().par_bridge().for_each(read)));
let code = if !check.json && io::stdout().is_terminal() {
shown(received, files, check, profiles.len() > 1, started, code)
} else {
printed(received, files, check.json, code)
};
stop.store(true, Ordering::Relaxed);
code
})
}
fn status(result: &Result<Box<Checked>, Unread>) -> u8 {
match result {
Ok(checked)
if checked
.readings
.iter()
.any(|read| lossy(read.reading.verdict)) =>
{
LOSSY
}
Ok(_) => CLEAN,
Err(_) => FAILED,
}
}
fn printed(
received: Receiver<Event>,
files: &[PathBuf],
json: bool,
mut code: u8,
) -> Result<u8, Failed> {
let mut out = io::stdout().lock();
for event in received {
let Event::Done { file, result } = event else {
continue;
};
let file = &files[file];
code = code.max(status(&result));
let line = match result {
Ok(checked) => {
if json {
serde_json::to_string(&Analyzed {
file: file.to_string_lossy(),
file_bytes: raw(file),
claimed: &checked.claimed,
duration_seconds: checked.duration,
bitrate_kbps: checked.bitrate_kbps,
sampled: checked.sampled,
windows: checked.windows,
grid: checked.grid.as_ref(),
opus: &checked.opus,
he_aac: checked.he_aac,
dsd_rate: checked.dsd_rate,
readings: &checked.readings,
})
.expect("a reading serializes")
} else {
piped(file, &checked)
}
}
Err(Unread::Open(e)) => {
eprintln!("{}", complaint(file, e));
continue;
}
Err(Unread::NotRead(why)) => {
if json {
serde_json::to_string(&NotAudio {
file: file.to_string_lossy(),
file_bytes: raw(file),
error: why,
})
.expect("strings and bytes serialize")
} else {
format!("{}: {NOT_READ} ({why})", file.display())
}
}
};
if !print(&mut out, &line)? {
return Ok(code);
}
}
Ok(code)
}
fn shown(
received: Receiver<Event>,
files: &[PathBuf],
check: &Check,
profiled: bool,
started: Instant,
mut code: u8,
) -> Result<u8, Failed> {
let colour = match check.color {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => {
anstream::AutoStream::choice(&io::stdout()) != anstream::ColorChoice::Never
}
};
let one = files.len() == 1;
let mut bars = Bars::new(files.len());
let mut rows = Vec::with_capacity(files.len());
loop {
match received.recv_timeout(TICK) {
Ok(Event::Started { file, progress }) => {
let name = files[file].file_name().unwrap_or(files[file].as_os_str());
bars.started(file, terminal::legible(Path::new(name)), progress);
}
Ok(Event::Done { file, result }) => {
bars.ended(file);
let path = &files[file];
code = code.max(status(&result));
let outcome = match result {
Ok(checked) => {
let flagged = checked
.readings
.iter()
.any(|read| flagged(read.reading.verdict));
if !one && (flagged || check.verbose) {
bars
.say(&listed(path, &checked, colour))
.map_err(Failed::Output)?;
}
Ok(checked)
}
Err(Unread::Open(e)) => {
bars.complain(&complaint(path, &e));
Err(e.to_string())
}
Err(Unread::NotRead(why)) => {
bars
.say(¬_read(path, &why, colour))
.map_err(Failed::Output)?;
Err(why)
}
};
rows.push(Row { path, outcome });
}
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => break,
}
bars.tick();
}
bars.finish();
rows.sort_by_key(|row| row.path);
let width = console::Term::stdout().size().1;
let text = match &rows[..] {
[
Row {
path,
outcome: Ok(checked),
},
] => terminal::readout(path, checked, profiled, check.verbose, colour, width),
[_] => return Ok(code),
_ => terminal::tables(
&rows,
profiled,
check.verbose,
started.elapsed(),
colour,
width,
),
};
print(&mut io::stdout().lock(), text.trim_end())?;
Ok(code)
}
#[cfg(unix)]
fn raw(path: &Path) -> Option<&[u8]> {
use std::os::unix::ffi::OsStrExt;
path.to_str().is_none().then(|| path.as_os_str().as_bytes())
}
#[cfg(not(unix))]
fn raw(_: &Path) -> Option<&[u8]> {
None
}
fn complaint(path: &Path, why: impl std::fmt::Display) -> String {
if io::stderr().is_terminal() {
format!("choon: {}: {why}", terminal::legible(path))
} else {
format!("choon: {}: {why}", path.display())
}
}
fn print(out: &mut impl Write, line: &str) -> Result<bool, Failed> {
match writeln!(out, "{line}") {
Ok(()) => Ok(true),
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(false),
Err(e) => Err(Failed::Output(e)),
}
}
fn lossy(verdict: Verdict) -> bool {
match verdict {
Verdict::LikelyTranscode | Verdict::MixedSources => true,
Verdict::Consistent | Verdict::PossiblyLossy | Verdict::Inconclusive => false,
}
}
fn flagged(verdict: Verdict) -> bool {
lossy(verdict) || verdict == Verdict::PossiblyLossy
}
fn label(verdict: Verdict) -> &'static str {
match verdict {
Verdict::Consistent => "consistent",
Verdict::LikelyTranscode => "likely transcode",
Verdict::MixedSources => "mixed sources",
Verdict::PossiblyLossy => "possibly lossy",
Verdict::Inconclusive => "inconclusive",
}
}
fn encoder(reading: &Reading) -> Option<String> {
let profile = reading
.encoder_guess
.as_ref()
.filter(|_| flagged(reading.verdict))
.map(|guess| &guess.profile);
let Some(grid) = &reading.grid else {
return profile.map(ToString::to_string);
};
let codec = grid.codec;
let setting = profile
.filter(|_| matches!(grid.family, None | Some(Family::Lame | Family::Untagged)))
.filter(|profile| profile.codec == codec.to_string())
.map(|profile| profile.setting.as_str());
match (grid.family.and_then(family_words), setting) {
(None, None) => None,
(None, Some(setting)) => Some(format!("{codec} {setting}")),
(Some(family), None) => Some(format!("{codec} ({family})")),
(Some(family), Some(setting)) => Some(format!("{codec} ({family}) {setting}")),
}
}
const MAX_JOBS: usize = 1024;
fn jobs(text: &str) -> Result<NonZeroUsize, String> {
let jobs: NonZeroUsize = text.parse().map_err(|e| format!("{e}"))?;
if jobs.get() > MAX_JOBS {
return Err(format!("more than {MAX_JOBS} jobs"));
}
Ok(jobs)
}
fn minutes(text: &str) -> Result<f64, String> {
let minutes: f64 = text.parse().map_err(|e| format!("{e}"))?;
if !minutes.is_finite() || minutes <= 0.0 {
return Err("not a length in minutes".to_string());
}
Ok(minutes)
}
fn profile(path: &Path) -> Result<(String, Thresholds), Failed> {
let failed = |e: String| Failed::Profile(path.to_path_buf(), e);
let text = std::fs::read_to_string(path).map_err(|e| failed(e.to_string()))?;
let thresholds = serde_json::from_str(&text).map_err(|e| failed(e.to_string()))?;
let name = path.file_stem().map_or_else(
|| path.display().to_string(),
|stem| stem.to_string_lossy().into_owned(),
);
Ok((name, thresholds))
}
fn analyzed(
path: &Path,
profiles: &[(String, Thresholds)],
coverage: Coverage,
progress: &[Arc<AtomicU32>],
canceled: &CancelCheck,
) -> Result<Checked, Error> {
let mut measured: Vec<(&MeasureThresholds, Measured, f64)> = Vec::new();
let mut readings = Vec::new();
for (name, thresholds) in profiles {
let at = match measured
.iter()
.position(|(alike, ..)| **alike == thresholds.measure)
{
Some(at) => at,
None => {
let settings = Settings {
coverage,
thresholds: thresholds.measure.clone(),
progress: Some(progress[measured.len()].clone()),
canceled: Some(canceled.clone()),
..Settings::default()
};
let started = Instant::now();
let done = choon::measure(path, &settings)?;
measured.push((&thresholds.measure, done, started.elapsed().as_secs_f64()));
measured.len() - 1
}
};
let (_, done, measure_seconds) = &measured[at];
let started = Instant::now();
let reading = done.read(&thresholds.reading);
readings.push(Read {
profile: name.clone(),
measure_seconds: *measure_seconds,
read_seconds: started.elapsed().as_secs_f64(),
reading,
});
}
let (_, first, _) = measured.swap_remove(0);
Ok(Checked {
dsd_multiple: first.dsd_multiple(),
claimed: first.claimed,
duration: first.duration,
bitrate_kbps: first.bitrate_kbps,
sampled: first.sampled,
windows: first.windows.len(),
grid: first.grid,
opus: first.opus,
he_aac: first.he_aac,
dsd_rate: first.dsd_rate,
readings,
})
}
const VERDICT_WIDTH: usize = "likely transcode".len();
fn piped(path: &Path, checked: &Checked) -> String {
format!("{}: {}", path.display(), said(checked, false))
}
fn listed(path: &Path, checked: &Checked, colour: bool) -> String {
let path = terminal::legible(path);
let [read] = &checked.readings[..] else {
return format!("{} {path}", said(checked, colour));
};
let reading = &read.reading;
let mut found = found(reading, checked.grid.is_some());
found.extend(kind(checked));
status_line(
&terminal::painted(Some(reading.verdict), label(reading.verdict), colour),
label(reading.verdict).len(),
&found.join(", "),
&path,
)
}
fn not_read(path: &Path, why: &str, colour: bool) -> String {
status_line(
&terminal::painted(None, NOT_READ, colour),
NOT_READ.len(),
why,
&terminal::legible(path),
)
}
const NOT_READ: &str = "not read";
fn status_line(status: &str, width: usize, found: &str, path: &str) -> String {
let pad = " ".repeat(VERDICT_WIDTH.saturating_sub(width));
if found.is_empty() {
format!("{status}{pad} {path}")
} else {
format!("{status}{pad} ({found}) {path}")
}
}
fn said(checked: &Checked, colour: bool) -> String {
let kind = kind(checked);
let words = |reading: &Reading, kind: Option<String>| {
let mut found = found(reading, checked.grid.is_some());
found.extend(kind);
let verdict = terminal::painted(Some(reading.verdict), label(reading.verdict), colour);
if found.is_empty() {
verdict
} else {
format!("{verdict} ({})", found.join(", "))
}
};
match &checked.readings[..] {
[read] => words(&read.reading, kind),
readings => readings
.iter()
.map(|read| format!("[{}] {}", read.profile, words(&read.reading, None)))
.chain(kind)
.collect::<Vec<_>>()
.join("; "),
}
}
fn kind(checked: &Checked) -> Option<String> {
match (
OpusUse::describe(&checked.opus),
checked.he_aac,
checked.dsd_multiple,
) {
(Some(coding), ..) => Some(format!("Opus {coding}")),
(None, Some(he_aac), _) => Some(he_aac.to_string()),
(None, None, Some(multiple)) => Some(format!("DSD{multiple}")),
(None, None, None) => None,
}
}
fn khz(hz: f64) -> String {
format!("{:.1} kHz", hz / 1000.0)
}
fn found(reading: &Reading, scanned: bool) -> Vec<String> {
let mut found = Vec::new();
let cutoff = reading.cutoff_hz.map_or_else(
|| "no cutoff".to_string(),
|hz| format!("cutoff {}", khz(hz)),
);
let guess = encoder(reading);
if let Some(grid) = &reading.grid {
let codec = grid.codec;
found.push(match &grid.evidence {
Evidence::Count(_)
| Evidence::Steps { .. }
| Evidence::AacSteps { .. }
| Evidence::Lattice(_) => format!("{codec} grid"),
Evidence::CountAt { rate, .. } => {
format!("{codec} grid at {} kHz", f64::from(*rate) / 1000.0)
}
Evidence::Shifted { .. } => format!("{codec} grid at another level"),
Evidence::Hybrid { .. } => format!("{codec} hybrid grid"),
Evidence::Short { short } => format!("{codec} grid, {} ms frames", short.milliseconds()),
});
found.push(cutoff);
found.extend(guess);
return found;
}
if reading.verdict == Verdict::PossiblyLossy {
if reading.cutoff_verdict == Verdict::MixedSources {
found.push("mixed sources by cutoff".to_string());
}
found.push(cutoff);
found.extend(guess);
found.push(
if scanned {
"no grid"
} else {
"grid not scanned"
}
.to_string(),
);
return found;
}
if reading.cutoff_hz.is_some() {
found.push(cutoff);
}
found.extend(guess);
found.extend(
reading
.flags
.iter()
.map(|flag| flag_words(*flag).to_string()),
);
if reading.verdict != Verdict::Inconclusive {
found.push(format!("confidence {:.2}", reading.confidence));
}
found
}
fn family_words(family: Family) -> Option<&'static str> {
match family {
Family::Lame => Some("LAME"),
Family::Helix => Some("Helix"),
Family::Apple => Some("Apple"),
Family::Faac => Some("FAAC"),
Family::Nero => Some("Nero"),
Family::FfmpegOrFdk => Some("ffmpeg or fdk-aac"),
Family::Fraunhofer => Some("Fraunhofer"),
Family::Libopus => Some("libopus"),
_ => None,
}
}
fn flag_words(flag: Flag) -> &'static str {
match flag {
Flag::Upsampled => "upsampled",
Flag::BitPadded => "bit padded",
Flag::DualMono => "dual mono",
}
}