Compare commits

..

6 Commits

Author SHA1 Message Date
36c047caf7 note 2026-03-12 15:56:36 -06:00
f4f074b8e7 drop debug 2026-03-12 15:56:04 -06:00
a08246b30f revert 2026-03-12 15:55:51 -06:00
eb1a537ce2 1*ANY for counts since 60 was arbitrary between middleC and 6*0 2026-03-12 15:55:37 -06:00
31faeb3c1b parse all strings and strip comments whether file or not 2026-03-12 15:42:30 -06:00
a3bb1cf11b more debug strings 2026-03-12 15:06:58 -06:00
8 changed files with 112 additions and 35 deletions

View File

@@ -12,29 +12,53 @@ fn from_string(s: String) -> String {
.read_to_string(&mut content)
.expect(format!("failed to read {}", s).as_ref());
content
.split("\n")
.map(|x| x.split_whitespace().collect::<Vec<_>>().join(" "))
.collect::<Vec<_>>()
.join("\n")
}
Err(_) => s,
}
}
fn parse(s: String) -> Vec<String> {
let s = s
.split("\n")
.filter(|x: &&str| !x // doesnt start with #
.split_whitespace()
.collect::<Vec<_>>()
.join("")
.starts_with("#")
)
.map(|x| x
.split("#")
.take(1) // drop after #
.collect::<Vec<_>>()
.join("")
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
)
.collect::<Vec<_>>()
.join("\n");
let mut channels = vec![];
let lines = s.split("\n").collect::<Vec<_>>();
let mut j = 0;
for i in 0..lines.len() {
match lines[i] {
"" => {
j = 0;
},
_ => {
while channels.len() <= j {
channels.push("".to_string());
}
match lines[i] {
"" => { j = 0; },
_ => channels[{ let tmp = j; j += 1; tmp }] += &(" ".to_string() + lines[i]),
channels[{ let tmp = j; j += 1; tmp }] += &(" ".to_string() + lines[i]);
},
};
}
channels.iter().map(|x| x.split_whitespace().collect::<Vec<_>>().join(" ")).collect()
channels
.iter()
.map(|x| x.split_whitespace().collect::<Vec<_>>()
.join(" ")
)
.collect()
}
#[cfg(test)]
@@ -57,7 +81,7 @@ mod test {
fn test_two_channels_one_bar() {
assert_eq!(
super::new("src/testdata/two_channels_one_bar.txt")[0],
"2a 2b 2c".to_string()
"2*a 2*b 2*c".to_string()
);
assert_eq!(
super::new("src/testdata/two_channels_one_bar.txt")[1],
@@ -73,7 +97,23 @@ mod test {
);
assert_eq!(
super::new("src/testdata/two_channels_two_bars.txt")[1],
"2a 2b 2c 2d 2e 2f".to_string()
"2*a 2*b 2*c 2*d 2*e 2*f".to_string()
);
}
#[test]
fn drop_comment_lines() {
assert_eq!(
super::new("# hello\n # world\na b c")[0],
"a b c".to_string()
);
}
#[test]
fn drop_comment_trailer() {
assert_eq!(
super::new("a b c # hello world")[0],
"a b c".to_string()
);
}
}

View File

@@ -117,7 +117,7 @@ impl Seq {
}
fn append_one(&mut self, s: String) {
let re = regex::Regex::new(r"^(?<count>[0-9]*)(?<tone>.*)$").unwrap();
let re = regex::Regex::new(r"^((?<count>[0-9]+)\*)?(?<tone>.*)$").unwrap();
let captures = re.captures(&s).unwrap();
let n = match captures.name("count") {
@@ -125,7 +125,8 @@ impl Seq {
_ => 1,
} as usize;
let tone = tone::new(captures.name("tone").unwrap().as_str());
let tone_s = captures.name("tone").unwrap().as_str();
let tone = tone::new(tone_s);
self.beats.push((n, tone));
}
}
@@ -139,9 +140,9 @@ mod test {
let mut seq = new();
seq.append("c c");
seq.append("4d");
seq.append("2-");
seq.append("g 2e");
seq.append("4*d");
seq.append("2*-");
seq.append("g 2*e");
assert_eq!(seq.beats.len(), 6);
assert_eq!(seq.len(), 11);

9
src/testdata/chord_progressions.txt vendored Normal file
View File

@@ -0,0 +1,9 @@
CM0 CM3 CM4 CM0
.
CM1 CM4 CM0
.
CM0 CM4 CM5 CM3

12
src/testdata/plus_three_sounds_nice.txt vendored Normal file
View File

@@ -0,0 +1,12 @@
#CM0 CM3 CM4 CM0
#CM1 CM4 CM0
#CM0 CM4 CM5 CM3
# 1 4 6 4 # pop101
#GM0 GM3 GM5 GM3
60
63
66
69
72

12
src/testdata/sandbox.txt vendored Normal file
View File

@@ -0,0 +1,12 @@
#CM0 CM3 CM4 CM0
#CM1 CM4 CM0
#CM0 CM4 CM5 CM3
# 1 4 6 4 # pop101
#GM0 GM3 GM5 GM3
60
63
66
69
72

View File

@@ -1,2 +1,2 @@
2a 2b 2c
2*a 2*b 2*c
. a5 . b5 . c5

View File

@@ -1,5 +1,5 @@
a b c
2a 2b 2c
2*a 2*b 2*c
d e f
2d 2e 2f
2*d 2*e 2*f

View File

@@ -84,21 +84,24 @@ impl Tone {
}
pub fn string(&self) -> String {
match self.i32() {
57|69 => "a",
58|70 => "a+",
59|71 => "b",
60|72 => "c",
61|73 => "c+",
62|74 => "d",
63|75 => "d+",
64|76 => "e",
65|77 => "f",
66|78 => "f+",
67|79 => "g",
68|80 => "g+",
let v = self.i32();
let modifier = if v > 0 && v < 57 { "-" } else if v >= 69 { "+" } else { "" };
modifier.to_string() + match v {
45|57|69 => "a",
46|58|70 => "a+",
47|59|71 => "b",
48|60|72 => "c",
49|61|73 => "c+",
50|62|74 => "d",
51|63|75 => "d+",
52|64|76 => "e",
53|65|77 => "f",
54|66|78 => "f+",
55|67|79 => "g",
56|68|80 => "g+",
0 => " ",
_ => "?",
}.to_string()
}
}
}