Skip to content

Commit

Permalink
compiles. fixed clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
subalterngames committed Dec 31, 2023
1 parent 149834b commit d77fdcc
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 200 deletions.
15 changes: 11 additions & 4 deletions audio/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,23 @@ impl Conn {
let mut end_time = 0;
for track in tracks.iter() {
let notes = track.get_playback_notes(state.time.playback);
let track_effects = track
.get_audio_effects();
let mut effects = track_effects.iter().filter(|e| e.time >= state.time.playback).copied().collect::<Vec<Effect>>();
let track_effects = track.get_audio_effects();
let mut effects = track_effects
.iter()
.filter(|e| e.time >= state.time.playback)
.copied()
.collect::<Vec<Effect>>();
effects.sort();
// Set the track's effect values to the last values up until playback time.
let program = &self.state.programs[&track.channel];
let mut chorus = program.chorus;
let mut pan = program.pan;
let mut reverb = program.reverb;
let mut prior_effects = track_effects.iter().filter(|e| e.time < state.time.playback).copied().collect::<Vec<Effect>>();
let mut prior_effects = track_effects
.iter()
.filter(|e| e.time < state.time.playback)
.copied()
.collect::<Vec<Effect>>();
prior_effects.sort();
for effect in prior_effects {
match effect.effect {
Expand Down
14 changes: 14 additions & 0 deletions common/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};

use std::cmp::Ordering;

use crate::EffectType;

/// A timed synthesizer effect.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct Effect {
Expand All @@ -12,6 +14,18 @@ pub struct Effect {
pub effect: effect_type::EffectType,
}

impl Effect {
/// Returns true if the effect is at, or is ongoing during, time `time` (in PPQ).
pub fn at_time(&self, time: u64) -> bool {
match &self.effect {
EffectType::PitchBend { value: _, duration } => {
self.time == time || (self.time < time && self.time + duration >= time)
}
_ => self.time == time,
}
}
}

impl Ord for Effect {
fn cmp(&self, other: &Self) -> Ordering {
self.time.cmp(&other.time)
Expand Down
71 changes: 43 additions & 28 deletions common/src/effect/effect_type.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::MIDDLE_C;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use strum::EnumCount;
use strum_macros::EnumCount as EnumCountMacro;
use std::hash::{Hash, Hasher};


pub const MAX_PITCH_BEND: u16 = 16383;

Expand Down Expand Up @@ -41,9 +40,15 @@ impl EffectType {
EffectType::Chorus(500),
EffectType::Reverb(500),
EffectType::Pan(0),
EffectType::PitchBend { value: 0, duration: 0},
EffectType::PitchBend {
value: 0,
duration: 0,
},
EffectType::ChannelPressure(0),
EffectType::PolyphonicKeyPressure { key: MIDDLE_C,value: 0}
EffectType::PolyphonicKeyPressure {
key: MIDDLE_C,
value: 0,
},
]
}

Expand All @@ -55,11 +60,9 @@ impl EffectType {
*value += 1;
return true;
}
} else {
if *value > 0 {
*value -= 1;
return true;
}
} else if *value > 0 {
*value -= 1;
return true;
}
}
Self::Pan(value) => {
Expand All @@ -68,11 +71,9 @@ impl EffectType {
*value += 1;
return true;
}
} else {
if *value > -500 {
*value -= 1;
return true;
}
} else if *value > -500 {
*value -= 1;
return true;
}
}
Self::PitchBend { value, duration: _ } => {
Expand All @@ -81,11 +82,9 @@ impl EffectType {
*value += 1;
return true;
}
} else {
if *value > 0 {
*value -= 1;
return true;
}
} else if *value > 0 {
*value -= 1;
return true;
}
}
Self::ChannelPressure(value) | Self::PolyphonicKeyPressure { key: _, value } => {
Expand All @@ -94,11 +93,9 @@ impl EffectType {
*value += 1;
return true;
}
} else {
if *value > 0 {
*value -= 1;
return true;
}
} else if *value > 0 {
*value -= 1;
return true;
}
}
}
Expand All @@ -111,10 +108,28 @@ impl EffectType {
}

/// Source: https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=21e3ab42f76ccbc05b6b61560cbd29ec
fn get_ordinal(&self) -> u8 {
pub fn get_ordinal(&self) -> u8 {
let ptr_to_option = (self as *const Self) as *const u8;
unsafe {
*ptr_to_option
unsafe { *ptr_to_option }
}

pub fn get_value_string(&self) -> String {
match self {
Self::Chorus(value) | Self::Reverb(value) | Self::PitchBend { value, duration: _ } => {
value.to_string()
}
Self::Pan(value) => value.to_string(),
Self::ChannelPressure(value) | Self::PolyphonicKeyPressure { key: _, value } => {
value.to_string()
}
}
}

pub fn get_secondary_value(&self) -> Option<String> {
match self {
Self::PitchBend { value: _, duration } => Some(duration.to_string()),
Self::PolyphonicKeyPressure { key, value: _ } => Some(key.to_string()),
_ => None,
}
}
}
Expand All @@ -129,4 +144,4 @@ impl Hash for EffectType {
fn hash<H: Hasher>(&self, state: &mut H) {
self.get_ordinal().hash(state)
}
}
}
5 changes: 4 additions & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ mod paths_state;
mod state;
pub mod time;
pub mod view;
pub use effect::{effect_type::{EffectType, MAX_PITCH_BEND}, Effect};
pub use effect::{
effect_type::{EffectType, MAX_PITCH_BEND},
Effect,
};
pub use event::Event;
pub use index::Index;
mod indexed_values;
Expand Down
14 changes: 10 additions & 4 deletions common/src/midi_track.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Effect, EffectType, Note, MAX_VOLUME, MAX_PITCH_BEND};
use crate::{Effect, EffectType, Note, MAX_PITCH_BEND, MAX_VOLUME};
use serde::{Deserialize, Serialize};

/// A MIDI track has some notes.
Expand Down Expand Up @@ -59,15 +59,21 @@ impl MidiTrack {
let mut effects = vec![];
for effect in self.effects.iter() {
// Lerp the pitch bends.
if let EffectType::PitchBend { value, duration} = effect.effect {
if let EffectType::PitchBend { value, duration } = effect.effect {
// Prevent a divide by zero error.
if duration == 0 {
continue;
}
let dv = (value as f64 / duration as f64) as u16;
let mut pitch_value = 0;
for t in 0..duration {
effects.push(Effect { time: effect.time + t, effect: EffectType::PitchBend { value: pitch_value, duration: 1 }});
effects.push(Effect {
time: effect.time + t,
effect: EffectType::PitchBend {
value: pitch_value,
duration: 1,
},
});
pitch_value += dv;
if pitch_value > MAX_PITCH_BEND {
break;
Expand All @@ -76,7 +82,7 @@ impl MidiTrack {
}
// Add the effect.
else {
effects.push(effect.clone());
effects.push(*effect);
}
}
effects.sort();
Expand Down
9 changes: 3 additions & 6 deletions common/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::music_panel_field::MusicPanelField;
use crate::{
EditMode, EffectType, Index, IndexedEditModes, IndexedValues, InputState, Music, PanelType, PianoRollMode,
Selection, Time, View,
EditMode, EffectType, Index, IndexedEditModes, IndexedValues, InputState, Music, PanelType,
PianoRollMode, Selection, Time, View,
};
use ini::Ini;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -56,10 +56,7 @@ impl State {
let piano_roll_mode = PianoRollMode::Time;
let edit_mode = EditMode::indexed();
let selection = Selection::default();
let effect_types = IndexedValues::new(
0,
EffectType::get_array()
);
let effect_types = IndexedValues::new(0, EffectType::get_array());
Self {
music,
view,
Expand Down
1 change: 1 addition & 0 deletions data/text.csv
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ TITLE_EXPORT_STATE,Exporting...
TITLE_EXPORT_SETTINGS,Settings
TITLE_QUIT,Really quit?
TITLE_LINKS,Open a link in your browser
TITLE_EFFECTS,Effects
TITLE_BPM,BPM
TITLE_GAIN,Gain
MAIN_MENU_HELP,Help:
Expand Down
Loading

0 comments on commit d77fdcc

Please sign in to comment.