43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
use tokio::net::TcpStream;
|
|
use tokio::io::{AsyncWriteExt};
|
|
use std::error::Error;
|
|
|
|
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() -> Result<(), Box<dyn Error>> {
|
|
// Connect to a peer
|
|
let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
|
|
let mut buffer = [0u8, 1024];
|
|
// Write some data.
|
|
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 msg = stream.read(&mut buffer[..]).await?;
|
|
println!("{:?}", str::from_utf8(&buffer[..msg]));
|
|
io.write_all(&buffer[..]).unwrap()
|
|
}
|
|
Ok(())
|
|
}
|