seq maintains state, pop returns changed, none is stop singing

This commit is contained in:
2026-03-11 13:33:56 -06:00
parent f50a435200
commit acdac24d1a
2 changed files with 110 additions and 101 deletions

View File

@@ -6,88 +6,87 @@ use std::sync::Arc;
use crate::tone;
pub enum Syn {
Real(Synthesizer),
Text{
m: std::collections::HashMap<i32, std::collections::HashMap<i32, i32>>,
i: u32,
},
Real(Synthesizer),
Text{
m: std::collections::HashMap<i32, std::collections::HashMap<i32, i32>>,
i: u32,
},
}
impl Syn {
pub fn new(debug: bool, sound_font: String, sample_rate: usize) -> Syn {
match debug {
false => Syn::new_real(sound_font, sample_rate),
true => Syn::Text{m: std::collections::HashMap::new(), i: 0},
}
}
pub fn new(debug: bool, sound_font: String, sample_rate: usize) -> Syn {
match debug {
false => Syn::new_real(sound_font, sample_rate),
true => Syn::Text{m: std::collections::HashMap::new(), i: 0},
}
}
fn new_real(sound_font: String, sample_rate: usize) -> Syn {
let mut sf2 = std::fs::File::open(sound_font).unwrap();
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());
fn new_real(sound_font: String, sample_rate: usize) -> Syn {
let mut sf2 = std::fs::File::open(sound_font).unwrap();
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());
let settings = SynthesizerSettings::new(sample_rate as i32);
let settings = SynthesizerSettings::new(sample_rate as i32);
let synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
Syn::Real(synthesizer)
}
let synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
Syn::Real(synthesizer)
}
pub fn set(&mut self, b: tone::Tone) {
let a = 0 as i32;
match self {
// channel=[0..16)
// velocity=[0..128)
Syn::Real(syn) => {
syn.note_off_all_channel(a, false);
},
Syn::Text{m, ..} => {
m.clear();
},
};
self.tone_on(b);
}
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(ch, false);
},
Syn::Text{m, ..} => {
m.remove(&ch);
},
};
if let Some(tone) = b {
self.tone_on(ch, tone);
};
}
fn tone_on(&mut self, b: tone::Tone) {
let a = 0 as i32;
match self {
// channel=[0..16)
// velocity=[0..128)
Syn::Real(syn) => syn.note_on(a, b.i32(), 127),
Syn::Text{m, ..} => {
match m.get_mut(&a) {
Some(m2) => { m2.insert(b.i32(), 127); },
None => {
let mut m2 = std::collections::HashMap::new();
m2.insert(b.i32(), 127);
m.insert(a, m2);
},
};
},
};
}
fn tone_on(&mut self, ch: i32, b: tone::Tone) {
match self {
// channel=[0..16)
// velocity=[0..128)
Syn::Real(syn) => syn.note_on(ch, b.i32(), 127),
Syn::Text{m, ..} => {
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(ch, m2);
},
};
},
};
}
fn tone_off(&mut self, b: tone::Tone) {
let a = 0 as i32;
match self {
Syn::Real(syn) => syn.note_off(a, b.i32()),
Syn::Text{m, ..} => {
match m.get_mut(&a) {
Some(m) => { m.remove(&b.i32()); },
None => {},
};
fn tone_off(&mut self, ch: i32, b: tone::Tone) {
match self {
Syn::Real(syn) => syn.note_off(ch, b.i32()),
Syn::Text{m, ..} => {
match m.get_mut(&ch) {
Some(m) => { m.remove(&b.i32()); },
None => {},
};
},
};
}
},
};
}
pub fn render(&mut self, a: &mut [f32], b: &mut [f32]) {
match self {
Syn::Real(syn) => syn.render(a, b),
Syn::Text{m, i} => {
eprintln!("{} | render[{}]({:?})", chrono::prelude::Utc::now(), i, m);
*i += 1;
},
};
}
pub fn render(&mut self, a: &mut [f32], b: &mut [f32]) {
match self {
Syn::Real(syn) => syn.render(a, b),
Syn::Text{m, i} => {
eprintln!("{} | render[{}]({:?})", chrono::prelude::Utc::now(), i, m);
*i += 1;
},
};
}
}
#[cfg(test)]
@@ -96,29 +95,29 @@ mod test {
#[test]
fn test_new_real() {
let mut syn = Syn::new(false, "super_small_font.sf2".to_string(), 44100);
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();
syn.render(&mut buffer1, &mut buffer2);
let mut buffer1 = Vec::<f32>::new();
let mut buffer2 = Vec::<f32>::new();
syn.render(&mut buffer1, &mut buffer2);
}
#[test]
fn test_text() {
let mut syn = Syn::new(true, ".sf2".to_string(), 1);
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();
syn.render(&mut buffer1, &mut buffer2);
let mut buffer1 = Vec::<f32>::new();
let mut buffer2 = Vec::<f32>::new();
syn.render(&mut buffer1, &mut buffer2);
}
}