mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
add logic to read and write persistent volume
This commit is contained in:
parent
fed89882d5
commit
9cd9737613
2 changed files with 32 additions and 2 deletions
|
@ -196,7 +196,17 @@ fn calc_logarithmic_volume(volume: u16) -> u16 {
|
|||
val
|
||||
}
|
||||
|
||||
fn volume_to_mixer(volume: u16, linear_volume: bool) -> u16 {
|
||||
// write linear volume (0..100) to file
|
||||
fn write_volume(volume: u16, filepath: Option<String>) {
|
||||
let volume = volume as i32 * 100 / 0xFFFF;
|
||||
let mut file = File::create(filepath.unwrap()).expect("Could not create persistent volume");
|
||||
file.write_all(volume.to_string().as_bytes()).expect("Could not write persistent volume");
|
||||
}
|
||||
|
||||
fn volume_to_mixer(volume: u16, linear_volume: bool, persist_volume: Option<String>) -> u16 {
|
||||
if persist_volume.is_some() {
|
||||
write_volume(volume, persist_volume);
|
||||
}
|
||||
if linear_volume {
|
||||
debug!("linear volume: {}", volume);
|
||||
volume
|
||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -87,6 +87,19 @@ fn list_backends() {
|
|||
}
|
||||
}
|
||||
|
||||
// read linear volume (0..100) from file
|
||||
// convert to 0..0xFFFF scale
|
||||
fn read_volume(filepath: String) -> i32 {
|
||||
let mut data = String::new();
|
||||
let mut file = File::open(filepath).expect("Could not open persistent volume");
|
||||
file.read_to_string(&mut data).expect("Could not read persistent volume");
|
||||
let volume = data.trim().parse::<i32>().expect("Persistent volume must be an integer");
|
||||
if volume < 0 || volume > 100 {
|
||||
panic!("Initial volume must be in the range 0-100");
|
||||
}
|
||||
volume * 0xFFFF / 100
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Setup {
|
||||
backend: fn(Option<String>) -> Box<Sink>,
|
||||
|
@ -211,7 +224,7 @@ fn setup(args: &[String]) -> Setup {
|
|||
let mixer_name = matches.opt_str("mixer");
|
||||
let mixer = mixer::find(mixer_name.as_ref()).expect("Invalid mixer");
|
||||
|
||||
let initial_volume = matches
|
||||
let mut initial_volume = matches
|
||||
.opt_str("initial-volume")
|
||||
.map(|volume| {
|
||||
let volume = volume.parse::<i32>().unwrap();
|
||||
|
@ -222,6 +235,13 @@ fn setup(args: &[String]) -> Setup {
|
|||
})
|
||||
.unwrap_or(0x8000);
|
||||
|
||||
// check if user has requested persistent volume
|
||||
// if the file exists, override initial volume
|
||||
let persist_volume = matches.opt_str("persist-volume");
|
||||
if persist_volume.is_some() && Path::new(&persist_volume.clone().unwrap()).exists() {
|
||||
initial_volume = read_volume(persist_volume.clone().unwrap());
|
||||
}
|
||||
|
||||
let zeroconf_port = matches
|
||||
.opt_str("zeroconf-port")
|
||||
.map(|port| port.parse::<u16>().unwrap())
|
||||
|
|
Loading…
Reference in a new issue