Compare commits

...
12 Commits
Author SHA1 Message Date
ab ddcd523282 bumnp lock 2026-11-22 15:32:47 +02:00
ab 5aef2cb1e7 Add offline podcast downloads and release 0.5.3 2026-11-22 15:27:40 +02:00
ab 3cd6d94895 Merge pull request #5: VFD visualization platform v3 for 0.5.1 2026-11-22 13:44:44 +02:00
sanapachandClaude Opus 5.5 3e60195352 VFD panel: live signal path output row
The lower row of the signal path schematic was decoration (TONE, AMP,
SPEAKERS A). It now shows what leaves the player, read right to left
after the volume stage: the sample rate, STEREO or MONO, and the output
peak after the volume (the analyzer measures before rodio's gain, which
is volume cubed), refreshed four times a second and red near full scale.
The compact strip line and the transport STEREO lamp use the same values.

Comparing the L and R levels cannot tell stereo from mono: a wide mix
with balanced channels has equal levels. The analyzer therefore also
accumulates the side signal (L - R) / 2 per window, exposed as
level_side, and the meter decides stereo or mono on loud raw windows
(side within 30 dB of the louder channel), before any ballistics, and
keeps the decision through silence; scripts get it as channel_mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 22:12:41 +02:00
sanapachandClaude Opus 5.5 5a00a1c8e8 VFD panel: display modes with a hardware-style switch
DISPLAY_MODE() replaces LAYOUT() with "full", "spectrum", "minimal" and
"deck" ("auto" follows Shift+M; the old names still work). When the
layout on screen changes, the time and L/R meters stay lit while the
other sections go dark and light up one after another with a short
overshoot, like a display switching modes in hardware. A pinned mode or
a fallback to the same layout only flashes the mode lamps.

The analyzer's display range follows the music: its top rises with the
loudest band and falls 4 dB/s within -26..-6 dBFS, never reaching below
the analyzer floor, so silence stays dark. Spectrum mode caps the
analyzer at 18 rows and centres the panel; minimal mode is at most 132
columns wide and centred. Deck mode adds AMS, NR [B], AUTO REVERSE and
REW / FF lamps and a tape counter that races during a seek.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 21:38:26 +02:00
sanapachandClaude Opus 5.5 d1c89511ed Finish the VFD panel: 14-segment transport word and display behaviour polish
PLAY / PAUSE / STOP are drawn as full 14-segment letters (box-drawing
diagonals, a split middle bar, centre strokes) with faint unlit segments,
so the word no longer mixes segment and dot-matrix styles. Unlit lamps are
brighter and keep their level at night; the balance marker is two thin
segments over a bright centre mark; analyzer peak marks hold, then fall with
growing speed; and the lower-right block is one digital module with captions
and equally tall digits for track and volume.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 21:14:12 +02:00
sanapachandClaude Opus 5.5 2288392825 Switch visualizer display modes with Shift+M and polish the VFD panel
A new CycleVisualizerMode action (Shift+M by default) steps the fullscreen
visualizer's display mode; the choice is saved in visualizations.toml and
reaches scripts as input.mode. The VFD panel uses it for its classic, focus,
minimal and tape modes, with a row of mode lamps after each change and a
step-down when a mode does not fit the window.

Polish: quieter signal-path lines with lit boxes and nodes, two-cell peak
marks and a mid-level red zone in the level meter, A and Y in a 14-segment
style with thin diagonals, an analyzer tint from warm amber in the bass to
light yellow in the treble, and bar inertia that is separate from peak hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:49:38 +02:00
sanapachandClaude Opus 5.5 204d17f14c Give the VFD panel glow levels, a measuring module and a signal schematic
The analyzer shows three levels of glow (a dimmer foot, the body, a nearly
white top segment), faint unlit segments and a light peak-hold mark, on a
nonlinear scale; loud columns glow brighter on their own instead of the
whole panel brightening. The peak frequency becomes one measuring module
(caption, held value, rule, sample rate) with a real hold. The signal path
is a schematic of framed boxes on a bus with nodes, a wire to the lower row,
a pulse that now and then runs along it and a SIGNAL PATH state label.
PLAY / PAUSE / STOP use segment letters with centre strokes, mode lamps get
indicator bars, paused colours fade into unlit phosphor, and a new source
goes dark and relights with its value. LAYOUT() chooses between the full
panel, a spectrum focus and a tape deck composition, or picks by size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 10:04:07 +02:00
sanapachandClaude Opus 5.5 73a8e94c78 Even out the VFD panel's thin lines and clean up its routing
Verticals of the segment digits now use quarter blocks, as thick as the
eighth-block horizontals, and the mesh behind them is gone; unlit segments
are fainter so lit digits read clearly. The routing display uses label
plates without sides, and its wire runs along cell edges from the volume
plate's corner straight down to the tone plate's corner, so it joins
cleanly. The bass / treble fans are single thin strokes instead of braille
lines, and the braille amplifier and speakers are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 08:48:48 +02:00
sanapachandClaude Opus 5.5 61027d22ca Add VFD panel effects and a local-time clock for visualizations
The VFD panel gains a dot-matrix title line (5x7 Latin and Cyrillic in
braille, smooth scrolling), palettes, optional needle VU meters, a night
dimmer, a filament "breath" and loudness brightening, uneven digit
brightness, a mesh behind the big digits, a tape counter, LOUDNESS / MUTE /
SEARCH lamps, a burst of static on track and source changes, and a standby
clock with a sweep at the top of every minute.

The visualization clock now shows local time (it showed UTC), and scripts
get clock_hour, clock_minute, clock_second and clock_millis from the same
reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 00:28:59 +02:00
sanapachandClaude Opus 5.5 8606f08a36 Add visualization protocol v3 and the VFD panel preset
Scripts now receive the spectrum, channel levels, sample rate and track,
queue and transport details; may return #{ commands, state } to keep state
between frames (with dt and frame, reset on reload and on opening); can draw
thin braille dots and lines (clipped to the area); and can set bold, dim,
italic, underline or reversed on any command. v2 scripts keep working.

The bundled "VFD panel" preset uses all of it for a vacuum-fluorescent hi-fi
front panel in a thin style: segment counter, peak level meter, 25-band
analyzer with peak hold, tone fans, routing diagram and transport lamps,
with a power-on segment test, a counter search on track changes and an
OVER lamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 23:48:13 +02:00
sanapachandClaude Opus 5.5 aabca327b2 Measure a third-octave spectrum and channel levels for visualizations
The analyzer now keeps a full-rate mono PCM history and per-window left/right
RMS and peaks. The FFT and all ballistics run on the reader side when a
snapshot is taken, so the audio callback only appends samples: 31 bands with
exact base-2 edges that partition the FFT bins, an ~85 ms window that follows
the sample rate, time-based attack/fall/peak hold, and silence only after the
PCM counter has stalled for longer than a few device callbacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 23:48:13 +02:00
31 changed files with 4217 additions and 77 deletions

No files matched your search

+46 -2
View File
@@ -481,6 +481,9 @@ rodio source
│
v
audio analyzer ──> normalized features + scope samples
│ (PCM history, channel levels)
v
display meter ───> third-octave spectrum, L/R levels, peak hold
│
v
Rhai render(input)
@@ -499,6 +502,36 @@ script implements `render(input)` and returns declarative commands such as
clear, cell, line, rectangle, trace, and text. Scripts never receive the
ratatui frame or audio engine directly.
Protocol v3 adds what front-panel displays need: a third-octave spectrum
(31 bands, 20 Hz to 20 kHz, nominal ISO labels with exact base-2 edges that
partition the FFT bins) with per-band peak hold, left/right RMS and held
sample peaks, the RMS of the side signal (L - R) / 2 and a
stereo/mono decision made on loud windows, the analyzed sample rate, and track, queue and transport
details (format, bit depth, bitrate, track number, queue position, repeat,
shuffle, source), and the local wall-clock time. The audio thread only appends mono samples to a lock-free
ring and accumulates per-window channel levels; the FFT (an ~85 ms window
whose length follows the sample rate) and all ballistics run when the UI
takes a snapshot, under a mutex the audio thread never touches. Ballistics
are time-based (fast attack, steady fall, hold then fall): they advance with
the clock rather than per snapshot. Audio arrives in device-callback bursts,
so a snapshot that sees no new samples reuses the last measurement; only when
the PCM write counter stays still for longer than a few callbacks (pause,
stop) does the meter fall to the floor instead of freezing. In the control
role, and briefly when a track starts, the host passes an empty analysis, so
the meter reads zero. Meter values report the decoded signal on a fixed dBFS
scale and are therefore not scaled by the output volume, unlike the v2
energy features. Scripts choose their own display range within that scale.
v3 also lets a script keep state: `render` may return
`#{ commands, state }`, and the host passes `state` back with `dt` and a frame
counter on the next frame. State lives in the host's script runtime, is reset
whenever the script is recompiled or the visualizer is opened, and is subject
to the same sandbox size limits as any other value. Thin drawing comes from braille `dots` and `line`
commands in a 2 x 4 dot grid per cell (dots merge with braille already in the
cell; lines are clipped to the area first, so far endpoints cost nothing) and
from optional bold/dim/italic/underline/reversed attributes, which each
command sets for its cells rather than adding to what was drawn before.
This command boundary is intentional:
- scripts remain independent of Rust UI internals;
@@ -538,8 +571,8 @@ is not a plugin mechanism for accessing the library, network, or player
controls.
Fullscreen execution currently happens synchronously during UI drawing, with a fresh
Rhai scope for each frame; scripts should derive motion from time and the
already-smoothed audio features rather than rely on retained mutable state.
Rhai scope for each frame; retained state is limited to the explicit `state`
value described above, and motion should still be derived from time and `dt`.
The 50 ms animation timer gives a nominal 20 FPS cadence (other events can
also trigger redraws). Operation limits are not wall-clock deadlines. The
host also checks the script modification time during drawing, so slow script
@@ -666,3 +699,14 @@ Future changes should preserve these rules:
5. Network and storage work must remain outside the interactive UI path.
6. More peers should improve availability; losing peers should only reduce
remote capabilities.
### Device-local podcast audio
Episode downloads are explicit local actions. Completed files live beside the
podcast database in `podcast-downloads`, keyed by a hash of the shared episode
id. Temporary files become visible only after a complete response is saved.
Playback checks this directory on the blocking pool before opening HTTP and
uses the same progress and queue model for local audio. Downloads and retention
settings never enter the podcast store registers or device-sync protocol.
A background maintenance pass removes downloads after the configured age
(default 30 days from completion); catalog, progress and subscriptions remain.
+50 -1
View File
@@ -7,6 +7,54 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.5.1] - 2026-11-22
### Added
- "VFD panel" visualization: a vacuum-fluorescent hi-fi front panel with
thin segment digits, a stereo peak level meter (bars or needle VU meters),
a 25-band analyzer with three levels of glow, a nonlinear scale and peak
hold, a peak frequency readout with hold, tone fans, a signal path
schematic with a travelling pulse (source, format, volume, sample rate,
stereo or mono, and the output level in dBFS), the transport word in segment letters,
mode lamps (REPEAT, SHUFFLE, source, LOUDNESS, MUTE, SEARCH, OVER), a tape
counter and a dot-matrix title line (Latin and Cyrillic). Effects: a
power-on segment test, a counter "search" on track changes, static on
track and source changes, a source block that relights with its new
value, colours that fade into unlit phosphor on pause, a night dimmer, a
slow filament "breath", slightly uneven digit brightness and a standby
clock when nothing is loaded. Display modes "full", "spectrum",
"minimal" and "deck" switch with Shift+M or can be pinned with
`DISPLAY_MODE()` at the top of the script, like the palettes ("classic",
"green", "amber", "ice"); a mode that does not fit the window steps down.
A switch looks like hardware: the time and L/R meters stay lit, the other
sections go dark and light up one by one, and a row of mode lamps shows
the choice for a moment. The analyzer's display range follows the
loudness of the music (the measurements stay absolute), and deck mode has
AMS, NR, AUTO REVERSE and REW/FF lamps with a counter that races during a
seek. Bars settle with a little inertia, the analyzer's
tint runs from warm amber in the bass to light yellow in the treble, peak
marks fall with a little gravity after their hold, and the transport word
uses 14-segment letters with faint unlit segments.
- `Shift+M` in the fullscreen visualizer switches its display mode; the
choice is saved in `visualizations.toml`.
- Visualization protocol v3: a real third-octave spectrum (31 bands) with
peak hold, left/right RMS and held peaks, the side-signal level
(`level_side`, the floor for mono) and `channel_mode` ("stereo" or
"mono"), the sample rate, and track,
queue and transport details in the script input; `render` may return
`#{ commands, state }` to keep state between frames (with `dt` and
`frame`); new braille `dots` and `line` commands for thin drawing; bold,
dim, italic, underline and reversed attributes on any command. v2 scripts
keep working unchanged. Scripts also get the local time as
`clock_hour`, `clock_minute`, `clock_second` and `clock_millis`, and the
display mode as `mode`, a counter each script takes modulo its own number
of modes.
### Fixed
- The visualization clock shows local time instead of UTC.
## [0.5.0] - 2026-11-20
### Added
@@ -212,6 +260,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Music-directory validation and migration now reject overlapping changes,
resolve canonical paths, and produce Windows-portable managed filenames.
[Unreleased]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.8...HEAD
[Unreleased]: https://gt.hexor.cy/Furumi/furumi-tui/compare/v0.5.1...HEAD
[0.5.1]: https://gt.hexor.cy/Furumi/furumi-tui/compare/v0.5.0...v0.5.1
[0.2.8]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.7...v0.2.8
[0.2.5]: https://gt.hexor.cy/ab/furumi_tui/compare/v0.2.4...v0.2.5
Generated
+2 -1
View File
@@ -1715,11 +1715,12 @@ dependencies = [
[[package]]
name = "furumi_tui"
version = "0.5.0"
version = "0.5.2"
dependencies = [
"anyhow",
"base64",
"blake3",
"chrono",
"core-foundation 0.10.1",
"crokey",
"crossterm",
+3 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "furumi_tui"
version = "0.5.0"
version = "0.5.2"
edition = "2024"
rust-version = "1.97"
description = "A federated P2P player for personal music libraries"
@@ -23,6 +23,8 @@ object = { version = "0.36", default-features = false, features = ["read", "std"
anyhow = "1.0.102"
blake3 = "1"
# Local wall-clock time for visualizations (the clock and its hour/minute).
chrono = { version = "0.4.45", default-features = false, features = ["clock"] }
crokey = "1.4.0"
crossterm = { version = "0.29.0", features = ["event-stream"] }
directories = "6.0.0"
+14 -4
View File
@@ -67,7 +67,10 @@ context-aware bindings, and user-defined rebinding through TOML. Built-in
audio visualizations, OS media controls, gapless queue playback, and local
library management are included. Visualizations are runtime-loadable Rhai
scripts executed in a resource-limited sandbox, so they can be added or edited
without rebuilding the player.
without rebuilding the player. The bundled "VFD panel" preset draws a hi-fi
front panel from a real third-octave spectrum and stereo level meter; scripts
can use the same spectrum, levels, track details, thin braille lines and
per-frame state.
Optional similarity search calculates versioned embeddings for local tracks
in the background and keeps them in SQLite. It works offline; after a separate
@@ -183,9 +186,16 @@ take them literally and silence the notice.
### Podcasts
The Podcasts tab (`4`) follows RSS and Atom podcast feeds. Furumi stores only
the feed's episode information; audio stays on the publisher's server and is
never imported into the music library or offered to other peers.
The Podcasts tab (`4`) follows RSS and Atom podcast feeds. Audio streams from
the publisher unless you download an episode. Downloads stay on this device,
are never imported into the music library or offered to other peers.
- Press `y` on an episode to download it; `[offline]` marks a complete local
copy. Playback uses that copy, including resume and seeking, without HTTP.
- Settings → Additional settings → Podcast downloads: keep days controls
automatic cleanup (default 30 days from download completion). Cleanup runs
while Furumi is open; expired files can be downloaded again. No episodes
are downloaded automatically.
- **Add podcast** (`o`, or the first row) takes a feed URL; `feed://`,
`podcast://` and URLs without a scheme are accepted. Nothing is stored unless
+5 -2
View File
@@ -32,6 +32,7 @@ pub enum Action {
ToggleShuffle,
CycleRepeat,
ToggleVisualizer,
CycleVisualizerMode,
ToggleLike,
ToggleTrackSelection,
OpenTrackInfo,
@@ -40,7 +41,7 @@ pub enum Action {
QueueAddLast,
MoveQueueUp,
MoveQueueDown,
/// Download the selected federated track(s) into the local library.
/// Download selected federated tracks, or a podcast episode for offline playback.
DownloadSelected,
RemoveFromQueue,
ClearQueue,
@@ -129,6 +130,7 @@ impl Action {
| Action::ToggleShuffle
| Action::CycleRepeat
| Action::ToggleVisualizer
| Action::CycleVisualizerMode
| Action::OpenConnectedDevices
| Action::OpenListenHistory => Category::Playback,
Action::QueueAddNext
@@ -228,6 +230,7 @@ impl Action {
Action::ToggleShuffle => "Toggle shuffle".into(),
Action::CycleRepeat => "Cycle repeat mode".into(),
Action::ToggleVisualizer => "Toggle fullscreen visualizer".into(),
Action::CycleVisualizerMode => "Next visualizer display mode".into(),
Action::ToggleLike => "Like / unlike".into(),
Action::ToggleTrackSelection => "Track line selection".into(),
Action::OpenTrackInfo => "Track info".into(),
@@ -236,7 +239,7 @@ impl Action {
Action::QueueAddLast => "Queue: add to end".into(),
Action::MoveQueueUp => "Queue: move track/selection up".into(),
Action::MoveQueueDown => "Queue: move track/selection down".into(),
Action::DownloadSelected => "Federation: download to library".into(),
Action::DownloadSelected => "Download selected music / podcast episode".into(),
Action::RemoveFromQueue => "Queue: remove selected".into(),
Action::ClearQueue => "Queue: clear".into(),
Action::OpenConnectedDevices => "Connected devices…".into(),
+5
View File
@@ -273,6 +273,11 @@ pub enum AppEvent {
},
/// Scheduler timer: ask the store which feeds are due.
PodcastRefreshTick,
PodcastDownloads(Result<std::collections::HashSet<String>, String>),
PodcastDownloaded {
id: String,
result: Result<(), String>,
},
/// Feeds due for a background refresh.
PodcastsDue(Result<Vec<(music_dht::podcast::PodcastId, music_dht::podcast::FeedUrl)>, String>),
/// What sharing a podcast would publish (`Effect::PodcastPrepareShare`).
+14 -4
View File
@@ -422,6 +422,7 @@ pub async fn run(
status_message,
..AppState::default()
};
state.podcast_retention_days = settings.podcast_retention_days;
state.player.volume = settings.volume;
state.global.filters = settings.library;
state.music_dir = settings.music_dir.clone();
@@ -1728,7 +1729,9 @@ fn perform_effect(state: &mut AppState, runtime: &mut Runtime, effect: Effect) {
});
}
// Executed by podcast_runtime::perform above.
Effect::PodcastsReload
Effect::SavePodcastSettings
| Effect::PodcastDownload(_)
| Effect::PodcastsReload
| Effect::PodcastLoadSeasons(_)
| Effect::PodcastLoadEpisodes { .. }
| Effect::PodcastSubscribe(_)
@@ -3133,14 +3136,19 @@ fn refresh_artists(state: &mut AppState, runtime: &Runtime) {
});
}
fn save_app_settings(state: &AppState) {
let settings = crate::config::settings::AppSettings {
fn app_settings(state: &AppState) -> crate::config::settings::AppSettings {
crate::config::settings::AppSettings {
podcast_retention_days: state.podcast_retention_days,
playback: state.device_playback.config.clone(),
volume: state.player.volume,
library: state.global.filters,
music_dir: state.music_dir.clone(),
similarity: state.similarity.settings.clone(),
};
}
}
fn save_app_settings(state: &AppState) {
let settings = app_settings(state);
if let Err(err) = crate::config::settings::save(&settings) {
tracing::warn!(%err, "saving app settings failed");
}
@@ -3535,6 +3543,8 @@ fn handle_app_event(state: &mut AppState, runtime: &mut Runtime, event: AppEvent
| AppEvent::PodcastProgress(_)
| AppEvent::PodcastEpisodeStarted { .. }
| AppEvent::PodcastEpisodeFailed { .. }
| AppEvent::PodcastDownloads(_)
| AppEvent::PodcastDownloaded { .. }
| AppEvent::PodcastRefreshTick
| AppEvent::PodcastsDue(_)
| AppEvent::PodcastSharePrepared { .. }
+76 -1
View File
@@ -41,6 +41,8 @@ const PUBLICATION_FLUSH_TIMEOUT: Duration = Duration::from_secs(4);
pub struct PodcastRuntime {
pub services: Services,
refresh_slots: Arc<tokio::sync::Semaphore>,
download_slots: Arc<tokio::sync::Semaphore>,
downloads_cancelled: Arc<std::sync::atomic::AtomicBool>,
/// Writes progress in order on its own thread; `None` if it could not
/// start (episodes then play without saving progress).
progress: Option<ProgressWriter>,
@@ -187,6 +189,18 @@ fn services(runtime: &Runtime) -> Option<Services> {
/// Executes a podcast [`Effect`]; any other effect is handed back.
pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> Option<Effect> {
if matches!(effect, Effect::SavePodcastSettings) {
let settings = super::app_settings(state);
let tx = runtime.event_tx.clone();
tokio::task::spawn_blocking(move || {
let message = match crate::config::settings::save(&settings) {
Ok(()) => "podcasts: download retention saved".to_string(),
Err(_) => "podcasts: could not save download retention".to_string(),
};
let _ = tx.send(AppEvent::StatusMessage(message));
});
return None;
}
let Some(services) = services(runtime) else {
if is_podcast_effect(&effect) {
if let Effect::PodcastRefresh { targets, .. } = &effect {
@@ -219,6 +233,30 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O
};
let tx = runtime.event_tx.clone();
match effect {
Effect::SavePodcastSettings => unreachable!("handled before store lookup"),
Effect::PodcastDownload(episode) => {
let slots = Arc::clone(&runtime.podcasts.as_ref().unwrap().download_slots);
let cancelled = Arc::clone(&runtime.podcasts.as_ref().unwrap().downloads_cancelled);
tokio::spawn(async move {
let Ok(permit) = slots.acquire_owned().await else {
return;
};
let _ = tokio::task::spawn_blocking(move || {
let _permit = permit;
let id = episode.episode_id.as_str().to_owned();
let result = crate::podcasts::offline::download_with_cancel(
&services.downloads_dir,
&episode,
CancelToken::new(move || {
cancelled.load(std::sync::atomic::Ordering::Relaxed)
}),
)
.map_err(|_| "could not download episode audio".to_string());
let _ = tx.send(AppEvent::PodcastDownloaded { id, result });
})
.await;
});
}
Effect::PodcastsReload => {
tokio::task::spawn_blocking(move || {
let result = load_rows(&services.store).map_err(err_string);
@@ -411,7 +449,9 @@ fn set_shared(
fn is_podcast_effect(effect: &Effect) -> bool {
matches!(
effect,
Effect::PodcastsReload
Effect::SavePodcastSettings
| Effect::PodcastDownload(_)
| Effect::PodcastsReload
| Effect::PodcastLoadSeasons(_)
| Effect::PodcastLoadEpisodes { .. }
| Effect::PodcastSubscribe(_)
@@ -477,6 +517,8 @@ pub fn handle_event(
runtime.podcasts = Some(PodcastRuntime {
services,
refresh_slots: Arc::new(tokio::sync::Semaphore::new(MAX_CONCURRENT_REFRESHES)),
download_slots: Arc::new(tokio::sync::Semaphore::new(2)),
downloads_cancelled: Arc::new(std::sync::atomic::AtomicBool::new(false)),
progress,
publication,
});
@@ -599,7 +641,34 @@ pub fn handle_event(
episode_failed(state, runtime, generation, message);
Vec::new()
}
AppEvent::PodcastDownloaded { id, result } => {
state.podcast_downloading.remove(&id);
state.status_message = Some(match result {
Ok(()) => "podcasts: episode downloaded".into(),
Err(e) => e,
});
let _ = runtime.event_tx.send(AppEvent::PodcastRefreshTick);
Vec::new()
}
AppEvent::PodcastDownloads(result) => {
if let Ok(downloads) = result {
state.podcast_downloads = downloads;
}
Vec::new()
}
AppEvent::PodcastRefreshTick => {
let offline_services = services(runtime)?;
let days = state.podcast_retention_days.max(1);
let tx = runtime.event_tx.clone();
tokio::task::spawn_blocking(move || {
let result = crate::podcasts::offline::cleanup(
&offline_services.downloads_dir,
days,
std::time::SystemTime::now(),
)
.map_err(err_string);
let _ = tx.send(AppEvent::PodcastDownloads(result));
});
if let Some(services) = services(runtime) {
let tx = runtime.event_tx.clone();
tokio::task::spawn_blocking(move || {
@@ -1148,6 +1217,12 @@ fn progress_event(
/// also one still being written) reach the overlay. Logs a warning when a
/// card of a private podcast may still be live.
pub(crate) async fn shutdown(runtime: &mut Runtime) {
if let Some(podcasts) = &runtime.podcasts {
podcasts
.downloads_cancelled
.store(true, std::sync::atomic::Ordering::Relaxed);
podcasts.download_slots.close();
}
end_playback(runtime);
let Some(podcasts) = runtime.podcasts.as_mut() else {
return;
+14
View File
@@ -614,6 +614,20 @@ fn handle_fed_input(
!state.federation.settings.network_id.is_empty();
super::fed_apply_settings(state, runtime);
}
FedInputField::PodcastRetention => match value.parse::<u32>() {
Ok(days) if (1..=36500).contains(&days) => {
state.podcast_retention_days = days;
super::perform_effect(
state,
runtime,
crate::app::update::Effect::SavePodcastSettings,
);
}
_ => {
state.status_message =
Some("enter a retention period from 1 to 36500 days".into())
}
},
FedInputField::MusicDirectory => {
if value.is_empty() {
state.status_message = Some("music directory is empty".into());
+10
View File
@@ -923,6 +923,7 @@ impl StatusDetailFocus {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FedInputField {
MusicDirectory,
PodcastRetention,
SimilarityWorkers,
SimilarityMinimumScore,
SimilarityMaxTracksPerArtist,
@@ -940,6 +941,7 @@ pub enum FedInputField {
impl FedInputField {
pub fn title(self) -> &'static str {
match self {
FedInputField::PodcastRetention => "Keep podcast downloads (days)",
FedInputField::MusicDirectory => "Music save directory",
FedInputField::SimilarityWorkers => "Similarity background workers",
FedInputField::SimilarityMinimumScore => "Minimum similarity score",
@@ -956,6 +958,9 @@ impl FedInputField {
pub fn help(self) -> &'static str {
match self {
FedInputField::PodcastRetention => {
"Enter 1–36500 days. Downloads expire from the time they finish. Default: 30 days. Only this device is affected."
}
FedInputField::MusicDirectory => {
"Federated tracks saved to your library use this directory. The directory is checked for write access before anything changes."
}
@@ -1047,6 +1052,7 @@ pub enum SettingsRow {
CheckUpdate,
InstallUpdate,
MusicDirectory,
PodcastRetention,
Similarity(SimilarityRow),
Federation(FedRow),
StatusDetails,
@@ -1188,6 +1194,7 @@ pub fn device_status_order(state: &AppState) -> Vec<usize> {
pub fn additional_settings_rows(state: &AppState) -> Vec<SettingsRow> {
let mut rows = vec![
SettingsRow::MusicDirectory,
SettingsRow::PodcastRetention,
SettingsRow::CheckUpdate,
SettingsRow::InstallUpdate,
];
@@ -1958,6 +1965,9 @@ pub struct AppState {
/// Root for music permanently downloaded from federation peers.
pub music_dir: std::path::PathBuf,
pub music_dir_changing: bool,
pub podcast_retention_days: u32,
pub podcast_downloads: std::collections::HashSet<String>,
pub podcast_downloading: std::collections::HashSet<String>,
pub player: PlayerBar,
pub device_playback: DevicePlaybackState,
pub jam: crate::jam::JamStatus,
+21
View File
@@ -112,6 +112,8 @@ pub enum Effect {
LoadListenHistory,
/// Reload the subscribed podcasts with their fetch state.
PodcastsReload,
PodcastDownload(music_dht::podcast::PodcastEpisodeRef),
SavePodcastSettings,
/// Load the season groups of a podcast.
PodcastLoadSeasons(music_dht::podcast::PodcastId),
/// Load one page of episodes; `seq` drops stale results.
@@ -389,6 +391,18 @@ pub fn update(state: &mut AppState, action: Action) -> Option<Effect> {
}
None
}
Action::CycleVisualizerMode => {
if state.visualizer.active {
if let Err(err) = state.visualizer.cycle_mode() {
// the fullscreen visualizer hides the status line
tracing::warn!(error = %format!("{err:#}"), "saving the visualizer mode failed");
state.status_message = Some(format!("visualization settings: {err:#}"));
}
} else {
state.status_message = Some("open the visualizer first (Shift+L)".into());
}
None
}
Action::Select => select_current(state),
Action::Back if state.visualizer.active => {
state.visualizer.close();
@@ -3055,6 +3069,13 @@ fn select_settings_row(state: &mut AppState, row: super::state::SettingsRow) ->
state.updater.message = "Downloading update...".into();
Some(Effect::InstallUpdate(update))
}
SettingsRow::PodcastRetention => {
state.popup = Some(Popup::FedInput {
field: FedInputField::PodcastRetention,
input: crate::app::input::LineEdit::new(state.podcast_retention_days.to_string()),
});
None
}
SettingsRow::MusicDirectory => {
if state.music_dir_changing {
state.status_message = Some("music directory change is already running".into());
+25
View File
@@ -54,6 +54,31 @@ pub(super) fn handle(state: &mut AppState, action: &Action) -> Option<Option<Eff
open_add(state);
None
}
Action::DownloadSelected => {
if !store_ready(state) {
return Some(None);
}
let Some(track) = state.podcasts.selected_episode_track() else {
return Some(None);
};
let episode = track.podcast_episode_ref().unwrap().clone();
if state
.podcast_downloads
.contains(&crate::podcasts::offline::key(episode.episode_id.as_str()))
{
state.status_message =
Some("podcasts: episode is already available offline".into());
return Some(None);
}
if !state
.podcast_downloading
.insert(episode.episode_id.as_str().to_owned())
{
return Some(None);
}
state.status_message = Some("podcasts: downloading episode…".into());
Some(Effect::PodcastDownload(episode))
}
Action::RefreshPodcast => refresh_current(state),
Action::RefreshAllPodcasts => refresh_all(state),
Action::CycleEpisodeSort => cycle_sort(state),
+65
View File
@@ -776,6 +776,20 @@ fn visualizer_requires_current_track() {
);
}
#[test]
fn visualizer_mode_needs_an_open_visualizer() {
let mut state = AppState::default();
let mode = state.visualizer.config.mode;
assert_eq!(update(&mut state, Action::CycleVisualizerMode), None);
assert_eq!(state.visualizer.config.mode, mode);
assert_eq!(
state.status_message.as_deref(),
Some("open the visualizer first (Shift+L)")
);
}
#[test]
fn visualizer_toggles_and_back_closes_it() {
let mut state = AppState::default();
@@ -2813,3 +2827,54 @@ mod podcast_discovery {
));
}
}
#[test]
fn episode_download_requires_explicit_action_and_deduplicates_requests() {
use crate::app::podcast_fixtures as fx;
let mut state = fx::with_episodes(
fx::ready_state(vec![fx::row("Show", 1, 1, 0)]),
vec![fx::episode(1, "Episode", None)],
);
assert!(state.podcast_downloading.is_empty());
let expected = state
.podcasts
.selected_episode_track()
.unwrap()
.podcast_episode_ref()
.unwrap()
.clone();
assert_eq!(
update(&mut state, Action::DownloadSelected),
Some(Effect::PodcastDownload(expected.clone()))
);
assert_eq!(update(&mut state, Action::DownloadSelected), None);
assert_eq!(state.podcast_downloading.len(), 1);
state.podcast_downloading.clear();
state
.podcast_downloads
.insert(crate::podcasts::offline::key(expected.episode_id.as_str()));
assert_eq!(update(&mut state, Action::DownloadSelected), None);
assert!(state.podcast_downloading.is_empty());
}
#[test]
fn podcast_retention_opens_a_numeric_editor_in_additional_settings() {
use crate::app::state::{FedInputField, Popup, SettingsRow, additional_settings_rows};
let mut state = AppState {
additional_settings_open: true,
podcast_retention_days: 30,
..Default::default()
};
state.additional_settings_cursor = additional_settings_rows(&state)
.iter()
.position(|row| *row == SettingsRow::PodcastRetention)
.unwrap();
assert_eq!(update(&mut state, Action::Select), None);
assert!(matches!(
state.popup,
Some(Popup::FedInput {
field: FedInputField::PodcastRetention,
..
})
));
}
+4
View File
@@ -223,6 +223,10 @@ command = "CycleRepeat"
key_sequence = "shift-l"
command = "ToggleVisualizer"
[[keymaps]]
key_sequence = "shift-m"
command = "CycleVisualizerMode"
[[keymaps]]
key_sequence = "shift-h"
command = "OpenListenHistory"
+5
View File
@@ -584,6 +584,11 @@ mod tests {
km.resolve(shift_l, KeyContext::Library),
KeyResolution::Action(Action::ToggleVisualizer)
);
let shift_m = KeyCombination::new(KeyCode::Char('M'), KeyModifiers::SHIFT);
assert_eq!(
km.resolve(shift_m, KeyContext::Library),
KeyResolution::Action(Action::CycleVisualizerMode)
);
}
#[test]
+9
View File
@@ -50,6 +50,8 @@ impl Default for SimilaritySettings {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AppSettings {
#[serde(default = "default_podcast_retention_days")]
pub podcast_retention_days: u32,
#[serde(default)]
pub playback: music_dht::playback::Config,
#[serde(default = "default_volume")]
@@ -66,6 +68,7 @@ pub struct AppSettings {
impl Default for AppSettings {
fn default() -> Self {
Self {
podcast_retention_days: 30,
playback: music_dht::playback::Config::default(),
volume: default_volume(),
library: LibraryFilters::default(),
@@ -77,6 +80,7 @@ impl Default for AppSettings {
impl AppSettings {
pub fn normalized(mut self) -> Self {
self.podcast_retention_days = self.podcast_retention_days.clamp(1, 36500);
self.volume = self.volume.min(100);
if self.music_dir.as_os_str().is_empty() {
self.music_dir = default_music_dir();
@@ -97,6 +101,10 @@ impl AppSettings {
}
}
fn default_podcast_retention_days() -> u32 {
30
}
fn default_volume() -> u8 {
80
}
@@ -186,6 +194,7 @@ hide_featured_only = true
let settings = settings.normalized();
assert_eq!(settings.volume, 100);
assert_eq!(settings.podcast_retention_days, 30);
assert!(settings.library.hide_featured_only);
assert_eq!(settings.library.source_mode, LibrarySourceMode::Global);
assert_eq!(settings.music_dir, default_music_dir());
+265 -15
View File
@@ -1,10 +1,12 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicI16, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::time::Duration;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use rodio::source::SeekError;
use rodio::{ChannelCount, Sample, SampleRate, Source};
use super::spectrum::{Meter, PCM_SAMPLES, StereoReading};
const LEVEL_SCALE: f32 = 1_000_000.0;
const SCOPE_SAMPLES: usize = 256;
const SCOPE_SAMPLE_SCALE: f32 = i16::MAX as f32;
@@ -20,6 +22,26 @@ pub struct AudioAnalysisSnapshot {
pub treble: f64,
pub beat: f64,
pub scope: Vec<f64>,
/// Third-octave band levels (see `spectrum::BAND_CENTERS_HZ`), 0.0..1.0
/// over `spectrum::FLOOR_DB`..0 dBFS, with analyzer ballistics.
pub spectrum: Vec<f64>,
/// Per-band peak-hold markers on the same scale as `spectrum`.
pub spectrum_peaks: Vec<f64>,
/// Left/right RMS levels and held sample peaks on the same dB scale.
/// Mono sources report the same value for both channels.
pub level_l: f64,
pub level_r: f64,
pub peak_l: f64,
pub peak_r: f64,
/// RMS of the side signal (L - R) / 2 on the same scale: the floor for
/// mono sources and for stereo files that carry the same signal twice.
pub level_side: f64,
/// Whether the channels carry different signals: decided on loud windows
/// only (side within 30 dB of the louder channel), `None` until then.
pub stereo: Option<bool>,
/// Sample rate of the analyzed stream in Hz (0 when nothing has played
/// since the player last stopped).
pub sample_rate: u32,
}
#[derive(Debug)]
@@ -32,6 +54,20 @@ pub struct AnalyzerShared {
beat: AtomicU32,
scope_write: AtomicUsize,
scope: Box<[AtomicI16]>,
/// Full-rate mono history for the spectrum; written by the audio thread.
pcm_write: AtomicUsize,
pcm: Box<[AtomicI16]>,
sample_rate: AtomicU32,
/// Per-window channel levels (linear, `LEVEL_SCALE`). Peaks accumulate
/// with `fetch_max` until the meter consumes them.
rms_l: AtomicU32,
rms_r: AtomicU32,
rms_side: AtomicU32,
peak_l: AtomicU32,
peak_r: AtomicU32,
/// Ballistics and FFT state. Only snapshot readers (the UI side) lock it;
/// the audio thread never does.
meter: Mutex<Meter>,
}
impl Default for AnalyzerShared {
@@ -44,14 +80,27 @@ impl Default for AnalyzerShared {
treble: AtomicU32::new(0),
beat: AtomicU32::new(0),
scope_write: AtomicUsize::new(0),
scope: (0..SCOPE_SAMPLES)
.map(|_| AtomicI16::new(0))
.collect::<Vec<_>>()
.into_boxed_slice(),
scope: atomic_ring(SCOPE_SAMPLES),
pcm_write: AtomicUsize::new(0),
pcm: atomic_ring(PCM_SAMPLES),
sample_rate: AtomicU32::new(0),
rms_l: AtomicU32::new(0),
rms_r: AtomicU32::new(0),
rms_side: AtomicU32::new(0),
peak_l: AtomicU32::new(0),
peak_r: AtomicU32::new(0),
meter: Mutex::new(Meter::default()),
}
}
}
fn atomic_ring(len: usize) -> Box<[AtomicI16]> {
(0..len)
.map(|_| AtomicI16::new(0))
.collect::<Vec<_>>()
.into_boxed_slice()
}
impl AnalyzerShared {
pub fn clear(&self) {
self.energy.store(0, Ordering::Relaxed);
@@ -63,10 +112,37 @@ impl AnalyzerShared {
for sample in self.scope.iter() {
sample.store(0, Ordering::Relaxed);
}
self.pcm_write.store(0, Ordering::Relaxed);
for sample in self.pcm.iter() {
sample.store(0, Ordering::Relaxed);
}
for level in [
&self.rms_l,
&self.rms_r,
&self.rms_side,
&self.peak_l,
&self.peak_r,
] {
level.store(0, Ordering::Relaxed);
}
self.sequence.store(0, Ordering::Relaxed);
}
/// Like [`Self::clear`], and also forgets the sample rate: nothing is
/// loaded any more.
pub fn reset(&self) {
self.clear();
self.sample_rate.store(0, Ordering::Relaxed);
}
pub fn snapshot(&self) -> AudioAnalysisSnapshot {
self.snapshot_at(Instant::now())
}
/// Like [`Self::snapshot`] with an explicit clock, so meter ballistics are
/// deterministic in tests. Ballistics are time-based: how often callers
/// read does not change how fast bars rise or fall.
pub fn snapshot_at(&self, now: Instant) -> AudioAnalysisSnapshot {
let write = self.scope_write.load(Ordering::Relaxed);
let len = self.scope.len().max(1);
let scope = (0..self.scope.len())
@@ -76,6 +152,25 @@ impl AnalyzerShared {
})
.collect();
let sample_rate = self.sample_rate.load(Ordering::Relaxed);
let pcm_write = self.pcm_write.load(Ordering::Relaxed);
let stereo = StereoReading {
rms_l: load_norm(&self.rms_l) as f32,
rms_r: load_norm(&self.rms_r) as f32,
rms_side: load_norm(&self.rms_side) as f32,
peak_l: f64::from(self.peak_l.swap(0, Ordering::Relaxed)) as f32 / LEVEL_SCALE,
peak_r: f64::from(self.peak_r.swap(0, Ordering::Relaxed)) as f32 / LEVEL_SCALE,
};
let reading = {
let mut meter = self
.meter
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
meter.update(now, pcm_write, sample_rate, stereo, |out| {
self.copy_pcm(pcm_write, out)
})
};
AudioAnalysisSnapshot {
sequence: self.sequence.load(Ordering::Relaxed),
energy: load_norm(&self.energy),
@@ -84,6 +179,31 @@ impl AnalyzerShared {
treble: load_norm(&self.treble),
beat: load_norm(&self.beat),
scope,
spectrum: reading.spectrum,
spectrum_peaks: reading.spectrum_peaks,
level_l: reading.level_l,
level_r: reading.level_r,
peak_l: reading.peak_l,
peak_r: reading.peak_r,
level_side: reading.level_side,
stereo: reading.stereo,
sample_rate,
}
}
/// Copies the newest `out.len()` mono samples, oldest first. Slots not
/// written since the last clear read as silence.
fn copy_pcm(&self, write: usize, out: &mut [f32]) {
let len = self.pcm.len();
let count = out.len().min(len);
let start = write.wrapping_sub(count);
for (offset, slot) in out.iter_mut().enumerate() {
*slot = if offset < count && write >= count - offset {
let index = start.wrapping_add(offset) % len;
f32::from(self.pcm[index].load(Ordering::Relaxed)) / SCOPE_SAMPLE_SCALE
} else {
0.0
};
}
}
@@ -96,6 +216,22 @@ impl AnalyzerShared {
self.sequence.fetch_add(1, Ordering::Relaxed);
}
fn push_pcm(&self, sample: f32) {
let index = self.pcm_write.fetch_add(1, Ordering::Relaxed) % self.pcm.len();
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
self.pcm[index].store(value, Ordering::Relaxed);
}
fn store_channels(&self, sample_rate: f32, rms: [f32; 3], peak: [f32; 2]) {
self.sample_rate
.store(sample_rate.round() as u32, Ordering::Relaxed);
store_norm(&self.rms_l, rms[0]);
store_norm(&self.rms_r, rms[1]);
store_norm(&self.rms_side, rms[2]);
self.peak_l.fetch_max(norm_bits(peak[0]), Ordering::Relaxed);
self.peak_r.fetch_max(norm_bits(peak[1]), Ordering::Relaxed);
}
fn push_scope(&self, sample: f32) {
let index = self.scope_write.fetch_add(1, Ordering::Relaxed) % self.scope.len();
let value = (sample.clamp(-1.0, 1.0) * SCOPE_SAMPLE_SCALE).round() as i16;
@@ -181,6 +317,12 @@ struct AnalyzerState {
mid_alpha: f32,
channel_index: usize,
frame_sum: f32,
/// Left/right of the current frame (mono feeds both).
frame_lr: [f32; 2],
channel_sq: [f32; 2],
/// Sum of squared side samples (L - R) / 2 in the current window.
side_sq: f32,
channel_peak: [f32; 2],
frame_count: usize,
window_frames: usize,
scope_counter: usize,
@@ -208,6 +350,10 @@ impl AnalyzerState {
mid_alpha: 0.0,
channel_index: 0,
frame_sum: 0.0,
frame_lr: [0.0; 2],
channel_sq: [0.0; 2],
side_sq: 0.0,
channel_peak: [0.0; 2],
frame_count: 0,
window_frames: 0,
scope_counter: 0,
@@ -252,6 +398,10 @@ impl AnalyzerState {
fn reset_filters(&mut self) {
self.channel_index = 0;
self.frame_sum = 0.0;
self.frame_lr = [0.0; 2];
self.channel_sq = [0.0; 2];
self.side_sq = 0.0;
self.channel_peak = [0.0; 2];
self.frame_count = 0;
self.scope_counter = 0;
self.low_bass = 0.0;
@@ -275,10 +425,23 @@ impl AnalyzerState {
0.0
};
self.frame_sum += sample;
// First channel is left, second right; extra channels only feed mono.
if self.channel_index < 2 {
self.frame_lr[self.channel_index] = sample;
}
self.channel_index += 1;
if self.channel_index < self.channels {
return;
}
if self.channels == 1 {
self.frame_lr[1] = self.frame_lr[0];
}
for (channel, value) in self.frame_lr.iter().enumerate() {
self.channel_sq[channel] += value * value;
self.channel_peak[channel] = self.channel_peak[channel].max(value.abs());
}
let side = (self.frame_lr[0] - self.frame_lr[1]) * 0.5;
self.side_sq += side * side;
let mono = self.frame_sum / self.channels as f32;
self.channel_index = 0;
@@ -299,6 +462,7 @@ impl AnalyzerState {
self.mid_sq += mid * mid;
self.treble_sq += treble * treble;
self.frame_count += 1;
shared.push_pcm(sample);
self.scope_counter += 1;
if self.scope_counter >= self.scope_stride {
@@ -339,10 +503,23 @@ impl AnalyzerState {
self.smooth_beat,
);
shared.store_channels(
self.sample_rate,
[
(self.channel_sq[0] / frames).sqrt(),
(self.channel_sq[1] / frames).sqrt(),
(self.side_sq / frames).sqrt(),
],
self.channel_peak,
);
self.full_sq = 0.0;
self.bass_sq = 0.0;
self.mid_sq = 0.0;
self.treble_sq = 0.0;
self.channel_sq = [0.0; 2];
self.side_sq = 0.0;
self.channel_peak = [0.0; 2];
self.frame_count = 0;
}
}
@@ -361,10 +538,11 @@ fn smooth_level(previous: f32, next: f32, attack: f32, release: f32) -> f32 {
}
fn store_norm(target: &AtomicU32, value: f32) {
target.store(
(value.clamp(0.0, 1.0) * LEVEL_SCALE).round() as u32,
Ordering::Relaxed,
);
target.store(norm_bits(value), Ordering::Relaxed);
}
fn norm_bits(value: f32) -> u32 {
(value.clamp(0.0, 1.0) * LEVEL_SCALE).round() as u32
}
fn load_norm(source: &AtomicU32) -> f64 {
@@ -384,19 +562,24 @@ mod tests {
impl TestSource {
fn sine(frames: usize, channels: u16, sample_rate: u32, frequency: f32) -> Self {
let channels_count = channels.max(1);
let mut samples = Vec::with_capacity(frames * usize::from(channels_count));
let gains = vec![1.0; usize::from(channels.max(1))];
Self::sine_gains(frames, sample_rate, frequency, &gains)
}
/// A sine with a gain per channel (one entry per channel).
fn sine_gains(frames: usize, sample_rate: u32, frequency: f32, gains: &[f32]) -> Self {
let mut samples = Vec::with_capacity(frames * gains.len());
for frame in 0..frames {
let time = frame as f32 / sample_rate as f32;
let sample = (std::f32::consts::TAU * frequency * time).sin() * 0.5;
for _ in 0..channels_count {
samples.push(sample as Sample);
for gain in gains {
samples.push((sample * gain) as Sample);
}
}
Self {
samples,
cursor: 0,
channels: ChannelCount::new(channels_count).unwrap(),
channels: ChannelCount::new(u16::try_from(gains.len()).unwrap()).unwrap(),
sample_rate: SampleRate::new(sample_rate).unwrap(),
}
}
@@ -452,4 +635,71 @@ mod tests {
assert!(snapshot.bass > 0.0);
assert!(snapshot.scope.iter().any(|sample| sample.abs() > 0.001));
}
#[test]
fn analyzed_source_feeds_the_spectrum_and_channel_meters() {
let shared = Arc::new(AnalyzerShared::default());
let source = TestSource::sine(48_000, 2, 48_000, 1_000.0);
let analyzed = AnalyzedSource::new(source, Arc::clone(&shared));
for _ in analyzed {}
let snapshot = shared.snapshot();
assert_eq!(snapshot.sample_rate, 48_000);
let bands = crate::player::BAND_CENTERS_HZ;
let at_1k = bands.iter().position(|hz| *hz == 1_000.0).unwrap();
let loudest = snapshot
.spectrum
.iter()
.enumerate()
.max_by(|left, right| left.1.total_cmp(right.1))
.map(|(index, _)| index);
assert_eq!(loudest, Some(at_1k));
assert!(snapshot.level_l > 0.5 && (snapshot.level_l - snapshot.level_r).abs() < 1e-6);
// the same signal in both channels has no side component
assert_eq!(snapshot.level_side, 0.0);
assert_eq!(snapshot.stereo, Some(false));
assert!(snapshot.peak_l >= snapshot.level_l);
}
#[test]
fn analyzed_source_measures_the_side_signal() {
let measure = |gains: &[f32]| {
let shared = Arc::new(AnalyzerShared::default());
let source = TestSource::sine_gains(48_000, 48_000, 1_000.0, gains);
for _ in AnalyzedSource::new(source, Arc::clone(&shared)) {}
shared.snapshot()
};
// R = -L: the side signal (L - R) / 2 equals L
let opposite = measure(&[1.0, -1.0]);
assert!(opposite.level_side > 0.5);
assert!((opposite.level_side - opposite.level_l).abs() < 1e-3);
assert_eq!(opposite.stereo, Some(true));
// mono duplicates L into R
let mono = measure(&[1.0]);
assert_eq!(mono.level_side, 0.0);
assert_eq!(mono.stereo, Some(false));
// a third channel only feeds the mono sum
let surround = measure(&[1.0, 0.0, 1.0]);
assert!(surround.level_side > 0.0 && surround.level_side < surround.level_l);
}
#[test]
fn pcm_history_reads_newest_samples_and_silence_before_the_first() {
let shared = AnalyzerShared::default();
for value in [0.25_f32, 0.5, -0.5] {
shared.push_pcm(value);
}
let mut out = [1.0_f32; 5];
shared.copy_pcm(shared.pcm_write.load(Ordering::Relaxed), &mut out);
let rounded: Vec<f32> = out
.iter()
.map(|value| (value * 4.0).round() / 4.0)
.collect();
assert_eq!(rounded, [0.0, 0.0, 0.25, 0.5, -0.5]);
shared.clear();
shared.copy_pcm(shared.pcm_write.load(Ordering::Relaxed), &mut out);
assert!(out.iter().all(|value| *value == 0.0));
}
}
+3 -1
View File
@@ -6,6 +6,7 @@
mod analyzer;
mod opus;
mod spectrum;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
@@ -15,6 +16,7 @@ use std::time::Duration;
use rodio::{Decoder, DeviceSinkBuilder, Player, Source, stream::MixerDeviceSink};
pub use analyzer::AudioAnalysisSnapshot;
pub use spectrum::{BAND_CENTERS_HZ, FLOOR_DB};
pub trait TrackReadSeek: std::io::Read + std::io::Seek + Send + Sync {}
@@ -453,7 +455,7 @@ fn handle(
if let Some(out) = output.take() {
out.player.stop();
}
shared.analysis.clear();
shared.analysis.reset();
playing.track_loaded = false;
playing.base = Duration::ZERO;
}
+650
View File
@@ -0,0 +1,650 @@
//! Display meter for visualizations: a third-octave spectrum and stereo levels
//! with analyzer ballistics (fast attack, steady fall, peak hold).
//!
//! The audio thread only writes samples and per-window channel levels into
//! `AnalyzerShared`; the FFT and all smoothing run here, on the reader side,
//! when a snapshot is taken. Ballistics are time-based: rises and falls
//! advance with the clock, not with the number of snapshots.
use std::sync::Arc;
use std::time::Instant;
use rustfft::num_complex::Complex;
use rustfft::{Fft, FftPlanner};
/// Mono samples kept for the spectrum: enough for an ~85 ms window at
/// 192 kHz. The FFT uses the newest `fft_len(rate)` of them.
pub const PCM_SAMPLES: usize = 16_384;
/// Bottom of the display scale: values are normalized from `FLOOR_DB`..0 dBFS
/// to 0.0..1.0, so a full-scale sine reads 1.0.
pub const FLOOR_DB: f32 = -60.0;
/// Nominal ISO third-octave band centres (Hz), 20 Hz .. 20 kHz. These are
/// labels; band edges come from the exact base-2 centres (see `exact_center`)
/// so neighbouring bands meet without gaps or overlaps.
pub const BAND_CENTERS_HZ: [f32; 31] = [
20.0, 25.0, 31.5, 40.0, 50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0, 250.0, 315.0, 400.0,
500.0, 630.0, 800.0, 1_000.0, 1_250.0, 1_600.0, 2_000.0, 2_500.0, 3_150.0, 4_000.0, 5_000.0,
6_300.0, 8_000.0, 10_000.0, 12_500.0, 16_000.0, 20_000.0,
];
const BANDS: usize = BAND_CENTERS_HZ.len();
/// Index of the 1 kHz band.
const KHZ_BAND: i32 = 17;
const MIN_FFT: usize = 4096;
const BAND_ATTACK_SECS: f32 = 0.025;
const BAND_FALL_DB_PER_SEC: f32 = 36.0;
const PEAK_HOLD_SECS: f32 = 1.2;
const PEAK_FALL_DB_PER_SEC: f32 = 18.0;
const LEVEL_ATTACK_SECS: f32 = 0.05;
const LEVEL_RELEASE_SECS: f32 = 0.3;
/// A window counts as stereo when its side signal (L - R) / 2 is no more
/// than this far below the louder channel: lossy coding of a mono recording
/// leaves a side signal far below that, real stereo mixes sit well above it.
const STEREO_SIDE_DB: f32 = -30.0;
/// Only windows this far above the floor decide; quieter ones keep the last
/// decision, since their side signal is lost in the floor.
const STEREO_DECIDE_ABOVE_FLOOR_DB: f32 = 30.0;
/// Longest step applied at once; a stalled reader must not fast-forward a
/// whole second of decay in one frame.
const MAX_STEP_SECS: f32 = 0.25;
/// Audio arrives in device-callback bursts (rodio uses ~46 ms buffers), so a
/// snapshot may see no new samples while playback runs. Only a longer stall
/// (pause, stop) counts as silence.
const STALL_SECS: f32 = 0.15;
/// FFT length for a sample rate: ~85 ms of audio, a power of two.
fn fft_len(sample_rate: u32) -> usize {
(sample_rate as usize * MIN_FFT / 48_000)
.next_power_of_two()
.clamp(MIN_FFT, PCM_SAMPLES)
}
/// Exact base-2 third-octave centre of band `index` (1 kHz at index 17).
fn exact_center(index: usize) -> f32 {
1_000.0 * 2f32.powf((index as i32 - KHZ_BAND) as f32 / 3.0)
}
/// Linear channel levels of the latest analysis window.
#[derive(Debug, Clone, Copy, Default)]
pub struct StereoReading {
pub rms_l: f32,
pub rms_r: f32,
/// RMS of the side signal (L - R) / 2.
pub rms_side: f32,
/// Highest sample peaks since the previous reading.
pub peak_l: f32,
pub peak_r: f32,
}
#[derive(Debug, Clone, Default)]
pub struct MeterReading {
pub spectrum: Vec<f64>,
pub spectrum_peaks: Vec<f64>,
pub level_l: f64,
pub level_r: f64,
pub peak_l: f64,
pub peak_r: f64,
pub level_side: f64,
/// Whether the two channels carry different signals (see
/// `STEREO_SIDE_DB`); `None` until a loud enough window was measured.
pub stereo: Option<bool>,
}
struct Hold {
db: f32,
age: f32,
}
impl Hold {
fn new() -> Self {
Self {
db: FLOOR_DB,
age: 0.0,
}
}
fn update(&mut self, value_db: f32, dt: f32) {
if value_db >= self.db {
self.db = value_db;
self.age = 0.0;
return;
}
self.age += dt;
if self.age > PEAK_HOLD_SECS {
self.db = (self.db - PEAK_FALL_DB_PER_SEC * dt).max(value_db);
}
}
}
/// FFT plan, window and band bin ranges for one sample rate.
struct Analysis {
sample_rate: u32,
fft: Arc<dyn Fft<f32>>,
window: Vec<f32>,
/// Sum of squared window weights, for power normalization.
window_power: f32,
pcm: Vec<f32>,
buffer: Vec<Complex<f32>>,
scratch: Vec<Complex<f32>>,
/// Inclusive bin range per band; `None` above Nyquist.
bins: [Option<(usize, usize)>; BANDS],
}
impl Analysis {
fn new(sample_rate: u32) -> Self {
let len = fft_len(sample_rate);
let fft = FftPlanner::<f32>::new().plan_fft_forward(len);
let scratch = vec![Complex::default(); fft.get_inplace_scratch_len()];
let window: Vec<f32> = (0..len)
.map(|index| {
let phase = std::f32::consts::TAU * index as f32 / len as f32;
0.5 - 0.5 * phase.cos()
})
.collect();
let window_power = window.iter().map(|weight| weight * weight).sum();
Self {
sample_rate,
fft,
window,
window_power,
pcm: vec![0.0; len],
buffer: vec![Complex::default(); len],
scratch,
bins: band_bins(sample_rate, len),
}
}
/// Band power in dBFS (a full-scale sine reads 0 dB in its band). The
/// caller has filled `pcm`.
fn band_targets(&mut self) -> [f32; BANDS] {
for ((slot, sample), weight) in self.buffer.iter_mut().zip(&self.pcm).zip(&self.window) {
*slot = Complex::new(sample * weight, 0.0);
}
self.fft
.process_with_scratch(&mut self.buffer, &mut self.scratch);
// One-sided power per bin, normalized so the bins of a sine sum to
// its mean square (A^2 / 2); doubled below so the sine reads 0 dB.
let scale = 2.0 / (self.buffer.len() as f32 * self.window_power);
let mut out = [FLOOR_DB; BANDS];
for (slot, range) in out.iter_mut().zip(self.bins) {
if let Some((first, last)) = range {
let power: f32 = self.buffer[first..=last]
.iter()
.map(|bin| bin.norm_sqr() * scale)
.sum();
*slot = to_db_power(2.0 * power);
}
}
out
}
}
/// Partitions the FFT bins between the bands: each bin belongs to exactly one
/// band, from the first bin at or above the band's lower edge up to the bin
/// before the next band starts. A band narrower than one bin (deep bass at
/// short windows) reads the bin that contains its centre, which it then shares
/// with its neighbour.
fn band_bins(sample_rate: u32, len: usize) -> [Option<(usize, usize)>; BANDS] {
let bin_hz = sample_rate as f32 / len as f32;
let last_bin = len / 2 - 1;
let nyquist = sample_rate as f32 / 2.0;
let edge = 2f32.powf(1.0 / 6.0);
let first_bin = |index: usize| ((exact_center(index) / edge / bin_hz).ceil() as usize).max(1);
std::array::from_fn(|index| {
let low = exact_center(index) / edge;
if low >= nyquist {
return None;
}
let first = first_bin(index);
let last = if index + 1 < BANDS {
first_bin(index + 1).saturating_sub(1)
} else {
((exact_center(index) * edge / bin_hz).floor() as usize).max(first)
}
.min(last_bin);
if first <= last {
Some((first, last))
} else {
let center = ((exact_center(index) / bin_hz).round() as usize).clamp(1, last_bin);
Some((center, center))
}
})
}
pub struct Meter {
analysis: Option<Analysis>,
bands_db: [f32; BANDS],
band_peaks: [Hold; BANDS],
/// Left, right and side RMS levels.
level_db: [f32; 3],
level_peaks: [Hold; 2],
last_update: Option<Instant>,
last_write: usize,
/// When the PCM counter last moved, and what that audio measured; reused
/// between device callbacks until `STALL_SECS` pass.
last_fresh: Option<Instant>,
held_targets: [f32; BANDS],
held_rms: [f32; 3],
/// Last stereo decision; kept through silence, pauses and stalls.
stereo: Option<bool>,
}
impl std::fmt::Debug for Meter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Meter")
.field("bands_db", &self.bands_db)
.field("level_db", &self.level_db)
.finish_non_exhaustive()
}
}
impl Default for Meter {
fn default() -> Self {
Self {
analysis: None,
bands_db: [FLOOR_DB; BANDS],
band_peaks: std::array::from_fn(|_| Hold::new()),
level_db: [FLOOR_DB; 3],
level_peaks: std::array::from_fn(|_| Hold::new()),
last_update: None,
last_write: 0,
last_fresh: None,
held_targets: [FLOOR_DB; BANDS],
held_rms: [0.0; 3],
stereo: None,
}
}
}
impl Meter {
/// Advances the meter to `now`. `write` is the analyzer's PCM write
/// counter: while it keeps moving the newest audio is measured; once it
/// has not moved for `STALL_SECS` (paused, stopped) the meter is fed
/// silence and falls instead of freezing. `fill_pcm` copies the newest
/// `out.len()` mono samples, oldest first.
pub fn update(
&mut self,
now: Instant,
write: usize,
sample_rate: u32,
stereo: StereoReading,
fill_pcm: impl FnOnce(&mut [f32]),
) -> MeterReading {
let dt = self
.last_update
.map(|last| now.saturating_duration_since(last).as_secs_f32())
.unwrap_or(MAX_STEP_SECS)
.min(MAX_STEP_SECS);
self.last_update = Some(now);
let fresh = write != self.last_write;
self.last_write = write;
let (targets, rms, peaks) = if fresh && sample_rate > 0 {
if self
.analysis
.as_ref()
.is_none_or(|analysis| analysis.sample_rate != sample_rate)
{
self.analysis = Some(Analysis::new(sample_rate));
}
let analysis = self.analysis.as_mut().expect("analysis was just set");
fill_pcm(&mut analysis.pcm);
self.held_targets = analysis.band_targets();
self.held_rms = [stereo.rms_l, stereo.rms_r, stereo.rms_side];
// decided on the raw window, before ballistics: smoothed levels
// converge on the floor together and would read as stereo
let loud = stereo.rms_l.max(stereo.rms_r);
if to_db(loud) > FLOOR_DB + STEREO_DECIDE_ABOVE_FLOOR_DB {
self.stereo = Some(to_db(stereo.rms_side) > to_db(loud) + STEREO_SIDE_DB);
}
self.last_fresh = Some(now);
(
self.held_targets,
self.held_rms,
[stereo.peak_l, stereo.peak_r],
)
} else if self
.last_fresh
.is_some_and(|last| now.saturating_duration_since(last).as_secs_f32() <= STALL_SECS)
{
(self.held_targets, self.held_rms, [0.0; 2])
} else {
([FLOOR_DB; BANDS], [0.0; 3], [0.0; 2])
};
for ((current, target), peak) in self
.bands_db
.iter_mut()
.zip(targets)
.zip(self.band_peaks.iter_mut())
{
*current = follow_band(*current, target, dt);
peak.update(*current, dt);
}
for (level, value) in self.level_db.iter_mut().zip(rms) {
*level = follow_level(*level, to_db(value), dt);
}
for (hold, value) in self.level_peaks.iter_mut().zip(peaks) {
hold.update(to_db(value), dt);
}
MeterReading {
spectrum: self.bands_db.iter().map(|db| normalize(*db)).collect(),
spectrum_peaks: self
.band_peaks
.iter()
.map(|hold| normalize(hold.db))
.collect(),
level_l: normalize(self.level_db[0]),
level_r: normalize(self.level_db[1]),
peak_l: normalize(self.level_peaks[0].db),
peak_r: normalize(self.level_peaks[1].db),
level_side: normalize(self.level_db[2]),
stereo: self.stereo,
}
}
}
fn follow_band(current: f32, target: f32, dt: f32) -> f32 {
if target > current {
current + (target - current) * (1.0 - (-dt / BAND_ATTACK_SECS).exp())
} else {
(current - BAND_FALL_DB_PER_SEC * dt).max(target)
}
}
fn follow_level(current: f32, target: f32, dt: f32) -> f32 {
let tau = if target > current {
LEVEL_ATTACK_SECS
} else {
LEVEL_RELEASE_SECS
};
current + (target - current) * (1.0 - (-dt / tau).exp())
}
fn to_db(amplitude: f32) -> f32 {
if amplitude <= 0.0 || !amplitude.is_finite() {
FLOOR_DB
} else {
(20.0 * amplitude.log10()).max(FLOOR_DB)
}
}
fn to_db_power(power: f32) -> f32 {
if power <= 0.0 || !power.is_finite() {
FLOOR_DB
} else {
(10.0 * power.log10()).max(FLOOR_DB)
}
}
fn normalize(db: f32) -> f64 {
f64::from(((db - FLOOR_DB) / -FLOOR_DB).clamp(0.0, 1.0))
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
fn sine(frequency: f32, amplitude: f32, sample_rate: u32, len: usize) -> Vec<f32> {
(0..len)
.map(|index| {
let time = index as f32 / sample_rate as f32;
amplitude * (std::f32::consts::TAU * frequency * time).sin()
})
.collect()
}
fn band(center: f32) -> usize {
BAND_CENTERS_HZ
.iter()
.position(|value| *value == center)
.unwrap()
}
/// Feeds a steady tone for `steps` snapshots 50 ms apart.
fn settle(meter: &mut Meter, frequency: f32, sample_rate: u32, steps: u64) -> MeterReading {
let samples = sine(frequency, 0.5, sample_rate, PCM_SAMPLES);
let start = Instant::now();
let mut reading = MeterReading::default();
for step in 1..=steps {
reading = meter.update(
start + Duration::from_millis(50 * step),
step as usize,
sample_rate,
StereoReading::default(),
|out| out.copy_from_slice(&samples[..out.len()]),
);
}
reading
}
fn band_powers_db(frequency: f32, sample_rate: u32) -> [f32; BANDS] {
let mut analysis = Analysis::new(sample_rate);
let len = analysis.pcm.len();
analysis
.pcm
.copy_from_slice(&sine(frequency, 1.0, sample_rate, len));
analysis.band_targets()
}
#[test]
fn a_sine_lights_its_own_third_octave_band() {
let reading = settle(&mut Meter::default(), 1_000.0, 48_000, 10);
let at_1k = reading.spectrum[band(1_000.0)];
// -6 dBFS on a 60 dB scale reads 0.9.
assert!((at_1k - 0.9).abs() < 0.03, "1 kHz band read {at_1k}");
assert!(reading.spectrum[band(250.0)] < 0.2);
assert!(reading.spectrum[band(4_000.0)] < 0.2);
assert!(reading.spectrum_peaks[band(1_000.0)] >= at_1k);
}
#[test]
fn bands_cover_the_axis_without_gaps_or_double_counting() {
for sample_rate in [44_100, 48_000, 96_000] {
// Up to just below the top band's upper edge (~22.4 kHz) or
// Nyquist; 1/24-octave steps also land right on band edges.
let top = (sample_rate as f32 / 2.0 * 0.85).min(exact_center(BANDS - 1) * 1.1);
let mut frequency = 80.0_f32;
while frequency < top {
let powers = band_powers_db(frequency, sample_rate);
let loudest = powers.iter().copied().fold(FLOOR_DB, f32::max);
let total: f32 = powers
.iter()
.filter(|db| **db > FLOOR_DB)
.map(|db| 10f32.powf(db / 10.0))
.sum();
assert!(
(-3.5..=0.5).contains(&loudest),
"{frequency} Hz at {sample_rate}: loudest band {loudest} dB"
);
let total_db = 10.0 * total.log10();
assert!(
total_db.abs() < 0.5,
"{frequency} Hz at {sample_rate}: bands sum to {total_db} dB"
);
frequency *= 2f32.powf(1.0 / 24.0);
}
}
}
#[test]
fn deep_bass_reads_at_every_sample_rate() {
for sample_rate in [44_100, 48_000, 96_000, 192_000] {
let powers = band_powers_db(31.5, sample_rate);
assert!(
powers[band(31.5)] > -3.0,
"31.5 Hz at {sample_rate}: {} dB",
powers[band(31.5)]
);
assert!(powers[band(1_000.0)] < -40.0);
}
// Bands above Nyquist stay at the floor.
assert_eq!(band_powers_db(1_000.0, 32_000)[band(20_000.0)], FLOOR_DB);
}
#[test]
fn callback_gaps_keep_the_meter_steady_but_a_stall_falls() {
let mut meter = Meter::default();
let samples = sine(1_000.0, 0.5, 48_000, PCM_SAMPLES);
let tone = StereoReading {
rms_l: 0.35,
rms_r: 0.35,
rms_side: 0.0,
peak_l: 0.5,
peak_r: 0.5,
};
let start = Instant::now();
let mut write = 0;
let mut steady = MeterReading::default();
for step in 1..=40u64 {
// Every third snapshot lands between two device callbacks.
if step % 3 != 0 {
write += 1;
}
let reading = meter.update(
start + Duration::from_millis(50 * step),
write,
48_000,
tone,
|out| out.copy_from_slice(&samples[..out.len()]),
);
if step > 20 {
assert!(
(reading.level_l - steady.level_l).abs() < 0.02 || step == 21,
"level jumped at step {step}"
);
assert!(
(reading.spectrum[band(1_000.0)] - 0.9).abs() < 0.03,
"band jumped at step {step}"
);
}
steady = reading;
}
// The counter stops for longer than a callback gap: paused.
let index = band(1_000.0);
let paused_at = start + Duration::from_millis(2_000);
let first = meter.update(paused_at, write, 48_000, tone, |_| {});
let later = meter.update(
paused_at + Duration::from_millis(500),
write,
48_000,
tone,
|_| {},
);
assert!(later.spectrum[index] < first.spectrum[index]);
assert!(later.level_l < first.level_l);
// The peak marker holds for 1.2 s before it falls too.
assert!((later.spectrum_peaks[index] - 0.9).abs() < 0.03);
let mut reading = later;
for step in 1..=20u64 {
reading = meter.update(
paused_at + Duration::from_millis(500 + 250 * step),
write,
48_000,
tone,
|_| {},
);
}
assert_eq!(reading.spectrum[index], 0.0);
assert!(reading.spectrum_peaks[index] < 0.5);
}
#[test]
fn channel_levels_follow_rms_and_hold_peaks() {
let mut meter = Meter::default();
let start = Instant::now();
let loud_left = StereoReading {
rms_l: 0.5,
rms_r: 0.05,
rms_side: 0.2,
peak_l: 0.9,
peak_r: 0.1,
};
let mut reading = MeterReading::default();
for step in 1..=20 {
reading = meter.update(
start + Duration::from_millis(50 * step),
step as usize,
48_000,
loud_left,
|out| out.fill(0.0),
);
}
assert!(reading.level_l > reading.level_r);
assert!((reading.level_l - normalize(to_db(0.5))).abs() < 0.02);
assert!((reading.peak_l - normalize(to_db(0.9))).abs() < 0.01);
assert!(reading.peak_l >= reading.level_l);
assert!((reading.level_side - normalize(to_db(0.2))).abs() < 0.02);
assert_eq!(reading.stereo, Some(true));
}
#[test]
fn stereo_is_decided_on_loud_windows_and_kept_through_silence() {
let mut meter = Meter::default();
let start = Instant::now();
let mut read = |step: u64, stereo: StereoReading| {
meter.update(
start + Duration::from_millis(50 * step),
step as usize,
48_000,
stereo,
|out| out.fill(0.0),
)
};
let quiet = StereoReading {
rms_l: 0.0005,
rms_r: 0.0001,
rms_side: 0.0002,
..StereoReading::default()
};
assert_eq!(read(1, quiet).stereo, None);
let near_mono = StereoReading {
rms_l: 0.3,
rms_r: 0.3,
rms_side: 0.003,
..StereoReading::default()
};
assert_eq!(read(2, near_mono).stereo, Some(false));
// silence and then a stall keep the decision
assert_eq!(read(3, StereoReading::default()).stereo, Some(false));
let wide = StereoReading {
rms_side: 0.1,
..near_mono
};
assert_eq!(read(4, wide).stereo, Some(true));
let later = meter.update(
start + Duration::from_secs(5),
4,
48_000,
StereoReading::default(),
|out| out.fill(0.0),
);
assert_eq!(later.stereo, Some(true));
}
#[test]
fn silence_and_unknown_rate_read_the_floor() {
let mut meter = Meter::default();
let reading = meter.update(Instant::now(), 1, 0, StereoReading::default(), |_| {});
assert!(reading.spectrum.iter().all(|value| *value == 0.0));
assert_eq!(reading.level_l, 0.0);
assert_eq!(reading.level_side, 0.0);
assert_eq!(reading.spectrum.len(), BAND_CENTERS_HZ.len());
}
#[test]
fn fft_length_tracks_the_sample_rate() {
assert_eq!(fft_len(44_100), 4096);
assert_eq!(fft_len(48_000), 4096);
assert_eq!(fft_len(96_000), 8192);
assert_eq!(fft_len(192_000), 16_384);
assert_eq!(fft_len(0), 4096);
}
}
+3 -1
View File
@@ -25,10 +25,11 @@
//! "already subscribed" mark, an episode preview read from the feed and
//! subscribing from a card.
//!
//! Only RSS metadata is stored. Audio stays on the publisher's server.
//! Audio is streamed unless the user explicitly downloads an episode.
pub mod discovery;
pub mod fetch;
pub mod offline;
pub mod playback;
pub mod progress;
pub mod publication;
@@ -125,6 +126,7 @@ pub fn open_services(db_path: &Path, covers_dir: PathBuf, log: Arc<dyn OpLog>) -
let confirmations =
publication::ShareConfirmations::open(publication::confirmations_path(db_path));
Ok(Services {
downloads_dir: db_path.with_file_name("podcast-downloads"),
store: Arc::new(store),
fetcher: Arc::new(Fetcher::new(covers_dir)?),
log,
+99
View File
@@ -0,0 +1,99 @@
//! Explicit device-local downloads, outside the podcast sync store.
use anyhow::{Result, bail};
use music_dht::podcast::PodcastEpisodeRef;
use std::{
fs::{self, File},
io::{Read, Write},
path::{Path, PathBuf},
time::{Duration, SystemTime},
};
pub fn key(id: &str) -> String {
blake3::hash(id.as_bytes()).to_hex().to_string()
}
pub fn path(dir: &Path, ep: &PodcastEpisodeRef) -> PathBuf {
dir.join(key(ep.episode_id.as_str()))
}
pub fn available(dir: &Path) -> Result<std::collections::HashSet<String>> {
if !dir.exists() {
return Ok(Default::default());
}
Ok(fs::read_dir(dir)?
.filter_map(Result::ok)
.filter(|e| e.file_type().is_ok_and(|t| t.is_file()))
.filter_map(|e| {
let n = e.file_name().to_str()?.to_owned();
(n.len() == 64 && n.bytes().all(|b| b.is_ascii_hexdigit())).then_some(n)
})
.collect())
}
pub fn cleanup(
dir: &Path,
days: u32,
now: SystemTime,
) -> Result<std::collections::HashSet<String>> {
for name in available(dir)? {
let p = dir.join(name);
if now
.duration_since(fs::metadata(&p)?.modified()?)
.unwrap_or_default()
>= Duration::from_secs(u64::from(days.max(1)) * 86400)
{
let _ = fs::remove_file(p);
}
}
available(dir)
}
pub fn open(dir: &Path, ep: &PodcastEpisodeRef) -> Result<Option<File>> {
match File::open(path(dir, ep)) {
Ok(f) => Ok(Some(f)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
#[cfg(test)]
pub fn download(dir: &Path, ep: &PodcastEpisodeRef) -> Result<()> {
download_with_cancel(dir, ep, super::stream::CancelToken::never())
}
pub fn download_with_cancel(
dir: &Path,
ep: &PodcastEpisodeRef,
cancel: super::stream::CancelToken,
) -> Result<()> {
let target = path(dir, ep);
let dir = target.parent().unwrap();
fs::create_dir_all(dir)?;
if target.is_file() {
return Ok(());
}
let started = std::time::Instant::now();
let mut stream = super::stream::open_with(
&ep.audio_url,
super::stream::CancelToken::new(move || {
cancel.is_cancelled() || started.elapsed() > Duration::from_secs(7200)
}),
)?;
let mut temp = tempfile::NamedTempFile::new_in(dir)?;
let mut buf = [0; 65536];
let mut total = 0u64;
loop {
let n = stream.reader.read(&mut buf)?;
if n == 0 {
break;
}
total += n as u64;
if total > 4 * 1024 * 1024 * 1024 {
bail!("episode exceeds 4 GiB");
}
temp.write_all(&buf[..n])?;
}
if total == 0
|| stream.probe.end() == super::stream::StreamEnd::Failed
|| stream.byte_len.is_some_and(|len| len != total)
{
bail!("incomplete episode download");
}
temp.as_file().sync_all()?;
temp.persist(target)
.map_err(|_| anyhow::anyhow!("cannot save episode"))?;
Ok(())
}
+58 -1
View File
@@ -163,6 +163,9 @@ pub fn open_at(
start_ms: i64,
cancel: &CancelToken,
) -> Result<PreparedEpisode> {
if cancel.is_cancelled() {
return Err(Cancelled.into());
}
let opened = stream::open_with(&episode.audio_url, cancel.clone())?;
let seekable = opened.seekable;
let probe = opened.probe.clone();
@@ -218,6 +221,43 @@ pub fn open_at(
})
}
/// Decode a completed local download. Blocking; no HTTP requests.
pub fn open_local(
episode: &PodcastEpisodeRef,
start_ms: i64,
file: std::fs::File,
) -> Result<PreparedEpisode> {
let len = file.metadata()?.len();
let mut source = decode_source_with(
Box::new(file),
Some(len),
episode.mime.as_deref(),
true,
true,
)
.map_err(|e| anyhow!(e))?;
if start_ms > 0 {
source
.try_seek(Duration::from_millis(start_ms as u64))
.map_err(|_| CannotResume {
position_ms: start_ms,
})?;
}
let measured = source
.total_duration()
.and_then(|d| i64::try_from(d.as_millis()).ok())
.filter(|ms| *ms > 0);
Ok(PreparedEpisode {
episode: episode.clone(),
source,
start_ms,
duration_ms: measured.or(episode.duration_ms),
duration_measured: measured.is_some(),
seekable: true,
probe: StreamProbe::local(),
})
}
/// [`resolve`] + [`open_at`], refreshing the feed once when the audio URL
/// answers with a client error (moved or expired file).
pub async fn prepare(
@@ -233,8 +273,25 @@ pub async fn prepare(
let lookup = queued.clone();
let (episode, start_ms) = blocking(move || resolve(store.as_deref(), &lookup, start)).await?;
let attempt = episode.clone();
let downloads_dir = services.as_ref().map(|s| s.downloads_dir.clone());
let attempt_cancel = cancel.clone();
let error = match blocking(move || open_at(&attempt, start_ms, &attempt_cancel)).await {
let error = match blocking(move || {
if attempt_cancel.is_cancelled() {
return Err(Cancelled.into());
}
let local = downloads_dir
.as_ref()
.map(|dir| super::offline::open(dir, &attempt))
.transpose()?
.flatten();
if let Some(file) = local {
open_local(&attempt, start_ms, file)
} else {
open_at(&attempt, start_ms, &attempt_cancel)
}
})
.await
{
Ok(prepared) => return Ok(prepared),
Err(error) => error,
};
+95
View File
@@ -834,3 +834,98 @@ fn a_superseded_resume_stops_reading_up_to_its_offset() {
started.elapsed()
);
}
#[tokio::test]
async fn downloaded_episode_plays_and_seeks_without_publisher() {
let f = fixture();
let (_, mut episode) = subscribed(&f).await;
f.server
.set("/offline.wav", Route::new(wav(8), "audio/wav"));
episode.audio_url = f.server.url("/offline.wav");
let download_dir = f.services.downloads_dir.clone();
let download_episode = episode.clone();
tokio::task::spawn_blocking(move || super::offline::download(&download_dir, &download_episode))
.await
.unwrap()
.unwrap();
assert!(
super::offline::available(&f.services.downloads_dir)
.unwrap()
.contains(&super::offline::key(episode.episode_id.as_str()))
);
// No store lookup can replace this reference with the fixture's old URL.
episode.audio_url = "http://127.0.0.1:1/unavailable".into();
let file = super::offline::open(&f.services.downloads_dir, &episode)
.unwrap()
.unwrap();
let prepared = playback::open_local(&episode, 2000, file).unwrap();
assert_eq!(prepared.start_ms, 2000);
assert!(prepared.seekable);
assert_eq!(prepared.probe.end(), StreamEnd::Complete);
assert!(prepared.source.take(100).count() > 0);
// Prepare must prefer the same local file even though resolve finds the old HTTP URL.
let prepared = playback::prepare(
Some(f.services.clone()),
episode,
StartAt::Position { ms: 2000 },
NOW,
CancelToken::never(),
)
.await
.unwrap();
assert_eq!(prepared.start_ms, 2000);
}
#[test]
fn download_cleanup_expires_only_completed_files_at_the_boundary() {
use std::fs::{File, FileTimes};
use std::time::SystemTime;
let dir = tempfile::tempdir().unwrap();
let now = SystemTime::now();
let expired = dir.path().join(super::offline::key("old"));
let fresh = dir.path().join(super::offline::key("new"));
File::create(&expired)
.unwrap()
.set_times(FileTimes::new().set_modified(now - Duration::from_secs(30 * 86400)))
.unwrap();
File::create(&fresh)
.unwrap()
.set_times(FileTimes::new().set_modified(now - Duration::from_secs(29 * 86400)))
.unwrap();
let unrelated = dir.path().join("notes.txt");
std::fs::write(&unrelated, b"keep").unwrap();
let remaining = super::offline::cleanup(dir.path(), 30, now).unwrap();
assert!(!expired.exists());
assert!(fresh.exists());
assert!(unrelated.exists());
assert_eq!(remaining.len(), 1);
assert_eq!(
super::offline::cleanup(dir.path(), 30, now).unwrap(),
remaining
);
}
#[tokio::test]
async fn failed_download_never_becomes_available_offline() {
let f = fixture();
let (_, mut episode) = subscribed(&f).await;
episode.audio_url = f.server.url("/missing-audio");
let download_dir = f.services.downloads_dir.clone();
assert!(
tokio::task::spawn_blocking(move || super::offline::download(&download_dir, &episode))
.await
.unwrap()
.is_err()
);
assert!(
super::offline::available(&f.services.downloads_dir)
.unwrap()
.is_empty()
);
assert_eq!(
std::fs::read_dir(&f.services.downloads_dir)
.unwrap()
.count(),
0
);
}
+1
View File
@@ -28,6 +28,7 @@ pub const FAILURE_BACKOFF_MAX: Duration = Duration::from_secs(24 * 60 * 60);
#[derive(Clone)]
pub struct Services {
pub store: Arc<PodcastStore>,
pub downloads_dir: std::path::PathBuf,
pub fetcher: Arc<Fetcher>,
pub log: Arc<dyn OpLog>,
/// Device-local privacy confirmations for sharing (never synced).
+9 -3
View File
@@ -162,7 +162,7 @@ pub enum StreamEnd {
/// Read-only view of a stream's state that outlives handing the reader to
/// the decoder.
#[derive(Clone)]
pub struct StreamProbe(Arc<Shared>);
pub struct StreamProbe(Option<Arc<Shared>>);
impl std::fmt::Debug for StreamProbe {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -171,8 +171,14 @@ impl std::fmt::Debug for StreamProbe {
}
impl StreamProbe {
pub fn local() -> Self {
Self(None)
}
pub fn end(&self) -> StreamEnd {
let state = self.0.lock();
let Some(shared) = &self.0 else {
return StreamEnd::Complete;
};
let state = shared.lock();
if state.read_failed {
StreamEnd::Failed
} else if state.declared_total {
@@ -385,7 +391,7 @@ pub fn open_with(url: &str, cancel: CancelToken) -> Result<OpenedStream> {
.spawn(move || downloader.run(response))
.context("spawning the podcast stream thread")?;
Ok(OpenedStream {
probe: StreamProbe(Arc::clone(&shared)),
probe: StreamProbe(Some(Arc::clone(&shared))),
reader: HttpStream { pos: 0, shared },
byte_len: opened.total,
mime_type,
+5
View File
@@ -57,6 +57,11 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
selected = items.len();
}
let (label, value, enabled) = match row {
SettingsRow::PodcastRetention => (
"Podcast downloads: keep days".into(),
state.podcast_retention_days.to_string(),
true,
),
SettingsRow::MusicDirectory => (
"Music save directory".into(),
if state.music_dir_changing {
+11
View File
@@ -652,6 +652,17 @@ fn draw_episode_row(
_ => String::new(),
};
let mut title = episode.title.clone();
if state
.podcast_downloading
.contains(episode.episode_id.as_str())
{
title.push_str(" [downloading]");
} else if state
.podcast_downloads
.contains(&crate::podcasts::offline::key(episode.episode_id.as_str()))
{
title.push_str(" [offline]");
}
if !item.in_feed {
title.push_str(" (no longer in the feed)");
}
File diff suppressed because it is too large. Load diff
+980 -25
View File
File diff suppressed because it is too large. Load diff
+17 -15
View File
@@ -495,15 +495,16 @@ mod tests {
#[test]
fn loud_recordings_use_multiple_heights_instead_of_a_solid_row() {
let file = tempfile::NamedTempFile::new().unwrap();
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("braille.rhai");
let mut runtime = ScriptRuntime::default();
std::fs::write(file.path(), LEGACY_SOURCE).unwrap();
let old = prepare(&mut runtime, file.path(), input(64, 1.0), 64).unwrap();
std::fs::write(&path, LEGACY_SOURCE).unwrap();
let old = prepare(&mut runtime, &path, input(64, 1.0), 64).unwrap();
assert!(old.content.iter().all(|cell| cell.symbol() == "⣿"));
install_script_at(file.path()).unwrap();
install_script_at(&path).unwrap();
// A fresh runtime also models a restart installing the new default.
let mut runtime = ScriptRuntime::default();
let new = prepare(&mut runtime, file.path(), input(64, 1.0), 64).unwrap();
let new = prepare(&mut runtime, &path, input(64, 1.0), 64).unwrap();
let symbols: std::collections::HashSet<_> =
new.content.iter().map(|cell| cell.symbol()).collect();
assert!(symbols.len() >= 4, "{symbols:?}");
@@ -524,23 +525,24 @@ mod tests {
.collect::<Array>()
.into(),
);
let silent = prepare(&mut runtime, file.path(), silence, 64).unwrap();
let silent = prepare(&mut runtime, &path, silence, 64).unwrap();
assert!(silent.content.iter().all(|cell| cell.symbol() == "⠀"));
}
#[test]
fn only_the_untouched_previous_default_is_upgraded() {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), LEGACY_SOURCE).unwrap();
install_script_at(file.path()).unwrap();
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), SOURCE);
install_script_at(file.path()).unwrap();
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), SOURCE);
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("braille.rhai");
std::fs::write(&path, LEGACY_SOURCE).unwrap();
install_script_at(&path).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), SOURCE);
install_script_at(&path).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), SOURCE);
for stock in [LEGACY_SOURCE, SOURCE] {
let custom = format!("{stock}\n// user customization\n");
std::fs::write(file.path(), &custom).unwrap();
install_script_at(file.path()).unwrap();
assert_eq!(std::fs::read_to_string(file.path()).unwrap(), custom);
std::fs::write(&path, &custom).unwrap();
install_script_at(&path).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), custom);
}
}