Compare commits

..

6 Commits

Author SHA1 Message Date
Bel LaPointe
251ae7f308 boring 2026-03-13 13:23:21 -06:00
Bel LaPointe
5b9d3a8bb7 tone accepts T,t,f for majorThird, minorThird, perfectFifth lazily 2026-03-13 13:01:05 -06:00
Bel LaPointe
fedd6abbff notes 2026-03-13 12:43:20 -06:00
Bel LaPointe
7f0b55ac13 add chord references 2026-03-13 12:25:37 -06:00
Bel LaPointe
fb44f680e6 accept -i for interactive 2026-03-13 12:06:07 -06:00
Bel LaPointe
c6a6d59f1c SHELL 2026-03-13 11:58:10 -06:00
13 changed files with 256 additions and 37 deletions

View File

@@ -5,6 +5,9 @@ pub struct Flags {
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
pub debug: bool, pub debug: bool,
#[arg(short, long, default_value_t = false)]
pub interactive: bool,
#[arg(long, default_value_t = 120)] #[arg(long, default_value_t = 120)]
pub bpm: usize, pub bpm: usize,

View File

@@ -2,27 +2,32 @@ use io_prompt_prototype::prompt;
use itertools::Itertools; use itertools::Itertools;
mod flags; mod flags;
mod play; mod parse;
mod player;
mod seq; mod seq;
mod syn; mod syn;
mod tone; mod tone;
fn main() { fn main() {
let flags = flags::Flags::new(); let mut flags = flags::Flags::new();
if flags.debug { if flags.debug {
eprintln!("{:?}", flags); eprintln!("{:?}", flags);
} }
if flags.interactive {
flags.play = vec![];
}
let once = flags.play.iter().filter(|x| x.len() > 0).count() > 0; let once = flags.play.iter().filter(|x| x.len() > 0).count() > 0;
let mut player = player::new(flags.sample_rate.clone(), flags.bpm.clone());
loop { loop {
play_with_flags(&flags); play_with_flags(&flags, &mut player);
if once { if once {
break; break;
} }
} }
} }
fn play_with_flags(flags: &flags::Flags) { fn play_with_flags(flags: &flags::Flags, player: &mut player::Player) {
let mut syn_seq = seq::new_syn(syn::Syn::new( let mut syn_seq = seq::new_syn(syn::Syn::new(
flags.debug.clone(), flags.debug.clone(),
flags.sound_font.clone(), flags.sound_font.clone(),
@@ -31,7 +36,7 @@ fn play_with_flags(flags: &flags::Flags) {
let mut i = 0; let mut i = 0;
for p in flags.play.iter() { for p in flags.play.iter() {
for p in play::new(p.clone()) { for p in parse::new(p.clone()) {
syn_seq.append(i as i32, p); syn_seq.append(i as i32, p);
i += 1; i += 1;
} }
@@ -60,41 +65,14 @@ fn play_with_flags(flags: &flags::Flags) {
}; };
} }
for p in play::new(payload.join("\n")) { for p in parse::new(payload.join("\n")) {
syn_seq.append(i as i32, p); syn_seq.append(i as i32, p);
i += 1; i += 1;
} }
} }
play(syn_seq, flags.sample_rate, flags.bpm, sync); match sync {
} true => player.play(syn_seq),
false => player.play_async(syn_seq),
fn play(mut s: seq::SynSeq, sample_rate: usize, bpm: usize, sync: bool) {
let samples_per_beat = sample_rate / bpm * 60;
let params = tinyaudio::prelude::OutputDeviceParameters {
channels_count: 2,
sample_rate: sample_rate,
channel_sample_count: samples_per_beat,
}; };
let beats = s.beats() + 3;
let duration = 60 * beats / bpm;
let sample_count = (params.channel_sample_count) as usize;
let mut left: Vec<f32> = vec![0_f32; sample_count];
let mut right: Vec<f32> = vec![0_f32; sample_count];
let _device = tinyaudio::prelude::run_output_device(params, {
move |data| {
s.render(&mut left[..], &mut right[..]); // put in a state of rendering the next loop of these notes
for (i, value) in left.iter().interleave(right.iter()).enumerate() {
data[i] = *value;
}
}
})
.unwrap();
if sync {
// Wait it out
std::thread::sleep(std::time::Duration::from_secs(duration as u64));
}
} }

61
src/player.rs Normal file
View File

@@ -0,0 +1,61 @@
use itertools::Itertools;
use crate::seq;
pub struct Player {
bpm: usize,
params: tinyaudio::OutputDeviceParameters,
device: Option<tinyaudio::OutputDevice>,
}
pub fn new(sample_rate: usize, bpm: usize) -> Player {
let samples_per_beat = sample_rate / bpm * 60;
Player {
bpm: bpm,
params: tinyaudio::prelude::OutputDeviceParameters {
channels_count: 2,
sample_rate: sample_rate,
channel_sample_count: samples_per_beat,
},
device: None,
}
}
impl Player {
pub fn play(&mut self, s: seq::SynSeq) {
self.play_with_sync(s, true);
}
pub fn play_async(&mut self, s: seq::SynSeq) {
self.play_with_sync(s, false);
}
fn play_with_sync(&mut self, mut s: seq::SynSeq, sync: bool) {
let sample_count = (self.params.channel_sample_count) as usize;
let mut left: Vec<f32> = vec![0_f32; sample_count];
let mut right: Vec<f32> = vec![0_f32; sample_count];
let beats = s.beats() + 3;
let duration = 60 * beats / self.bpm;
if self.device.is_some() {
self.device.as_mut().expect("lost device").close();
}
self.device = Some(
tinyaudio::prelude::run_output_device(self.params, {
move |data| {
s.render(&mut left[..], &mut right[..]); // put in a state of rendering the next loop of these notes
for (i, value) in left.iter().interleave(right.iter()).enumerate() {
data[i] = *value;
}
}
})
.unwrap(),
);
if sync {
// Wait it out
std::thread::sleep(std::time::Duration::from_secs(duration as u64));
}
}
}

View File

@@ -0,0 +1,9 @@
# https://www.musicnotes.com/sheetmusic/chrissy-ricker/battle-against-a-true-hero-easy/MN0235808
# c major
CM4 CM5 CM5 CM1 CM1 CM2 . .
CM3 CM0 CM0 CM4 CM4 . . .
CM4 CM5 CM5 CM1 CM1 CM2 . .

View File

@@ -1,9 +1,12 @@
# x+0 x+5 x+7 x+0
CM0 CM3 CM4 CM0 CM0 CM3 CM4 CM0
. .
# x+2 x+7 x+0
CM1 CM4 CM0 CM1 CM4 CM0
. .
# x+0 x+7 x+9 x+5
CM0 CM4 CM5 CM3 CM0 CM4 CM5 CM3

5
src/testdata/fifth_wheel.txt vendored Normal file
View File

@@ -0,0 +1,5 @@
# fifth wheel = +7 semitones
# f -> c -> g -> d -> a -> e -> b
f c g d a e b
ff cf gf df af ef bf

View File

@@ -0,0 +1,19 @@
CM0 CM3 CM4 CM0
CM0T CM3T CM4T CM0T
CM0f CM3f CM4f CM0f
.
.
.
CM1 CM4 CM0
CM1T CM4T CM0T
CM1f CM4f CM0f
.
.
.
CM0 CM4 CM5 CM3
CM0T CM4T CM5T CM3T
CM0f CM4f CM5f CM3f

27
src/testdata/major_chords.txt vendored Normal file
View File

@@ -0,0 +1,27 @@
# major CHORD = root + major_third + perfect_fifth
# c = c,e,g
f
a
c
c
e
g
g
b
d
d
f+
a
a
c+
e
e
g+
b

18
src/testdata/major_third.txt vendored Normal file
View File

@@ -0,0 +1,18 @@
# major third
# +2 letters (the third letter) with exactly 2 sharps between
# c,e c+,f d,f+
c c+ d
cT c+T dT
# d+,g e,g+ f,a
d+ e f
d+T eT fT
# f+,a+ g,b g+,c
f+ g g+
f+T gT g+T
# a,c+ a+,d b,d+
a a+ b
aT a+T bT

27
src/testdata/minor_chords.txt vendored Normal file
View File

@@ -0,0 +1,27 @@
# minor CHORD = root + minor_third + perfect_fifth
# c = c,e-,g
f
g+
c
c
e-
g
g
a+
d
d
f
a
a
c
e
e
g
b

18
src/testdata/minor_third.txt vendored Normal file
View File

@@ -0,0 +1,18 @@
# minor third
# +2 letters (the third letter) with exactly 1 half step between
# c,e- c+,e d,f
c c+ d
ct c+t dt
# d+,f+ e,g f,g+
d+ e f
d+t et ft
# f+,a g,a+ g+,b
f+ g g+
f+t gt g+t
# a,c a+,c+ b,d
a a+ b
at a+t bt

View File

@@ -8,7 +8,7 @@ pub fn new<S: ToString>(s: S) -> Tone {
impl Tone { impl Tone {
fn new(s: String) -> Tone { fn new(s: String) -> Tone {
let re = regex::Regex::new(r"^(((?<letter>^[a-g])(?<sharpness>[+-]?)|(?<major>[A-G]M[0-6])|(?<minor>[A-G]m[0-6]))(?<octave>[1-5]?)|(?<numeric>[0-9]+)|(?<rest>[^a-gA-G0-9]))$").unwrap(); let re = regex::Regex::new(r"^(((?<letter>^[a-g])(?<sharpness>[+-]?)|(?<major>[A-G]M[0-6])|(?<minor>[A-G]m[0-6]))(?<octave>[1-5]?)((?<major_third>T)|(?<minor_third>t)|(?<fifth>f))?|(?<numeric>[0-9]+)|(?<rest>[^a-gA-G0-9]))$").unwrap();
let captures = re let captures = re
.captures(&s) .captures(&s)
.expect(format!("tone '{}' does not match regex", s).as_ref()); .expect(format!("tone '{}' does not match regex", s).as_ref());
@@ -65,6 +65,21 @@ impl Tone {
None => 0, None => 0,
} as i32; } as i32;
result += match captures.name("major_third") {
Some(_) => 4, // TODO not all are good
None => 0,
} as i32;
result += match captures.name("minor_third") {
Some(_) => 3, // TODO not all are good
None => 0,
} as i32;
result += match captures.name("fifth") {
Some(_) => 7,
None => 0,
} as i32;
result result
} }
}, },
@@ -144,6 +159,42 @@ mod test {
assert_eq!(super::new("c+4").i32(), 60 + 12 + 1); assert_eq!(super::new("c+4").i32(), 60 + 12 + 1);
} }
#[test]
fn test_tone_new_major_third() {
assert_eq!(super::new("cT").i32(), 60 + 4);
assert_eq!(super::new("c1T").i32(), 60 + 4 - 24);
assert_eq!(super::new("c+T").i32(), 60 + 1 + 4);
assert_eq!(super::new("c+1T").i32(), 60 + 1 + 4 - 24);
assert_eq!(super::new("CM0T").i32(), 60 + 4);
assert_eq!(super::new("CM01T").i32(), 60 + 4 - 24);
}
#[test]
fn test_tone_new_minor_third() {
assert_eq!(super::new("ct").i32(), 60 + 3);
assert_eq!(super::new("c1t").i32(), 60 + 3 - 24);
assert_eq!(super::new("c+t").i32(), 60 + 1 + 3);
assert_eq!(super::new("c+1t").i32(), 60 + 1 + 3 - 24);
assert_eq!(super::new("CM0t").i32(), 60 + 3);
assert_eq!(super::new("CM01t").i32(), 60 + 3 - 24);
}
#[test]
fn test_tone_new_fifth() {
assert_eq!(super::new("cf").i32(), 60 + 7);
assert_eq!(super::new("c1f").i32(), 60 + 7 - 24);
assert_eq!(super::new("c+f").i32(), 60 + 1 + 7);
assert_eq!(super::new("c+1f").i32(), 60 + 1 + 7 - 24);
assert_eq!(super::new("CM0f").i32(), 60 + 7);
assert_eq!(super::new("CM01f").i32(), 60 + 7 - 24);
}
#[test] #[test]
fn test_tone_c_major() { fn test_tone_c_major() {
assert_eq!(super::new("CM0"), super::new("c")); assert_eq!(super::new("CM0"), super::new("c"));