summaryrefslogtreecommitdiff
path: root/src/midi_input.rs
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-09-28 14:29:14 -0400
committerpommicket <pommicket@gmail.com>2022-09-28 14:29:14 -0400
commit318533fd0b4b416719c7f4f76268e0415c11a674 (patch)
treeccafc2199ca76c1ca70de7b495038b4ce20cf8a5 /src/midi_input.rs
parent2a8d4a848b36589140bc81abfd4afcc484d637df (diff)
reading soundfonts
Diffstat (limited to 'src/midi_input.rs')
-rw-r--r--src/midi_input.rs46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/midi_input.rs b/src/midi_input.rs
index e8671ec..27b2c1c 100644
--- a/src/midi_input.rs
+++ b/src/midi_input.rs
@@ -5,11 +5,7 @@ use std::ffi::{c_char, c_int, c_void, CStr, CString};
/// Basic usage:
/// ```
/// let mut manager = DeviceManager::new().unwrap();
-/// let mut devices = manager.list();
-/// while devices.len() == 0 {
-/// std::thread::sleep(std::time::Duration::from_millis(100));
-/// devices = manager.list();
-/// }
+/// let mut devices = manager.list().unwrap();
/// let mut i = devices.default;
/// for device in devices {
/// println!("{}",device.name);
@@ -67,18 +63,28 @@ pub struct Device {
#[derive(Debug)]
pub enum DeviceOpenError {
/// the device was not found.
- /// this can hapen if it was disconnected between calling `list_devices`
- /// and `open_device`.
+ /// this can hapen if it was disconnected between calling `list`
+ /// and `open`.
NotFound(String),
/// other error
Other(String),
}
+/// A list of available MIDI input devices.
pub struct DeviceList {
pub devices: Vec<DeviceInfo>,
pub default: usize,
}
+/// Error produced by `DeviceManager::list`
+#[derive(Debug, PartialEq, Eq)]
+pub enum DeviceListError {
+ /// There are no MIDI input devices available
+ NoDevices,
+ /// Other error
+ Other(String),
+}
+
/// A MIDI event.
/// All notes and velocities are from 0 to 127.
/// A note of 60 +/- n indicates n semitones above/below middle C.
@@ -186,6 +192,22 @@ impl From<DeviceOpenError> for String {
}
}
+impl From<&DeviceListError> for String {
+ fn from(e: &DeviceListError) -> String {
+ use DeviceListError::*;
+ match e {
+ NoDevices => "no devices found".to_string(),
+ Other(s) => s.clone(),
+ }
+ }
+}
+
+impl From<DeviceListError> for String {
+ fn from(e: DeviceListError) -> 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,
@@ -239,7 +261,7 @@ impl DeviceManager {
}
/// Returns a `DeviceList` containing descriptions for all MIDI input devices.
- pub fn list(&self) -> Result<DeviceList, String> {
+ pub fn list(&self) -> Result<DeviceList, DeviceListError> {
let mut hints: *mut *mut c_void = 0 as _;
let err: c_int;
unsafe {
@@ -250,7 +272,10 @@ impl DeviceManager {
// in theory hints should never be null if err != 0.
// but you can never be sure.
if err != 0 || hints == 0 as _ {
- return Err(format!("failed to get device hints (error code {})", err));
+ return Err(DeviceListError::Other(format!(
+ "failed to get device hints (error code {})",
+ err
+ )));
}
let mut idx: usize = 0;
@@ -307,6 +332,9 @@ impl DeviceManager {
snd_device_name_free_hint(hints);
}
+ if devices.is_empty() {
+ return Err(DeviceListError::NoDevices);
+ }
Ok(DeviceList {
devices,
default: default.unwrap_or(0),