forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

voiceover_record.rs49 lines, 2.1 KiBHistoryRawDownload
1//! Voice-over recording against the private audio stack. Ignored by default: it
2//! needs a PulseAudio-protocol server and a tone playing into the `edtest` sink
3//! while it runs. It only ever talks to the server named by `PULSE_SERVER`, so
4//! point it at the private stack (`scratchpad/pulse/private-audio.sh`):
5//!
6//! ```text
7//! PULSE_SERVER=unix:/tmp/edpa-run/pulse/native \
8//! cargo test -p engine --release --test voiceover_record -- --ignored --nocapture --test-threads=1
9//! ```
10
11use std::time::Duration;
12
13use engine::pulse;
14use engine::recorder::Recorder;
15
16/// The private stack's null sink monitor, the input the tone is played into.
17const SOURCE: &str = "edtest.monitor";
18/// The tone the test expects to hear.
19const TONE_HZ: f64 = 1000.0;
20
21#[test]
22#[ignore = "needs a private pulse-protocol server with a 1 kHz tone playing into edtest"]
23fn a_second_of_tone_from_the_monitor_records_at_its_frequency() {
24 let sources = pulse::list_sources().expect("the server lists its sources");
25 assert!(
26 sources.iter().any(|s| s.name == SOURCE && s.monitor),
27 "{SOURCE} is listed as a monitor: {sources:?}"
28 );
29
30 let dir = std::env::temp_dir().join(format!("edith-voiceover-{}", std::process::id()));
31 let recorder = Recorder::start(&dir, Some(SOURCE)).expect("the recording starts");
32 std::thread::sleep(Duration::from_secs(1));
33 let take = recorder.stop().expect("the recording stops");
34 let bytes = std::fs::read(&take.path).expect("the take is on disk");
35 let _ = std::fs::remove_dir_all(&dir);
36
37 assert!(take.duration_secs > 0.9, "about a second written: {take:?}");
38 // Left channel, 16-bit. A sine's rising zero crossings count its cycles.
39 let (frames, _) = bytes[44..].as_chunks::<4>();
40 let samples: Vec<i16> = frames.iter().map(|b| i16::from_le_bytes([b[0], b[1]])).collect();
41 let mut rising = 0usize;
42 for pair in samples.windows(2) {
43 if pair[0] <= 0 && pair[1] > 0 {
44 rising += 1;
45 }
46 }
47 let hz = rising as f64 / take.duration_secs;
48 assert!((hz - TONE_HZ).abs() < 0.05 * TONE_HZ, "heard {hz:.1} Hz, expected {TONE_HZ} Hz");
49}