mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
SoftMixer: Change volume to AtomicUsize and pass AtomicUsize to SoftVolumeApplier
This commit is contained in:
parent
134239d298
commit
2de5d10a2f
1 changed files with 9 additions and 10 deletions
|
@ -1,5 +1,6 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
use spirc::UpdateMessageSender;
|
use spirc::UpdateMessageSender;
|
||||||
use spirc::UpdateMessage;
|
use spirc::UpdateMessage;
|
||||||
|
@ -8,14 +9,14 @@ use super::Mixer;
|
||||||
use super::AudioFilter;
|
use super::AudioFilter;
|
||||||
|
|
||||||
pub struct SoftMixer {
|
pub struct SoftMixer {
|
||||||
volume: Arc<RwLock<u16>>,
|
volume: Arc<AtomicUsize>,
|
||||||
tx: Option<UpdateMessageSender>
|
tx: Option<UpdateMessageSender>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SoftMixer {
|
impl SoftMixer {
|
||||||
pub fn new() -> SoftMixer {
|
pub fn new() -> SoftMixer {
|
||||||
SoftMixer {
|
SoftMixer {
|
||||||
volume: Arc::new(RwLock::new(0xFFFF)),
|
volume: Arc::new(AtomicUsize::new(0xFFFF)),
|
||||||
tx: None
|
tx: None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,27 +31,25 @@ impl Mixer for SoftMixer {
|
||||||
fn stop(&self) {
|
fn stop(&self) {
|
||||||
}
|
}
|
||||||
fn volume(&self) -> u16 {
|
fn volume(&self) -> u16 {
|
||||||
*self.volume.read().unwrap()
|
self.volume.load(Ordering::Relaxed) as u16
|
||||||
}
|
}
|
||||||
fn set_volume(&self, volume: u16) {
|
fn set_volume(&self, volume: u16) {
|
||||||
*self.volume.write().unwrap() = volume;
|
self.volume.store(volume as usize, Ordering::Relaxed);
|
||||||
let tx = self.tx.as_ref().expect("SoftMixer not initialized");
|
let tx = self.tx.as_ref().expect("SoftMixer not initialized");
|
||||||
tx.send(UpdateMessage).unwrap();
|
tx.send(UpdateMessage).unwrap();
|
||||||
}
|
}
|
||||||
fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
|
fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
|
||||||
let vol = self.volume.clone();
|
Some(Box::new(SoftVolumeApplier { volume: self.volume.clone() }))
|
||||||
let get_volume = Box::new(move || *vol.read().unwrap());
|
|
||||||
Some(Box::new(SoftVolumeApplier { get_volume: get_volume }))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SoftVolumeApplier {
|
struct SoftVolumeApplier {
|
||||||
get_volume: Box<Fn() -> u16 + Send>
|
volume: Arc<AtomicUsize>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AudioFilter for SoftVolumeApplier {
|
impl AudioFilter for SoftVolumeApplier {
|
||||||
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
fn modify_stream<'a>(&self, data: &'a [i16]) -> Cow<'a, [i16]> {
|
||||||
let volume = (self.get_volume)();
|
let volume = self.volume.load(Ordering::Relaxed) as u16;
|
||||||
if volume == 0xFFFF {
|
if volume == 0xFFFF {
|
||||||
Cow::Borrowed(data)
|
Cow::Borrowed(data)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue