summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-09-27 16:40:34 -0400
committerpommicket <pommicket@gmail.com>2022-09-27 16:40:34 -0400
commit922fa6b649fac4e2f983186a1a2dbd77847b938c (patch)
tree2a11fc0baa5e007f186d47b4a1cf90b6786bcffc /src/main.rs
midi input
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..3656f48
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,94 @@
+extern crate cpal;
+
+use std::io::Write;
+
+//use cpal::traits::{HostTrait, DeviceTrait, StreamTrait};
+
+mod midi_input;
+
+fn main() {
+ let mut device_mgr = midi_input::DeviceManager::new().expect("Couldn't create device manager");
+ device_mgr.set_quiet(true);
+ let devices = device_mgr.list().expect("couldn't list MIDI devices");
+ let mut index = 0;
+ for device in &devices {
+ print!("{:3} | ", index + 1);
+ let mut first = true;
+ for line in device.name.lines() {
+ if !first {
+ print!(" | ");
+ }
+ println!("{}", line);
+ first = false;
+ }
+ println!(" -----------------");
+ index += 1;
+ }
+ print!("Select a device (default {}): ", devices.default + 1);
+ match std::io::stdout().flush() {
+ Err(_) => {} // whatever
+ _ => {}
+ }
+
+ let device_id;
+ {
+ let mut buf = String::new();
+ std::io::stdin()
+ .read_line(&mut buf)
+ .expect("error reading stdin");
+ let s = buf.trim();
+ if s.len() == 0 {
+ device_id = &devices[devices.default].id;
+ } else {
+ match s.parse::<usize>() {
+ Ok(idx) if idx >= 1 && idx <= devices.len() => {
+ device_id = &devices[idx - 1].id;
+ }
+ _ => {
+ eprintln!("Bad device ID: {}", s);
+ return;
+ }
+ }
+ }
+ }
+ let mut device = device_mgr
+ .open(&device_id)
+ .expect("error opening MIDI device");
+
+ loop {
+ let byte = device.read_byte();
+ if let Some(x) = byte {
+ println!("{}", x);
+ } else {
+ println!("nothing");
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ }
+
+ /*
+ 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()
+ .expect("error while querying configs");
+ let config = supported_configs_range.next()
+ .expect("no supported config?!")
+ .with_max_sample_rate()
+ .into();
+ let stream = device.build_output_stream(
+ &config,
+ move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
+ for sample in data.iter_mut() {
+ *sample = 0;
+ }
+ },
+ move |err| {
+ println!("audio stream error: {}", err);
+ },
+ ).expect("couldn't build output stream");
+ stream.play().expect("couldn't play stream");
+
+ loop {
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ }
+ */
+}