From b4f9ae31e2832ba6c0bd753eeb36902dd2cb07cc Mon Sep 17 00:00:00 2001 From: johannesd3 Date: Sat, 10 Apr 2021 14:06:41 +0200 Subject: [PATCH] Fix clippy warnings --- audio/src/convert.rs | 37 ++++++++++++------------- audio/src/lib.rs | 3 +- playback/src/audio_backend/alsa.rs | 8 ++---- playback/src/audio_backend/jackaudio.rs | 2 +- playback/src/audio_backend/mod.rs | 10 +++---- playback/src/audio_backend/portaudio.rs | 7 ++--- playback/src/audio_backend/rodio.rs | 4 +-- playback/src/audio_backend/sdl.rs | 6 ++-- playback/src/config.rs | 2 +- playback/src/mixer/alsamixer.rs | 7 ++--- playback/src/player.rs | 6 ++-- src/main.rs | 4 +-- 12 files changed, 44 insertions(+), 52 deletions(-) diff --git a/audio/src/convert.rs b/audio/src/convert.rs index e291c804..450910b0 100644 --- a/audio/src/convert.rs +++ b/audio/src/convert.rs @@ -36,24 +36,21 @@ macro_rules! convert_samples_to { }; } -pub struct SamplesConverter {} -impl SamplesConverter { - pub fn to_s32(samples: &[f32]) -> Vec { - convert_samples_to!(i32, samples) - } - - pub fn to_s24(samples: &[f32]) -> Vec { - convert_samples_to!(i32, samples, 8) - } - - pub fn to_s24_3(samples: &[f32]) -> Vec { - Self::to_s32(samples) - .iter() - .map(|sample| i24::pcm_from_i32(*sample)) - .collect() - } - - pub fn to_s16(samples: &[f32]) -> Vec { - convert_samples_to!(i16, samples) - } +pub fn to_s32(samples: &[f32]) -> Vec { + convert_samples_to!(i32, samples) +} + +pub fn to_s24(samples: &[f32]) -> Vec { + convert_samples_to!(i32, samples, 8) +} + +pub fn to_s24_3(samples: &[f32]) -> Vec { + to_s32(samples) + .iter() + .map(|sample| i24::pcm_from_i32(*sample)) + .collect() +} + +pub fn to_s16(samples: &[f32]) -> Vec { + convert_samples_to!(i16, samples) } diff --git a/audio/src/lib.rs b/audio/src/lib.rs index e0a4858c..b587f038 100644 --- a/audio/src/lib.rs +++ b/audio/src/lib.rs @@ -3,7 +3,7 @@ #[macro_use] extern crate log; -mod convert; +pub mod convert; mod decrypt; mod fetch; @@ -24,7 +24,6 @@ pub use passthrough_decoder::{PassthroughDecoder, PassthroughError}; mod range_set; -pub use convert::{i24, SamplesConverter}; pub use decrypt::AudioDecrypt; pub use fetch::{AudioFile, StreamLoaderController}; pub use fetch::{ diff --git a/playback/src/audio_backend/alsa.rs b/playback/src/audio_backend/alsa.rs index 1d551878..54fed319 100644 --- a/playback/src/audio_backend/alsa.rs +++ b/playback/src/audio_backend/alsa.rs @@ -87,7 +87,7 @@ impl Open for AlsaSink { Self { pcm: None, - format: format, + format, device: name, buffer: vec![], } @@ -146,7 +146,7 @@ impl SinkAsBytes for AlsaSink { .extend_from_slice(&data[processed_data..processed_data + data_to_buffer]); processed_data += data_to_buffer; if self.buffer.len() == self.buffer.capacity() { - self.write_buf().expect("could not append to buffer"); + self.write_buf(); self.buffer.clear(); } } @@ -156,14 +156,12 @@ impl SinkAsBytes for AlsaSink { } impl AlsaSink { - fn write_buf(&mut self) -> io::Result<()> { + fn write_buf(&mut self) { let pcm = self.pcm.as_mut().unwrap(); let io = pcm.io_bytes(); match io.writei(&self.buffer) { Ok(_) => (), Err(err) => pcm.try_recover(err, false).unwrap(), }; - - Ok(()) } } diff --git a/playback/src/audio_backend/jackaudio.rs b/playback/src/audio_backend/jackaudio.rs index b38c9014..aca2edd2 100644 --- a/playback/src/audio_backend/jackaudio.rs +++ b/playback/src/audio_backend/jackaudio.rs @@ -47,7 +47,7 @@ impl Open for JackSink { } 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) = Client::new(&client_name[..], ClientOptions::NO_START_SERVER).unwrap(); let ch_r = client.register_port("out_0", AudioOut::default()).unwrap(); diff --git a/playback/src/audio_backend/mod.rs b/playback/src/audio_backend/mod.rs index 24f5f855..72659f19 100644 --- a/playback/src/audio_backend/mod.rs +++ b/playback/src/audio_backend/mod.rs @@ -26,25 +26,25 @@ fn mk_sink(device: Option, format: AudioFormat macro_rules! sink_as_bytes { () => { fn write(&mut self, packet: &AudioPacket) -> io::Result<()> { - use crate::audio::{i24, SamplesConverter}; + use crate::audio::convert::{self, i24}; use zerocopy::AsBytes; match packet { AudioPacket::Samples(samples) => match self.format { AudioFormat::F32 => self.write_bytes(samples.as_bytes()), 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()) } 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()) } 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()) } 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()) } }, diff --git a/playback/src/audio_backend/portaudio.rs b/playback/src/audio_backend/portaudio.rs index 5faff6ca..234a9af6 100644 --- a/playback/src/audio_backend/portaudio.rs +++ b/playback/src/audio_backend/portaudio.rs @@ -1,8 +1,7 @@ use super::{Open, Sink}; -use crate::audio::{AudioPacket, SamplesConverter}; +use crate::audio::{convert, AudioPacket}; use crate::config::AudioFormat; use crate::player::{NUM_CHANNELS, SAMPLE_RATE}; -use portaudio_rs; use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo}; use portaudio_rs::stream::*; use std::io; @@ -157,11 +156,11 @@ impl<'a> Sink for PortAudioSink<'a> { write_sink!(ref mut stream, samples) } 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) } 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) } }; diff --git a/playback/src/audio_backend/rodio.rs b/playback/src/audio_backend/rodio.rs index 035cd328..65436a32 100644 --- a/playback/src/audio_backend/rodio.rs +++ b/playback/src/audio_backend/rodio.rs @@ -5,7 +5,7 @@ use cpal::traits::{DeviceTrait, HostTrait}; use thiserror::Error; use super::Sink; -use crate::audio::{AudioPacket, SamplesConverter}; +use crate::audio::{convert, AudioPacket}; use crate::config::AudioFormat; use crate::player::{NUM_CHANNELS, SAMPLE_RATE}; @@ -189,7 +189,7 @@ impl Sink for RodioSink { self.rodio_sink.append(source); } AudioFormat::S16 => { - let samples_s16: &[i16] = &SamplesConverter::to_s16(samples); + let samples_s16: &[i16] = &convert::to_s16(samples); let source = rodio::buffer::SamplesBuffer::new( NUM_CHANNELS as u16, SAMPLE_RATE, diff --git a/playback/src/audio_backend/sdl.rs b/playback/src/audio_backend/sdl.rs index 0a3fd433..29566533 100644 --- a/playback/src/audio_backend/sdl.rs +++ b/playback/src/audio_backend/sdl.rs @@ -1,5 +1,5 @@ use super::{Open, Sink}; -use crate::audio::{AudioPacket, SamplesConverter}; +use crate::audio::{convert, AudioPacket}; use crate::config::AudioFormat; use crate::player::{NUM_CHANNELS, SAMPLE_RATE}; use sdl2::audio::{AudioQueue, AudioSpecDesired}; @@ -97,12 +97,12 @@ impl Sink for SdlSink { queue.queue(samples) } 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()); queue.queue(samples_s32) } 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()); queue.queue(samples_s16) } diff --git a/playback/src/config.rs b/playback/src/config.rs index 95c97092..f8f02893 100644 --- a/playback/src/config.rs +++ b/playback/src/config.rs @@ -1,4 +1,4 @@ -use crate::audio::i24; +use crate::audio::convert::i24; use std::convert::TryFrom; use std::mem; use std::str::FromStr; diff --git a/playback/src/mixer/alsamixer.rs b/playback/src/mixer/alsamixer.rs index d9dbe311..5e0a963f 100644 --- a/playback/src/mixer/alsamixer.rs +++ b/playback/src/mixer/alsamixer.rs @@ -33,13 +33,12 @@ impl AlsaMixer { let mixer = alsa::mixer::Mixer::new(&config.card, false)?; let sid = alsa::mixer::SelemId::new(&config.mixer, config.index); - let selem = mixer.find_selem(&sid).expect( - format!( + let selem = mixer.find_selem(&sid).unwrap_or_else(|| { + panic!( "Couldn't find simple mixer control for {},{}", &config.mixer, &config.index, ) - .as_str(), - ); + }); let (min, max) = selem.get_playback_volume_range(); let (min_db, max_db) = selem.get_playback_db_range(); let hw_mix = selem diff --git a/playback/src/player.rs b/playback/src/player.rs index f53abbfb..3f0778f9 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -206,11 +206,11 @@ pub struct NormalisationData { impl NormalisationData { 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 { - return ratio.log10() * DB_VOLTAGE_RATIO; + ratio.log10() * DB_VOLTAGE_RATIO } fn parse_from_file(mut file: T) -> io::Result { @@ -1161,7 +1161,7 @@ impl PlayerInternal { } if self.config.normalisation - && (normalisation_factor != 1.0 + && (f32::abs(normalisation_factor - 1.0) < f32::EPSILON || self.config.normalisation_method != NormalisationMethod::Basic) { for sample in data.iter_mut() { diff --git a/src/main.rs b/src/main.rs index 4abe4e5e..318e1757 100644 --- a/src/main.rs +++ b/src/main.rs @@ -322,7 +322,7 @@ fn setup(args: &[String]) -> Setup { .opt_str("format") .as_ref() .map(|format| AudioFormat::try_from(format).expect("Invalid output format")) - .unwrap_or(AudioFormat::default()); + .unwrap_or_default(); let device = matches.opt_str("device"); if device == Some("?".into()) { @@ -470,7 +470,7 @@ fn setup(args: &[String]) -> Setup { bitrate, gapless: !matches.opt_present("disable-gapless"), normalisation: matches.opt_present("enable-volume-normalisation"), - normalisation_method: normalisation_method, + normalisation_method, normalisation_type: gain_type, normalisation_pregain: matches .opt_str("normalisation-pregain")