Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

The repeated-error helper lives once, in io/trace (once.rs): the mixer and raea voice each carried a copy, which put raea-sys-mix over its size limit and added three duplicate groups

shinyobjectzf2351359c852
diff --git a/Cargo.lock b/Cargo.lock
index 4bdfa5f8..0dc9e259 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14044,6 +14044,7 @@ dependencies = [
"anyhow",
"cpal 0.15.3",
"ort",
+ "raea-io-trace",
"raea-sys-ml",
"raea-sys-speech",
"serde_json",
diff --git a/apps/voice/Cargo.toml b/apps/voice/Cargo.toml
index ff995452..bd5bb01f 100644
--- a/apps/voice/Cargo.toml
+++ b/apps/voice/Cargo.toml
@@ -12,6 +12,7 @@ path = "src/main.rs"
[dependencies]
# Pocket TTS and its word clock
+raea-io-trace = { path = "../../io/trace" }
raea-sys-speech = { path = "../../systems/speech" }
# ort sessions the way raea builds them, and the model cache
raea-sys-ml = { path = "../../systems/ml" }
diff --git a/apps/voice/src/audio.rs b/apps/voice/src/audio.rs
index 4e8ccf73..a9fb0500 100644
--- a/apps/voice/src/audio.rs
+++ b/apps/voice/src/audio.rs
@@ -108,7 +108,7 @@ pub fn microphone(tx: Sender<Vec<f32>>) -> anyhow::Result<cpal::Stream> {
rs.push(&mono, &mut out);
let _ = tx.send(out);
};
- let bad = said_once::<cpal::StreamError>("raea voice: the microphone");
+ let bad = raea_io_trace::once::said_once::<cpal::StreamError>("raea voice: the microphone");
let stream = match cfg.sample_format() {
cpal::SampleFormat::F32 => {
dev.build_input_stream(&cfg.config(), move |d: &[f32], _: &cpal::InputCallbackInfo| take(mono(d, ch)), bad, None)
@@ -242,7 +242,7 @@ impl Speaker {
f.fill(ready.pop_front().unwrap_or(0.0));
}
};
- let stream = dev.build_output_stream(&cfg.config(), play, said_once::<cpal::StreamError>("raea voice: the speakers"), None).map_err(|e| anyhow!("the speakers didn't open: {e}"))?;
+ let stream = dev.build_output_stream(&cfg.config(), play, raea_io_trace::once::said_once::<cpal::StreamError>("raea voice: the speakers"), None).map_err(|e| anyhow!("the speakers didn't open: {e}"))?;
stream.play().map_err(|e| anyhow!("the speakers didn't start: {e}"))?;
Ok(Speaker { line, rate, to, _stream: Some(stream) })
}
@@ -286,43 +286,3 @@ mod tests {
assert!(x.iter().zip(&y).all(|(a, b)| (a - b).abs() < 1e-3));
}
}
-
-/// A device's error said once, then only how often it came back (at its 10th, 100th, ... time, and never more than once
-/// a second): a failing ALSA device calls a stream's error callback without pause, and every call printed a line: #239's
-/// app.log reached 40 GB of one line in two days.
-fn said_once<E: std::fmt::Display>(what: &'static str) -> impl FnMut(E) + Send + 'static {
- let mut seen = Repeats::default();
- move |e| {
- let m = e.to_string();
- match seen.hit(&m, std::time::Instant::now()) {
- Some(1) => eprintln!("{what}: {m}"),
- Some(n) => eprintln!("{what}: {m} ({n} times now)"),
- None => {}
- }
- }
-}
-
-/// Which of an error's repeats get said: `hit` answers with the count to print, or nothing.
-#[derive(Default)]
-struct Repeats {
- last: String,
- n: u64,
- next: u64,
- at: Option<std::time::Instant>,
-}
-
-impl Repeats {
- fn hit(&mut self, m: &str, now: std::time::Instant) -> Option<u64> {
- if m != self.last || self.n == 0 {
- (self.last, self.n, self.next) = (m.to_string(), 0, 1);
- }
- self.n += 1;
- let soon = self.at.is_some_and(|t| now.duration_since(t).as_secs() < 1);
- if self.n < self.next || (soon && self.n > 1) {
- return None;
- }
- self.next = self.n.saturating_mul(10);
- self.at = Some(now);
- Some(self.n)
- }
-}
diff --git a/io/trace/src/lib.rs b/io/trace/src/lib.rs
index 3a2549f2..3f2ebb39 100644
--- a/io/trace/src/lib.rs
+++ b/io/trace/src/lib.rs
@@ -24,6 +24,7 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH};
use serde_json::{Value, json};
+pub mod once;
mod crash;
mod look;
mod props;
diff --git a/io/trace/src/once.rs b/io/trace/src/once.rs
new file mode 100644
index 00000000..205894ee
--- /dev/null
+++ b/io/trace/src/once.rs
@@ -0,0 +1,59 @@
+//! An error that repeats, said once (#239): a failing audio device calls its stream's error callback without pause.
+
+/// A device's error said once, then only how often it came back (at its 10th, 100th, ... time, and never more than once
+/// a second): a failing ALSA device calls a stream's error callback without pause, and every call printed a line: #239's
+/// app.log reached 40 GB of one line in two days.
+pub fn said_once<E: std::fmt::Display>(what: &'static str) -> impl FnMut(E) + Send + 'static {
+ let mut seen = Repeats::default();
+ move |e| {
+ let m = e.to_string();
+ match seen.hit(&m, std::time::Instant::now()) {
+ Some(1) => eprintln!("{what}: {m}"),
+ Some(n) => eprintln!("{what}: {m} ({n} times now)"),
+ None => {}
+ }
+ }
+}
+
+/// Which of an error's repeats get said: `hit` answers with the count to print, or nothing.
+#[derive(Default)]
+struct Repeats {
+ last: String,
+ n: u64,
+ next: u64,
+ at: Option<std::time::Instant>,
+}
+
+impl Repeats {
+ fn hit(&mut self, m: &str, now: std::time::Instant) -> Option<u64> {
+ if m != self.last || self.n == 0 {
+ (self.last, self.n, self.next) = (m.to_string(), 0, 1);
+ }
+ self.n += 1;
+ let soon = self.at.is_some_and(|t| now.duration_since(t).as_secs() < 1);
+ if self.n < self.next || (soon && self.n > 1) {
+ return None;
+ }
+ self.next = self.n.saturating_mul(10);
+ self.at = Some(now);
+ Some(self.n)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Repeats;
+ use std::time::{Duration, Instant};
+
+ /// #239: half a billion repeats of one ALSA error were half a billion lines. Two days of them are a handful.
+ #[test]
+ fn a_repeating_error_is_a_handful_of_lines() {
+ let (mut r, t0) = (Repeats::default(), Instant::now());
+ // 5,000 a second for a minute
+ let said = (0..300_000u64).filter(|i| r.hit("alsa::poll() returned POLLERR", t0 + Duration::from_micros(i * 200)).is_some()).count();
+ assert!(said <= 8, "{said} lines for 300,000 repeats");
+ assert_eq!(Repeats::default().hit("x", t0), Some(1), "the first is always said");
+ // another error is said at once, when a second has gone by
+ assert_eq!(r.hit("the device went away", t0 + Duration::from_secs(120)), Some(1));
+ }
+}
diff --git a/systems/mix/src/live.rs b/systems/mix/src/live.rs
index bf389d6f..b485a599 100644
--- a/systems/mix/src/live.rs
+++ b/systems/mix/src/live.rs
@@ -451,51 +451,11 @@ fn start(cx: &mut FirewheelContext, block: u32) -> Result<(cpal::Stream, u32), S
// the block size when the device's range takes it, else its own
let fits = dev.supported_output_configs().map(|mut c| c.any(|r| r.channels() == channels && matches!(r.buffer_size(), cpal::SupportedBufferSize::Range { min, max } if *min <= block && block <= *max))).unwrap_or(false);
let config = cpal::StreamConfig { channels, sample_rate: cpal::SampleRate(rate), buffer_size: if fits { cpal::BufferSize::Fixed(block) } else { cpal::BufferSize::Default } };
- let stream = dev.build_output_stream(&config, move |o: &mut [f32], i: &cpal::OutputCallbackInfo| callback(o, i), said_once::<cpal::StreamError>("raea mixer"), None).map_err(|e| format!("audio stream: {e}"))?;
+ let stream = dev.build_output_stream(&config, move |o: &mut [f32], i: &cpal::OutputCallbackInfo| callback(o, i), raea_io_trace::once::said_once::<cpal::StreamError>("raea mixer"), None).map_err(|e| format!("audio stream: {e}"))?;
stream.play().map_err(|e| format!("audio stream: {e}"))?;
Ok((stream, rate))
}
-/// A device's error said once, then only how often it came back (at its 10th, 100th, ... time, and never more than once
-/// a second): a failing ALSA device calls a stream's error callback without pause, and every call printed a line: #239's
-/// app.log reached 40 GB of one line in two days.
-fn said_once<E: std::fmt::Display>(what: &'static str) -> impl FnMut(E) + Send + 'static {
- let mut seen = Repeats::default();
- move |e| {
- let m = e.to_string();
- match seen.hit(&m, std::time::Instant::now()) {
- Some(1) => eprintln!("{what}: {m}"),
- Some(n) => eprintln!("{what}: {m} ({n} times now)"),
- None => {}
- }
- }
-}
-
-/// Which of an error's repeats get said: `hit` answers with the count to print, or nothing.
-#[derive(Default)]
-struct Repeats {
- last: String,
- n: u64,
- next: u64,
- at: Option<std::time::Instant>,
-}
-
-impl Repeats {
- fn hit(&mut self, m: &str, now: std::time::Instant) -> Option<u64> {
- if m != self.last || self.n == 0 {
- (self.last, self.n, self.next) = (m.to_string(), 0, 1);
- }
- self.n += 1;
- let soon = self.at.is_some_and(|t| now.duration_since(t).as_secs() < 1);
- if self.n < self.next || (soon && self.n > 1) {
- return None;
- }
- self.next = self.n.saturating_mul(10);
- self.at = Some(now);
- Some(self.n)
- }
-}
-
impl Drop for Live {
fn drop(&mut self) {
let Some((path, buf)) = self.tap.take() else { return };
@@ -510,21 +470,3 @@ impl Drop for Live {
let _ = std::fs::write(&side, format!("{{\"anchors\": [{}], \"report\": {}}}", anchors.join(", "), self.report()));
}
}
-
-#[cfg(test)]
-mod repeats {
- use super::Repeats;
- use std::time::{Duration, Instant};
-
- /// #239: half a billion repeats of one ALSA error were half a billion lines. Two days of them are a handful.
- #[test]
- fn a_repeating_error_is_a_handful_of_lines() {
- let (mut r, t0) = (Repeats::default(), Instant::now());
- // 5,000 a second for a minute
- let said = (0..300_000u64).filter(|i| r.hit("alsa::poll() returned POLLERR", t0 + Duration::from_micros(i * 200)).is_some()).count();
- assert!(said <= 8, "{said} lines for 300,000 repeats");
- assert_eq!(Repeats::default().hit("x", t0), Some(1), "the first is always said");
- // another error is said at once, when a second has gone by
- assert_eq!(r.hit("the device went away", t0 + Duration::from_secs(120)), Some(1));
- }
-}