feat: ✨ play && stop checks
This commit is contained in:
parent
eabe8e0521
commit
4b158d5195
8 changed files with 176 additions and 141 deletions
|
@ -1,9 +1,53 @@
|
|||
use crate::status::{
|
||||
coin_status_check, server_status_check, Coin, CoinStatus, Server, ServerStatus,
|
||||
use crate::{
|
||||
status::{coin_status_check, server_status_check, Coin, CoinStatus, Server, ServerStatus},
|
||||
streaming::start_listening,
|
||||
};
|
||||
use dioxus::prelude::*;
|
||||
use std::time::Duration;
|
||||
|
||||
#[component]
|
||||
pub fn listen_renderer() -> Element {
|
||||
let mut is_listening = use_signal(|| false);
|
||||
let is_maintaining = use_signal(|| (false, false));
|
||||
let call_start_listening = move |_| {
|
||||
if !is_listening() {
|
||||
if !is_maintaining().0 && !is_maintaining().1 {
|
||||
spawn({
|
||||
to_owned![is_listening];
|
||||
to_owned![is_maintaining];
|
||||
is_listening.set(true);
|
||||
async move {
|
||||
start_listening(is_maintaining, is_listening).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
is_listening.set(false);
|
||||
}
|
||||
};
|
||||
rsx! {
|
||||
div {
|
||||
button {
|
||||
disabled: !is_listening()&&(is_maintaining().0 || is_maintaining().1),
|
||||
onclick: call_start_listening,
|
||||
"style":"width: 100px; height: 100px;",
|
||||
if is_listening() {
|
||||
"Disconnect & Stop Listening"
|
||||
}
|
||||
else {
|
||||
if is_maintaining().0 || is_maintaining().1 {
|
||||
"Maintaining, Be Right Back Soon"
|
||||
}
|
||||
else {
|
||||
"Connect & Listen"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn server_status_renderer(server_address: String) -> Element {
|
||||
let server_check_time = 1_u64;
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
use dioxus::prelude::*;
|
||||
use front::{
|
||||
components::{coin_status_renderer, server_status_renderer},
|
||||
streaming::start_listening,
|
||||
};
|
||||
use front::components::{coin_status_renderer, listen_renderer, server_status_renderer};
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
@ -14,13 +11,7 @@ fn app() -> Element {
|
|||
let server_address = "https://tahinli.com.tr:2323".to_string();
|
||||
rsx! {
|
||||
page_base {}
|
||||
div {
|
||||
button {
|
||||
onclick: move |_| start_listening(),
|
||||
"style":"width: 80px; height: 50px;",
|
||||
"Listen"
|
||||
}
|
||||
}
|
||||
listen_renderer {}
|
||||
coin_status_renderer {server_address:server_address.clone()}
|
||||
server_status_renderer {server_address:server_address.clone()}
|
||||
}
|
||||
|
|
|
@ -1,61 +1,97 @@
|
|||
use std::{mem::MaybeUninit, sync::Arc, time::Duration};
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use dioxus::hooks::{use_coroutine, Coroutine};
|
||||
use dioxus::{
|
||||
prelude::spawn,
|
||||
signals::{Signal, Writable},
|
||||
};
|
||||
use futures_util::StreamExt;
|
||||
|
||||
use ringbuf::{Consumer, HeapRb, Producer, SharedRb};
|
||||
use tokio_tungstenite_wasm::WebSocketStream;
|
||||
use tokio_with_wasm::tokio::time::sleep;
|
||||
|
||||
pub async fn start_listening() {
|
||||
log::info!("Trying Sir");
|
||||
let connect_addr = "ws://192.168.1.2:2424";
|
||||
let stream = tokio_tungstenite_wasm::connect(connect_addr).await.unwrap();
|
||||
let ring = HeapRb::<f32>::new(1000000);
|
||||
let (producer, consumer) = ring.split();
|
||||
let _sound_stream: Coroutine<()> = use_coroutine(|_| async move {
|
||||
sound_stream(stream, producer).await;
|
||||
});
|
||||
tokio_with_wasm::tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
let _listen: Coroutine<()> = use_coroutine(|_| async move {
|
||||
listen(consumer).await;
|
||||
});
|
||||
static BUFFER_LIMIT: usize = 800000;
|
||||
static BUFFER_LENGTH: usize = 1000000;
|
||||
|
||||
pub async fn start_listening(
|
||||
mut is_maintaining: Signal<(bool, bool)>,
|
||||
mut is_listening: Signal<bool>,
|
||||
) {
|
||||
if is_listening() {
|
||||
log::info!("Trying Sir");
|
||||
let connect_addr = "ws://192.168.1.2:2424";
|
||||
let stream: WebSocketStream;
|
||||
match tokio_tungstenite_wasm::connect(connect_addr).await {
|
||||
Ok(ws_stream) => stream = ws_stream,
|
||||
Err(_) => {
|
||||
is_listening.set(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
is_maintaining.set((true, true));
|
||||
let ring = HeapRb::<f32>::new(BUFFER_LENGTH);
|
||||
let (producer, consumer) = ring.split();
|
||||
spawn({
|
||||
async move {
|
||||
sound_stream(is_listening, stream, producer).await;
|
||||
is_listening.set(false);
|
||||
is_maintaining.set((false, is_maintaining().1));
|
||||
}
|
||||
});
|
||||
spawn({
|
||||
async move {
|
||||
listen_podcast(is_listening, consumer).await;
|
||||
is_listening.set(false);
|
||||
//buffer time waiting actually
|
||||
tokio_with_wasm::tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
log::info!("{:#?}", is_maintaining());
|
||||
is_maintaining.set((is_maintaining().0, false));
|
||||
log::info!("pod{:#?}", is_maintaining());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn sound_stream(
|
||||
pub async fn sound_stream(
|
||||
is_listening: Signal<bool>,
|
||||
mut stream: WebSocketStream,
|
||||
mut producer: Producer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>,
|
||||
) {
|
||||
log::info!("Attention! We need cables");
|
||||
|
||||
while let Some(msg) = stream.next().await {
|
||||
let data = String::from_utf8(msg.unwrap().into()).unwrap();
|
||||
let data_parsed:Vec<&str> = data.split("#").collect();
|
||||
//let mut sound_data:Vec<f32> = vec![];
|
||||
for element in data_parsed {
|
||||
let single_data:f32 = match element.parse() {
|
||||
Ok(single) => single,
|
||||
Err(_) => 0.0,
|
||||
};
|
||||
producer.push(single_data).unwrap();
|
||||
if is_listening() {
|
||||
let data = String::from_utf8(msg.unwrap().into()).unwrap();
|
||||
let data_parsed: Vec<&str> = data.split("#").collect();
|
||||
for element in data_parsed {
|
||||
let single_data: f32 = match element.parse() {
|
||||
Ok(single) => single,
|
||||
Err(_) => 0.0,
|
||||
};
|
||||
if let Err(_) = producer.push(single_data) {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 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");
|
||||
}
|
||||
async fn listen(mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>) {
|
||||
log::info!("Hi");
|
||||
async fn listen_podcast(
|
||||
is_listening: Signal<bool>,
|
||||
mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>,
|
||||
) {
|
||||
log::info!("Attention! Show must start!");
|
||||
let host = cpal::default_host();
|
||||
let output_device = host.default_output_device().unwrap();
|
||||
let config: cpal::StreamConfig = output_device.default_output_config().unwrap().into();
|
||||
|
||||
let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||
if consumer.len() > BUFFER_LIMIT {
|
||||
consumer.clear();
|
||||
log::error!("Slow Consumer: DROPPED ALL Packets");
|
||||
}
|
||||
for sample in data {
|
||||
*sample = match consumer.pop() {
|
||||
Some(s) => s,
|
||||
|
@ -69,52 +105,13 @@ async fn listen(mut consumer: Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f3
|
|||
.unwrap();
|
||||
|
||||
output_stream.play().unwrap();
|
||||
sleep(Duration::from_secs(100)).await;
|
||||
|
||||
while is_listening() {
|
||||
tokio_with_wasm::tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
|
||||
output_stream.pause().unwrap();
|
||||
// let host = cpal::default_host();
|
||||
// let devices = host.devices().unwrap();
|
||||
// for (_derive_index, device) in devices.enumerate() {
|
||||
// log::info!("{:?}", device.name());
|
||||
// }
|
||||
// let device = host.default_output_device().unwrap();
|
||||
|
||||
// let mut supported_config = device.supported_output_configs().unwrap();
|
||||
// let config = supported_config.next().unwrap().with_max_sample_rate();
|
||||
// log::info!("{:?}", config);
|
||||
// match config.sample_format() {
|
||||
// cpal::SampleFormat::I8 => {log::info!("i8")},
|
||||
// cpal::SampleFormat::I16 => {log::info!("i16")},
|
||||
// //cpal::SampleFormat::I24 => {log::info!("i24")},
|
||||
// cpal::SampleFormat::I32 => {log::info!("i32")},
|
||||
// //cpal::SampleFormat::I48 => {log::info!("i48")},
|
||||
// cpal::SampleFormat::I64 => {log::info!("i64")},
|
||||
// cpal::SampleFormat::U8 => {log::info!("u8")},
|
||||
// cpal::SampleFormat::U16 => {log::info!("u16")},
|
||||
// //cpal::SampleFormat::U24 => {log::info!("u24")},
|
||||
// cpal::SampleFormat::U32 => {log::info!("u32")},
|
||||
// //cpal::SampleFormat::U48 => {log::info!("u48")},
|
||||
// cpal::SampleFormat::U64 => {log::info!("u64")},
|
||||
// cpal::SampleFormat::F32 => {log::info!("f32");
|
||||
// run::<f32>(consumer, &device, &config.clone().into()).await.unwrap();},
|
||||
// cpal::SampleFormat::F64 => {log::info!("f64")},
|
||||
// sample_format => panic!("Unsupported sample format '{sample_format}'"),
|
||||
// }
|
||||
// let config:StreamConfig = config.into();
|
||||
// let stream = device.build_output_stream(
|
||||
// &config,
|
||||
// move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||
|
||||
// log::info!("{:?}", data);
|
||||
// //I need to do something here, I think
|
||||
// },
|
||||
// move |_err| {
|
||||
|
||||
// },
|
||||
// None).unwrap();
|
||||
|
||||
// stream.play().unwrap();
|
||||
// tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
// stream.pause().unwrap();
|
||||
log::info!("Attention! Time to turn home!");
|
||||
}
|
||||
fn err_fn(err: cpal::StreamError) {
|
||||
eprintln!("Something Happened: {}", err);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue