| 1 |
#![deny(missing_docs)] |
| 2 |
|
| 3 |
use ::errors::*; |
| 4 |
use ::backend::{ |
| 5 |
MidiInputPort as MidiInputPortImpl, |
| 6 |
MidiInput as MidiInputImpl, |
| 7 |
MidiInputConnection as MidiInputConnectionImpl, |
| 8 |
MidiOutputPort as MidiOutputPortImpl, |
| 9 |
MidiOutput as MidiOutputImpl, |
| 10 |
MidiOutputConnection as MidiOutputConnectionImpl |
| 11 |
}; |
| 12 |
use ::Ignore; |
| 13 |
|
| 14 |
/// Trait that abstracts over input and output ports. |
| 15 |
pub trait MidiIO { |
| 16 |
/// Type of an input or output port structure. |
| 17 |
type Port: Clone; |
| 18 |
|
| 19 |
/// Get a collection of all MIDI input or output ports. |
| 20 |
/// The resulting vector contains one object per port, which you can use to |
| 21 |
/// query metadata about the port or connect to it. |
| 22 |
fn ports(&self) -> Vec<Self::Port>; |
| 23 |
|
| 24 |
/// Get the number of available MIDI input or output ports. |
| 25 |
fn port_count(&self) -> usize; |
| 26 |
|
| 27 |
/// Get the name of a specified MIDI input or output port. |
| 28 |
/// |
| 29 |
/// An error will be returned when the port is no longer valid |
| 30 |
/// (e.g. the respective device has been disconnected). |
| 31 |
fn port_name(&self, port: &Self::Port) -> Result<String, PortInfoError>; |
| 32 |
} |
| 33 |
|
| 34 |
/// An object representing a single input port. |
| 35 |
/// How the port is identified internally is backend-dependent. |
| 36 |
/// If the backend allows it, port objects remain valid when |
| 37 |
/// other ports in the system change (i.e. it is not just an index). |
| 38 |
/// |
| 39 |
/// Use the `ports` method of a `MidiInput` instance to obtain |
| 40 |
/// available ports. |
| 41 |
#[derive(Clone, PartialEq)] |
| 42 |
pub struct MidiInputPort { |
| 43 |
pub(crate) imp: MidiInputPortImpl |
| 44 |
} |
| 45 |
|
| 46 |
/// A collection of input ports. |
| 47 |
pub type MidiInputPorts = Vec<MidiInputPort>; |
| 48 |
|
| 49 |
/// An instance of `MidiInput` is required for anything related to MIDI input. |
| 50 |
/// Create one with `MidiInput::new`. |
| 51 |
pub struct MidiInput { |
| 52 |
//ignore_flags: Ignore |
| 53 |
imp: MidiInputImpl |
| 54 |
} |
| 55 |
|
| 56 |
impl MidiInput { |
| 57 |
/// Creates a new `MidiInput` object that is required for any MIDI input functionality. |
| 58 |
pub fn new(client_name: &str) -> Result<Self, InitError> { |
| 59 |
MidiInputImpl::new(client_name).map(|imp| MidiInput { imp: imp }) |
| 60 |
} |
| 61 |
|
| 62 |
/// Set flags to decide what kind of messages should be ignored (i.e., filtered out) |
| 63 |
/// by this `MidiInput`. By default, no messages are ignored. |
| 64 |
pub fn ignore(&mut self, flags: Ignore) { |
| 65 |
self.imp.ignore(flags); |
| 66 |
} |
| 67 |
|
| 68 |
/// Get a collection of all MIDI input ports that *midir* can connect to. |
| 69 |
/// The resulting vector contains one object per port, which you can use to |
| 70 |
/// query metadata about the port or connect to it in order to receive |
| 71 |
/// MIDI messages. |
| 72 |
pub fn ports(&self) -> MidiInputPorts { |
| 73 |
self.imp.ports_internal() |
| 74 |
} |
| 75 |
|
| 76 |
/// Get the number of available MIDI input ports that *midir* can connect to. |
| 77 |
pub fn port_count(&self) -> usize { |
| 78 |
self.imp.port_count() |
| 79 |
} |
| 80 |
|
| 81 |
/// Get the name of a specified MIDI input port. |
| 82 |
/// |
| 83 |
/// An error will be returned when the port is no longer valid |
| 84 |
/// (e.g. the respective device has been disconnected). |
| 85 |
pub fn port_name(&self, port: &MidiInputPort) -> Result<String, PortInfoError> { |
| 86 |
self.imp.port_name(&port.imp) |
| 87 |
} |
| 88 |
|
| 89 |
/// Connect to a specified MIDI input port in order to receive messages. |
| 90 |
/// For each incoming MIDI message, the provided `callback` function will |
| 91 |
/// be called. The first parameter of the callback function is a timestamp |
| 92 |
/// (in microseconds) designating the time since some unspecified point in |
| 93 |
/// the past (which will not change during the lifetime of a |
| 94 |
/// `MidiInputConnection`). The second parameter contains the actual bytes |
| 95 |
/// of the MIDI message. |
| 96 |
/// |
| 97 |
/// Additional data that should be passed whenever the callback is |
| 98 |
/// invoked can be specified by `data`. Use the empty tuple `()` if |
| 99 |
/// you do not want to pass any additional data. |
| 100 |
/// |
| 101 |
/// The connection will be kept open as long as the returned |
| 102 |
/// `MidiInputConnection` is kept alive. |
| 103 |
/// |
| 104 |
/// The `port_name` is an additional name that will be assigned to the |
| 105 |
/// connection. It is only used by some backends. |
| 106 |
/// |
| 107 |
/// An error will be returned when the port is no longer valid |
| 108 |
/// (e.g. the respective device has been disconnected). |
| 109 |
pub fn connect<F, T: Send>( |
| 110 |
self, port: &MidiInputPort, port_name: &str, callback: F, data: T |
| 111 |
) -> Result<MidiInputConnection<T>, ConnectError<MidiInput>> |
| 112 |
where F: FnMut(u64, &[u8], &mut T) + Send + 'static { |
| 113 |
match self.imp.connect(&port.imp, port_name, callback, data) { |
| 114 |
Ok(imp) => Ok(MidiInputConnection { imp: imp }), |
| 115 |
Err(imp) => { |
| 116 |
let kind = imp.kind(); |
| 117 |
Err(ConnectError::new(kind, MidiInput { imp: imp.into_inner() })) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl MidiIO for MidiInput { |
| 124 |
type Port = MidiInputPort; |
| 125 |
|
| 126 |
fn ports(&self) -> MidiInputPorts { |
| 127 |
self.imp.ports_internal() |
| 128 |
} |
| 129 |
|
| 130 |
fn port_count(&self) -> usize { |
| 131 |
self.imp.port_count() |
| 132 |
} |
| 133 |
|
| 134 |
fn port_name(&self, port: &MidiInputPort) -> Result<String, PortInfoError> { |
| 135 |
self.imp.port_name(&port.imp) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[cfg(unix)] |
| 140 |
impl<T: Send> ::os::unix::VirtualInput<T> for MidiInput { |
| 141 |
fn create_virtual<F>( |
| 142 |
self, port_name: &str, callback: F, data: T |
| 143 |
) -> Result<MidiInputConnection<T>, ConnectError<Self>> |
| 144 |
where F: FnMut(u64, &[u8], &mut T) + Send + 'static { |
| 145 |
match self.imp.create_virtual(port_name, callback, data) { |
| 146 |
Ok(imp) => Ok(MidiInputConnection { imp: imp }), |
| 147 |
Err(imp) => { |
| 148 |
let kind = imp.kind(); |
| 149 |
Err(ConnectError::new(kind, MidiInput { imp: imp.into_inner() })) |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/// Represents an open connection to a MIDI input port. |
| 156 |
pub struct MidiInputConnection<T: 'static> { |
| 157 |
imp: MidiInputConnectionImpl<T> |
| 158 |
} |
| 159 |
|
| 160 |
impl<T> MidiInputConnection<T> { |
| 161 |
/// Closes the connection. The returned values allow you to |
| 162 |
/// inspect the additional data passed to the callback (the `data` |
| 163 |
/// parameter of `connect`), or to reuse the `MidiInput` object, |
| 164 |
/// but they can be safely ignored. |
| 165 |
pub fn close(self) -> (MidiInput, T) { |
| 166 |
let (imp, data) = self.imp.close(); |
| 167 |
(MidiInput { imp: imp }, data) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/// An object representing a single output port. |
| 172 |
/// How the port is identified internally is backend-dependent. |
| 173 |
/// If the backend allows it, port objects remain valid when |
| 174 |
/// other ports in the system change (i.e. it is not just an index). |
| 175 |
/// |
| 176 |
/// Use the `ports` method of a `MidiOutput` instance to obtain |
| 177 |
/// available ports. |
| 178 |
#[derive(Clone, PartialEq)] |
| 179 |
pub struct MidiOutputPort { |
| 180 |
pub(crate) imp: MidiOutputPortImpl |
| 181 |
} |
| 182 |
|
| 183 |
/// A collection of output ports. |
| 184 |
pub type MidiOutputPorts = Vec<MidiOutputPort>; |
| 185 |
|
| 186 |
/// An instance of `MidiOutput` is required for anything related to MIDI output. |
| 187 |
/// Create one with `MidiOutput::new`. |
| 188 |
pub struct MidiOutput { |
| 189 |
imp: MidiOutputImpl |
| 190 |
} |
| 191 |
|
| 192 |
impl MidiOutput { |
| 193 |
/// Creates a new `MidiOutput` object that is required for any MIDI output functionality. |
| 194 |
pub fn new(client_name: &str) -> Result<Self, InitError> { |
| 195 |
MidiOutputImpl::new(client_name).map(|imp| MidiOutput { imp: imp }) |
| 196 |
} |
| 197 |
|
| 198 |
/// Get a collection of all MIDI output ports that *midir* can connect to. |
| 199 |
/// The resulting vector contains one object per port, which you can use to |
| 200 |
/// query metadata about the port or connect to it in order to send |
| 201 |
/// MIDI messages. |
| 202 |
pub fn ports(&self) -> MidiOutputPorts { |
| 203 |
self.imp.ports_internal() |
| 204 |
} |
| 205 |
|
| 206 |
/// Get the number of available MIDI output ports that *midir* can connect to. |
| 207 |
pub fn port_count(&self) -> usize { |
| 208 |
self.imp.port_count() |
| 209 |
} |
| 210 |
|
| 211 |
/// Get the name of a specified MIDI output port. |
| 212 |
/// |
| 213 |
/// An error will be returned when the port is no longer valid |
| 214 |
/// (e.g. the respective device has been disconnected). |
| 215 |
pub fn port_name(&self, port: &MidiOutputPort) -> Result<String, PortInfoError> { |
| 216 |
self.imp.port_name(&port.imp) |
| 217 |
} |
| 218 |
|
| 219 |
/// Connect to a specified MIDI output port in order to send messages. |
| 220 |
/// The connection will be kept open as long as the returned |
| 221 |
/// `MidiOutputConnection` is kept alive. |
| 222 |
/// |
| 223 |
/// The `port_name` is an additional name that will be assigned to the |
| 224 |
/// connection. It is only used by some backends. |
| 225 |
/// |
| 226 |
/// An error will be returned when the port is no longer valid |
| 227 |
/// (e.g. the respective device has been disconnected). |
| 228 |
pub fn connect(self, port: &MidiOutputPort, port_name: &str) -> Result<MidiOutputConnection, ConnectError<MidiOutput>> { |
| 229 |
match self.imp.connect(&port.imp, port_name) { |
| 230 |
Ok(imp) => Ok(MidiOutputConnection { imp: imp }), |
| 231 |
Err(imp) => { |
| 232 |
let kind = imp.kind(); |
| 233 |
Err(ConnectError::new(kind, MidiOutput { imp: imp.into_inner() })) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
impl MidiIO for MidiOutput { |
| 240 |
type Port = MidiOutputPort; |
| 241 |
|
| 242 |
fn ports(&self) -> MidiOutputPorts { |
| 243 |
self.imp.ports_internal() |
| 244 |
} |
| 245 |
|
| 246 |
fn port_count(&self) -> usize { |
| 247 |
self.imp.port_count() |
| 248 |
} |
| 249 |
|
| 250 |
fn port_name(&self, port: &MidiOutputPort) -> Result<String, PortInfoError> { |
| 251 |
self.imp.port_name(&port.imp) |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
#[cfg(unix)] |
| 256 |
impl ::os::unix::VirtualOutput for MidiOutput { |
| 257 |
fn create_virtual(self, port_name: &str) -> Result<MidiOutputConnection, ConnectError<MidiOutput>> { |
| 258 |
match self.imp.create_virtual(port_name) { |
| 259 |
Ok(imp) => Ok(MidiOutputConnection { imp: imp }), |
| 260 |
Err(imp) => { |
| 261 |
let kind = imp.kind(); |
| 262 |
Err(ConnectError::new(kind, MidiOutput { imp: imp.into_inner() })) |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/// Represents an open connection to a MIDI output port. |
| 269 |
pub struct MidiOutputConnection { |
| 270 |
imp: MidiOutputConnectionImpl |
| 271 |
} |
| 272 |
|
| 273 |
impl MidiOutputConnection { |
| 274 |
/// Closes the connection. The returned value allows you to |
| 275 |
/// reuse the `MidiOutput` object, but it can be safely ignored. |
| 276 |
pub fn close(self) -> MidiOutput { |
| 277 |
MidiOutput { imp: self.imp.close() } |
| 278 |
} |
| 279 |
|
| 280 |
/// Send a message to the port that this output connection is connected to. |
| 281 |
/// The message must be a valid MIDI message (see https://www.midi.org/specifications-old/item/table-1-summary-of-midi-message). |
| 282 |
pub fn send(&mut self, message: &[u8]) -> Result<(), SendError> { |
| 283 |
self.imp.send(message) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
#[cfg(test)] |
| 288 |
mod tests { |
| 289 |
use super::*; |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn test_trait_impls() { |
| 293 |
// make sure that all the structs implement `Send` |
| 294 |
fn is_send<T: Send>() {} |
| 295 |
is_send::<MidiInput>(); |
| 296 |
is_send::<MidiOutput>(); |
| 297 |
#[cfg(not(target_arch = "wasm32"))] { |
| 298 |
// The story around threading and `Send` on WASM is not clear yet |
| 299 |
// Tracking issue: https://github.com/Boddlnagg/midir/issues/49 |
| 300 |
// Prev. discussion: https://github.com/Boddlnagg/midir/pull/47 |
| 301 |
is_send::<MidiInputPort>(); |
| 302 |
is_send::<MidiInputConnection<()>>(); |
| 303 |
is_send::<MidiOutputPort>(); |
| 304 |
is_send::<MidiOutputConnection>(); |
| 305 |
} |
| 306 |
|
| 307 |
// make sure that Midi port structs implement `PartialEq` |
| 308 |
fn is_partial_eq<T: PartialEq>() {} |
| 309 |
is_partial_eq::<MidiInputPortImpl>(); |
| 310 |
is_partial_eq::<MidiOutputPortImpl>(); |
| 311 |
|
| 312 |
is_partial_eq::<MidiInputPort>(); |
| 313 |
is_partial_eq::<MidiOutputPort>(); |
| 314 |
} |
| 315 |
} |