Disgusting audio code
This commit is contained in:
109
src/main.rs
109
src/main.rs
@ -12,6 +12,7 @@ mod ted;
|
||||
mod units;
|
||||
mod windows;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use egui_plot::{Legend, Line, Plot};
|
||||
use hound::WavWriter;
|
||||
use rand::{Rng, rand_core::le, seq::index::sample};
|
||||
@ -24,6 +25,7 @@ use std::{
|
||||
ops::DerefMut,
|
||||
sync::{Arc, mpsc::RecvTimeoutError},
|
||||
time::Duration,
|
||||
u64,
|
||||
};
|
||||
use tokio::{
|
||||
io::{self, AsyncReadExt, AsyncWriteExt},
|
||||
@ -482,64 +484,69 @@ impl EguiApp {
|
||||
// .await
|
||||
// .unwrap();
|
||||
|
||||
let mut stream;
|
||||
|
||||
if instance_id == 0 {
|
||||
let socket = TcpSocket::new_v4().unwrap();
|
||||
socket.bind("0.0.0.0:9000".parse().unwrap()).unwrap();
|
||||
stream = socket.listen(1).unwrap().accept().await.unwrap().0;
|
||||
} else {
|
||||
stream = TcpStream::connect("127.0.0.1:9000").await.unwrap();
|
||||
}
|
||||
|
||||
println!("Connected");
|
||||
|
||||
let (mut sock_recv, mut sock_send) = io::split(stream);
|
||||
// Receiving end
|
||||
tokio::spawn(async move {
|
||||
let mut buf = [0u8; 65536];
|
||||
while let Ok(size) = sock_recv.read(&mut buf).await {
|
||||
assert!(buf.len() % 4 == 0);
|
||||
for x in buf.chunks(4).take(size / 4) {
|
||||
let _ = down_sender.try_send(f32::from_ne_bytes(x.try_into().unwrap()));
|
||||
}
|
||||
}
|
||||
});
|
||||
let host = cpal::default_host();
|
||||
|
||||
// Sending end
|
||||
let mut sending = false;
|
||||
let mut wait_countdown = 0;
|
||||
loop {
|
||||
// Up link
|
||||
//let mut sample = 0.;
|
||||
let noise = rand::random::<f32>() * 0.1;
|
||||
if let Ok(x) = up_receiver.try_recv() {
|
||||
match x {
|
||||
SampleSenderCommand::Open => {
|
||||
sending = true;
|
||||
let device = host.default_input_device().expect("No input device");
|
||||
let mut config = device
|
||||
.supported_input_configs()
|
||||
.unwrap()
|
||||
.next()
|
||||
.unwrap()
|
||||
.with_sample_rate(cpal::SampleRate(48000));
|
||||
|
||||
let stream = device
|
||||
.build_input_stream(
|
||||
&config.into(),
|
||||
move |data: &[f32], _| {
|
||||
for x in data.iter() {
|
||||
let _ = down_sender.try_send(*x * 30.); // non-blocking send
|
||||
}
|
||||
SampleSenderCommand::Close => {
|
||||
sending = false;
|
||||
}
|
||||
SampleSenderCommand::Sample(x) => {
|
||||
if sending {
|
||||
let _ = sock_send.write(&(x + noise).to_ne_bytes()).await;
|
||||
wait_countdown += 1;
|
||||
},
|
||||
move |err| eprintln!("Stream error: {}", err),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
stream.play().unwrap();
|
||||
|
||||
let device = host.default_output_device().unwrap();
|
||||
let mut supported_configs_range = device.supported_output_configs().unwrap();
|
||||
let supported_config = supported_configs_range
|
||||
.find(|config| {
|
||||
config.sample_format() == cpal::SampleFormat::F32
|
||||
&& config.min_sample_rate().0 <= 48000
|
||||
&& config.max_sample_rate().0 >= 48000
|
||||
})
|
||||
.expect("Device does not support 48kHz f32 output");
|
||||
let config = supported_config
|
||||
.with_sample_rate(cpal::SampleRate(48_000))
|
||||
.config();
|
||||
|
||||
let send_stream = device
|
||||
.build_output_stream(
|
||||
&config,
|
||||
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||
for d in data.iter_mut() {
|
||||
loop {
|
||||
if let Ok(SampleSenderCommand::Sample(smpl)) =
|
||||
up_receiver.try_recv()
|
||||
{
|
||||
*d = smpl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
move |err| {
|
||||
eprintln!("Stream error: {}", err);
|
||||
},
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
if !sending {
|
||||
let _ = sock_send.write(&noise.to_ne_bytes()).await;
|
||||
wait_countdown += 1;
|
||||
}
|
||||
send_stream.play().unwrap();
|
||||
|
||||
if wait_countdown >= 480 {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
wait_countdown = 0;
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(Duration::from_secs(u64::MAX)).await;
|
||||
});
|
||||
|
||||
EguiApp {
|
||||
|
||||
Reference in New Issue
Block a user