desktop recording sorta works

This commit is contained in:
2025-10-26 17:37:36 -06:00
parent f2bdc665f5
commit fea6800bea
6 changed files with 104 additions and 32 deletions
+71 -11
View File
@@ -1,5 +1,5 @@
use crate::app::Command;
use crate::effects::AudioProcessor;
use crate::effects::{AudioProcessor, AudioProcessorSender};
use color_eyre::eyre::{eyre, Error};
use cpal::traits::{DeviceTrait, HostTrait};
use dioxus::hooks::{UnboundedReceiver, UnboundedSender};
@@ -7,6 +7,7 @@ use futures::io::{AsyncRead, AsyncWrite};
use mumble_protocol::control::{ClientControlCodec, ControlPacket};
use mumble_protocol::Serverbound;
use mumble_web2_common::ClientConfig;
use std::mem::replace;
use std::net::ToSocketAddrs;
use std::sync::Mutex;
use std::{fmt, io, sync::Arc};
@@ -18,7 +19,7 @@ use tokio_rustls::rustls::ClientConfig as RlsClientConfig;
use tokio_rustls::rustls::DigitallySignedStruct;
use tokio_rustls::TlsConnector;
use tokio_util::compat::{TokioAsyncReadCompatExt as _, TokioAsyncWriteCompatExt as _};
use tracing::{error, info, instrument, warn};
use tracing::{debug, error, info, instrument, warn};
pub use tokio::task::spawn;
pub use tokio::time::sleep;
@@ -32,8 +33,13 @@ impl<T: AsyncWrite + Unpin + Send + 'static> ImpWrite for T {}
pub struct AudioSystem {
output: cpal::Device,
input: cpal::Device,
processors: AudioProcessorSender,
recording_stream: Option<cpal::Stream>,
}
const SAMPLE_RATE: u32 = 48_000;
const PACKET_SAMPLES: u32 = 960;
type Buffer = Arc<Mutex<dasp_ring_buffer::Bounded<Vec<i16>>>>;
impl AudioSystem {
@@ -41,6 +47,7 @@ impl AudioSystem {
// TODO
let host = cpal::default_host();
let name = host.id();
let processors = AudioProcessorSender::default();
Ok(AudioSystem {
output: host
.default_output_device()
@@ -48,16 +55,66 @@ impl AudioSystem {
input: host
.default_input_device()
.ok_or(eyre!("no input devices from {name:?}"))?,
processors,
recording_stream: None,
})
}
pub fn set_processor(&self, processor: AudioProcessor) {
// TODO
self.processors.store(Some(processor))
}
pub fn start_recording(&mut self, each: impl FnMut(Vec<u8>) + 'static) -> Result<(), Error> {
// TODO
Ok(())
pub fn start_recording(
&mut self,
mut each: impl FnMut(Vec<u8>) + Send + 'static,
) -> Result<(), Error> {
let mut encoder =
opus::Encoder::new(48_000, opus::Channels::Mono, opus::Application::Voip)?;
let mut current_processor = AudioProcessor::default();
let mut output_buffer = Vec::new();
let processors = self.processors.clone();
let error_callback = move |e: cpal::StreamError| error!("error recording: {e:?}");
let data_callback = move |frame: &[f32], _: &cpal::InputCallbackInfo| {
if let Some(new_processor) = processors.take() {
current_processor = new_processor;
}
info!("recieved {} samples", frame.len());
current_processor.process(frame, &mut output_buffer);
if output_buffer.len() < PACKET_SAMPLES as usize {
return;
}
let remainder = output_buffer.split_off(PACKET_SAMPLES as usize);
let frame = replace(&mut output_buffer, remainder);
match encoder.encode_vec_float(&frame, frame.len() * 4) {
Ok(buf) => {
info!("encoded {} samples to {} bytes", frame.len(), buf.len());
each(buf);
}
Err(e) => {
error!("error encoding {} samples: {e:?}", frame.len());
}
}
};
match self.input.build_input_stream(
&cpal::StreamConfig {
channels: 1,
sample_rate: cpal::SampleRate(SAMPLE_RATE),
buffer_size: cpal::BufferSize::Fixed(PACKET_SAMPLES),
},
data_callback,
error_callback,
None,
) {
Ok(stream) => {
self.recording_stream = Some(stream);
Ok(())
}
Err(err) => {
self.recording_stream = None;
Err(err.into())
}
}
}
pub fn create_player(&mut self) -> Result<AudioPlayer, Error> {
@@ -69,13 +126,13 @@ impl AudioSystem {
2400 // 50ms of buffer
],
)));
let decoder = opus::Decoder::new(48_000, opus::Channels::Mono)?;
let decoder = opus::Decoder::new(SAMPLE_RATE, opus::Channels::Mono)?;
let stream = {
let buffer = buffer.clone();
self.output.build_output_stream(
&cpal::StreamConfig {
channels: 1,
sample_rate: cpal::SampleRate(48_000),
sample_rate: cpal::SampleRate(SAMPLE_RATE),
buffer_size: cpal::BufferSize::Fixed(480), // 10ms playback delay
},
move |frame, info| {
@@ -234,9 +291,12 @@ pub fn load_username() -> Option<String> {
}
pub async fn load_config() -> color_eyre::Result<ClientConfig> {
color_eyre::eyre::bail!(
"there is no config on desktop because desktops cannot be configured as they are tables"
)
Ok(ClientConfig {
proxy_url: None,
status_url: None,
cert_hash: None,
any_server: true,
})
}
pub fn init_logging() {
+4 -6
View File
@@ -82,11 +82,8 @@ impl AudioSystem {
// Create MediaStreams to playback decoded audio
// The audio context is used to reproduce audio.
let webctx = configure_audio_context();
let processor = AudioProcessorSender::default();
Ok(AudioSystem {
webctx,
processors: processor,
})
let processors = AudioProcessorSender::default();
Ok(AudioSystem { webctx, processors })
}
pub fn set_processor(&self, processor: AudioProcessor) {
@@ -210,7 +207,8 @@ fn process_audio(frame: &JsValue, processor: &mut AudioProcessor) {
return;
};
let input = samples.to_vec();
let output = processor.process(&input);
let mut output = Vec::with_capacity(input.len());
processor.process(&input, &mut output);
samples.copy_from(&output);
}