Skip to content

Commit

Permalink
new parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mikea committed Jan 12, 2025
1 parent 087f879 commit 5504d01
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 67 deletions.
135 changes: 105 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clave"
version = "0.4.9"
version = "0.4.10"
edition = "2021"
description = "Midi click-track generator"
license = "MIT"
Expand All @@ -11,4 +11,4 @@ repository = "https://github.com/mikea/clave"
[dependencies]
clap = { version = "4", features = ["derive"] }
midly = { version = "0" }
regex = { version = "1" }
chumsky = { version = "0"}
3 changes: 3 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ alias w := watch
watch +WATCH_TARGET='run':
watchexec -rc -w . --ignore *.results -- just {{WATCH_TARGET}}

test:
cargo test

run:
cargo run -- --bpm 180 --out target/4_4.mid
cargo run -- --bpm 180 --subs 2 --out target/clave23.mid --pattern "rrcrcrrrcrrcrrcr"
Expand Down
46 changes: 12 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::{fs::File, io::BufWriter, path::PathBuf};

use clap::Parser;
use midly::{Header, TrackEvent};
use regex::Regex;
use pattern::PatternItem;

mod pattern;

#[derive(Parser, Debug)]
struct Args {
Expand Down Expand Up @@ -73,7 +75,7 @@ impl Args {
kind: midly::TrackEventKind::Meta(midly::MetaMessage::EndOfTrack),
};

let pattern = self.parse_pattern();
let pattern = pattern::parse(&self.pattern);

let mut events = vec![set_tempo];
let mut d = 0;
Expand All @@ -88,7 +90,7 @@ impl Args {
channel: channel.into(),
message: midly::MidiMessage::NoteOn {
key: (*key).into(),
vel: (*vel).into(),
vel: self.vel(&vel),
},
},
});
Expand All @@ -110,38 +112,14 @@ impl Args {
midly::write_std(&header, tracks, &mut writer).expect("error writing file");
}
}

fn parse_pattern(&self) -> Vec<PatternItem> {
let re = Regex::new(r"[cmrh][>,]?").expect("bad regexp");
let mut events = vec![];

for cap in re.captures_iter(&self.pattern) {
let cap = cap.get(0).unwrap().as_str();
if cap.starts_with("r") {
events.push(PatternItem::Rest);
continue;
}
let key = match cap.chars().next().unwrap() {
'm' => 32,
'c' => 75,
'h' => 42,
_ => unimplemented!("bad key: {cap:?}"),
};
let vel = if cap.ends_with(">") {
self.acc_vel
} else if cap.ends_with(",") {
self.ghost_vel
} else {
self.vel
};
events.push(PatternItem::Note { key, vel });

fn vel(&self, vel: &pattern::Velocity) -> midly::num::u7 {
match vel {
pattern::Velocity::Default => self.vel.into(),
pattern::Velocity::Accented => self.acc_vel.into(),
pattern::Velocity::Ghost => self.ghost_vel.into(),
}
assert!(!events.is_empty(), "bad pattern: {}", self.pattern);
events

}
}

enum PatternItem {
Rest,
Note { key: u8, vel: u8 },
}
Loading

0 comments on commit 5504d01

Please sign in to comment.