Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy Lint Fixes #9

Merged
merged 2 commits into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/analyze/base.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ pub fn get_notes_from_audio_data(data: &[f32], length_in_seconds: u8) -> Res<Vec

let num_nan = data.iter().filter(|n| n.is_nan()).count();
if num_nan > 0 {
return Err(anyhow::Error::msg(format!("{} NaNs in audio data.", num_nan)));
return Err(anyhow::Error::msg(format!("{num_nan} NaNs in audio data.")));
}

let frequency_space = get_frequency_space(data, length_in_seconds);
@@ -229,7 +229,7 @@ fn get_likely_notes_from_peak_space(peak_space: &[(f32, f32)], cutoff: f32) -> V

let mut candidates = HashMap::new();

for (frequency, magnitude) in peak_space.iter() {
for (frequency, magnitude) in &peak_space {
if let Some(pair) = binary_search_closest(ALL_PITCH_NOTES_WITH_FREQUENCY.deref(), *frequency, |t| t.1) {
let note = pair.0;
let entry = candidates.entry(note).or_insert(*magnitude);
8 changes: 4 additions & 4 deletions src/bin.rs
Original file line number Diff line number Diff line change
@@ -346,14 +346,14 @@ fn start(args: Args) -> Void {

let chord = Chord::parse(parts.next().unwrap()).unwrap();

let length = parts.next().map(|l| l.parse::<u16>().unwrap()).unwrap_or(32);
let length = parts.next().map_or(32, |l| l.parse::<u16>().unwrap());

(chord, length)
})
.collect::<Vec<_>>();

loop {
for (chord, length) in chord_pairs.iter() {
for (chord, length) in &chord_pairs {
let length = (*length as f32) * 60f32 / bpm / 8f32;
play(chord, 0.0, length, 0.1)?;
}
@@ -596,7 +596,7 @@ fn start(args: Args) -> Void {
}

fn describe(chord: &Chord) {
println!("{}", chord);
println!("{chord}");
}

fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) -> Void {
@@ -615,7 +615,7 @@ fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) -> Void {
}

fn show_notes_and_chords(notes: &[Note]) -> Res<()> {
println!("Notes: {}", notes.iter().map(|n| n.to_string()).collect::<Vec<_>>().join(" "));
println!("Notes: {}", notes.iter().map(ToString::to_string).collect::<Vec<_>>().join(" "));

let candidates = Chord::try_from_notes(notes)?;

12 changes: 6 additions & 6 deletions src/core/chord.rs
Original file line number Diff line number Diff line change
@@ -408,7 +408,7 @@ impl Chord {
}

// Remove extensions and modifiers that are expressed elsewhere in the chord.
result.iter_mut().for_each(|c| {
for c in &mut result {
let dominant_degree = c.dominant_degree();

if let Some(degree) = dominant_degree {
@@ -425,7 +425,7 @@ impl Chord {
c.extensions.remove(&Extension::Add11);
c.extensions.remove(&Extension::Add13);
}
_ => {}
Degree::Seven => {}
}
}

@@ -434,7 +434,7 @@ impl Chord {
c.modifiers.remove(&Modifier::Flat5);
c.modifiers.remove(&Modifier::Augmented5);
}
});
}

// Order the candidates by "simplicity" (i.e., least slashes, least extensions, least modifiers, and least inversion).
result.sort();
@@ -480,7 +480,7 @@ impl HasName for Chord {

// Add extensions.
if !self.extensions.is_empty() {
for e in self.extensions.iter() {
for e in &self.extensions {
name.push_str(&format!("({})", e.static_name()));
}
}
@@ -559,8 +559,8 @@ impl HasIsCrunchy for Chord {

impl Display for Chord {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let scale = self.scale().iter().map(|n| n.static_name()).collect::<Vec<_>>().join(", ");
let chord = self.chord().iter().map(|n| n.static_name()).collect::<Vec<_>>().join(", ");
let scale = self.scale().iter().map(HasStaticName::static_name).collect::<Vec<_>>().join(", ");
let chord = self.chord().iter().map(HasStaticName::static_name).collect::<Vec<_>>().join(", ");

write!(f, "{}\n {}\n {}\n {}", self.precise_name(), self.description(), scale, chord)
}
4 changes: 1 addition & 3 deletions src/core/named_pitch.rs
Original file line number Diff line number Diff line change
@@ -342,9 +342,7 @@ impl Add<i8> for NamedPitch {

let new_index = index as i8 + rhs;

if !(0..=49).contains(&new_index) {
panic!("NamedPitch out of range.");
}
assert!((0..=49).contains(&new_index), "NamedPitch out of range.");

ALL_PITCHES[new_index as usize]
}
2 changes: 1 addition & 1 deletion src/core/note.rs
Original file line number Diff line number Diff line change
@@ -396,7 +396,7 @@ impl Sub for Note {
}
}

panic!("{} - {} is not a valid interval", high, low);
panic!("{high} - {low} is not a valid interval");
}
}

17 changes: 4 additions & 13 deletions src/core/octave.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ pub trait HasOctave {
// Enum.

/// An enum representing the octave of a note.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Default, Ord, PartialOrd)]
#[repr(u8)]
pub enum Octave {
/// The octave 0.
@@ -29,6 +29,7 @@ pub enum Octave {
/// The octave 3.
Three,
/// The octave 4.
#[default]
Four,
/// The octave 5.
Five,
@@ -86,9 +87,7 @@ impl Add for Octave {
fn add(self, rhs: Self) -> Self::Output {
let new_octave = self as u8 + rhs as u8;

if new_octave > 15 {
panic!("Octave overflow.");
}
assert!(new_octave <= 15, "Octave overflow");

// SAFETY: The new octave is guaranteed to be less than or equal to 15.
unsafe { std::mem::transmute(new_octave) }
@@ -101,9 +100,7 @@ impl Sub for Octave {
fn sub(self, rhs: Self) -> Self::Output {
let new_octave = (self as u8).checked_sub(rhs as u8).expect("Octave underflow.");

if new_octave > 15 {
panic!("Octave overflow.");
}
assert!(new_octave <= 15, "Octave overflow");

// SAFETY: The new octave is guaranteed to be less than or equal to 15.
unsafe { std::mem::transmute(new_octave) }
@@ -172,12 +169,6 @@ impl HasOctave for Octave {
}
}

impl Default for Octave {
fn default() -> Self {
Octave::Four
}
}

// Statics.

/// An array of all octaves.
84 changes: 28 additions & 56 deletions src/core/parser.rs
Original file line number Diff line number Diff line change
@@ -22,68 +22,40 @@ pub struct ChordParser;
pub fn note_str_to_note(note_str: &str) -> Res<Note> {
let chord = match note_str {
"A" => note::A,
"A#" => note::ASharp,
"A♯" => note::ASharp,
"A##" => note::ADoubleSharp,
"A𝄪" => note::ADoubleSharp,
"Ab" => note::AFlat,
"A♭" => note::AFlat,
"Abb" => note::ADoubleFlat,
"A𝄫" => note::ADoubleFlat,
"A#" | "A♯" => note::ASharp,
"A##" | "A𝄪" => note::ADoubleSharp,
"Ab" | "A♭" => note::AFlat,
"Abb" | "A𝄫" => note::ADoubleFlat,
"B" => note::B,
"B#" => note::BSharp,
"B♯" => note::BSharp,
"B##" => note::BDoubleSharp,
"B𝄪" => note::BDoubleSharp,
"Bb" => note::BFlat,
"B♭" => note::BFlat,
"Bbb" => note::BDoubleFlat,
"B𝄫" => note::BDoubleFlat,
"B#" | "B♯" => note::BSharp,
"B##" | "B𝄪" => note::BDoubleSharp,
"Bb" | "B♭" => note::BFlat,
"Bbb" | "B𝄫" => note::BDoubleFlat,
"C" => note::C,
"C#" => note::CSharp,
"C♯" => note::CSharp,
"C##" => note::CDoubleSharp,
"C𝄪" => note::CDoubleSharp,
"Cb" => note::CFlat,
"C♭" => note::CFlat,
"Cbb" => note::CDoubleFlat,
"C𝄫" => note::CDoubleFlat,
"C#" | "C♯" => note::CSharp,
"C##" | "C𝄪" => note::CDoubleSharp,
"Cb" | "C♭" => note::CFlat,
"Cbb" | "C𝄫" => note::CDoubleFlat,
"D" => note::D,
"D#" => note::DSharp,
"D♯" => note::DSharp,
"D##" => note::DDoubleSharp,
"D𝄪" => note::DDoubleSharp,
"Db" => note::DFlat,
"D♭" => note::DFlat,
"Dbb" => note::DDoubleFlat,
"D𝄫" => note::DDoubleFlat,
"D#" | "D♯" => note::DSharp,
"D##" | "D𝄪" => note::DDoubleSharp,
"Db" | "D♭" => note::DFlat,
"Dbb" | "D𝄫" => note::DDoubleFlat,
"E" => note::E,
"E#" => note::ESharp,
"E♯" => note::ESharp,
"E##" => note::EDoubleSharp,
"E𝄪" => note::EDoubleSharp,
"Eb" => note::EFlat,
"E♭" => note::EFlat,
"Ebb" => note::EDoubleFlat,
"E𝄫" => note::EDoubleFlat,
"E#" | "E♯" => note::ESharp,
"E##" | "E𝄪" => note::EDoubleSharp,
"Eb" | "E♭" => note::EFlat,
"Ebb" | "E𝄫" => note::EDoubleFlat,
"F" => note::F,
"F#" => note::FSharp,
"F♯" => note::FSharp,
"F##" => note::FDoubleSharp,
"F𝄪" => note::FDoubleSharp,
"Fb" => note::FFlat,
"F♭" => note::FFlat,
"Fbb" => note::FDoubleFlat,
"F𝄫" => note::FDoubleFlat,
"F#" | "F♯" => note::FSharp,
"F##" | "F𝄪" => note::FDoubleSharp,
"Fb" | "F♭" => note::FFlat,
"Fbb" | "F𝄫" => note::FDoubleFlat,
"G" => note::G,
"G#" => note::GSharp,
"G♯" => note::GSharp,
"G##" => note::GDoubleSharp,
"G𝄪" => note::GDoubleSharp,
"Gb" => note::GFlat,
"G♭" => note::GFlat,
"Gbb" => note::GDoubleFlat,
"G𝄫" => note::GDoubleFlat,
"G#" | "G♯" => note::GSharp,
"G##" | "G𝄪" => note::GDoubleSharp,
"Gb" | "G♭" => note::GFlat,
"Gbb" | "G𝄫" => note::GDoubleFlat,
_ => return Err(crate::core::base::Err::msg("Please use fairly standard notes (e.g., don't use triple sharps / flats).")),
};

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@
pub mod core;
pub mod helpers;

#[cfg(any(feature = "analyze_base"))]
#[cfg(feature = "analyze_base")]
pub mod analyze;

#[cfg(feature = "ml_base")]