Clippy lints

This commit is contained in:
Polochon-street 2023-02-14 17:52:23 +01:00
parent 9cf425a8ba
commit 266913ef13
13 changed files with 40 additions and 54 deletions

View File

@ -26,6 +26,8 @@ jobs:
run: sudo apt-get update && sudo apt-get install build-essential yasm libavutil-dev libavcodec-dev libavformat-dev libavfilter-dev libavfilter-dev libavdevice-dev libswresample-dev libfftw3-dev ffmpeg
- name: Check format
run: cargo fmt -- --check
- name: Lint
run: cargo clippy --examples --features=serde,library -- -D warnings
- name: Build
run: cargo build --verbose
- name: Run tests
@ -38,8 +40,6 @@ jobs:
run: cargo +nightly-2022-02-16 bench --verbose --features=bench --no-run
- name: Build examples
run: cargo build --examples --verbose --features=serde,library
- name: Lint
run: cargo clippy --examples --features=serde,library -- -D warnings
build-test-lint-windows:
name: Windows - build, test and lint
@ -70,11 +70,11 @@ jobs:
toolchain: stable
override: true
components: rustfmt, clippy
- name: Build
run: cargo build --examples
- name: Test
run: cargo test --examples --features=serde
- name: Lint
run: cargo clippy --examples --features=serde -- -D warnings
- name: Check format
run: cargo fmt -- --check
- name: Build
run: cargo build --examples
- name: Test
run: cargo test --examples --features=serde

View File

@ -9,9 +9,9 @@ use std::env;
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
for path in &args {
match Song::from_path(&path) {
match Song::from_path(path) {
Ok(song) => println!("{}: {:?}", path, song.analysis),
Err(e) => println!("{}: {}", path, e),
Err(e) => println!("{path}: {e}"),
}
}
}

View File

@ -13,8 +13,8 @@ fn main() -> Result<(), String> {
let first_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
let second_path = paths.next().ok_or("Help: ./distance <song1> <song2>")?;
let song1 = Song::from_path(&first_path).map_err(|x| x.to_string())?;
let song2 = Song::from_path(&second_path).map_err(|x| x.to_string())?;
let song1 = Song::from_path(first_path).map_err(|x| x.to_string())?;
let song2 = Song::from_path(second_path).map_err(|x| x.to_string())?;
println!(
"d({:?}, {:?}) = {}",

View File

@ -196,7 +196,7 @@ fn main() -> Result<()> {
.map(|s| s.bliss_song.path.to_string_lossy().to_string())
.collect::<Vec<String>>();
for song in song_paths {
println!("{:?}", song);
println!("{song:?}");
}
}

View File

@ -219,7 +219,7 @@ fn main() -> Result<()> {
})
.collect::<Vec<(String, String)>>();
for (path, mime_type) in playlist {
println!("{} <{}>", path, mime_type,);
println!("{path} <{mime_type}>");
}
}

View File

@ -89,7 +89,7 @@ fn main() -> Result<()> {
if let Some(m) = matches.value_of("output-playlist") {
fs::write(m, playlist)?;
} else {
println!("{}", playlist);
println!("{playlist}");
}
Ok(())
}

View File

@ -53,13 +53,7 @@ impl ChromaDesc {
*/
pub fn do_(&mut self, signal: &[f32]) -> BlissResult<()> {
let mut stft = stft(signal, ChromaDesc::WINDOW_SIZE, 2205);
let tuning = estimate_tuning(
self.sample_rate as u32,
&stft,
ChromaDesc::WINDOW_SIZE,
0.01,
12,
)?;
let tuning = estimate_tuning(self.sample_rate, &stft, ChromaDesc::WINDOW_SIZE, 0.01, 12)?;
let chroma = chroma_stft(
self.sample_rate,
&mut stft,
@ -162,7 +156,7 @@ fn chroma_filter(
let n_chroma2 = (n_chroma_float / 2.0).round() as u32;
let n_chroma2_float = f64::from(n_chroma2);
let frequencies = Array::linspace(0., f64::from(sample_rate), (n_fft + 1) as usize);
let frequencies = Array::linspace(0., f64::from(sample_rate), n_fft + 1);
let mut freq_bins = frequencies;
hz_to_octs_inplace(&mut freq_bins, tuning, n_chroma);
@ -213,12 +207,12 @@ fn chroma_filter(
}
let mut b = Array::from(uninit)
.into_shape(wts.dim())
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e)))?;
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {e}")))?;
b.slice_mut(s![-3.., ..]).assign(&wts.slice(s![..3, ..]));
b.slice_mut(s![..-3, ..]).assign(&wts.slice(s![3.., ..]));
wts = b;
let non_aliased = (1 + n_fft / 2) as usize;
let non_aliased = 1 + n_fft / 2;
Ok(wts.slice_move(s![.., ..non_aliased]))
}
@ -308,7 +302,7 @@ fn pitch_tuning(
}
let max_index = counts
.argmax()
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e)))?;
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {e}")))?;
// Return the bin with the most reoccuring frequency.
Ok((-50. + (100. * resolution * max_index as f64)) / 100.)
@ -336,7 +330,7 @@ fn estimate_tuning(
let threshold: N64 = Array::from(filtered_mag.to_vec())
.quantile_axis_mut(Axis(0), n64(0.5), &Midpoint)
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {}", e)))?
.map_err(|e| BlissError::AnalysisError(format!("in chroma: {e}")))?
.into_scalar();
let mut pitch = filtered_pitch
.iter()

View File

@ -534,7 +534,7 @@ impl<Config: AppConfigTrait> Library<Config> {
G: DistanceMetric + Copy,
{
let first_song: LibrarySong<T> = self.song_from_path(song_path).map_err(|_| {
BlissError::ProviderError(format!("song '{}' has not been analyzed", song_path))
BlissError::ProviderError(format!("song '{song_path}' has not been analyzed"))
})?;
let mut songs = self.songs_from_library()?;
sort_by(&first_song, &mut songs, distance, |s: &LibrarySong<T>| {
@ -886,8 +886,7 @@ impl<Config: AppConfigTrait> Library<Config> {
pb.inc(1);
}
pb.finish_with_message(format!(
"Analyzed {} song(s) successfully. {} Failure(s).",
success_count, failure_count
"Analyzed {success_count} song(s) successfully. {failure_count} Failure(s).",
));
log::info!(
@ -1059,8 +1058,7 @@ impl<Config: AppConfigTrait> Library<Config> {
.try_into()
.map_err(|_| {
BlissError::ProviderError(format!(
"song has more or less than {} features",
NUMBER_FEATURES
"song has more or less than {NUMBER_FEATURES} features",
))
})?,
};

View File

@ -268,7 +268,7 @@ pub fn closest_album_to_group(group: Vec<Song>, pool: Vec<Song>) -> BlissResult<
analysis
.push_row(song.analysis.as_arr1().view())
.map_err(|e| {
BlissError::ProviderError(format!("while computing distances: {}", e))
BlissError::ProviderError(format!("while computing distances: {e}"))
})?;
} else {
let mut array = Array::zeros((1, song.analysis.as_arr1().len()));
@ -370,7 +370,7 @@ where
analysis
.push_row(song.analysis.as_arr1().view())
.map_err(|e| {
BlissError::ProviderError(format!("while computing distances: {}", e))
BlissError::ProviderError(format!("while computing distances: {e}"))
})?;
} else {
let mut array = Array::zeros((1, song.analysis.as_arr1().len()));

View File

@ -158,7 +158,7 @@ impl fmt::Debug for Analysis {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug_struct = f.debug_struct("Analysis");
for feature in AnalysisIndex::iter() {
debug_struct.field(&format!("{:?}", feature), &self[feature]);
debug_struct.field(&format!("{feature:?}"), &self[feature]);
}
debug_struct.finish()?;
f.write_str(&format!(" /* {:?} */", &self.as_vec()))
@ -657,8 +657,7 @@ fn resample_frame(
)
.map_err(|e| {
BlissError::DecodingError(format!(
"while trying to allocate resampling context: {:?}",
e
"while trying to allocate resampling context: {e:?}",
))
})?;
let mut resampled = ffmpeg::frame::Audio::empty();
@ -680,7 +679,7 @@ fn resample_frame(
resample_context
.run(&decoded, &mut resampled)
.map_err(|e| {
BlissError::DecodingError(format!("while trying to resample song: {:?}", e))
BlissError::DecodingError(format!("while trying to resample song: {e:?}"))
})?;
push_to_sample_array(&resampled, &mut sample_array);
}
@ -691,7 +690,7 @@ fn resample_frame(
// `resampled` again?
loop {
match resample_context.flush(&mut resampled).map_err(|e| {
BlissError::DecodingError(format!("while trying to resample song: {:?}", e))
BlissError::DecodingError(format!("while trying to resample song: {e:?}"))
})? {
Some(_) => {
push_to_sample_array(&resampled, &mut sample_array);

View File

@ -48,7 +48,7 @@ impl BPMDesc {
sample_rate,
)
.map_err(|e| {
BlissError::AnalysisError(format!("error while loading aubio tempo object: {}", e))
BlissError::AnalysisError(format!("error while loading aubio tempo object: {e}"))
})?,
bpms: Vec::new(),
})
@ -56,7 +56,7 @@ impl BPMDesc {
pub fn do_(&mut self, chunk: &[f32]) -> BlissResult<()> {
let result = self.aubio_obj.do_result(chunk).map_err(|e| {
BlissError::AnalysisError(format!("aubio error while computing tempo {}", e))
BlissError::AnalysisError(format!("aubio error while computing tempo {e}"))
})?;
if result > 0. {

View File

@ -125,23 +125,20 @@ impl SpectralDesc {
centroid_aubio_desc: SpecDesc::new(SpecShape::Centroid, SpectralDesc::WINDOW_SIZE)
.map_err(|e| {
BlissError::AnalysisError(format!(
"error while loading aubio centroid object: {}",
e
"error while loading aubio centroid object: {e}",
))
})?,
rolloff_aubio_desc: SpecDesc::new(SpecShape::Rolloff, SpectralDesc::WINDOW_SIZE)
.map_err(|e| {
BlissError::AnalysisError(format!(
"error while loading aubio rolloff object: {}",
e
"error while loading aubio rolloff object: {e}",
))
})?,
phase_vocoder: PVoc::new(SpectralDesc::WINDOW_SIZE, SpectralDesc::HOP_SIZE).map_err(
|e| {
BlissError::AnalysisError(format!(
"error while loading aubio pvoc object: {}",
e
))
BlissError::AnalysisError(
format!("error while loading aubio pvoc object: {e}",),
)
},
)?,
values_centroid: Vec::new(),
@ -163,7 +160,7 @@ impl SpectralDesc {
self.phase_vocoder
.do_(chunk, fftgrain.as_mut_slice())
.map_err(|e| {
BlissError::AnalysisError(format!("error while processing aubio pv object: {}", e))
BlissError::AnalysisError(format!("error while processing aubio pv object: {e}"))
})?;
let bin = self
@ -171,8 +168,7 @@ impl SpectralDesc {
.do_result(fftgrain.as_slice())
.map_err(|e| {
BlissError::AnalysisError(format!(
"error while processing aubio centroid object: {}",
e
"error while processing aubio centroid object: {e}",
))
})?;

View File

@ -63,7 +63,7 @@ pub(crate) fn stft(signal: &[f32], window_length: usize, hop_length: usize) -> A
}
pub(crate) fn mean<T: Clone + Into<f32>>(input: &[T]) -> f32 {
input.iter().map(|x| x.clone().into() as f32).sum::<f32>() / input.len() as f32
input.iter().map(|x| x.clone().into()).sum::<f32>() / input.len() as f32
}
pub(crate) trait Normalize {
@ -112,8 +112,7 @@ pub(crate) fn geometric_mean(input: &[f32]) -> f32 {
}
let n = input.len() as u32;
((((mantissas as f32).log2() + exponents as f32) as f32) / n as f32 - (1023. + 500.) / 8.)
.exp2()
(((mantissas as f32).log2() + exponents as f32) / n as f32 - (1023. + 500.) / 8.).exp2()
}
pub(crate) fn hz_to_octs_inplace(
@ -121,7 +120,7 @@ pub(crate) fn hz_to_octs_inplace(
tuning: f64,
bins_per_octave: u32,
) -> &mut Array1<f64> {
let a440 = 440.0 * (2_f64.powf(tuning / f64::from(bins_per_octave)) as f64);
let a440 = 440.0 * 2_f64.powf(tuning / f64::from(bins_per_octave));
*frequencies /= a440 / 16.;
frequencies.mapv_inplace(f64::log2);