1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
use core_foundation::base::OSStatus;
use coremidi_sys::{MIDIPortConnectSource, MIDIPortDisconnectSource, MIDIPortDispose, MIDISend};
use std::ops::Deref;
use std::ptr;
use crate::callback::BoxedCallback;
use crate::endpoints::destinations::Destination;
use crate::endpoints::sources::Source;
use crate::object::Object;
use crate::packets::PacketList;
/// A MIDI connection port owned by a client.
/// See [MIDIPortRef](https://developer.apple.com/reference/coremidi/midiportref).
///
/// Ports can't be instantiated directly, but through a client.
///
#[derive(Debug)]
pub struct Port {
pub(crate) object: Object,
}
impl Deref for Port {
type Target = Object;
fn deref(&self) -> &Object {
&self.object
}
}
impl Drop for Port {
fn drop(&mut self) {
unsafe { MIDIPortDispose(self.object.0) };
}
}
/// An output [MIDI port](https://developer.apple.com/reference/coremidi/midiportref) owned by a client.
///
/// A simple example to create an output port and send a MIDI event:
///
/// ```rust,no_run
/// let client = coremidi::Client::new("example-client").unwrap();
/// let output_port = client.output_port("example-port").unwrap();
/// let destination = coremidi::Destination::from_index(0).unwrap();
/// let packets = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]);
/// output_port.send(&destination, &packets).unwrap();
/// ```
#[derive(Debug)]
pub struct OutputPort {
pub(crate) port: Port,
}
impl OutputPort {
/// Send a list of packets to a destination.
/// See [MIDISend](https://developer.apple.com/reference/coremidi/1495289-midisend).
///
pub fn send(
&self,
destination: &Destination,
packet_list: &PacketList,
) -> Result<(), OSStatus> {
let status = unsafe {
MIDISend(
self.port.object.0,
destination.endpoint.object.0,
packet_list.as_ptr(),
)
};
if status == 0 {
Ok(())
} else {
Err(status)
}
}
}
impl Deref for OutputPort {
type Target = Port;
fn deref(&self) -> &Port {
&self.port
}
}
/// An input [MIDI port](https://developer.apple.com/reference/coremidi/midiportref) owned by a client.
///
/// A simple example to create an input port:
///
/// ```rust,no_run
/// let client = coremidi::Client::new("example-client").unwrap();
/// let input_port = client.input_port("example-port", |packet_list| println!("{}", packet_list)).unwrap();
/// let source = coremidi::Source::from_index(0).unwrap();
/// input_port.connect_source(&source);
/// ```
#[derive(Debug)]
pub struct InputPort {
// Note: the order is important here, port needs to be dropped first
pub(crate) port: Port,
pub(crate) callback: BoxedCallback<PacketList>,
}
impl InputPort {
pub fn connect_source(&self, source: &Source) -> Result<(), OSStatus> {
let status =
unsafe { MIDIPortConnectSource(self.object.0, source.object.0, ptr::null_mut()) };
if status == 0 {
Ok(())
} else {
Err(status)
}
}
pub fn disconnect_source(&self, source: &Source) -> Result<(), OSStatus> {
let status = unsafe { MIDIPortDisconnectSource(self.object.0, source.object.0) };
if status == 0 {
Ok(())
} else {
Err(status)
}
}
}
impl Deref for InputPort {
type Target = Port;
fn deref(&self) -> &Port {
&self.port
}
}
|