monolib + monoclient: Volume modification test

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2024-05-20 07:48:32 +03:00
parent 39cc35e16d
commit c24d6d0175
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
2 changed files with 36 additions and 3 deletions

View file

@ -1,10 +1,11 @@
use clap::Parser; use clap::Parser;
use crossterm::cursor::MoveToColumn; use crossterm::cursor::MoveToColumn;
use crossterm::event::{poll, read, Event};
use crossterm::style::Print; use crossterm::style::Print;
use crossterm::terminal::{Clear, ClearType}; use crossterm::terminal::{Clear, ClearType};
use std::io::{stdout, IsTerminal}; use std::io::{stdout, IsTerminal};
use std::path::PathBuf; use std::path::PathBuf;
use std::time::{Duration, Instant}; use std::time::Instant;
#[derive(Parser)] #[derive(Parser)]
struct Args { struct Args {
@ -46,7 +47,25 @@ fn main() {
.unwrap(); .unwrap();
let mut track_length = md.track_length_secs as f64 + md.track_length_frac as f64; let mut track_length = md.track_length_secs as f64 + md.track_length_frac as f64;
let mut next_md = md.clone(); let mut next_md = md.clone();
crossterm::terminal::enable_raw_mode().unwrap();
loop { loop {
if let Ok(true) = poll(std::time::Duration::from_micros(1)) {
if let Event::Key(event) = read().unwrap() {
match event.code {
crossterm::event::KeyCode::Up => {
monolib::set_volume(monolib::get_volume().saturating_add(16));
}
crossterm::event::KeyCode::Down => {
monolib::set_volume(monolib::get_volume().saturating_sub(16));
}
crossterm::event::KeyCode::Char('q') => {
crossterm::terminal::disable_raw_mode().unwrap();
std::process::exit(0)
}
_ => {}
}
}
}
if monolib::get_metadata().unwrap() != md if monolib::get_metadata().unwrap() != md
&& track_length <= (Instant::now() - track_start).as_secs_f64() && track_length <= (Instant::now() - track_start).as_secs_f64()
{ {
@ -84,6 +103,6 @@ fn main() {
) )
.unwrap(); .unwrap();
} }
std::thread::sleep(Duration::from_secs_f32(0.25)) //std::thread::sleep(Duration::from_secs_f32(0.25))
} }
} }

View file

@ -24,12 +24,14 @@ use rodio::buffer::SamplesBuffer;
use rodio::{OutputStream, Sink}; use rodio::{OutputStream, Sink};
use std::io::BufReader; use std::io::BufReader;
use std::net::TcpStream; use std::net::TcpStream;
use std::sync::atomic::AtomicU8;
use std::sync::RwLock; use std::sync::RwLock;
use std::time::Instant; use std::time::Instant;
const CACHE_SIZE: usize = 32; const CACHE_SIZE: usize = 128;
static SINK: RwLock<Option<Sink>> = RwLock::new(None); static SINK: RwLock<Option<Sink>> = RwLock::new(None);
static VOLUME: AtomicU8 = AtomicU8::new(255);
static MD: RwLock<Option<TrackMetadata>> = RwLock::new(None); static MD: RwLock<Option<TrackMetadata>> = RwLock::new(None);
static STATE: RwLock<State> = RwLock::new(State::NotStarted); static STATE: RwLock<State> = RwLock::new(State::NotStarted);
@ -118,6 +120,18 @@ fn watching_sleep(dur: f32) -> bool {
false false
} }
pub fn get_volume() -> u8 {
VOLUME.load(std::sync::atomic::Ordering::Acquire)
}
pub fn set_volume(volume: u8) {
let sink = SINK.read().unwrap();
if let Some(sink) = sink.as_ref() {
sink.set_volume(get_volume() as f32 / 255.0)
}
VOLUME.store(volume, std::sync::atomic::Ordering::Relaxed)
}
/// Download track as samples /// Download track as samples
pub fn get_track(server: &str, xor_key: Option<Vec<u8>>) -> Option<(TrackMetadata, Vec<i16>)> { pub fn get_track(server: &str, xor_key: Option<Vec<u8>>) -> Option<(TrackMetadata, Vec<i16>)> {
let mut stream = BufReader::new(match xor_key { let mut stream = BufReader::new(match xor_key {