Compare commits
2 Commits
04645c0d17
...
c470f7ae40
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c470f7ae40 | ||
|
|
e9cbb86735 |
@@ -5,11 +5,17 @@ pub struct Flags {
|
|||||||
#[arg(short, long, default_value_t = false)]
|
#[arg(short, long, default_value_t = false)]
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
|
|
||||||
#[arg(long, default_value_t = 4)]
|
#[arg(long, default_value_t = 16)]
|
||||||
pub smallest_note: usize,
|
pub smallest_note: usize,
|
||||||
|
|
||||||
|
#[arg(long, default_value_t = 60)]
|
||||||
|
pub bpm: usize,
|
||||||
|
|
||||||
#[arg(long, default_value_t = 44100)]
|
#[arg(long, default_value_t = 44100)]
|
||||||
pub sample_rate: usize,
|
pub sample_rate: usize,
|
||||||
|
|
||||||
|
#[arg(long, default_value = "super_small_font.sf2")]
|
||||||
|
pub sound_font: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Flags {
|
impl Flags {
|
||||||
|
|||||||
72
src/main.rs
72
src/main.rs
@@ -1,34 +1,22 @@
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustysynth::SoundFont;
|
|
||||||
use rustysynth::Synthesizer;
|
|
||||||
use rustysynth::SynthesizerSettings;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tinyaudio::prelude::*;
|
use tinyaudio::prelude::*;
|
||||||
|
|
||||||
mod flags;
|
mod flags;
|
||||||
|
mod syn;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let flags = flags::Flags::new();
|
let flags = flags::Flags::new();
|
||||||
|
|
||||||
// Load the SoundFont.
|
let mut syn = syn::Syn::new(
|
||||||
let mut sf2 = File::open("super_small_font.sf2").unwrap();
|
flags.debug,
|
||||||
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());
|
flags.sound_font,
|
||||||
|
flags.sample_rate,
|
||||||
|
);
|
||||||
|
|
||||||
// Setup the audio output.
|
|
||||||
let params = OutputDeviceParameters {
|
let params = OutputDeviceParameters {
|
||||||
channels_count: 2,
|
channels_count: 2,
|
||||||
sample_rate: flags.sample_rate,
|
sample_rate: flags.sample_rate,
|
||||||
channel_sample_count: flags.sample_rate / flags.smallest_note,
|
channel_sample_count: flags.sample_rate / flags.bpm * 60 / flags.smallest_note,
|
||||||
};
|
|
||||||
|
|
||||||
// Create the synthesizer.
|
|
||||||
let settings = SynthesizerSettings::new(params.sample_rate as i32);
|
|
||||||
let synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
|
|
||||||
|
|
||||||
let mut syn = match &flags.debug{
|
|
||||||
false => Syn::Real(synthesizer),
|
|
||||||
true => Syn::Text(std::collections::HashMap::new()),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Play some notes (middle C, E, G). // 16 channels actually // 60=c 64=e 67=g //up to 128velocity though dont go below 50 tbh // 12 notes per octave
|
// Play some notes (middle C, E, G). // 16 channels actually // 60=c 64=e 67=g //up to 128velocity though dont go below 50 tbh // 12 notes per octave
|
||||||
@@ -54,49 +42,3 @@ fn main() {
|
|||||||
std::thread::sleep(std::time::Duration::from_secs(2));
|
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Syn {
|
|
||||||
Real(Synthesizer),
|
|
||||||
Text(std::collections::HashMap<i32, std::collections::HashMap<i32, i32>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Syn {
|
|
||||||
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);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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 => {},
|
|
||||||
};
|
|
||||||
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
72
src/syn.rs
Normal file
72
src/syn.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
use rustysynth::Synthesizer;
|
||||||
|
use rustysynth::SoundFont;
|
||||||
|
use rustysynth::SynthesizerSettings;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub enum Syn {
|
||||||
|
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},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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, i} => {
|
||||||
|
eprintln!("render[{}]({:?})", i, m);
|
||||||
|
*i += 1;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user