use std::ptr;
pub use crate::types::{Device, LevelType};
use super::VoicemeeterRemote;
impl VoicemeeterRemote {
pub fn get_level(
&self,
level_type: LevelType,
device: Device,
channel: usize,
) -> Result<Option<f32>, GetLevelError> {
let mut f = std::f32::NAN;
let dev_num =
if let Some(dev_num) = device.as_level_device_num(&self.program, level_type, channel) {
dev_num as i32
} else {
return Ok(None);
};
let res = unsafe { self.raw.VBVMR_GetLevel(level_type as i32, dev_num, &mut f) };
match res {
0 => Ok(Some(f)),
-1 => Err(GetLevelError::CannotGetClient),
-2 => Err(GetLevelError::NoServer),
-3 => Err(GetLevelError::NoLevel),
-4 => Err(GetLevelError::OutOfRange),
s => Err(GetLevelError::Other(s)),
}
}
pub fn get_midi_message(&self) -> Result<Vec<u8>, GetMidiMessageError> {
let mut v = vec![0; 1024];
let len = self.get_midi_message_buff(&mut v)?;
v.truncate(len);
Ok(v)
}
#[inline]
pub fn get_midi_message_buff(&self, buffer: &mut [u8]) -> Result<usize, GetMidiMessageError> {
let res = unsafe {
self.raw
.VBVMR_GetMidiMessage(ptr::addr_of_mut!(buffer[0]), buffer.len() as _)
};
match res {
res if res >= 0 => Ok(res as usize),
-1 => Err(GetMidiMessageError::CannotGetClient),
-2 => Err(GetMidiMessageError::NoServer),
v @ -5 | v @ -6 => Err(GetMidiMessageError::NoMidiData(v)),
s => Err(GetMidiMessageError::Other(s)),
}
}
}
#[derive(Debug, thiserror::Error, Clone)]
#[non_exhaustive]
pub enum GetLevelError {
#[error("cannot get client (unexpected)")]
CannotGetClient,
#[error("no server")]
NoServer,
#[error("no level available")]
NoLevel,
#[error("out of range")]
OutOfRange,
#[error("unexpected error occurred: error code {0}")]
Other(i32),
}
#[derive(Debug, thiserror::Error, Clone)]
#[non_exhaustive]
pub enum GetMidiMessageError {
#[error("cannot get client (unexpected)")]
CannotGetClient,
#[error("no server")]
NoServer,
#[error("no level available")]
NoMidiData(i32),
#[error("unexpected error occurred: error code {0}")]
Other(i32),
}