diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index 47bc913..4f6089e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ +// @TODO: sort presets alphabetically extern crate cpal; use std::io::Write; -//use cpal::traits::{HostTrait, DeviceTrait, StreamTrait}; +use cpal::traits::{HostTrait, DeviceTrait, StreamTrait}; mod midi_input; mod soundfont; @@ -69,33 +70,64 @@ fn playmidi_main() -> Result<(), String> { } fn main() { - let _sf = match soundfont::SoundFont::open("/etc/alternatives/default-GM.sf2") { + let mut sf = match soundfont::SoundFont::open("/etc/alternatives/default-GM.sf2") { Err(x) => { eprintln!("Error: {}", String::from(x)); return; - }, + } Ok(s) => s, }; - - + + for i in 0..sf.preset_count() { + println!("{}. {}", i, sf.preset_name(i).unwrap()); + } + // let result = playmidi_main(); // if let Err(s) = result { // eprintln!("Error: {}", s); // } - /* let host = cpal::default_host(); let device = host.default_output_device().expect("no output device available"); - let mut supported_configs_range = device.supported_output_configs() + let supported_configs = device.supported_output_configs() .expect("error while querying configs"); - let config = supported_configs_range.next() - .expect("no supported config?!") + let mut chosen_config = None; + for config in supported_configs { + if config.channels() != 2 || config.sample_format() != cpal::SampleFormat::I16 { + continue; + } + chosen_config = Some(config); + } + let chosen_config = match chosen_config { + None => { + eprintln!("Couldn't configure audio device to have 2 16-bit channels."); + return; + }, + Some(x) => x, + }; + let supp_config: cpal::SupportedStreamConfig = chosen_config .with_max_sample_rate() .into(); + if supp_config.channels() != 2 { + } + let config = supp_config.into(); + let mut time = 0.0; + let mut key = 60; let stream = device.build_output_stream( &config, move |data: &mut [i16], _: &cpal::OutputCallbackInfo| { - for sample in data.iter_mut() { - *sample = 0; + for x in data.iter_mut() { + *x = 0; + } + let sample_rate = config.sample_rate.0 as f64; + match sf.add_samples_interlaced(126, 1.0, key, 60, time, data, sample_rate) { + Ok(false) => {println!("stop")}, + Err(e) => eprintln!("{}", e), + _ => {}, + } + time += (data.len() as f64) / (2.0 * sample_rate); + if time >= 1.0 { + time = 0.0; + key += 1; } }, move |err| { @@ -107,5 +139,4 @@ fn main() { loop { std::thread::sleep(std::time::Duration::from_millis(100)); } - */ } |