1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-06 03:50:06 +02:00

Various code improvements (#777)

* Remove deprecated use of std::u16::MAX
* Use `FromStr` for fallible `&str` conversions
* DRY up strings into constants
* Change `as_ref().map()` into `as_deref()`
* Use `Duration` for time constants and functions
* Optimize `Vec` with response times
* Move comments for `rustdoc` to parse
This commit is contained in:
Roderick van Domburg 2021-05-31 22:32:39 +02:00 committed by GitHub
parent bae1834988
commit ad19b69bfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 433 additions and 309 deletions

View file

@ -2,7 +2,7 @@ use super::{Open, Sink, SinkAsBytes};
use crate::config::AudioFormat;
use crate::convert::Converter;
use crate::decoder::AudioPacket;
use crate::player::{NUM_CHANNELS, SAMPLES_PER_SECOND, SAMPLE_RATE};
use crate::{NUM_CHANNELS, SAMPLES_PER_SECOND, SAMPLE_RATE};
use alsa::device_name::HintIter;
use alsa::pcm::{Access, Format, Frames, HwParams, PCM};
use alsa::{Direction, Error, ValueOr};
@ -10,8 +10,9 @@ use std::cmp::min;
use std::ffi::CString;
use std::io;
use std::process::exit;
use std::time::Duration;
const BUFFERED_LATENCY: f32 = 0.125; // seconds
const BUFFERED_LATENCY: Duration = Duration::from_millis(125);
const BUFFERED_PERIODS: Frames = 4;
pub struct AlsaSink {
@ -57,7 +58,8 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, Frames), Box
// latency = period_size * periods / (rate * bytes_per_frame)
// For stereo samples encoded as 32-bit float, one frame has a length of eight bytes.
let mut period_size = ((SAMPLES_PER_SECOND * format.size() as u32) as f32
* (BUFFERED_LATENCY / BUFFERED_PERIODS as f32)) as Frames;
* (BUFFERED_LATENCY.as_secs_f32() / BUFFERED_PERIODS as f32))
as Frames;
{
let hwp = HwParams::any(&pcm)?;
hwp.set_access(Access::RWInterleaved)?;
@ -80,7 +82,7 @@ impl Open for AlsaSink {
fn open(device: Option<String>, format: AudioFormat) -> Self {
info!("Using Alsa sink with format: {:?}", format);
let name = match device.as_ref().map(AsRef::as_ref) {
let name = match device.as_deref() {
Some("?") => {
println!("Listing available Alsa outputs:");
list_outputs();
@ -162,6 +164,8 @@ impl SinkAsBytes for AlsaSink {
}
impl AlsaSink {
pub const NAME: &'static str = "alsa";
fn write_buf(&mut self) {
let pcm = self.pcm.as_mut().unwrap();
let io = pcm.io_bytes();