Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
subalterngames committed Dec 7, 2023
1 parent a0e2ce8 commit 45635c5
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 27 deletions.
6 changes: 3 additions & 3 deletions audio/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub struct Program {
/// The name of the preset.
pub preset_name: String,
#[serde(skip)]
pub(crate) chorus: f32,
pub chorus: f32,
#[serde(skip)]
pub(crate) pan: f32,
pub pan: f32,
#[serde(skip)]
pub(crate) reverb: f32,
pub reverb: f32,
}

impl Clone for Program {
Expand Down
6 changes: 3 additions & 3 deletions common/src/effect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod effect_type;
pub use effect_type::EffectType;
pub(crate) mod effect_type;
pub(crate) mod valueless_effect_type;
use serde::{Deserialize, Serialize};

use std::cmp::Ordering;
Expand All @@ -10,7 +10,7 @@ pub struct Effect {
/// The time of the event in PPQ.
pub time: u64,
/// The type of effect.
pub effect: EffectType,
pub effect: effect_type::EffectType,
}

impl Ord for Effect {
Expand Down
59 changes: 51 additions & 8 deletions common/src/effect/effect_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,57 @@ pub enum EffectType {
}

impl EffectType {
/// Returns true if the effect values are valid.
pub fn valid(&self) -> bool {
pub fn increment(&mut self) -> bool {
match self {
Self::Reverb(value) | Self::Chorus(value) => *value < 1000,
Self::Pan(value) => *value >= -500 && *value <= 500,
Self::PitchBend(value) => *value <= 16383,
Self::ChannelPressure(value) => *value <= 127,
Self::PolyphonicKeyPressure { key, value } => *key <= 127 && *value <= 127,
}
Self::Reverb(value) | Self::Chorus(value) | Self::PitchBend(value) => {
if *value > 0 {
*value -= 1;
return true
}
},
Self::Pan(value) => {
if *value > -500 {
*value -= 1;
return true
}
}
Self::ChannelPressure(value) | Self::PolyphonicKeyPressure { key: _, value } => {
if *value > 0 {
*value -= 1;
return true
}
}
}
false
}

pub fn decrement(&mut self) -> bool {
match self {
Self::Reverb(value) | Self::Chorus(value) => {
if *value < 1000 {
*value += 1;
return true
}
},
Self::Pan(value) => {
if *value < 500 {
*value += 1;
return true
}
}
Self::PitchBend(value) => {
if *value < 16383 {
*value += 1;
return true
}
}
Self::ChannelPressure(value) | Self::PolyphonicKeyPressure { key: _, value } => {
if *value < 127 {
*value += 1;
return true
}
}
}
false
}
}
40 changes: 40 additions & 0 deletions common/src/effect/valueless_effect_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use serde::{Deserialize, Serialize};
use super::effect_type::EffectType;

/// A hashable EffectType.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub enum ValuelessEffectType {
#[default]
Chorus,
Reverb,
Pan,
PitchBend,
ChannelPressure,
PolyphonicKeyPressure,
}

impl From<EffectType> for ValuelessEffectType {
fn from(value: EffectType) -> Self {
match value {
EffectType::Chorus(_) => Self::Chorus,
EffectType::Reverb(_) => Self::Reverb,
EffectType::Pan(_) => Self::Pan,
EffectType::PitchBend(_) => Self::PitchBend,
EffectType::ChannelPressure(_) => Self::ChannelPressure,
EffectType::PolyphonicKeyPressure { key: _, value: _ } => Self::PolyphonicKeyPressure,
}
}
}

impl ValuelessEffectType {
pub fn eq(&self, value: &EffectType) -> bool {
match value {
EffectType::Chorus(_) => *self == Self::Chorus,
EffectType::Reverb(_) => *self == Self::Reverb,
EffectType::Pan(_) => *self == Self::Pan,
EffectType::PitchBend(_) => *self == Self::PitchBend,
EffectType::ChannelPressure(_) => *self == Self::ChannelPressure,
EffectType::PolyphonicKeyPressure { key: _, value: _ } => *self == Self::PolyphonicKeyPressure,
}
}
}
4 changes: 2 additions & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ mod paths_state;
mod state;
pub mod time;
pub mod view;
pub use effect::{Effect, EffectType};
pub use effect::{Effect, effect_type::EffectType, valueless_effect_type::ValuelessEffectType};
pub use event::Event;
pub use index::Index;
mod indexed_values;
pub use indexed_values::IndexedValues;
pub use input_state::InputState;
pub use midi_track::MidiTrack;
pub use music::*;
pub use note::{Note, MAX_NOTE, MIN_NOTE, NOTE_NAMES};
pub use note::{Note, MAX_NOTE, MIN_NOTE, NOTE_NAMES, MIDDLE_C};
pub use panel_type::PanelType;
pub use paths::Paths;
pub use state::State;
Expand Down
1 change: 1 addition & 0 deletions common/src/panel_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pub enum PanelType {
ExportSettings,
Quit,
Links,
Effects,
}
14 changes: 14 additions & 0 deletions common/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ValuelessEffectType;
use crate::music_panel_field::MusicPanelField;
use crate::{
EditMode, Index, IndexedEditModes, IndexedValues, InputState, Music, PanelType, PianoRollMode,
Expand Down Expand Up @@ -30,6 +31,7 @@ pub struct State {
pub edit_mode: IndexedEditModes,
/// The current selection.
pub selection: Selection,
pub effect_types: IndexedValues<ValuelessEffectType, 6>,
/// If true, there are unsaved changes.
#[serde(skip_serializing, skip_deserializing)]
pub unsaved_changes: bool,
Expand All @@ -54,6 +56,17 @@ impl State {
let piano_roll_mode = PianoRollMode::Time;
let edit_mode = EditMode::indexed();
let selection = Selection::default();
let effect_types = IndexedValues::new(
0,
[
ValuelessEffectType::Chorus,
ValuelessEffectType::Pan,
ValuelessEffectType::Reverb,
ValuelessEffectType::PitchBend,
ValuelessEffectType::ChannelPressure,
ValuelessEffectType::PolyphonicKeyPressure,
],
);
Self {
music,
view,
Expand All @@ -65,6 +78,7 @@ impl State {
piano_roll_mode,
edit_mode,
selection,
effect_types,
unsaved_changes: false,
}
}
Expand Down
7 changes: 7 additions & 0 deletions data/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ DiscordUrl = {"keys": ["2"]}
GitHubUrl = {"keys": ["3"]}
CloseLinksPanel = {"keys": ["Escape"]}

# Effects.
NextEffect = {"keys": ["Down"], "dt": 10}
PreviousEffect = {"keys": ["Up"], "dt": 10}
IncrementEffectValue = {"keys": ["Right"], "dt": 2}
DecrementEffectValue = {"keys": ["Left"], "dt": 2}
DeleteEffect = {"keys": ["Delete"]}

# Qwerty note input.
C = {"keys": ["A"]}
CSharp = {"keys": ["Q"]}
Expand Down
6 changes: 6 additions & 0 deletions input/src/input_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ pub enum InputEvent {
DiscordUrl,
GitHubUrl,
CloseLinksPanel,
// Effects panel.
NextEffect,
PreviousEffect,
IncrementEffectValue,
DecrementEffectValue,
DeleteEffect,
// Qwerty note input.
C,
CSharp,
Expand Down
115 changes: 115 additions & 0 deletions io/src/effects_panel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use crate::panel::*;
use common::{EffectType, Effect, ValuelessEffectType, MIDDLE_C};
use text::EFFECT_NAME_KEYS;

#[derive(Default)]
pub(crate) struct EffectsPanel {
}

impl EffectsPanel {
/// Increment or decrement the effect type index.
fn cycle_effect_type(state: &mut State, up: bool) -> Option<Snapshot> {
let s0 = state.clone();
state.effect_types.index.increment(up);
Some(Snapshot::from_states(s0, state))
}

fn increment_effect_value(state: &mut State, conn: &Conn, up: bool) -> Option<Snapshot> {
let s0 = state.clone();
let ve = state.effect_types.get();
match state.music.get_selected_track_mut() {
Some(track) => match track.effects.iter_mut().filter(|e| e.time == state.time.cursor && ve.eq(&e.effect)).next() {
Some(effect) => {
if (up && effect.effect.increment()) || (!up && effect.effect.decrement()) {
Some(Snapshot::from_states(s0, state))
}
else {
None
}
}
// Add a new effect.
None => {
let program = &conn.state.programs[&track.channel];
// Get a new effect type.
let effect_type = match ve {
ValuelessEffectType::Chorus => EffectType::Chorus(program.chorus as u16),
ValuelessEffectType::Pan => EffectType::Pan(program.pan as i16),
ValuelessEffectType::Reverb => EffectType::Reverb(program.reverb as u16),
ValuelessEffectType::PitchBend => EffectType::PitchBend(0),
ValuelessEffectType::ChannelPressure => EffectType::ChannelPressure(0),
ValuelessEffectType::PolyphonicKeyPressure => EffectType::PolyphonicKeyPressure { key: MIDDLE_C, value: 0 }
};
// Get a new effect.
track.effects.push(Effect { time: state.time.cursor, effect: effect_type });
Some(Snapshot::from_states(s0, state))
}
}
None => None
}
}
}

impl Panel for EffectsPanel {
fn update(
&mut self,
state: &mut State,
conn: &mut Conn,
input: &Input,
tts: &mut TTS,
text: &Text,
_: &mut PathsState,
) -> Option<Snapshot> {
// Cycle the selected input event.
if input.happened(&InputEvent::NextEffect) {
Self::cycle_effect_type(state, true)
}
else if input.happened(&InputEvent::PreviousEffect) {
Self::cycle_effect_type(state, false)
}
else if input.happened(&InputEvent::IncrementEffectValue) {
Self::increment_effect_value(state, conn, true)
}
else if input.happened(&InputEvent::DecrementEffectValue) {
Self::increment_effect_value(state, conn, false)
}
else if input.happened(&InputEvent::DeleteEffect) {
let s0 = state.clone();
let ve = state.effect_types.get();
match state.music.get_selected_track_mut() {
Some(track) => {
let has_effect = track.effects.iter().filter(|e| e.time == state.time.cursor && ve.eq(&e.effect)).next().is_some();
track.effects.retain(|e| e.time != state.time.cursor || !ve.eq(&e.effect));
if has_effect {
Some(Snapshot::from_states(s0, state))
}
else {
None
}
}
None => None
}
}
else {
None
}
}

fn on_disable_abc123(&mut self, _: &mut State, _: &mut Conn) {}

fn update_abc123(
&mut self,
_: &mut State,
_: &Input,
_: &mut Conn,
) -> (Option<Snapshot>, bool) {
(None, false)
}

fn allow_alphanumeric_input(&self, _: &State, _: &Conn) -> bool {
false
}

fn allow_play_music(&self) -> bool {
true
}
}
1 change: 1 addition & 0 deletions io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod abc123;
mod export_settings_panel;
mod quit_panel;
use quit_panel::QuitPanel;
mod effects_panel;
mod links_panel;
mod popup;
use links_panel::LinksPanel;
Expand Down
Loading

0 comments on commit 45635c5

Please sign in to comment.