trx/src/tx/main.rs
2022-08-21 06:57:48 -05:00

61 lines
1.8 KiB
Rust

use tokio::io::{self, AsyncReadExt,AsyncWriteExt};
use tokio::net::TcpListener;
use alsa::{Direction, ValueOr};
use alsa::pcm::{PCM, HwParams, Format, Access, State,};
use std::str;
use std::ffi::CStr;
use std::io::Read;
#[tokio::main]
async fn main() -> io::Result<()> {
let listener: TcpListener = TcpListener::bind("127.0.0.1:8080").await?;
/*SupportedStreamConfig {
channels: 1,
sample_rate: SampleRate(384000),
buffer_size: Range { min: 3, max: 4194304 },
sample_format: I16 }
THIS IS NOT ENTIRELY CORRECT
*/
//init device config
/* let device_config = Spec {
format: Format::S16NE,
channels: 2,
rate: 44100,
}; */
//temp bc buffer size will be set in cpal
let mut buffer = [0u8; 1024];
// Open default playback device
let pcm = PCM::new("default", Direction::Playback, false).unwrap();
// Set hardware parameters: 44100 Hz / Mono / 16 bit
let hwp = HwParams::any(&pcm).unwrap();
hwp.set_channels(1).unwrap();
hwp.set_rate(44100, ValueOr::Nearest).unwrap();
hwp.set_format(Format::s16()).unwrap();
hwp.set_access(Access::RWInterleaved).unwrap();
pcm.hw_params(&hwp).unwrap();
let mut io = pcm.io_i16().unwrap();
// Make sure we don't start the stream too early
let hwp = pcm.hw_params_current().unwrap();
let swp = pcm.sw_params_current().unwrap();
swp.set_start_threshold(hwp.get_buffer_size().unwrap()).unwrap();
pcm.sw_params(&swp).unwrap();
loop {
let (mut socket, addr) = listener.accept().await?;
io.read(&mut buffer).unwrap();
let msg = socket.read(&mut buffer[..]).await?;
println!("{}: {:?}",addr,str::from_utf8(&buffer[..msg]));
socket.write_all(&buffer[..msg]).await?;
}
}