sym to own file

This commit is contained in:
breel
2026-03-10 22:21:15 -06:00
parent 04645c0d17
commit e9cbb86735
3 changed files with 77 additions and 64 deletions

68
src/syn.rs Normal file
View File

@@ -0,0 +1,68 @@
use rustysynth::Synthesizer;
use rustysynth::SoundFont;
use rustysynth::SynthesizerSettings;
use std::sync::Arc;
pub enum Syn {
Real(Synthesizer),
Text(std::collections::HashMap<i32, std::collections::HashMap<i32, i32>>),
}
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(std::collections::HashMap::new()),
}
}
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 synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
Syn::Real(synthesizer)
}
pub fn note_on(&mut self, a: i32, b: i32, c: i32) {
match self {
Syn::Real(syn) => syn.note_on(a, b, c),
Syn::Text(m) => {
eprintln!("note_on({:?}, {:?}, {:?})", a, b, c);
match m.get_mut(&a) {
Some(m2) => { m2.insert(b, c); },
None => {
let mut m2 = std::collections::HashMap::new();
m2.insert(b, c);
m.insert(a, m2);
},
};
},
};
}
pub fn note_off(&mut self, a: i32, b: i32) {
match self {
Syn::Real(syn) => syn.note_off(a, b),
Syn::Text(m) => {
eprintln!("note_off({:?}, {:?})", a, b);
match m.get_mut(&a) {
Some(m) => { m.remove(&b); },
None => {},
};
},
};
}
pub fn render(&mut self, a: &mut [f32], b: &mut [f32]) {
match self {
Syn::Real(syn) => syn.render(a, b),
Syn::Text(m) => {
eprintln!("render({:?})", m)
},
};
}
}