Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Nov 9, 2024
1 parent 549801f commit 946336c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 48 deletions.
32 changes: 16 additions & 16 deletions src/audio/midi_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::rc::Rc;
const DEFAULT_DURATION_DEAD: i32 = 30;
const DEFAULT_DURATION_PM: i32 = 60;
const DEFAULT_BEND: i32 = 64;
const DEFAULT_BEND: f32 = 64.0;
const DEFAULT_BEND_SEMI_TONE: f32 = 2.75;

pub const NATURAL_FREQUENCIES: [(i32, i32); 6] = [
Expand Down Expand Up @@ -330,14 +330,13 @@ impl MidiBuilder {
let points = length / (QUARTER_TIME as usize / 8) as i32;
for p_offset in 1..=points {
let tone = ((length / points) * p_offset) * distance / length;
let bend =
DEFAULT_BEND as f32 + (tone as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
let bend = DEFAULT_BEND + (tone as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
let bend_tick = tick1 as i32 + (length / points) * p_offset;
self.add_pitch_bend(bend_tick as usize, track_id, channel_id, bend as i32);
}

// normalise the bend
self.add_pitch_bend(tick2, track_id, channel_id, DEFAULT_BEND);
self.add_pitch_bend(tick2, track_id, channel_id, DEFAULT_BEND as i32);
}
}
}
Expand Down Expand Up @@ -404,17 +403,17 @@ impl MidiBuilder {
} else {
next_start + 160
};
self.add_pitch_bend(next_start, track_id, channel_id as i32, DEFAULT_BEND);
self.add_pitch_bend(next_start, track_id, channel_id as i32, DEFAULT_BEND as i32);

next_start = if next_start + 160 > end {
end
} else {
next_start + 160
};
let value = DEFAULT_BEND as f32 + DEFAULT_BEND_SEMI_TONE / 2.0;
let value = DEFAULT_BEND + DEFAULT_BEND_SEMI_TONE / 2.0;
self.add_pitch_bend(next_start, track_id, channel_id as i32, value as i32);
}
self.add_pitch_bend(next_start, track_id, channel_id as i32, DEFAULT_BEND)
self.add_pitch_bend(next_start, track_id, channel_id as i32, DEFAULT_BEND as i32);
}

fn add_bend(
Expand All @@ -426,15 +425,15 @@ impl MidiBuilder {
bend: &BendEffect,
) {
for (point_id, point) in bend.points.iter().enumerate() {
let value = DEFAULT_BEND as f32
+ (point.value as f32 * DEFAULT_BEND_SEMI_TONE / SEMITONE_LENGTH);
let value =
DEFAULT_BEND + (point.value as f32 * 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 as f32
let next_value = DEFAULT_BEND
+ (next_point.value as f32 * DEFAULT_BEND_SEMI_TONE / SEMITONE_LENGTH);
self.process_next_bend_values(
track_id,
Expand All @@ -448,7 +447,7 @@ impl MidiBuilder {
);
}
}
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND)
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND as i32)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -499,15 +498,15 @@ impl MidiBuilder {
tremolo_bar: &TremoloBarEffect,
) {
for (point_id, point) in tremolo_bar.points.iter().enumerate() {
let value = DEFAULT_BEND as f32 + (point.value as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
let value = DEFAULT_BEND + (point.value as f32 * 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 as f32 + (next_point.value as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
DEFAULT_BEND + (next_point.value as f32 * DEFAULT_BEND_SEMI_TONE * 2.0);
self.process_next_bend_values(
track_id,
channel_id,
Expand All @@ -520,7 +519,7 @@ impl MidiBuilder {
);
}
}
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND)
self.add_pitch_bend(start + duration, track_id, channel_id, DEFAULT_BEND as i32)
}

fn add_note(
Expand Down Expand Up @@ -707,12 +706,13 @@ mod tests {
use std::collections::HashSet;

#[test]
fn test_midi_events_for_all_gp5_song() {
fn test_midi_events_for_all_files() {
let test_dir = std::path::Path::new("test-files");
for entry in std::fs::read_dir(test_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.extension().unwrap() != "gp5" {
let extension = path.extension().unwrap();
if extension != "gp5" && extension != "gp4" {
continue;
}
let file_name = path.file_name().unwrap().to_str().unwrap();
Expand Down
25 changes: 11 additions & 14 deletions src/audio/midi_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ impl AudioPlayer {
let events = builder.build_for_song(&song);

// sound font setup
let sound_font = match sound_font_file {
Some(sound_font_file) => {
let mut sf2 = File::open(sound_font_file).unwrap();
SoundFont::new(&mut sf2).unwrap()
}
None => {
let mut sf2 = TIMIDITY_SOUND_FONT;
SoundFont::new(&mut sf2).unwrap()
}
let sound_font = if let Some(sound_font_file) = sound_font_file {
let mut sf2 = File::open(sound_font_file).unwrap();
SoundFont::new(&mut sf2).unwrap()
} else {
let mut sf2 = TIMIDITY_SOUND_FONT;
SoundFont::new(&mut sf2).unwrap()
};
let sound_font = Arc::new(sound_font);

Expand All @@ -86,7 +83,7 @@ impl AudioPlayer {
let synthesizer_settings = SynthesizerSettings::new(sample_rate as i32);
let synthesizer_settings = Arc::new(synthesizer_settings);
assert_eq!(synthesizer_settings.sample_rate, sample_rate as i32);
Synthesizer::new(&sound_font.clone(), &synthesizer_settings).unwrap()
Synthesizer::new(&sound_font, &synthesizer_settings).unwrap()
}

pub fn is_playing(&self) -> bool {
Expand All @@ -97,7 +94,7 @@ impl AudioPlayer {
self.player_params.lock().unwrap().solo_track_id()
}

pub fn toggle_solo_mode(&mut self, new_track_id: usize) {
pub fn toggle_solo_mode(&self, new_track_id: usize) {
let mut params_guard = self.player_params.lock().unwrap();
if params_guard.solo_track_id() == Some(new_track_id) {
log::info!("Disable solo mode on track {}", new_track_id);
Expand All @@ -108,7 +105,7 @@ impl AudioPlayer {
}
}

pub fn set_tempo_percentage(&mut self, new_tempo_percentage: usize) {
pub fn set_tempo_percentage(&self, new_tempo_percentage: usize) {
let mut params_guard = self.player_params.lock().unwrap();
params_guard.set_tempo_percentage(new_tempo_percentage)
}
Expand Down Expand Up @@ -172,7 +169,7 @@ impl AudioPlayer {
}
}

pub fn focus_measure(&mut self, measure_id: usize) {
pub fn focus_measure(&self, measure_id: usize) {
log::debug!("Focus audio player on measure:{}", measure_id);
let measure = &self.song.measure_headers[measure_id];
let measure_start_tick = measure.start;
Expand Down Expand Up @@ -237,7 +234,7 @@ fn new_output_stream(
let mut synthesizer_guard = synthesizer.lock().unwrap();
if sample_rate != DEFAULT_SAMPLE_RATE {
// audio output is not using the default sample rate - recreate synthesizer with proper sample rate
let new_synthesizer = AudioPlayer::make_synthesizer(sound_font.clone(), sample_rate);
let new_synthesizer = AudioPlayer::make_synthesizer(sound_font, sample_rate);
*synthesizer_guard = new_synthesizer;
}

Expand Down
4 changes: 2 additions & 2 deletions src/audio/midi_sequencer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::audio::midi_event::MidiEvent;
use std::time::Instant;

const QUARTER_TIME: i32 = 960; // 1 quarter note = 960 ticks
const QUARTER_TIME: f64 = 960.0; // 1 quarter note = 960 ticks

pub struct MidiSequencer {
current_tick: usize, // current Midi tick
Expand Down Expand Up @@ -117,7 +117,7 @@ impl MidiSequencer {

fn tick_increase(tempo_bpm: i32, elapsed_seconds: f64) -> usize {
let tempo_bps = tempo_bpm as f64 / 60.0;
let bump = QUARTER_TIME as f64 * tempo_bps * elapsed_seconds;
let bump = QUARTER_TIME * tempo_bps * elapsed_seconds;
bump as usize
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ pub enum RuxError {

impl From<iced::Error> for RuxError {
fn from(error: iced::Error) -> Self {
RuxError::IcedError(error)
Self::IcedError(error)
}
}

impl From<io::Error> for RuxError {
fn from(error: io::Error) -> Self {
RuxError::OtherError(error.to_string())
Self::OtherError(error.to_string())
}
}
6 changes: 3 additions & 3 deletions src/ui/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl RuxApplication {
tablature.focus_on_measure(measure_id);
}
// focus measure in player
if let Some(audio_player) = &mut self.audio_player {
if let Some(audio_player) = &self.audio_player {
audio_player.focus_measure(measure_id);
}
Task::none()
Expand Down Expand Up @@ -308,7 +308,7 @@ impl RuxApplication {
}
}
Message::ToggleSolo => {
if let Some(audio_player) = &mut self.audio_player {
if let Some(audio_player) = &self.audio_player {
let track = self.track_selection.index;
audio_player.toggle_solo_mode(track);
}
Expand All @@ -326,7 +326,7 @@ impl RuxApplication {
Task::none()
}
Message::TempoSelected(tempos_selection) => {
if let Some(audio_player) = &mut self.audio_player {
if let Some(audio_player) = &self.audio_player {
audio_player.set_tempo_percentage(tempos_selection.percentage)
}
self.tempo_selection = tempos_selection;
Expand Down
20 changes: 10 additions & 10 deletions src/ui/canvas_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ use iced::widget::{canvas, Canvas};
use iced::{Color, Element, Length, Point, Rectangle, Renderer, Size, Theme};
use std::rc::Rc;

/// Drawing constants
// Drawing constants

/// Measure label + marker
// Measure label + marker
const MEASURE_ANNOTATION_Y: f32 = 3.0;

/// Chord label level
// Chord label level
const CHORD_ANNOTATION_Y: f32 = 15.0;

/// Note effect level
// Note effect level
const NOTE_EFFECT_ANNOTATION_Y: f32 = 27.0;

/// First string level
// First string level
const FIRST_STRING_Y: f32 = 50.0;

/// Distance between strings
// Distance between strings
const STRING_LINE_HEIGHT: f32 = 13.0;

/// Measure notes padding
// Measure notes padding
const MEASURE_NOTES_PADDING: f32 = 20.0;

/// Length of a beat
// Length of a beat
const BEAT_LENGTH: f32 = 25.0;

/// minimum measure width
// minimum measure width
const MIN_MEASURE_WIDTH: f32 = 60.0;

#[derive(Debug)]
Expand Down Expand Up @@ -449,7 +449,7 @@ fn draw_note(
frame.fill_text(note_effect_text);
}

/// Similar to https://www.tuxguitar.app/files/1.6.0/desktop/help/edit_effects.html
// Similar to `https://www.tuxguitar.app/files/1.6.0/desktop/help/edit_effects.html`
fn above_note_effect_annotation(note_effect: &NoteEffect) -> Vec<String> {
let mut annotations: Vec<String> = vec![];
if note_effect.accentuated_note {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/tablature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl LineTracker {
};
let mut current_line = 1;
let mut horizontal_cursor = 0.0;
for measure in measures.iter() {
for measure in measures {
horizontal_cursor += measure.total_measure_len;
if horizontal_cursor >= tablature_container_width {
current_line += 1;
Expand Down

0 comments on commit 946336c

Please sign in to comment.