audio: implement random SFX output

This commit is contained in:
2026-06-22 13:54:42 +02:00
parent 85fa485833
commit 74a823d1c2
6 changed files with 335 additions and 20 deletions
+10 -11
View File
@@ -85,10 +85,7 @@ impl Display for Role {
impl Role {
fn is_input(&self) -> bool {
match self {
Role::Mic => true,
_ => false
}
matches!(self, Role::Mic)
}
}
@@ -126,7 +123,7 @@ impl AudioSource {
fn process(&mut self, scope: &ProcessScope) -> Result<Option<f64>, AudioError> {
if self.port.connected_count()? > 0 {
let buf: Vec<_> = self.port.as_slice(scope).iter().copied().collect();
let buf: Vec<_> = self.port.as_slice(scope).to_vec();
self.meter.process_interleaved(&buf);
self.sample_sink.blocking_send(buf)?;
@@ -159,7 +156,11 @@ impl AudioSink {
}
fn process(&mut self, scope: &ProcessScope) -> Result<(), AudioError> {
let mut next_outbuf = self.sample_src.try_recv()?;
let mut next_outbuf = match self.sample_src.try_recv() {
Ok(buf) => buf,
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => return Ok(()),
Err(err) => return Err(err.into())
};
self.output_buf.append(&mut next_outbuf);
if self.port.connected_count()? > 0 && !self.output_buf.is_empty() {
@@ -167,9 +168,7 @@ impl AudioSink {
let mut next_segment: Vec<f32> = self.output_buf.drain(0..(outbuf.len()).min(self.output_buf.len())).collect();
let underrun = outbuf.len() - next_segment.len();
if underrun > 0 {
for _ in 0..underrun {
next_segment.push(0.);
}
next_segment.extend(std::iter::repeat_n(0., underrun));
}
outbuf.copy_from_slice(&next_segment);
@@ -228,7 +227,7 @@ impl NotificationHandler for Notify {
}).next();
if let Some((role, Ok(target_port))) = port_match {
let cfg_slot = self.config.connections.entry(*role).or_insert_with(|| Default::default());
let cfg_slot = self.config.connections.entry(*role).or_default();
if are_connected {
log::info!("{} connected to {}", role, target_port);
@@ -278,7 +277,7 @@ pub async fn start_audio_input() -> (AudioInputControl, AudioInStream, AudioOutS
} else {
(local_port, &peer)
};
if let Err(err) = client.connect_ports(&src, &dst) {
if let Err(err) = client.connect_ports(src, dst) {
log::error!("Failed to reconnect {} to {}: {:?}", role, peer_name, err);
} else {
log::info!("Reconnected {} to {}", role, peer_name);