Fix clippy warnings

This commit is contained in:
johannesd3 2021-04-10 14:06:41 +02:00
parent a576194b0e
commit b4f9ae31e2
12 changed files with 44 additions and 52 deletions

View file

@ -36,24 +36,21 @@ macro_rules! convert_samples_to {
}; };
} }
pub struct SamplesConverter {} pub fn to_s32(samples: &[f32]) -> Vec<i32> {
impl SamplesConverter { convert_samples_to!(i32, samples)
pub fn to_s32(samples: &[f32]) -> Vec<i32> { }
convert_samples_to!(i32, samples)
} pub fn to_s24(samples: &[f32]) -> Vec<i32> {
convert_samples_to!(i32, samples, 8)
pub fn to_s24(samples: &[f32]) -> Vec<i32> { }
convert_samples_to!(i32, samples, 8)
} pub fn to_s24_3(samples: &[f32]) -> Vec<i24> {
to_s32(samples)
pub fn to_s24_3(samples: &[f32]) -> Vec<i24> { .iter()
Self::to_s32(samples) .map(|sample| i24::pcm_from_i32(*sample))
.iter() .collect()
.map(|sample| i24::pcm_from_i32(*sample)) }
.collect()
} pub fn to_s16(samples: &[f32]) -> Vec<i16> {
convert_samples_to!(i16, samples)
pub fn to_s16(samples: &[f32]) -> Vec<i16> {
convert_samples_to!(i16, samples)
}
} }

View file

@ -3,7 +3,7 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
mod convert; pub mod convert;
mod decrypt; mod decrypt;
mod fetch; mod fetch;
@ -24,7 +24,6 @@ pub use passthrough_decoder::{PassthroughDecoder, PassthroughError};
mod range_set; mod range_set;
pub use convert::{i24, SamplesConverter};
pub use decrypt::AudioDecrypt; pub use decrypt::AudioDecrypt;
pub use fetch::{AudioFile, StreamLoaderController}; pub use fetch::{AudioFile, StreamLoaderController};
pub use fetch::{ pub use fetch::{

View file

@ -87,7 +87,7 @@ impl Open for AlsaSink {
Self { Self {
pcm: None, pcm: None,
format: format, format,
device: name, device: name,
buffer: vec![], buffer: vec![],
} }
@ -146,7 +146,7 @@ impl SinkAsBytes for AlsaSink {
.extend_from_slice(&data[processed_data..processed_data + data_to_buffer]); .extend_from_slice(&data[processed_data..processed_data + data_to_buffer]);
processed_data += data_to_buffer; processed_data += data_to_buffer;
if self.buffer.len() == self.buffer.capacity() { if self.buffer.len() == self.buffer.capacity() {
self.write_buf().expect("could not append to buffer"); self.write_buf();
self.buffer.clear(); self.buffer.clear();
} }
} }
@ -156,14 +156,12 @@ impl SinkAsBytes for AlsaSink {
} }
impl AlsaSink { impl AlsaSink {
fn write_buf(&mut self) -> io::Result<()> { fn write_buf(&mut self) {
let pcm = self.pcm.as_mut().unwrap(); let pcm = self.pcm.as_mut().unwrap();
let io = pcm.io_bytes(); let io = pcm.io_bytes();
match io.writei(&self.buffer) { match io.writei(&self.buffer) {
Ok(_) => (), Ok(_) => (),
Err(err) => pcm.try_recover(err, false).unwrap(), Err(err) => pcm.try_recover(err, false).unwrap(),
}; };
Ok(())
} }
} }

View file

@ -47,7 +47,7 @@ impl Open for JackSink {
} }
info!("Using JACK sink with format {:?}", AudioFormat::F32); info!("Using JACK sink with format {:?}", AudioFormat::F32);
let client_name = client_name.unwrap_or("librespot".to_string()); let client_name = client_name.unwrap_or_else(|| "librespot".to_string());
let (client, _status) = let (client, _status) =
Client::new(&client_name[..], ClientOptions::NO_START_SERVER).unwrap(); Client::new(&client_name[..], ClientOptions::NO_START_SERVER).unwrap();
let ch_r = client.register_port("out_0", AudioOut::default()).unwrap(); let ch_r = client.register_port("out_0", AudioOut::default()).unwrap();

View file

@ -26,25 +26,25 @@ fn mk_sink<S: Sink + Open + 'static>(device: Option<String>, format: AudioFormat
macro_rules! sink_as_bytes { macro_rules! sink_as_bytes {
() => { () => {
fn write(&mut self, packet: &AudioPacket) -> io::Result<()> { fn write(&mut self, packet: &AudioPacket) -> io::Result<()> {
use crate::audio::{i24, SamplesConverter}; use crate::audio::convert::{self, i24};
use zerocopy::AsBytes; use zerocopy::AsBytes;
match packet { match packet {
AudioPacket::Samples(samples) => match self.format { AudioPacket::Samples(samples) => match self.format {
AudioFormat::F32 => self.write_bytes(samples.as_bytes()), AudioFormat::F32 => self.write_bytes(samples.as_bytes()),
AudioFormat::S32 => { AudioFormat::S32 => {
let samples_s32: &[i32] = &SamplesConverter::to_s32(samples); let samples_s32: &[i32] = &convert::to_s32(samples);
self.write_bytes(samples_s32.as_bytes()) self.write_bytes(samples_s32.as_bytes())
} }
AudioFormat::S24 => { AudioFormat::S24 => {
let samples_s24: &[i32] = &SamplesConverter::to_s24(samples); let samples_s24: &[i32] = &convert::to_s24(samples);
self.write_bytes(samples_s24.as_bytes()) self.write_bytes(samples_s24.as_bytes())
} }
AudioFormat::S24_3 => { AudioFormat::S24_3 => {
let samples_s24_3: &[i24] = &SamplesConverter::to_s24_3(samples); let samples_s24_3: &[i24] = &convert::to_s24_3(samples);
self.write_bytes(samples_s24_3.as_bytes()) self.write_bytes(samples_s24_3.as_bytes())
} }
AudioFormat::S16 => { AudioFormat::S16 => {
let samples_s16: &[i16] = &SamplesConverter::to_s16(samples); let samples_s16: &[i16] = &convert::to_s16(samples);
self.write_bytes(samples_s16.as_bytes()) self.write_bytes(samples_s16.as_bytes())
} }
}, },

View file

@ -1,8 +1,7 @@
use super::{Open, Sink}; use super::{Open, Sink};
use crate::audio::{AudioPacket, SamplesConverter}; use crate::audio::{convert, AudioPacket};
use crate::config::AudioFormat; use crate::config::AudioFormat;
use crate::player::{NUM_CHANNELS, SAMPLE_RATE}; use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
use portaudio_rs;
use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo}; use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo};
use portaudio_rs::stream::*; use portaudio_rs::stream::*;
use std::io; use std::io;
@ -157,11 +156,11 @@ impl<'a> Sink for PortAudioSink<'a> {
write_sink!(ref mut stream, samples) write_sink!(ref mut stream, samples)
} }
Self::S32(stream, _parameters) => { Self::S32(stream, _parameters) => {
let samples_s32: &[i32] = &SamplesConverter::to_s32(samples); let samples_s32: &[i32] = &convert::to_s32(samples);
write_sink!(ref mut stream, samples_s32) write_sink!(ref mut stream, samples_s32)
} }
Self::S16(stream, _parameters) => { Self::S16(stream, _parameters) => {
let samples_s16: &[i16] = &SamplesConverter::to_s16(samples); let samples_s16: &[i16] = &convert::to_s16(samples);
write_sink!(ref mut stream, samples_s16) write_sink!(ref mut stream, samples_s16)
} }
}; };

View file

@ -5,7 +5,7 @@ use cpal::traits::{DeviceTrait, HostTrait};
use thiserror::Error; use thiserror::Error;
use super::Sink; use super::Sink;
use crate::audio::{AudioPacket, SamplesConverter}; use crate::audio::{convert, AudioPacket};
use crate::config::AudioFormat; use crate::config::AudioFormat;
use crate::player::{NUM_CHANNELS, SAMPLE_RATE}; use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
@ -189,7 +189,7 @@ impl Sink for RodioSink {
self.rodio_sink.append(source); self.rodio_sink.append(source);
} }
AudioFormat::S16 => { AudioFormat::S16 => {
let samples_s16: &[i16] = &SamplesConverter::to_s16(samples); let samples_s16: &[i16] = &convert::to_s16(samples);
let source = rodio::buffer::SamplesBuffer::new( let source = rodio::buffer::SamplesBuffer::new(
NUM_CHANNELS as u16, NUM_CHANNELS as u16,
SAMPLE_RATE, SAMPLE_RATE,

View file

@ -1,5 +1,5 @@
use super::{Open, Sink}; use super::{Open, Sink};
use crate::audio::{AudioPacket, SamplesConverter}; use crate::audio::{convert, AudioPacket};
use crate::config::AudioFormat; use crate::config::AudioFormat;
use crate::player::{NUM_CHANNELS, SAMPLE_RATE}; use crate::player::{NUM_CHANNELS, SAMPLE_RATE};
use sdl2::audio::{AudioQueue, AudioSpecDesired}; use sdl2::audio::{AudioQueue, AudioSpecDesired};
@ -97,12 +97,12 @@ impl Sink for SdlSink {
queue.queue(samples) queue.queue(samples)
} }
Self::S32(queue) => { Self::S32(queue) => {
let samples_s32: &[i32] = &SamplesConverter::to_s32(samples); let samples_s32: &[i32] = &convert::to_s32(samples);
drain_sink!(queue, AudioFormat::S32.size()); drain_sink!(queue, AudioFormat::S32.size());
queue.queue(samples_s32) queue.queue(samples_s32)
} }
Self::S16(queue) => { Self::S16(queue) => {
let samples_s16: &[i16] = &SamplesConverter::to_s16(samples); let samples_s16: &[i16] = &convert::to_s16(samples);
drain_sink!(queue, AudioFormat::S16.size()); drain_sink!(queue, AudioFormat::S16.size());
queue.queue(samples_s16) queue.queue(samples_s16)
} }

View file

@ -1,4 +1,4 @@
use crate::audio::i24; use crate::audio::convert::i24;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::mem; use std::mem;
use std::str::FromStr; use std::str::FromStr;

View file

@ -33,13 +33,12 @@ impl AlsaMixer {
let mixer = alsa::mixer::Mixer::new(&config.card, false)?; let mixer = alsa::mixer::Mixer::new(&config.card, false)?;
let sid = alsa::mixer::SelemId::new(&config.mixer, config.index); let sid = alsa::mixer::SelemId::new(&config.mixer, config.index);
let selem = mixer.find_selem(&sid).expect( let selem = mixer.find_selem(&sid).unwrap_or_else(|| {
format!( panic!(
"Couldn't find simple mixer control for {},{}", "Couldn't find simple mixer control for {},{}",
&config.mixer, &config.index, &config.mixer, &config.index,
) )
.as_str(), });
);
let (min, max) = selem.get_playback_volume_range(); let (min, max) = selem.get_playback_volume_range();
let (min_db, max_db) = selem.get_playback_db_range(); let (min_db, max_db) = selem.get_playback_db_range();
let hw_mix = selem let hw_mix = selem

View file

@ -206,11 +206,11 @@ pub struct NormalisationData {
impl NormalisationData { impl NormalisationData {
pub fn db_to_ratio(db: f32) -> f32 { pub fn db_to_ratio(db: f32) -> f32 {
return f32::powf(10.0, db / DB_VOLTAGE_RATIO); f32::powf(10.0, db / DB_VOLTAGE_RATIO)
} }
pub fn ratio_to_db(ratio: f32) -> f32 { pub fn ratio_to_db(ratio: f32) -> f32 {
return ratio.log10() * DB_VOLTAGE_RATIO; ratio.log10() * DB_VOLTAGE_RATIO
} }
fn parse_from_file<T: Read + Seek>(mut file: T) -> io::Result<NormalisationData> { fn parse_from_file<T: Read + Seek>(mut file: T) -> io::Result<NormalisationData> {
@ -1161,7 +1161,7 @@ impl PlayerInternal {
} }
if self.config.normalisation if self.config.normalisation
&& (normalisation_factor != 1.0 && (f32::abs(normalisation_factor - 1.0) < f32::EPSILON
|| self.config.normalisation_method != NormalisationMethod::Basic) || self.config.normalisation_method != NormalisationMethod::Basic)
{ {
for sample in data.iter_mut() { for sample in data.iter_mut() {

View file

@ -322,7 +322,7 @@ fn setup(args: &[String]) -> Setup {
.opt_str("format") .opt_str("format")
.as_ref() .as_ref()
.map(|format| AudioFormat::try_from(format).expect("Invalid output format")) .map(|format| AudioFormat::try_from(format).expect("Invalid output format"))
.unwrap_or(AudioFormat::default()); .unwrap_or_default();
let device = matches.opt_str("device"); let device = matches.opt_str("device");
if device == Some("?".into()) { if device == Some("?".into()) {
@ -470,7 +470,7 @@ fn setup(args: &[String]) -> Setup {
bitrate, bitrate,
gapless: !matches.opt_present("disable-gapless"), gapless: !matches.opt_present("disable-gapless"),
normalisation: matches.opt_present("enable-volume-normalisation"), normalisation: matches.opt_present("enable-volume-normalisation"),
normalisation_method: normalisation_method, normalisation_method,
normalisation_type: gain_type, normalisation_type: gain_type,
normalisation_pregain: matches normalisation_pregain: matches
.opt_str("normalisation-pregain") .opt_str("normalisation-pregain")