Skip to content

Commit

Permalink
clippy overload
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Feb 15, 2025
1 parent 81e4cd6 commit efc1de3
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 152 deletions.
28 changes: 28 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ readme = "README.md"
categories = ["multimedia"]
keywords = ["guitar", "tablature", "music"]

[lints.clippy]
cast_lossless = "warn"
doc_link_with_quotes = "warn"
enum_glob_use = "warn"
explicit_into_iter_loop = "warn"
filter_map_next = "warn"
flat_map_option = "warn"
from_iter_instead_of_collect = "warn"
implicit_clone = "warn"
inconsistent_struct_constructor = "warn"
inefficient_to_string = "warn"
manual_is_variant_and = "warn"
manual_let_else = "warn"
needless_continue = "warn"
needless_raw_string_hashes = "warn"
ptr_as_ptr = "warn"
ref_option_ref = "warn"
uninlined_format_args = "warn"
unnecessary_wraps = "warn"
unused_self = "warn"
used_underscore_binding = "warn"
match_wildcard_for_single_variants = "warn"
needless_pass_by_ref_mut = "warn"
missing_const_for_fn = "warn"
redundant_closure_for_method_calls = "warn"
semicolon_if_nothing_returned = "warn"
unreadable_literal = "warn"

[dependencies]
nom = "8.0.0"
encoding_rs = "0.8.35"
Expand Down
92 changes: 47 additions & 45 deletions src/audio/midi_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct MidiBuilder {
}

impl MidiBuilder {
pub fn new() -> Self {
pub const fn new() -> Self {
Self { events: Vec::new() }
}

Expand Down Expand Up @@ -166,7 +166,7 @@ impl MidiBuilder {
for note in &beat.notes {
if note.kind != NoteType::Tie {
let (string_id, string_tuning) = strings[note.string as usize - 1];
assert_eq!(string_id, note.string as i32);
assert_eq!(string_id, i32::from(note.string));

// surrounding notes on the same string on the previous & next beat
let previous_note =
Expand Down Expand Up @@ -197,7 +197,14 @@ impl MidiBuilder {
velocity,
midi_channel,
) {
self.add_note(track_id, key, start, duration, velocity, channel_id as i32)
self.add_note(
track_id,
key,
start,
duration,
velocity,
i32::from(channel_id),
);
}
}
}
Expand All @@ -217,11 +224,11 @@ impl MidiBuilder {
velocity: i16,
midi_channel: &MidiChannel,
) -> Option<i32> {
let channel_id = midi_channel.channel_id as i32;
let channel_id = i32::from(midi_channel.channel_id);
let is_percussion = midi_channel.is_percussion();

// compute key without effect
let initial_key = track_offset + note.value as i32 + string_tuning;
let initial_key = track_offset + i32::from(note.value) + string_tuning;

// key with effect
let mut key = initial_key;
Expand All @@ -242,7 +249,7 @@ impl MidiBuilder {

// grace note
if let Some(grace) = &note.effect.grace {
let grace_key = track_offset + grace.fret as i32 + string_tuning;
let grace_key = track_offset + i32::from(grace.fret) + string_tuning;
let grace_length = grace.duration_time() as usize;
let grace_velocity = grace.velocity;
let grace_duration = if grace.is_dead {
Expand All @@ -262,13 +269,13 @@ impl MidiBuilder {
grace_duration,
grace_velocity,
channel_id,
)
);
}

// trill
if let Some(trill) = &note.effect.trill {
if !is_percussion {
let trill_key = track_offset + trill.fret as i32 + string_tuning;
let trill_key = track_offset + i32::from(trill.fret) + string_tuning;
let mut trill_length = trill.duration.time() as usize;

let trill_tick_limit = *start + *duration;
Expand All @@ -288,10 +295,7 @@ impl MidiBuilder {
}
assert!(
counter > 0,
"No trill notes published! trill_length: {}, tick: {}, trill_tick_limit: {}",
trill_length,
tick,
trill_tick_limit
"No trill notes published! trill_length: {trill_length}, tick: {tick}, trill_tick_limit: {trill_tick_limit}"
);

// all notes published - the caller does not need to publish the note
Expand All @@ -315,10 +319,7 @@ impl MidiBuilder {
}
assert!(
counter > 0,
"No tremolo notes published! tp_length: {}, tick: {}, tp_tick_limit: {}",
tp_length,
tick,
tp_tick_limit
"No tremolo notes published! tp_length: {tp_length}, tick: {tick}, tp_tick_limit: {tp_tick_limit}"
);
// all notes published - the caller does not need to publish the note
return None;
Expand All @@ -327,23 +328,23 @@ impl MidiBuilder {
// bend
if let Some(bend_effect) = &note.effect.bend {
if !is_percussion {
self.add_bend(track_id, *start, *duration, channel_id, bend_effect)
self.add_bend(track_id, *start, *duration, channel_id, bend_effect);
}
}

// tremolo bar
if let Some(tremolo_bar) = &note.effect.tremolo_bar {
if !is_percussion {
self.add_tremolo_bar(track_id, *start, *duration, channel_id, tremolo_bar)
self.add_tremolo_bar(track_id, *start, *duration, channel_id, tremolo_bar);
}
}

// slide
if let Some(_slide) = &note.effect.slide {
if !is_percussion {
if let Some((next_beat, next_note)) = next_note_beat {
let value_1 = note.value as i32;
let value_2 = next_note.value as i32;
let value_1 = i32::from(note.value);
let value_2 = i32::from(next_note.value);

let tick1 = *start;
let tick2 = next_beat.start as usize;
Expand Down Expand Up @@ -377,7 +378,7 @@ impl MidiBuilder {
HarmonicType::Natural => {
for (harmonic_value, harmonic_frequency) in NATURAL_FREQUENCIES {
if note.value % 12 == (harmonic_value % 12) as i16 {
key = (initial_key + harmonic_frequency) - note.value as i32;
key = (initial_key + harmonic_frequency) - i32::from(note.value);
break;
}
}
Expand All @@ -400,7 +401,8 @@ impl MidiBuilder {
HarmonicType::Tapped => {
if let Some(right_hand_fret) = harmonic.right_hand_fret {
for (harmonic_value, harmonic_frequency) in NATURAL_FREQUENCIES {
if right_hand_fret as i16 - note.value == harmonic_value as i16 {
if i16::from(right_hand_fret) - note.value == harmonic_value as i16
{
key = initial_key + harmonic_frequency;
break;
}
Expand Down Expand Up @@ -450,15 +452,15 @@ impl MidiBuilder {
) {
for (point_id, point) in bend.points.iter().enumerate() {
let value =
DEFAULT_BEND + (point.value as f32 * DEFAULT_BEND_SEMI_TONE / SEMITONE_LENGTH);
DEFAULT_BEND + (f32::from(point.value) * DEFAULT_BEND_SEMI_TONE / SEMITONE_LENGTH);
let value = value.clamp(0.0, 127.0) as i32;
let bend_start = start + point.get_time(duration);
self.add_pitch_bend(bend_start, track_id, channel_id, value);

// look ahead to next bend point
if let Some(next_point) = bend.points.get(point_id + 1) {
let next_value = DEFAULT_BEND
+ (next_point.value as f32 * DEFAULT_BEND_SEMI_TONE / SEMITONE_LENGTH);
+ (f32::from(next_point.value) * DEFAULT_BEND_SEMI_TONE / SEMITONE_LENGTH);
self.process_next_bend_values(
track_id,
channel_id,
Expand All @@ -471,7 +473,7 @@ impl MidiBuilder {
);
}
}
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND as i32)
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND as i32);
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -522,15 +524,15 @@ impl MidiBuilder {
tremolo_bar: &TremoloBarEffect,
) {
for (point_id, point) in tremolo_bar.points.iter().enumerate() {
let value = DEFAULT_BEND + (point.value as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
let value = DEFAULT_BEND + (f32::from(point.value) * DEFAULT_BEND_SEMI_TONE * 2.0);
let value = value.clamp(0.0, 127.0) as i32;
let bend_start = start + point.get_time(duration);
self.add_pitch_bend(bend_start, track_id, channel_id, value);

// look ahead to next bend point
if let Some(next_point) = tremolo_bar.points.get(point_id + 1) {
let next_value =
DEFAULT_BEND + (next_point.value as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
DEFAULT_BEND + (f32::from(next_point.value) * DEFAULT_BEND_SEMI_TONE * 2.0);
self.process_next_bend_values(
track_id,
channel_id,
Expand All @@ -543,7 +545,7 @@ impl MidiBuilder {
);
}
}
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND as i32)
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND as i32);
}

fn add_note(
Expand Down Expand Up @@ -629,32 +631,32 @@ impl MidiBuilder {
self.add_volume_selection(
info_tick,
track_id,
channel_id as i32,
midi_channel.volume as i32,
i32::from(channel_id),
i32::from(midi_channel.volume),
);
self.add_expression_selection(info_tick, track_id, channel_id as i32, 127);
self.add_expression_selection(info_tick, track_id, i32::from(channel_id), 127);
self.add_chorus_selection(
info_tick,
track_id,
channel_id as i32,
midi_channel.chorus as i32,
i32::from(channel_id),
i32::from(midi_channel.chorus),
);
self.add_reverb_selection(
info_tick,
track_id,
channel_id as i32,
midi_channel.reverb as i32,
i32::from(channel_id),
i32::from(midi_channel.reverb),
);
self.add_bank_selection(
info_tick,
track_id,
channel_id as i32,
midi_channel.bank as i32,
i32::from(channel_id),
i32::from(midi_channel.bank),
);
self.add_program_selection(
info_tick,
track_id,
channel_id as i32,
i32::from(channel_id),
midi_channel.instrument,
);
}
Expand Down Expand Up @@ -696,7 +698,7 @@ fn apply_duration_effect(
// TODO handle chain of ties and not just the next one
if let Some((next_beat, next_note)) = next_note_beat {
if next_note.kind == NoteType::Tie {
duration += next_beat.duration.time() as usize
duration += next_beat.duration.time() as usize;
}
if note.effect.let_ring {
duration += next_beat.duration.time() as usize;
Expand Down Expand Up @@ -743,21 +745,21 @@ mod tests {
continue;
}
let file_name = path.file_name().unwrap().to_str().unwrap();
eprintln!("Parsing file: {}", file_name);
eprintln!("Parsing file: {file_name}");
let file_path = path.to_str().unwrap();
let song = parse_gp_file(file_path)
.unwrap_or_else(|err| panic!("Failed to parse file: {}\n{}", file_name, err));
.unwrap_or_else(|err| panic!("Failed to parse file: {file_name}\n{err}"));
let song = Rc::new(song);
let builder = MidiBuilder::new();
let events = builder.build_for_song(&song);
assert!(!events.is_empty(), "No events found for {}", file_name);
assert!(!events.is_empty(), "No events found for {file_name}");

// assert sorted by tick
assert!(events.windows(2).all(|w| w[0].tick <= w[1].tick));
assert_eq!(events[0].tick, 1);

// check against golden file
let gold_file_path = gold_dir.join(format!("{}.txt", file_name));
let gold_file_path = gold_dir.join(format!("{file_name}.txt"));
if !gold_file_path.exists() {
// create gold file
let mut file = std::fs::File::create(&gold_file_path).unwrap();
Expand Down Expand Up @@ -901,7 +903,7 @@ mod tests {

//print 100 first for debugging
for (i, event) in solo_track_events.iter().enumerate().take(100) {
eprintln!("{} {:?}", i, event);
eprintln!("{i} {event:?}");
}

// trill ON
Expand Down Expand Up @@ -1060,7 +1062,7 @@ mod tests {

assert_eq!(events.len(), 43702);
assert_eq!(events[0].tick, 1);
assert_eq!(events.iter().last().unwrap().tick, 795840);
assert_eq!(events.iter().last().unwrap().tick, 795_840);

// assert number of tracks
let track_count = song.tracks.len();
Expand Down
14 changes: 7 additions & 7 deletions src/audio/midi_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ pub struct MidiEvent {
}

impl MidiEvent {
pub fn is_midi_message(&self) -> bool {
pub const fn is_midi_message(&self) -> bool {
matches!(self.event, MidiEventType::MidiMessage(_, _, _, _))
}

pub fn is_tempo_change(&self) -> bool {
pub const fn is_tempo_change(&self) -> bool {
matches!(self.event, MidiEventType::TempoChange(_))
}

pub fn is_note_event(&self) -> bool {
pub const fn is_note_event(&self) -> bool {
!self.is_tempo_change() || !self.is_midi_message()
}

Expand Down Expand Up @@ -82,19 +82,19 @@ pub enum MidiEventType {
}

impl MidiEventType {
fn note_on(channel: i32, key: i32, velocity: i16) -> Self {
const fn note_on(channel: i32, key: i32, velocity: i16) -> Self {
Self::NoteOn(channel, key, velocity)
}

fn note_off(channel: i32, key: i32) -> Self {
const fn note_off(channel: i32, key: i32) -> Self {
Self::NoteOff(channel, key)
}

fn tempo_change(tempo: i32) -> Self {
const fn tempo_change(tempo: i32) -> Self {
Self::TempoChange(tempo)
}

fn midi_message(channel: i32, command: i32, data1: i32, data2: i32) -> Self {
const fn midi_message(channel: i32, command: i32, data1: i32, data2: i32) -> Self {
Self::MidiMessage(channel, command, data1, data2)
}
}
Loading

0 comments on commit efc1de3

Please sign in to comment.