Compare commits

...

6 Commits

Author SHA1 Message Date
acdac24d1a seq maintains state, pop returns changed, none is stop singing 2026-03-11 13:33:56 -06:00
f50a435200 default to faster 2026-03-11 13:13:48 -06:00
7524ca3192 accept notes via --play or -p 2026-03-11 13:12:57 -06:00
85b189fad2 IT PLAYS A SINGLE SET OF NOTES 2026-03-11 13:11:44 -06:00
63f96b2d5f time for strings 2026-03-11 13:06:07 -06:00
5f149414b2 duration is beats not repeat 2026-03-11 13:04:18 -06:00
4 changed files with 132 additions and 102 deletions

View File

@@ -5,7 +5,7 @@ pub struct Flags {
#[arg(short, long, default_value_t = false)]
pub debug: bool,
#[arg(long, default_value_t = 60)]
#[arg(long, default_value_t = 120)]
pub bpm: usize,
#[arg(long, default_value_t = 44100)]
@@ -13,6 +13,9 @@ pub struct Flags {
#[arg(long, default_value = "super_small_font.sf2")]
pub sound_font: String,
#[arg(short, long, default_value = "c 2e+")]
pub play: Option<String>,
}
impl Flags {

View File

@@ -13,10 +13,9 @@ fn main() {
flags.sound_font,
flags.sample_rate,
));
syn_seq.append("c");
syn_seq.append("2e");
syn_seq.append("g");
//syn.tone_on(0, tone::new("c+5"));
if let Some(play) = flags.play {
syn_seq.append(play);
}
play(syn_seq, flags.sample_rate, flags.bpm);
}

View File

@@ -19,9 +19,15 @@ impl SynSeq {
}
pub fn render(&mut self, left: &mut [f32], right: &mut [f32]) {
if let Some(tone) = self.seq.pop() {
self.syn.set(tone);
}
match self.seq.pop() {
(Some(tone), changed) if changed => {
self.syn.set(0, Some(tone.clone()));
},
(None, changed) if changed => {
self.syn.set(0, None);
},
_ => {},
};
self.syn.render(left, right);
}
@@ -33,9 +39,10 @@ impl SynSeq {
#[derive(PartialEq)]
pub struct Seq {
beats: Vec<(i32, tone::Tone)>,
state: Option<tone::Tone>,
}
pub fn new() -> Seq {
fn new() -> Seq {
Seq::new()
}
@@ -43,10 +50,20 @@ impl Seq {
fn new() -> Seq {
Seq{
beats: vec![],
state: None,
}
}
pub fn pop(&mut self) -> Option<tone::Tone> {
fn pop(&mut self) -> (Option<tone::Tone>, bool) {
let state_before = self.state.clone();
let tone_after = self._pop();
if state_before != tone_after {
self.state = tone_after.clone();
}
(tone_after, self.state != state_before)
}
fn _pop(&mut self) -> Option<tone::Tone> {
match self.beats.len() {
0 => None,
_ => match self.beats[0].0 {
@@ -59,8 +76,12 @@ impl Seq {
}
}
pub fn append<S: ToString>(&mut self, s: S) {
self.append_one(s.to_string());
fn append<S: ToString>(&mut self, s: S) {
let s: String = s.to_string();
let s: &str = s.as_ref();
for split in s.split_whitespace() {
self.append_one(split.to_string());
}
}
fn append_one(&mut self, s: String) {
@@ -86,14 +107,22 @@ mod test {
let mut seq = new();
seq.append("c");
seq.append("4d");
assert_eq!(seq.beats.len(), 2);
seq.append("g 2e");
assert_eq!(seq.beats.len(), 4);
assert_eq!(seq.beats[0], (1 as i32, tone::new("c")));
assert_eq!(seq.beats[1], (4 as i32, tone::new("d")));
assert_eq!(seq.beats[2], (1 as i32, tone::new("g")));
assert_eq!(seq.beats[3], (2 as i32, tone::new("e")));
assert_eq!(seq.pop(), Some(tone::new("c")));
for _ in 0..4 {
assert_eq!(seq.pop(), Some(tone::new("d")));
assert_eq!(seq.pop(), (Some(tone::new("c")), true));
assert_eq!(seq.pop(), (Some(tone::new("d")), true));
for _ in 1..4 {
assert_eq!(seq.pop(), (Some(tone::new("d")), false));
}
assert_eq!(seq.pop(), None);
assert_eq!(seq.pop(), (Some(tone::new("g")), true));
assert_eq!(seq.pop(), (Some(tone::new("e")), true));
assert_eq!(seq.pop(), (Some(tone::new("e")), false));
assert_eq!(seq.pop(), (None, true));
assert_eq!(seq.pop(), (None, false));
}
}

View File

@@ -31,46 +31,45 @@ impl Syn {
Syn::Real(synthesizer)
}
pub fn set(&mut self, b: tone::Tone) {
let a = 0 as i32;
pub fn set(&mut self, ch: i32, b: Option<tone::Tone>) {
match self {
// channel=[0..16)
// velocity=[0..128)
Syn::Real(syn) => {
syn.note_off_all_channel(a, false);
syn.note_off_all_channel(ch, false);
},
Syn::Text{m, ..} => {
m.clear();
m.remove(&ch);
},
};
self.tone_on(b);
if let Some(tone) = b {
self.tone_on(ch, tone);
};
}
fn tone_on(&mut self, b: tone::Tone) {
let a = 0 as i32;
fn tone_on(&mut self, ch: i32, b: tone::Tone) {
match self {
// channel=[0..16)
// velocity=[0..128)
Syn::Real(syn) => syn.note_on(a, b.i32(), 127),
Syn::Real(syn) => syn.note_on(ch, b.i32(), 127),
Syn::Text{m, ..} => {
match m.get_mut(&a) {
match m.get_mut(&ch) {
Some(m2) => { m2.insert(b.i32(), 127); },
None => {
let mut m2 = std::collections::HashMap::new();
m2.insert(b.i32(), 127);
m.insert(a, m2);
m.insert(ch, m2);
},
};
},
};
}
fn tone_off(&mut self, b: tone::Tone) {
let a = 0 as i32;
fn tone_off(&mut self, ch: i32, b: tone::Tone) {
match self {
Syn::Real(syn) => syn.note_off(a, b.i32()),
Syn::Real(syn) => syn.note_off(ch, b.i32()),
Syn::Text{m, ..} => {
match m.get_mut(&a) {
match m.get_mut(&ch) {
Some(m) => { m.remove(&b.i32()); },
None => {},
};
@@ -98,10 +97,10 @@ mod test {
fn test_new_real() {
let mut syn = Syn::new(false, "super_small_font.sf2".to_string(), 44100);
syn.tone_on(tone::new("c"));
syn.tone_on(tone::new("d"));
syn.tone_on(0, tone::new("c"));
syn.tone_on(0, tone::new("d"));
syn.tone_off(tone::new("d"));
syn.tone_off(0, tone::new("d"));
let mut buffer1 = Vec::<f32>::new();
let mut buffer2 = Vec::<f32>::new();
@@ -112,10 +111,10 @@ mod test {
fn test_text() {
let mut syn = Syn::new(true, ".sf2".to_string(), 1);
syn.tone_on(tone::new("c"));
syn.tone_on(tone::new("d"));
syn.tone_on(0, tone::new("c"));
syn.tone_on(0, tone::new("d"));
syn.tone_off(tone::new("d"));
syn.tone_off(0, tone::new("d"));
let mut buffer1 = Vec::<f32>::new();
let mut buffer2 = Vec::<f32>::new();