trx/src/tx/main.rs
2022-07-23 20:43:35 -05:00

85 lines
2.6 KiB
Rust

use tokio::io::{self, AsyncReadExt,AsyncWriteExt};
use tokio::net::TcpListener;
use std::str;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{BufferSize::Fixed,StreamConfig};
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
//init the host and device
let host = cpal::default_host();
let device = host.default_output_device().expect("No output device available");
// let in_device = host.default_input_device().expect("balls");
//println!("{:?}", host.output_devices().unwrap().next().unwrap().name());
for dev in host.output_devices().unwrap() {
println!(" {:?}", dev.name());
}
println!("{:?}",device.name());
/* println!("\n");
for dev in host.input_devices().unwrap() {
print!(" {:?}", dev.name());
} */
//output
let mut supported_output_configs_range = device.supported_output_configs()
.expect("error while querying configs");
let supported_output_config = supported_output_configs_range.next()
.expect("no supported config?!")
.with_max_sample_rate();
println!("{:?}", supported_output_config);
/* //input
let mut supported_input_configs_range = in_device.supported_input_configs()
.expect("error querying input");
let mut supported_input_config = supported_input_configs_range.next()
.expect("input no supported config :(")
.with_max_sample_rate();
println!("{:?}",supported_input_config); */
/*SupportedStreamConfig {
channels: 1,
sample_rate: SampleRate(384000),
buffer_size: Range { min: 3, max: 4194304 },
sample_format: I16 }
*/
//setting config due to default configs
let config = StreamConfig {
channels: 1,
sample_rate: cpal::SampleRate(441000),
buffer_size: Fixed(16),
};
//init stream
let stream_out = device.build_output_stream(
&config,
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
// react to stream events and read or write stream data here.
//println!("{:?}",data);
},
move |_err| {
// react to errors here.
panic!("shit fucked!");
},
).unwrap();
stream_out.play();
//temp bc buffer size will be set in cpal
let mut buffer = [0; 16];
loop {
let (mut socket, addr) = listener.accept().await?;
let msg = socket.read(&mut buffer[..]).await?;
println!("{}: {:?}",addr,str::from_utf8(&buffer[..msg]));
socket.write_all(b"pong").await?;
}
}