diff options
author | pommicket <pommicket@gmail.com> | 2022-09-27 21:04:13 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-09-27 21:04:13 -0400 |
commit | 2a8d4a848b36589140bc81abfd4afcc484d637df (patch) | |
tree | 02cb30e298e39dc755887484d3dfcc4f661c8115 /src | |
parent | 27b84998d93456d5965b5d9dd946f8861e9864e4 (diff) |
corrections
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 20 | ||||
-rw-r--r-- | src/midi_input.rs | 52 |
2 files changed, 43 insertions, 29 deletions
diff --git a/src/main.rs b/src/main.rs index 3460b3b..b6e39f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,7 @@ 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 { + for (index, device) in (&devices).into_iter().enumerate() { print!("{:3} | ", index + 1); let mut first = true; for line in device.name.lines() { @@ -22,14 +21,12 @@ fn main() { first = false; } println!(" -----------------"); - index += 1; } print!("Select a device (default {}): ", devices.default + 1); - match std::io::stdout().flush() { - Err(_) => {} // whatever - _ => {} + if std::io::stdout().flush().is_err() { + //who cares } - + let device_id; { let mut buf = String::new(); @@ -37,7 +34,7 @@ fn main() { .read_line(&mut buf) .expect("error reading stdin"); let s = buf.trim(); - if s.len() == 0 { + if s.is_empty() { device_id = &devices[devices.default].id; } else { match s.parse::<usize>() { @@ -51,9 +48,8 @@ fn main() { } } } - let mut device = device_mgr - .open(&device_id) - .expect("error opening MIDI device"); + let mut device = device_mgr.open(device_id) + .expect("error opening MIDI device"); while device.is_connected() { let maybe_event = device.read_event(); @@ -64,9 +60,9 @@ fn main() { } if let Some(err) = device.get_error() { eprintln!("Error: {}", err); + device.clear_error(); } } - /* let host = cpal::default_host(); let device = host.default_output_device().expect("no output device available"); diff --git a/src/midi_input.rs b/src/midi_input.rs index c5d6705..e8671ec 100644 --- a/src/midi_input.rs +++ b/src/midi_input.rs @@ -60,7 +60,7 @@ pub struct Device { // used to hold first data byte of two-data-byte MIDI events. buffered_byte: Option<u8>, last_status: u8, - connected: bool + connected: bool, } /// An error produced by opening a device. @@ -131,7 +131,7 @@ impl<'a> IntoIterator for &'a DeviceList { type IntoIter = std::slice::Iter<'a, DeviceInfo>; fn into_iter(self) -> Self::IntoIter { - (&self.devices).into_iter() + self.devices.iter() } } @@ -170,16 +170,22 @@ extern "C" { fn snd_lib_error_set_handler(handler: *mut c_void) -> c_int; } -impl Into<String> for DeviceOpenError { - fn into(self) -> String { +impl From<&DeviceOpenError> for String { + fn from(e: &DeviceOpenError) -> String { use DeviceOpenError::*; - match self { + match e { NotFound(s) => format!("device not found: {}", s), Other(s) => s.clone(), } } } +impl From<DeviceOpenError> for String { + fn from(e: DeviceOpenError) -> String { + String::from(&e) + } +} + // technically there should be varargs here but oh well pub unsafe extern "C" fn snd_lib_error_handler_quiet( _file: *const c_char, @@ -254,7 +260,7 @@ impl DeviceManager { loop { let hint; unsafe { - hint = *hints.offset(idx as isize); + hint = *hints.add(idx); } if hint.is_null() { break; @@ -278,7 +284,7 @@ impl DeviceManager { // we need the name to be able to do anything with the device if let Some(name_unwrapped) = name { let has_desc = desc.is_some(); - let desc_unwrapped = desc.unwrap_or("(no description)".to_string()); + let desc_unwrapped = desc.unwrap_or_else(|| "(no description)".to_string()); let desc_str = format!("{}\n{}", name_unwrapped, desc_unwrapped); let info = DeviceInfo { id: DeviceID { @@ -382,14 +388,14 @@ impl Device { None } } - + /// returns false if the device was disconnected. /// if a device is disconnected and reconnected, you need to reopen it. /// (its ID should remain the same) pub fn is_connected(&self) -> bool { - return self.connected; + self.connected } - + /// get the device error if there is one #[allow(dead_code)] pub fn get_error(&self) -> Option<String> { @@ -399,6 +405,10 @@ impl Device { Some(format!("ALSA error code {}", self.error)) } + pub fn clear_error(&mut self) { + self.error = 0; + } + /// read a MIDI event. /// things may get screwed up if you aren't careful about /// mixing `.read_bytes()` with this. @@ -425,10 +435,9 @@ impl Device { } let channel = self.last_status & 0xf; - + // at this point we have a data byte assert!((byte & 0x80) == 0); - match self.last_status & 0xf0 { 0x80 | 0x90 | 0xA0 | 0xB0 | 0xE0 => { @@ -467,15 +476,24 @@ impl Device { } else { self.buffered_byte = Some(byte); } - }, + } 0xC0 => { - return Some(Event::ProgramChange { channel, program: byte }); - }, + return Some(Event::ProgramChange { + channel, + program: byte, + }); + } 0xD0 => { - return Some(Event::ChannelAftertouch { channel, pressure: byte }); + return Some(Event::ChannelAftertouch { + channel, + pressure: byte, + }); } _ => { - return Some(Event::Other { status: self.last_status, data: Some(byte) }); + return Some(Event::Other { + status: self.last_status, + data: Some(byte), + }); } } None |