fix: 🐛 sound glitch & delay
feat: ✨ buffering
This commit is contained in:
parent
78a1d52976
commit
eabe8e0521
4 changed files with 53 additions and 21 deletions
|
@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub mod routing;
|
pub mod routing;
|
||||||
pub mod streaming;
|
pub mod streaming;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct AppState{
|
pub struct AppState{
|
||||||
|
|
|
@ -1,41 +1,60 @@
|
||||||
use std::{mem::MaybeUninit, sync::Arc};
|
use std::{mem::MaybeUninit, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||||
use ringbuf::{Consumer, HeapRb, Producer, SharedRb};
|
use ringbuf::{Consumer, HeapRb, Producer, SharedRb};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::{net::{TcpListener, TcpStream}, time::Instant};
|
||||||
use futures_util::SinkExt;
|
use futures_util::SinkExt;
|
||||||
use tokio_tungstenite::WebSocketStream;
|
use tokio_tungstenite::WebSocketStream;
|
||||||
|
|
||||||
|
|
||||||
pub async fn start() {
|
pub async fn start() {
|
||||||
let socket = TcpListener::bind("127.0.0.1:2424").await.unwrap();
|
let socket = TcpListener::bind("192.168.1.2:2424").await.unwrap();
|
||||||
while let Ok((tcp_stream, _)) = socket.accept().await {
|
while let Ok((tcp_stream, _)) = socket.accept().await {
|
||||||
println!("Dude Someone Triggered");
|
println!("Dude Someone Triggered");
|
||||||
let ring = HeapRb::<f32>::new(1000000);
|
let ring = HeapRb::<f32>::new(1000000);
|
||||||
let (producer, consumer) = ring.split();
|
let (producer, consumer) = ring.split();
|
||||||
let ws_stream = tokio_tungstenite::accept_async(tcp_stream).await.unwrap();
|
let ws_stream = tokio_tungstenite::accept_async(tcp_stream).await.unwrap();
|
||||||
|
|
||||||
|
let timer = Instant::now();
|
||||||
|
|
||||||
|
|
||||||
tokio::spawn(record(producer));
|
tokio::spawn(record(producer));
|
||||||
std::thread::sleep(std::time::Duration::from_secs(3));
|
tokio::spawn(stream(timer, ws_stream, consumer));
|
||||||
tokio::spawn(stream(ws_stream, consumer));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stream(mut ws_stream:WebSocketStream<TcpStream>, mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
pub async fn stream(timer:Instant, mut ws_stream:WebSocketStream<TcpStream>, mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
||||||
println!("Waiting");
|
println!("Waiting");
|
||||||
loop {
|
loop {
|
||||||
if !consumer.is_empty() {
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
|
let mut data:Vec<u8> = Vec::new();
|
||||||
|
let now = timer.elapsed().as_secs();
|
||||||
|
while !consumer.is_empty() && (timer.elapsed().as_secs()+2) > now{
|
||||||
match consumer.pop() {
|
match consumer.pop() {
|
||||||
Some(data) => {
|
Some(single_data) => {
|
||||||
ws_stream.send(data.to_string().into()).await.unwrap();
|
let ring = HeapRb::<u8>::new(1000000);
|
||||||
|
let (mut producer, mut consumer) = ring.split();
|
||||||
|
let single_data_packet = single_data.to_string().as_bytes().to_vec();
|
||||||
|
let terminator = "#".as_bytes().to_vec();
|
||||||
|
for element in single_data_packet {
|
||||||
|
producer.push(element).unwrap();
|
||||||
|
}
|
||||||
|
for element in terminator {
|
||||||
|
producer.push(element).unwrap();
|
||||||
|
}
|
||||||
|
while !consumer.is_empty() {
|
||||||
|
data.push(consumer.pop().unwrap());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
//ws_stream.send(0.0.to_string().into()).await.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
ws_stream.send(data.into()).await.unwrap();
|
||||||
ws_stream.flush().await.unwrap();
|
ws_stream.flush().await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn record(mut producer: Producer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
pub async fn record(mut producer: Producer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
0
back/src/utils.rs
Normal file
0
back/src/utils.rs
Normal file
|
@ -9,7 +9,7 @@ use tokio_with_wasm::tokio::time::sleep;
|
||||||
|
|
||||||
pub async fn start_listening() {
|
pub async fn start_listening() {
|
||||||
log::info!("Trying Sir");
|
log::info!("Trying Sir");
|
||||||
let connect_addr = "ws://127.0.0.1:2424";
|
let connect_addr = "ws://192.168.1.2:2424";
|
||||||
let stream = tokio_tungstenite_wasm::connect(connect_addr).await.unwrap();
|
let stream = tokio_tungstenite_wasm::connect(connect_addr).await.unwrap();
|
||||||
let ring = HeapRb::<f32>::new(1000000);
|
let ring = HeapRb::<f32>::new(1000000);
|
||||||
let (producer, consumer) = ring.split();
|
let (producer, consumer) = ring.split();
|
||||||
|
@ -27,14 +27,26 @@ async fn sound_stream(
|
||||||
mut producer: Producer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>,
|
mut producer: Producer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>,
|
||||||
) {
|
) {
|
||||||
while let Some(msg) = stream.next().await {
|
while let Some(msg) = stream.next().await {
|
||||||
match msg.unwrap().to_string().parse::<f32>() {
|
let data = String::from_utf8(msg.unwrap().into()).unwrap();
|
||||||
Ok(sound_data) => match producer.push(sound_data) {
|
let data_parsed:Vec<&str> = data.split("#").collect();
|
||||||
Ok(_) => {}
|
//let mut sound_data:Vec<f32> = vec![];
|
||||||
Err(_) => {}
|
for element in data_parsed {
|
||||||
},
|
let single_data:f32 = match element.parse() {
|
||||||
Err(_) => {}
|
Ok(single) => single,
|
||||||
|
Err(_) => 0.0,
|
||||||
};
|
};
|
||||||
|
producer.push(single_data).unwrap();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// while let Some(msg) = stream.next().await {
|
||||||
|
// match msg.unwrap().to_string().parse::<f32>() {
|
||||||
|
// Ok(sound_data) => match producer.push(sound_data) {
|
||||||
|
// Ok(_) => {}
|
||||||
|
// Err(_) => {}
|
||||||
|
// },
|
||||||
|
// Err(_) => {}
|
||||||
|
// };
|
||||||
|
// }
|
||||||
log::info!("Connection Lost Sir");
|
log::info!("Connection Lost Sir");
|
||||||
}
|
}
|
||||||
async fn listen(mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
async fn listen(mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue