simple not working tui for device select
This commit is contained in:
Generated
+1245
-19
File diff suppressed because it is too large
Load Diff
@@ -13,3 +13,6 @@ tokio = { version = "1.53.1", features = ["full"] }
|
|||||||
rand = "0.10.2"
|
rand = "0.10.2"
|
||||||
clap = { version = "4.6.6", features = ["derive"] }
|
clap = { version = "4.6.6", features = ["derive"] }
|
||||||
ropus = "0.12.18"
|
ropus = "0.12.18"
|
||||||
|
ratatui = "0.30.2"
|
||||||
|
crossterm = "0.29.0"
|
||||||
|
gag = "1.0.0"
|
||||||
|
|||||||
+20
-24
@@ -1,5 +1,5 @@
|
|||||||
use cpal::{
|
use cpal::{
|
||||||
Device,
|
Device, StreamConfig,
|
||||||
traits::{DeviceTrait, StreamTrait},
|
traits::{DeviceTrait, StreamTrait},
|
||||||
};
|
};
|
||||||
use ringbuf::{
|
use ringbuf::{
|
||||||
@@ -21,17 +21,13 @@ use crate::models::{
|
|||||||
|
|
||||||
pub async fn audio_stream_receiver(
|
pub async fn audio_stream_receiver(
|
||||||
output_device: Device,
|
output_device: Device,
|
||||||
|
output_device_config: StreamConfig,
|
||||||
active_input_audio_streams_producers: InputAudioStreamsProducers,
|
active_input_audio_streams_producers: InputAudioStreamsProducers,
|
||||||
active_input_audio_streams_consumers: InputAudioStreamsConsumers,
|
active_input_audio_streams_consumers: InputAudioStreamsConsumers,
|
||||||
active_input_audio_streams_encoders: InputAudioStreamsEncoders,
|
active_input_audio_streams_encoders: InputAudioStreamsEncoders,
|
||||||
active_input_audio_streams_info: InputAudioStreamsInfo,
|
active_input_audio_streams_info: InputAudioStreamsInfo,
|
||||||
udp_socket: Arc<UdpSocket>,
|
udp_socket: Arc<UdpSocket>,
|
||||||
) {
|
) {
|
||||||
let speaker_config: cpal::StreamConfig = output_device
|
|
||||||
.default_output_config()
|
|
||||||
.expect("stream config init failed")
|
|
||||||
.into();
|
|
||||||
|
|
||||||
let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||||
let mut streams = active_input_audio_streams_consumers.lock().unwrap();
|
let mut streams = active_input_audio_streams_consumers.lock().unwrap();
|
||||||
for sample in data {
|
for sample in data {
|
||||||
@@ -50,7 +46,7 @@ pub async fn audio_stream_receiver(
|
|||||||
|
|
||||||
let err_fn = |err| eprintln!("stream error: {}", err);
|
let err_fn = |err| eprintln!("stream error: {}", err);
|
||||||
let output_stream = output_device
|
let output_stream = output_device
|
||||||
.build_output_stream(speaker_config, output_data_fn, err_fn, None)
|
.build_output_stream(output_device_config, output_data_fn, err_fn, None)
|
||||||
.expect("building output stream failed");
|
.expect("building output stream failed");
|
||||||
|
|
||||||
output_stream.play().expect("failed to play output stream");
|
output_stream.play().expect("failed to play output stream");
|
||||||
@@ -106,11 +102,11 @@ pub async fn audio_stream_receiver(
|
|||||||
let mut active_streams_map = active_input_audio_streams_producers.lock().unwrap();
|
let mut active_streams_map = active_input_audio_streams_producers.lock().unwrap();
|
||||||
|
|
||||||
if let Some(producer) = active_streams_map.get_mut(&(client_id, stream_id)) {
|
if let Some(producer) = active_streams_map.get_mut(&(client_id, stream_id)) {
|
||||||
if stream_info.channels == speaker_config.channels {
|
if stream_info.channels == output_device_config.channels {
|
||||||
producer.push_slice(
|
producer.push_slice(
|
||||||
&decoded_samples[..decoded_len * stream_info.channels as usize],
|
&decoded_samples[..decoded_len * stream_info.channels as usize],
|
||||||
);
|
);
|
||||||
} else if stream_info.channels > speaker_config.channels {
|
} else if stream_info.channels > output_device_config.channels {
|
||||||
for (i, frame) in decoded_samples
|
for (i, frame) in decoded_samples
|
||||||
[..decoded_len * stream_info.channels as usize]
|
[..decoded_len * stream_info.channels as usize]
|
||||||
.chunks_exact(2)
|
.chunks_exact(2)
|
||||||
@@ -125,7 +121,7 @@ pub async fn audio_stream_receiver(
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
for (i, frame) in decoded_samples_converted
|
for (i, frame) in decoded_samples_converted
|
||||||
[..decoded_len * speaker_config.channels as usize]
|
[..decoded_len * output_device_config.channels as usize]
|
||||||
.chunks_exact_mut(2)
|
.chunks_exact_mut(2)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
@@ -135,7 +131,7 @@ pub async fn audio_stream_receiver(
|
|||||||
|
|
||||||
producer.push_slice(
|
producer.push_slice(
|
||||||
&decoded_samples_converted
|
&decoded_samples_converted
|
||||||
[..decoded_len * speaker_config.channels as usize],
|
[..decoded_len * output_device_config.channels as usize],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,16 +143,12 @@ pub async fn audio_stream_receiver(
|
|||||||
|
|
||||||
pub async fn audio_stream_spawner(
|
pub async fn audio_stream_spawner(
|
||||||
input_device: Device,
|
input_device: Device,
|
||||||
|
input_device_config: StreamConfig,
|
||||||
global_event_tx: Sender<ClientEvent>,
|
global_event_tx: Sender<ClientEvent>,
|
||||||
mut global_event_rx: Receiver<ClientEvent>,
|
mut global_event_rx: Receiver<ClientEvent>,
|
||||||
udp_socket: Arc<UdpSocket>,
|
udp_socket: Arc<UdpSocket>,
|
||||||
clinet_info: Arc<RwLock<ClientInfo>>,
|
clinet_info: Arc<RwLock<ClientInfo>>,
|
||||||
) {
|
) {
|
||||||
let config: cpal::StreamConfig = input_device
|
|
||||||
.default_input_config()
|
|
||||||
.expect("input stream config init failed")
|
|
||||||
.into();
|
|
||||||
|
|
||||||
let current_stream_id: u8 = rand::random();
|
let current_stream_id: u8 = rand::random();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -168,8 +160,8 @@ pub async fn audio_stream_spawner(
|
|||||||
let _ = global_event_tx.send(ClientEvent::NewAudioStreamRequest(AudioStreamInfo {
|
let _ = global_event_tx.send(ClientEvent::NewAudioStreamRequest(AudioStreamInfo {
|
||||||
client_id: client_id,
|
client_id: client_id,
|
||||||
stream_id: current_stream_id,
|
stream_id: current_stream_id,
|
||||||
channels: config.channels,
|
channels: input_device_config.channels,
|
||||||
sample_rate: config.sample_rate,
|
sample_rate: input_device_config.sample_rate,
|
||||||
}));
|
}));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -190,8 +182,8 @@ pub async fn audio_stream_spawner(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ring_buffer_ms = 150.0;
|
let ring_buffer_ms = 150.0;
|
||||||
let ring_buffer_frames = (ring_buffer_ms / 1000.0) * config.sample_rate as f32;
|
let ring_buffer_frames = (ring_buffer_ms / 1000.0) * input_device_config.sample_rate as f32;
|
||||||
let ring_buffer_samples = ring_buffer_frames as usize * config.channels as usize;
|
let ring_buffer_samples = ring_buffer_frames as usize * input_device_config.channels as usize;
|
||||||
let ring_buffer_size = ring_buffer_samples;
|
let ring_buffer_size = ring_buffer_samples;
|
||||||
let ring_buffer = HeapRb::<f32>::new(ring_buffer_size);
|
let ring_buffer = HeapRb::<f32>::new(ring_buffer_size);
|
||||||
let (mut producer, mut consumer) = ring_buffer.split();
|
let (mut producer, mut consumer) = ring_buffer.split();
|
||||||
@@ -204,23 +196,27 @@ pub async fn audio_stream_spawner(
|
|||||||
let err_fn = |err| eprintln!("stream error: {}", err);
|
let err_fn = |err| eprintln!("stream error: {}", err);
|
||||||
|
|
||||||
let input_stream = input_device
|
let input_stream = input_device
|
||||||
.build_input_stream(config, input_data_fn, err_fn, None)
|
.build_input_stream(input_device_config, input_data_fn, err_fn, None)
|
||||||
.expect("building input stream failed");
|
.expect("building input stream failed");
|
||||||
|
|
||||||
input_stream.play().expect("failed to play input stream");
|
input_stream.play().expect("failed to play input stream");
|
||||||
|
|
||||||
let frames_needed = ((20.0 / 1000.0) * config.sample_rate as f32) as usize;
|
let frames_needed = ((20.0 / 1000.0) * input_device_config.sample_rate as f32) as usize;
|
||||||
let mut frame_buffer = vec![0.0; frames_needed];
|
let mut frame_buffer = vec![0.0; frames_needed];
|
||||||
|
|
||||||
let channels: ropus::Channels = {
|
let channels: ropus::Channels = {
|
||||||
if config.channels == 1 {
|
if input_device_config.channels == 1 {
|
||||||
ropus::Channels::Mono
|
ropus::Channels::Mono
|
||||||
} else {
|
} else {
|
||||||
ropus::Channels::Stereo
|
ropus::Channels::Stereo
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut opus_encoder = Encoder::builder(config.sample_rate, channels, ropus::Application::Voip)
|
let mut opus_encoder = Encoder::builder(
|
||||||
|
input_device_config.sample_rate,
|
||||||
|
channels,
|
||||||
|
ropus::Application::Voip,
|
||||||
|
)
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
+33
-9
@@ -1,9 +1,10 @@
|
|||||||
mod audio_streams;
|
mod audio_streams;
|
||||||
mod models;
|
mod models;
|
||||||
mod tcp_connection;
|
mod tcp_connection;
|
||||||
|
mod tui;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cpal::traits::HostTrait;
|
use cpal::traits::{DeviceTrait, HostTrait};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{Arc, Mutex, RwLock},
|
sync::{Arc, Mutex, RwLock},
|
||||||
@@ -18,18 +19,13 @@ use crate::{
|
|||||||
InputAudioStreamsInfo, InputAudioStreamsProducers,
|
InputAudioStreamsInfo, InputAudioStreamsProducers,
|
||||||
},
|
},
|
||||||
tcp_connection::tcp_connection,
|
tcp_connection::tcp_connection,
|
||||||
|
tui::Tui,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let cli = CliArgs::parse();
|
let cli = CliArgs::parse();
|
||||||
|
|
||||||
let client_info: Arc<RwLock<ClientInfo>> = Arc::new(RwLock::new(ClientInfo {
|
|
||||||
client_id: 0,
|
|
||||||
server_address: cli.server_ip.unwrap(),
|
|
||||||
connected: false,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let (global_event_tx, _) = broadcast::channel::<ClientEvent>(16);
|
let (global_event_tx, _) = broadcast::channel::<ClientEvent>(16);
|
||||||
|
|
||||||
let active_input_audio_streams_producers: InputAudioStreamsProducers =
|
let active_input_audio_streams_producers: InputAudioStreamsProducers =
|
||||||
@@ -43,6 +39,26 @@ async fn main() {
|
|||||||
let active_input_audio_streams_info: InputAudioStreamsInfo =
|
let active_input_audio_streams_info: InputAudioStreamsInfo =
|
||||||
Arc::new(RwLock::new(HashMap::new()));
|
Arc::new(RwLock::new(HashMap::new()));
|
||||||
|
|
||||||
|
let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await.unwrap());
|
||||||
|
|
||||||
|
if cli.connect_with_defaults == None {
|
||||||
|
ratatui::run(|terminal| {
|
||||||
|
Tui::new().run(
|
||||||
|
terminal,
|
||||||
|
active_input_audio_streams_producers.clone(),
|
||||||
|
active_input_audio_streams_consumers.clone(),
|
||||||
|
active_input_audio_streams_encoders.clone(),
|
||||||
|
active_input_audio_streams_info.clone(),
|
||||||
|
udp_socket.clone(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
let client_info: Arc<RwLock<ClientInfo>> = Arc::new(RwLock::new(ClientInfo {
|
||||||
|
client_id: 0,
|
||||||
|
server_address: cli.connect_with_defaults.unwrap(),
|
||||||
|
connected: false,
|
||||||
|
}));
|
||||||
|
|
||||||
tokio::spawn(tcp_connection(
|
tokio::spawn(tcp_connection(
|
||||||
client_info.clone(),
|
client_info.clone(),
|
||||||
global_event_tx.clone(),
|
global_event_tx.clone(),
|
||||||
@@ -55,22 +71,28 @@ async fn main() {
|
|||||||
|
|
||||||
let _ = global_event_tx.send(ClientEvent::NewConnection);
|
let _ = global_event_tx.send(ClientEvent::NewConnection);
|
||||||
|
|
||||||
let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:0").await.unwrap());
|
|
||||||
|
|
||||||
let host = cpal::default_host();
|
let host = cpal::default_host();
|
||||||
|
|
||||||
let input_device = host
|
let input_device = host
|
||||||
.default_input_device()
|
.default_input_device()
|
||||||
.expect("no input device available");
|
.expect("no input device available");
|
||||||
|
let input_device_config = input_device
|
||||||
|
.default_input_config()
|
||||||
|
.expect("failed to get default input config");
|
||||||
|
|
||||||
let output_device = host
|
let output_device = host
|
||||||
.default_output_device()
|
.default_output_device()
|
||||||
.expect("no output device available");
|
.expect("no output device available");
|
||||||
|
let output_device_config = output_device
|
||||||
|
.default_output_config()
|
||||||
|
.expect("failed to get default output config");
|
||||||
|
|
||||||
println!("input device: {}", input_device);
|
println!("input device: {}", input_device);
|
||||||
println!("output device: {}", output_device);
|
println!("output device: {}", output_device);
|
||||||
|
|
||||||
tokio::spawn(audio_stream_receiver(
|
tokio::spawn(audio_stream_receiver(
|
||||||
output_device,
|
output_device,
|
||||||
|
output_device_config.into(),
|
||||||
active_input_audio_streams_producers.clone(),
|
active_input_audio_streams_producers.clone(),
|
||||||
active_input_audio_streams_consumers.clone(),
|
active_input_audio_streams_consumers.clone(),
|
||||||
active_input_audio_streams_encoders.clone(),
|
active_input_audio_streams_encoders.clone(),
|
||||||
@@ -80,6 +102,7 @@ async fn main() {
|
|||||||
|
|
||||||
tokio::spawn(audio_stream_spawner(
|
tokio::spawn(audio_stream_spawner(
|
||||||
input_device,
|
input_device,
|
||||||
|
input_device_config.into(),
|
||||||
global_event_tx.clone(),
|
global_event_tx.clone(),
|
||||||
global_event_tx.subscribe(),
|
global_event_tx.subscribe(),
|
||||||
udp_socket.clone(),
|
udp_socket.clone(),
|
||||||
@@ -88,4 +111,5 @@ async fn main() {
|
|||||||
|
|
||||||
tokio::signal::ctrl_c().await.expect("failed to close");
|
tokio::signal::ctrl_c().await.expect("failed to close");
|
||||||
println!("shutting down");
|
println!("shutting down");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,14 @@ use std::{
|
|||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about)]
|
#[command(version, about)]
|
||||||
pub struct CliArgs {
|
pub struct CliArgs {
|
||||||
#[arg(short, long, required(true))]
|
#[arg(short, long, required(false))]
|
||||||
pub server_ip: Option<String>,
|
pub connect_with_defaults: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum TuiScreen {
|
||||||
|
DeviceSelect,
|
||||||
|
Exit,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|||||||
@@ -0,0 +1,273 @@
|
|||||||
|
use cpal::{
|
||||||
|
Device, SAMPLE_RATE_48K, StreamConfig,
|
||||||
|
traits::{DeviceTrait, HostTrait},
|
||||||
|
};
|
||||||
|
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||||
|
use gag::Gag;
|
||||||
|
use ratatui::{
|
||||||
|
DefaultTerminal, Frame,
|
||||||
|
layout::{Constraint, Direction, Layout},
|
||||||
|
style::Style,
|
||||||
|
widgets::{Block, Row, Table, TableState},
|
||||||
|
};
|
||||||
|
use std::{sync::Arc, vec};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
use crate::models::{
|
||||||
|
InputAudioStreamsConsumers, InputAudioStreamsEncoders, InputAudioStreamsInfo,
|
||||||
|
InputAudioStreamsProducers, TuiScreen,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Tui {
|
||||||
|
current_screen: TuiScreen,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tui {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
current_screen: TuiScreen::DeviceSelect,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
&mut self,
|
||||||
|
terminal: &mut DefaultTerminal,
|
||||||
|
active_input_audio_streams_producers: InputAudioStreamsProducers,
|
||||||
|
active_input_audio_streams_consumers: InputAudioStreamsConsumers,
|
||||||
|
active_input_audio_streams_encoders: InputAudioStreamsEncoders,
|
||||||
|
active_input_audio_streams_info: InputAudioStreamsInfo,
|
||||||
|
udp_socket: Arc<UdpSocket>,
|
||||||
|
) {
|
||||||
|
loop {
|
||||||
|
match self.current_screen {
|
||||||
|
TuiScreen::Exit => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
TuiScreen::DeviceSelect => {
|
||||||
|
self.current_screen = DeviceSelectScreen::new().run(terminal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct DeviceSelectScreen {
|
||||||
|
exit: bool,
|
||||||
|
input_device_state: TableState,
|
||||||
|
output_device_state: TableState,
|
||||||
|
input_devices: Vec<(Device, StreamConfig)>,
|
||||||
|
output_devices: Vec<(Device, StreamConfig)>,
|
||||||
|
selected_selection: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeviceSelectScreen {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let _print_gag = Gag::stderr().ok();
|
||||||
|
|
||||||
|
let host = cpal::default_host();
|
||||||
|
|
||||||
|
let mut input_devices: Vec<(Device, StreamConfig)> = Vec::new();
|
||||||
|
|
||||||
|
if let Ok(possible_input_devices) = host.input_devices() {
|
||||||
|
for possible_input_device in possible_input_devices {
|
||||||
|
if let Ok(supported_configs) = possible_input_device.supported_input_configs() {
|
||||||
|
for supported_config in supported_configs {
|
||||||
|
if let Some(input_config) =
|
||||||
|
supported_config.try_with_sample_rate(SAMPLE_RATE_48K)
|
||||||
|
{
|
||||||
|
input_devices.push((possible_input_device, input_config.into()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut output_devices: Vec<(Device, StreamConfig)> = Vec::new();
|
||||||
|
|
||||||
|
if let Ok(possible_output_devices) = host.output_devices() {
|
||||||
|
for possible_output_device in possible_output_devices {
|
||||||
|
if let Ok(supported_configs) = possible_output_device.supported_output_configs() {
|
||||||
|
for supported_config in supported_configs {
|
||||||
|
if let Some(output_config) =
|
||||||
|
supported_config.try_with_sample_rate(SAMPLE_RATE_48K)
|
||||||
|
{
|
||||||
|
output_devices.push((possible_output_device, output_config.into()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Self {
|
||||||
|
exit: false,
|
||||||
|
input_device_state: TableState::default(),
|
||||||
|
output_device_state: TableState::default(),
|
||||||
|
input_devices: input_devices,
|
||||||
|
output_devices: output_devices,
|
||||||
|
selected_selection: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> TuiScreen {
|
||||||
|
while !self.exit {
|
||||||
|
terminal
|
||||||
|
.draw(|frame| self.draw(frame))
|
||||||
|
.expect("failed to draw");
|
||||||
|
self.handle_events();
|
||||||
|
}
|
||||||
|
|
||||||
|
return TuiScreen::Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(&mut self, frame: &mut Frame) {
|
||||||
|
let outer_layout = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
|
.split(frame.area());
|
||||||
|
|
||||||
|
let block = Block::new();
|
||||||
|
let left_block = block.inner(outer_layout[0]);
|
||||||
|
let right_block = block.inner(outer_layout[1]);
|
||||||
|
|
||||||
|
let widths = [
|
||||||
|
Constraint::Percentage(100),
|
||||||
|
Constraint::Min(8),
|
||||||
|
Constraint::Min(11),
|
||||||
|
];
|
||||||
|
|
||||||
|
let mut input_device_rows: Vec<Row> = Vec::new();
|
||||||
|
for (device, config) in self.input_devices.iter() {
|
||||||
|
input_device_rows.push(Row::new(vec![
|
||||||
|
device.description().unwrap().name().to_string(),
|
||||||
|
config.channels.to_string(),
|
||||||
|
config.sample_rate.to_string(),
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
let input_device_table = Table::new(input_device_rows, widths)
|
||||||
|
.header(
|
||||||
|
Row::new(vec!["device name", "channels", "sample rate"])
|
||||||
|
.style(Style::new().bold())
|
||||||
|
.bottom_margin(1),
|
||||||
|
)
|
||||||
|
.block(
|
||||||
|
Block::bordered()
|
||||||
|
.border_style({
|
||||||
|
if self.selected_selection == 0 {
|
||||||
|
Style::new().bold()
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.border_type({
|
||||||
|
if self.selected_selection == 0 {
|
||||||
|
ratatui::widgets::BorderType::Thick
|
||||||
|
} else {
|
||||||
|
ratatui::widgets::BorderType::Plain
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.title("select input device"),
|
||||||
|
)
|
||||||
|
.row_highlight_style(Style::new().reversed());
|
||||||
|
|
||||||
|
frame.render_stateful_widget(input_device_table, left_block, &mut self.input_device_state);
|
||||||
|
|
||||||
|
let mut output_device_rows: Vec<Row> = Vec::new();
|
||||||
|
for (device, config) in self.output_devices.iter() {
|
||||||
|
output_device_rows.push(Row::new(vec![
|
||||||
|
device.description().unwrap().name().to_string(),
|
||||||
|
config.channels.to_string(),
|
||||||
|
config.sample_rate.to_string(),
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
let output_device_table = Table::new(output_device_rows, widths)
|
||||||
|
.header(
|
||||||
|
Row::new(vec!["device name", "channels", "sample rate"])
|
||||||
|
.style(Style::new().bold())
|
||||||
|
.bottom_margin(1),
|
||||||
|
)
|
||||||
|
.block(
|
||||||
|
Block::bordered()
|
||||||
|
.title("select output device")
|
||||||
|
.border_style({
|
||||||
|
if self.selected_selection == 1 {
|
||||||
|
Style::new().bold()
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.border_type({
|
||||||
|
if self.selected_selection == 1 {
|
||||||
|
ratatui::widgets::BorderType::Thick
|
||||||
|
} else {
|
||||||
|
ratatui::widgets::BorderType::Plain
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.row_highlight_style(Style::new().reversed());
|
||||||
|
|
||||||
|
frame.render_stateful_widget(
|
||||||
|
output_device_table,
|
||||||
|
right_block,
|
||||||
|
&mut self.output_device_state,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_events(&mut self) {
|
||||||
|
match event::read().expect("failed to read event") {
|
||||||
|
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
|
||||||
|
self.handle_key_events(key_event);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_key_events(&mut self, key_event: KeyEvent) {
|
||||||
|
match key_event.code {
|
||||||
|
KeyCode::Char('q') => self.exit(),
|
||||||
|
KeyCode::Down => self.increment_table(),
|
||||||
|
KeyCode::Up => self.decrement_table(),
|
||||||
|
KeyCode::Left => self.selected_selection = 0,
|
||||||
|
KeyCode::Right => self.selected_selection = 1,
|
||||||
|
// KeyCode::Down => self.increment_list(),
|
||||||
|
// KeyCode::Enter => self.get_value(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exit(&mut self) {
|
||||||
|
self.exit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment_table(&mut self) {
|
||||||
|
if self.selected_selection == 0 {
|
||||||
|
self.input_device_state.scroll_down_by(1);
|
||||||
|
} else {
|
||||||
|
self.output_device_state.scroll_down_by(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement_table(&mut self) {
|
||||||
|
if self.selected_selection == 0 {
|
||||||
|
self.input_device_state.scroll_up_by(1);
|
||||||
|
} else {
|
||||||
|
self.output_device_state.scroll_up_by(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn get_value(&mut self) {
|
||||||
|
// self.table_state.selected();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn increment_list(&mut self) {
|
||||||
|
// self.list_state.scroll_down_by(1);
|
||||||
|
// self.table_state.scroll_down_by(1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn decrement_list(&mut self) {
|
||||||
|
// self.list_state.scroll_up_by(1);
|
||||||
|
// self.table_state.scroll_up_by(1);
|
||||||
|
// }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user