Files
Marcos Paulo 5e9b5d3cfb
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
feat: audio player, code files with highlighting, ico, draw tools fixed, activity panel, calendar-title link, lightbox and shell fixes
Audio tab and note embeds with waveform, live spectrum (own FFT), seek, speed, loop and volume through ffplay/ffmpeg; code and text files open in an editable source editor with syntax highlighting, a Language chip and a banner to install, pick or ignore a grammar; ico decoding through the largest entry; info card as a clean grid; draw tools: arrows and lines clamped and clipped to the picture with a head, inline text tool, hotkeys; Activity tab with unread count and hotkey; calendar title shows a link icon and a pointing hand; explorer trash button works on press; right panel tab pills shrink so they never cover the collapse button; math: unknown command keeps a parser tooltip; test files grouped into tests/ folders (12-file rule); flaky search-order test waits for the rows. Built by Claude Sonnet and MiniMax M3.1 Flash; the whole tree verified with cargo xtask ci (fmt, clippy -D warnings, 4422 tests, layers, tree, i18n) on a copy of the working tree.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 02:31:52 -03:00

376 lines
16 KiB
Rust

//! The audio tab and the audio embed, drawn headless: the controls and the waveform area are there,
//! Space asks the player for a start, the waveform fills from a real decode, and an ignored test
//! plays a second of sound through `ffplay`.
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use egui_kittest::kittest::Queryable;
use notescraft_engine::Session;
use notescraft_themes::builtin;
use notescraft_ui_egui::media_view::audio::bus::{AudioCommand, AudioState, commands_id, state_id};
use notescraft_ui_egui::media_view::audio::spectrum::{self, Feed, Ring};
use notescraft_ui_egui::media_view::audio::{self, waveform};
use notescraft_ui_egui::theme::Tokens;
const RATE: u32 = 44_100;
const SECONDS: u32 = 5;
/// The length of the sweep, in seconds, as a float.
const SECONDS_F: f32 = 5.0;
/// The time of sample `index` at `rate` samples per second, without lossy integer casts.
fn seconds_at(index: u32, rate: u32) -> f32 {
let whole = f32::from(u16::try_from(index / rate).unwrap_or(u16::MAX));
let part = f32::from(u16::try_from(index % rate).unwrap_or(0)) / f32::from(u16::try_from(rate).unwrap_or(1));
whole + part
}
/// A float sample as a 16-bit one, clamped to the range of the type.
#[allow(clippy::cast_possible_truncation, reason = "the value is clamped to the i16 range first")]
fn quantize(value: f32) -> i16 {
value.round().clamp(f32::from(i16::MIN), f32::from(i16::MAX)) as i16
}
fn tokens() -> Tokens {
Tokens::new(builtin("catppuccin-mocha").expect("the built-in theme"))
}
/// The sample `t` seconds into a 150 Hz to 3.5 kHz sweep (inside the 4 kHz the waveform stream keeps), loud in the middle.
fn sweep(t: f32) -> f32 {
let phase = std::f32::consts::TAU * (150.0 * t + 335.0 * t * t);
phase.sin() * (0.2 + 0.8 * (std::f32::consts::PI * t / SECONDS_F).sin())
}
/// A 16-bit mono WAV of the sweep.
fn wav_bytes() -> Vec<u8> {
let samples: Vec<i16> = (0..RATE * SECONDS).map(|i| quantize(sweep(seconds_at(i, RATE)) * 30_000.0)).collect();
let data = u32::try_from(samples.len() * 2).expect("small");
let mut out = Vec::new();
out.extend(b"RIFF");
out.extend((36 + data).to_le_bytes());
out.extend(b"WAVEfmt ");
out.extend(16_u32.to_le_bytes());
out.extend(1_u16.to_le_bytes());
out.extend(1_u16.to_le_bytes());
out.extend(RATE.to_le_bytes());
out.extend((RATE * 2).to_le_bytes());
out.extend(2_u16.to_le_bytes());
out.extend(16_u16.to_le_bytes());
out.extend(b"data");
out.extend(data.to_le_bytes());
out.extend(samples.iter().flat_map(|sample| sample.to_le_bytes()));
out
}
fn session_with_wav(name: &str) -> Session {
let root = std::env::temp_dir().join(format!("notescraft-audio-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let _ = std::fs::create_dir_all(&root);
let _ = std::fs::write(root.join("song.wav"), wav_bytes());
Session::open(&root).expect("a temp vault opens")
}
fn idle(available: bool) -> AudioState {
AudioState {
available,
path: None,
starting: false,
position: 0.0,
duration: None,
speed: 1.0,
volume: 100,
muted: false,
looping: false,
paused: false,
feed: None,
}
}
fn has_ffmpeg() -> bool {
std::process::Command::new("ffmpeg").arg("-version").stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok()
}
/// A harness over the audio tab of `song.wav`, with the state `state` published each frame.
fn harness(name: &str, state: impl Fn(&egui::Context) -> AudioState + 'static) -> egui_kittest::Harness<'static, bool> {
let session = session_with_wav(name);
let tokens = tokens();
egui_kittest::Harness::builder().with_size(egui::Vec2::new(1100.0, 700.0)).build_ui_state(
move |ui, warmed: &mut bool| {
if !*warmed {
notescraft_ui_egui::fonts::install(ui.ctx());
egui_extras::install_image_loaders(ui.ctx());
*warmed = true;
return;
}
let published = state(ui.ctx());
ui.ctx().data_mut(|data| data.insert_temp(state_id(), published));
audio::show(ui, &tokens, &session, "song.wav");
},
false,
)
}
#[test]
fn the_audio_tab_has_its_controls_and_a_waveform_and_space_asks_for_a_start() {
let mut harness = harness("controls", |_| idle(true));
harness.run_steps(4);
for label in [
"Play",
"Back 10 seconds",
"Forward 10 seconds",
"Play from the start",
"Loop",
"Mute",
"Spectrum",
"Waveform",
"Frequency spectrum",
"Open with the system",
"Show in the file manager",
"Copy the path",
] {
assert!(harness.get_all_by_label_contains(label).count() >= 1, "the {label} control is there");
}
harness.key_press(egui::Key::Space);
harness.step();
let queued = harness.ctx.data(|data| data.get_temp::<Vec<AudioCommand>>(commands_id())).unwrap_or_default();
assert!(matches!(queued.first(), Some(AudioCommand::Play { path, from }) if path == "song.wav" && *from == 0.0), "{queued:?}");
}
#[test]
fn without_ffplay_the_tab_offers_the_system_player() {
let mut harness = harness("noplayer", |_| idle(false));
harness.run_steps(3);
assert!(harness.get_all_by_label_contains("ffplay is not installed").count() >= 1);
assert_eq!(harness.get_all_by_label("Open with the system").count(), 1);
}
#[test]
fn the_main_button_holds_a_playing_file_and_the_volume_keys_step_the_volume() {
let mut harness = harness("hold", |_| AudioState { path: Some("song.wav".into()), position: 2.0, duration: Some(5.0), ..idle(true) });
harness.run_steps(3);
harness.get_by_label("Pause").click();
harness.step();
let queued = harness.ctx.data(|data| data.get_temp::<Vec<AudioCommand>>(commands_id())).unwrap_or_default();
assert!(queued.contains(&AudioCommand::Pause(true)), "{queued:?}");
harness.key_press(egui::Key::ArrowDown);
harness.key_press(egui::Key::M);
harness.step();
let queued = harness.ctx.data(|data| data.get_temp::<Vec<AudioCommand>>(commands_id())).unwrap_or_default();
assert!(queued.contains(&AudioCommand::Volume(90)) && queued.contains(&AudioCommand::Mute(true)), "{queued:?}");
}
#[test]
fn the_waveform_fills_from_a_real_decode_and_is_cached() {
if !has_ffmpeg() {
return;
}
let mut harness = harness("peaks", |_| idle(true));
let session_root = std::env::temp_dir().join(format!("notescraft-audio-peaks-{}", std::process::id())).join("song.wav");
let started = Instant::now();
let peaks = loop {
harness.run_steps(2);
let seen = waveform::analysis(&harness.ctx, "song.wav", Some(&session_root));
if let Some(peaks) = seen.peaks {
assert!((seen.duration.unwrap_or(0.0) - SECONDS_F).abs() < 0.1, "the probe read the length: {:?}", seen.duration);
break peaks;
}
assert!(started.elapsed() < Duration::from_secs(10), "the decode did not finish");
std::thread::sleep(Duration::from_millis(50));
};
assert_eq!(peaks.columns.len(), waveform::COLUMNS);
assert!((peaks.seconds - SECONDS_F).abs() < 0.1);
let loudest = peaks.columns.iter().fold(0.0_f32, |top, (min, max)| top.max(min.abs()).max(*max));
assert!((loudest - 1.0).abs() < 1e-3, "normalised: {loudest}");
let again = Instant::now();
assert!(waveform::analysis(&harness.ctx, "song.wav", Some(&session_root)).peaks.is_some(), "the second look is instant");
assert!(again.elapsed() < Duration::from_millis(50));
}
#[test]
fn the_embed_shows_a_play_button_and_a_waveform_and_asks_for_a_start() {
let tokens = tokens();
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(700.0, 200.0)).build_ui_state(
move |ui, warmed: &mut bool| {
if !*warmed {
notescraft_ui_egui::fonts::install(ui.ctx());
egui_extras::install_image_loaders(ui.ctx());
*warmed = true;
return;
}
ui.ctx().data_mut(|data| data.insert_temp(state_id(), idle(true)));
audio::embed::show(ui, &tokens, "song.wav");
},
false,
);
harness.run_steps(3);
assert!(harness.get_all_by_label_contains("Waveform").count() >= 1);
harness.get_by_label("Play").click();
harness.step();
let queued = harness.ctx.data(|data| data.get_temp::<Vec<AudioCommand>>(commands_id())).unwrap_or_default();
assert!(matches!(queued.first(), Some(AudioCommand::Play { path, .. }) if path == "song.wav"), "{queued:?}");
}
/// A feed holding the sweep, as the decoder would have filled it.
fn sweep_feed() -> Feed {
let mut ring = Ring::default();
let samples: Vec<f32> = (0..spectrum::RATE * SECONDS).map(|i| sweep(seconds_at(i, spectrum::RATE))).collect();
ring.push(&samples);
Feed { ring: Arc::new(Mutex::new(ring)), base: 0.0 }
}
#[test]
fn the_spectrum_follows_the_sweep_at_the_playback_position() {
let feed = sweep_feed();
let ctx = egui::Context::default();
let loudest_at = |position: f32| {
for _ in 0..40 {
spectrum::step(&ctx, Some(&feed), position, true, 1.0 / 60.0);
}
let shared = spectrum::shared(&ctx);
let guard = shared.lock().expect("the analyzer lock");
let levels = guard.as_ref().expect("planned").levels;
levels.iter().enumerate().max_by(|a, b| a.1.total_cmp(b.1)).map(|(band, _)| band).expect("bands")
};
let early = loudest_at(0.5);
let late = loudest_at(4.0);
assert!(late > early + 10, "the loudest band climbs with the sweep: {early} then {late}");
for _ in 0..300 {
spectrum::step(&ctx, None, 0.0, false, 1.0 / 60.0);
}
assert!(!spectrum::step(&ctx, None, 0.0, false, 1.0 / 60.0), "at rest no frame is wanted");
}
/// Renders the playing tab to `NOTESCRAFT_AUDIO_PNG` (a look for the eye), with the sweep on the spectrum.
#[test]
#[ignore = "needs a GPU adapter; run by hand with NOTESCRAFT_AUDIO_PNG=/path/out.png"]
fn the_playing_audio_tab_renders() {
let Ok(out) = std::env::var("NOTESCRAFT_AUDIO_PNG") else { return };
let feed = sweep_feed();
let mut harness = harness("render", move |_| AudioState {
path: Some("song.wav".into()),
position: 3.0,
duration: Some(SECONDS_F),
feed: Some(feed.clone()),
..idle(true)
});
let started = Instant::now();
loop {
harness.run_steps(5);
std::thread::sleep(Duration::from_millis(40));
if started.elapsed() > Duration::from_millis(2500) {
break;
}
}
harness.render().expect("the frame renders").save(out).expect("the png is written");
}
/// Renders the embed card (idle, then playing) to `NOTESCRAFT_AUDIO_PNG` with `.embed-*.png` appended.
#[test]
#[ignore = "needs a GPU adapter; run by hand with NOTESCRAFT_AUDIO_PNG=/path/out"]
fn the_audio_embed_renders() {
let Ok(out) = std::env::var("NOTESCRAFT_AUDIO_PNG") else { return };
let _session = session_with_wav("embed-render");
let tokens = tokens();
let root = std::env::temp_dir().join(format!("notescraft-audio-embed-render-{}", std::process::id()));
let _ = std::fs::create_dir_all(&root);
let _ = std::fs::write(root.join("song.wav"), wav_bytes());
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(720.0, 150.0)).build_ui_state(
move |ui, warmed: &mut bool| {
if !*warmed {
notescraft_ui_egui::fonts::install(ui.ctx());
egui_extras::install_image_loaders(ui.ctx());
*warmed = true;
return;
}
ui.ctx()
.data_mut(|data| data.insert_temp(state_id(), AudioState { path: Some("song.wav".into()), position: 2.0, duration: Some(5.0), ..idle(true) }));
ui.ctx().data_mut(|data| data.insert_temp(audio::bus::root_id(), root.clone()));
ui.add_space(8.0);
audio::embed::show(ui, &tokens, "song.wav");
},
false,
);
let started = Instant::now();
while started.elapsed() < Duration::from_millis(2000) {
harness.run_steps(3);
std::thread::sleep(Duration::from_millis(40));
}
harness.render().expect("the frame renders").save(format!("{out}.embed.png")).expect("the png is written");
}
/// Plays a second of the wav through `ffplay` and checks the position advances.
#[test]
#[ignore = "plays sound: needs ffplay and an audio device"]
fn a_wav_really_plays_and_its_position_advances() {
use notescraft_ui_egui::media_view::audio::bus::{command, state};
use notescraft_ui_egui::media_view::audio::player::AudioPlayer;
let dir = std::env::temp_dir().join(format!("notescraft-audio-play-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let file: &Path = &dir.join("song.wav");
std::fs::write(file, wav_bytes()).expect("the wav is written");
let ctx = egui::Context::default();
let mut player = AudioPlayer::default();
command(&ctx, AudioCommand::Play { path: "song.wav".into(), from: 1.0 });
let started = Instant::now();
let mut seen = Vec::new();
while started.elapsed() < Duration::from_millis(1800) {
player.tick(&ctx, &dir);
seen.push((started.elapsed().as_secs_f32(), state(&ctx).map_or(0.0, |state| state.position)));
std::thread::sleep(Duration::from_millis(20));
}
let last = state(&ctx).expect("the state is published");
assert!(last.available, "ffplay is installed");
assert_eq!(last.path.as_deref(), Some("song.wav"));
assert!(last.position > 1.8 && last.position < 2.8, "one second in from 1.0: {seen:?}");
assert!(last.feed.is_some(), "the spectrum stream runs");
command(&ctx, AudioCommand::Stop);
player.tick(&ctx, &dir);
assert!(state(&ctx).is_some_and(|state| state.path.is_none()), "stopped");
}
/// Plays every file of `NOTESCRAFT_AUDIO_DIR` for a second and prints one line per format.
#[test]
#[ignore = "plays sound: needs ffplay, an audio device and NOTESCRAFT_AUDIO_DIR=/folder/of/audio"]
fn every_format_of_a_folder_plays_and_gets_a_waveform() {
use notescraft_ui_egui::media_view::audio::bus::{command, state};
use notescraft_ui_egui::media_view::audio::player::AudioPlayer;
let Ok(dir) = std::env::var("NOTESCRAFT_AUDIO_DIR") else { return };
let dir = std::path::PathBuf::from(dir);
let ctx = egui::Context::default();
let mut player = AudioPlayer::default();
let mut names: Vec<String> =
std::fs::read_dir(&dir).expect("the folder reads").filter_map(|entry| Some(entry.ok()?.file_name().to_string_lossy().into_owned())).collect();
names.sort();
let mut failures = Vec::new();
for name in names {
command(&ctx, AudioCommand::Play { path: name.clone(), from: 0.0 });
let started = Instant::now();
while started.elapsed() < Duration::from_millis(1300) {
player.tick(&ctx, &dir);
std::thread::sleep(Duration::from_millis(20));
}
let seen = state(&ctx).expect("state");
let waveform_ready = loop {
let analysis = waveform::analysis(&ctx, &name, Some(&dir.join(&name)));
if analysis.peaks.is_some() || analysis.failed || started.elapsed() > Duration::from_secs(10) {
break analysis.peaks.is_some();
}
std::thread::sleep(Duration::from_millis(30));
};
let spectrum = seen.feed.as_ref().is_some_and(|feed| feed.ring.lock().is_ok_and(|ring| ring.written() > 0));
let plays = seen.has(&name) && seen.position > 0.5;
println!(
"FORMAT {name}: plays={plays} position={:.2} duration={:?} waveform={waveform_ready} spectrum_samples={spectrum}",
seen.position, seen.duration
);
if !(plays && waveform_ready && spectrum) {
failures.push(name);
}
command(&ctx, AudioCommand::Stop);
player.tick(&ctx, &dir);
}
assert!(failures.is_empty(), "{failures:?}");
}