enums are interfaces where 1 function needs to know all impls but at least no weird box shit
This commit is contained in:
34
src/main.rs
34
src/main.rs
@@ -20,11 +20,13 @@ fn main() {
|
||||
|
||||
// Create the synthesizer.
|
||||
let settings = SynthesizerSettings::new(params.sample_rate as i32);
|
||||
let mut synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
|
||||
let synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
|
||||
|
||||
let mut syn = Syn::Real(synthesizer);
|
||||
|
||||
// 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
|
||||
synthesizer.note_on(0, 64 + 12, 127);
|
||||
//synthesizer.render(&mut left[..], &mut right[..]); // puts in a state of rendering the first loop of these notes
|
||||
syn.note_on(0, 64 + 12, 127);
|
||||
//syn.render(&mut left[..], &mut right[..]); // puts in a state of rendering the first loop of these notes
|
||||
|
||||
// The recycled output buffer per-loop, could be in lamba but that'd be wasteful
|
||||
let sample_count = (params.channel_sample_count) as usize;
|
||||
@@ -33,7 +35,7 @@ fn main() {
|
||||
// Start the audio output. Executes channel_sample_count per loop, sample_rate/channel_sample_count per second forever.
|
||||
let _device = run_output_device(params, {
|
||||
move |data| {
|
||||
synthesizer.render(&mut left[..], &mut right[..]); // put in a state of rendering the next loop of these notes
|
||||
syn.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;
|
||||
}
|
||||
@@ -44,3 +46,27 @@ fn main() {
|
||||
// Let it ride
|
||||
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||
}
|
||||
|
||||
enum Syn {
|
||||
Real(Synthesizer),
|
||||
}
|
||||
|
||||
impl Syn {
|
||||
fn note_on(&mut self, a: i32, b: i32, c: i32) {
|
||||
match self {
|
||||
Syn::Real(syn) => syn.note_on(a, b, c),
|
||||
};
|
||||
}
|
||||
|
||||
fn note_off(&mut self, a: i32, b: i32) {
|
||||
match self {
|
||||
Syn::Real(syn) => syn.note_off(a, b),
|
||||
};
|
||||
}
|
||||
|
||||
fn render(&mut self, a: &mut [f32], b: &mut [f32]) {
|
||||
match self {
|
||||
Syn::Real(syn) => syn.render(a, b),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user