| 1 |
extern crate jack_sys; |
| 2 |
extern crate libc; |
| 3 |
|
| 4 |
use self::jack_sys::jack_nframes_t; |
| 5 |
use self::libc::c_void; |
| 6 |
|
| 7 |
use std::{mem, slice}; |
| 8 |
use std::ffi::CString; |
| 9 |
|
| 10 |
mod wrappers; |
| 11 |
use self::wrappers::*; |
| 12 |
|
| 13 |
use ::{Ignore, MidiMessage}; |
| 14 |
use ::errors::*; |
| 15 |
|
| 16 |
const OUTPUT_RINGBUFFER_SIZE: usize = 16384; |
| 17 |
|
| 18 |
struct InputHandlerData<T> { |
| 19 |
port: Option<MidiPort>, |
| 20 |
ignore_flags: Ignore, |
| 21 |
callback: Box<dyn FnMut(u64, &[u8], &mut T) + Send>, |
| 22 |
user_data: Option<T> |
| 23 |
} |
| 24 |
|
| 25 |
pub struct MidiInput { |
| 26 |
ignore_flags: Ignore, |
| 27 |
client: Option<Client>, |
| 28 |
} |
| 29 |
|
| 30 |
#[derive(Clone, PartialEq)] |
| 31 |
pub struct MidiInputPort { |
| 32 |
name: CString |
| 33 |
} |
| 34 |
|
| 35 |
pub struct MidiInputConnection<T> { |
| 36 |
handler_data: Box<InputHandlerData<T>>, |
| 37 |
client: Option<Client> |
| 38 |
} |
| 39 |
|
| 40 |
impl MidiInput { |
| 41 |
pub fn new(client_name: &str) -> Result<Self, InitError> { |
| 42 |
let client = match Client::open(client_name, JackOpenOptions::NoStartServer) { |
| 43 |
Ok(c) => c, |
| 44 |
Err(_) => { return Err(InitError); } // TODO: maybe add message that Jack server might not be running |
| 45 |
}; |
| 46 |
|
| 47 |
Ok(MidiInput { |
| 48 |
ignore_flags: Ignore::None, |
| 49 |
client: Some(client), |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
pub fn ignore(&mut self, flags: Ignore) { |
| 54 |
self.ignore_flags = flags; |
| 55 |
} |
| 56 |
|
| 57 |
pub(crate) fn ports_internal(&self) -> Vec<::common::MidiInputPort> { |
| 58 |
let ports = self.client.as_ref().unwrap().get_midi_ports(PortFlags::PortIsOutput); |
| 59 |
let mut result = Vec::with_capacity(ports.count()); |
| 60 |
for i in 0..ports.count() { |
| 61 |
result.push(::common::MidiInputPort { |
| 62 |
imp: MidiInputPort { |
| 63 |
name: ports.get_c_name(i).into() |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
result |
| 68 |
} |
| 69 |
|
| 70 |
pub fn port_count(&self) -> usize { |
| 71 |
self.client.as_ref().unwrap().get_midi_ports(PortFlags::PortIsOutput).count() |
| 72 |
} |
| 73 |
|
| 74 |
pub fn port_name(&self, port: &MidiInputPort) -> Result<String, PortInfoError> { |
| 75 |
Ok(port.name.to_string_lossy().into()) |
| 76 |
} |
| 77 |
|
| 78 |
fn activate_callback<F, T: Send>(&mut self, callback: F, data: T) |
| 79 |
-> Box<InputHandlerData<T>> |
| 80 |
where F: FnMut(u64, &[u8], &mut T) + Send + 'static |
| 81 |
{ |
| 82 |
let handler_data = Box::new(InputHandlerData { |
| 83 |
port: None, |
| 84 |
ignore_flags: self.ignore_flags, |
| 85 |
callback: Box::new(callback), |
| 86 |
user_data: Some(data) |
| 87 |
}); |
| 88 |
|
| 89 |
let data_ptr = unsafe { mem::transmute_copy::<_, *mut InputHandlerData<T>>(&handler_data) }; |
| 90 |
|
| 91 |
self.client.as_mut().unwrap().set_process_callback(handle_input::<T>, data_ptr as *mut c_void); |
| 92 |
self.client.as_mut().unwrap().activate(); |
| 93 |
handler_data |
| 94 |
} |
| 95 |
|
| 96 |
pub fn connect<F, T: Send>( |
| 97 |
mut self, port: &MidiInputPort, port_name: &str, callback: F, data: T |
| 98 |
) -> Result<MidiInputConnection<T>, ConnectError<MidiInput>> |
| 99 |
where F: FnMut(u64, &[u8], &mut T) + Send + 'static { |
| 100 |
|
| 101 |
let mut handler_data = self.activate_callback(callback, data); |
| 102 |
|
| 103 |
// Create port ... |
| 104 |
let dest_port = match self.client.as_mut().unwrap().register_midi_port(port_name, PortFlags::PortIsInput) { |
| 105 |
Ok(p) => p, |
| 106 |
Err(()) => { return Err(ConnectError::other("could not register JACK port", self)); } |
| 107 |
}; |
| 108 |
|
| 109 |
// ... and connect it to the output |
| 110 |
if let Err(_) = self.client.as_mut().unwrap().connect(&port.name, dest_port.get_name()) { |
| 111 |
return Err(ConnectError::new(ConnectErrorKind::InvalidPort, self)); |
| 112 |
} |
| 113 |
|
| 114 |
handler_data.port = Some(dest_port); |
| 115 |
|
| 116 |
Ok(MidiInputConnection { |
| 117 |
handler_data: handler_data, |
| 118 |
client: self.client.take() |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
pub fn create_virtual<F, T: Send>( |
| 123 |
mut self, port_name: &str, callback: F, data: T |
| 124 |
) -> Result<MidiInputConnection<T>, ConnectError<Self>> |
| 125 |
where F: FnMut(u64, &[u8], &mut T) + Send + 'static { |
| 126 |
|
| 127 |
let mut handler_data = self.activate_callback(callback, data); |
| 128 |
|
| 129 |
// Create port |
| 130 |
let port = match self.client.as_mut().unwrap().register_midi_port(port_name, PortFlags::PortIsInput) { |
| 131 |
Ok(p) => p, |
| 132 |
Err(()) => { return Err(ConnectError::other("could not register JACK port", self)); } |
| 133 |
}; |
| 134 |
|
| 135 |
handler_data.port = Some(port); |
| 136 |
|
| 137 |
Ok(MidiInputConnection { |
| 138 |
handler_data: handler_data, |
| 139 |
client: self.client.take() |
| 140 |
}) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl<T> MidiInputConnection<T> { |
| 145 |
pub fn close(mut self) -> (MidiInput, T) { |
| 146 |
self.close_internal(); |
| 147 |
|
| 148 |
(MidiInput { |
| 149 |
client: self.client.take(), |
| 150 |
ignore_flags: self.handler_data.ignore_flags, |
| 151 |
}, self.handler_data.user_data.take().unwrap()) |
| 152 |
} |
| 153 |
|
| 154 |
fn close_internal(&mut self) { |
| 155 |
let port = self.handler_data.port.take().unwrap(); |
| 156 |
self.client.as_mut().unwrap().unregister_midi_port(port); |
| 157 |
self.client.as_mut().unwrap().deactivate(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl<T> Drop for MidiInputConnection<T> { |
| 162 |
fn drop(&mut self) { |
| 163 |
if self.client.is_some() { |
| 164 |
self.close_internal(); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
extern "C" fn handle_input<T>(nframes: jack_nframes_t, arg: *mut c_void) -> i32 { |
| 170 |
let data: &mut InputHandlerData<T> = unsafe { &mut *(arg as *mut InputHandlerData<T>) }; |
| 171 |
|
| 172 |
// Is port created? |
| 173 |
if let Some(ref port) = data.port { |
| 174 |
let buff = port.get_midi_buffer(nframes); |
| 175 |
|
| 176 |
let mut message = MidiMessage::new(); // TODO: create MidiMessage once and reuse its buffer for every handle_input call |
| 177 |
|
| 178 |
// We have midi events in buffer |
| 179 |
let evcount = buff.get_event_count(); |
| 180 |
let mut event = mem::MaybeUninit::uninit(); |
| 181 |
|
| 182 |
for j in 0..evcount { |
| 183 |
message.bytes.clear(); |
| 184 |
unsafe { buff.get_event(event.as_mut_ptr(), j) }; |
| 185 |
let event = unsafe { event.assume_init() }; |
| 186 |
|
| 187 |
for i in 0..event.size { |
| 188 |
message.bytes.push(unsafe { *event.buffer.offset(i as isize) }); |
| 189 |
} |
| 190 |
|
| 191 |
message.timestamp = Client::get_time(); // this is in microseconds |
| 192 |
(data.callback)(message.timestamp, &message.bytes, data.user_data.as_mut().unwrap()); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
struct OutputHandlerData { |
| 200 |
port: Option<MidiPort>, |
| 201 |
buff_size: Ringbuffer, |
| 202 |
buff_message: Ringbuffer, |
| 203 |
} |
| 204 |
|
| 205 |
pub struct MidiOutput { |
| 206 |
client: Option<Client>, |
| 207 |
} |
| 208 |
|
| 209 |
#[derive(Clone, PartialEq)] |
| 210 |
pub struct MidiOutputPort { |
| 211 |
name: CString |
| 212 |
} |
| 213 |
|
| 214 |
pub struct MidiOutputConnection { |
| 215 |
handler_data: Box<OutputHandlerData>, |
| 216 |
client: Option<Client> |
| 217 |
} |
| 218 |
|
| 219 |
impl MidiOutput { |
| 220 |
pub fn new(client_name: &str) -> Result<Self, InitError> { |
| 221 |
let client = match Client::open(client_name, JackOpenOptions::NoStartServer) { |
| 222 |
Ok(c) => c, |
| 223 |
Err(_) => { return Err(InitError); } // TODO: maybe add message that Jack server might not be running |
| 224 |
}; |
| 225 |
|
| 226 |
Ok(MidiOutput { |
| 227 |
client: Some(client), |
| 228 |
}) |
| 229 |
} |
| 230 |
|
| 231 |
pub(crate) fn ports_internal(&self) -> Vec<::common::MidiOutputPort> { |
| 232 |
let ports = self.client.as_ref().unwrap().get_midi_ports(PortFlags::PortIsInput); |
| 233 |
let mut result = Vec::with_capacity(ports.count()); |
| 234 |
for i in 0..ports.count() { |
| 235 |
result.push(::common::MidiOutputPort { |
| 236 |
imp: MidiOutputPort { |
| 237 |
name: ports.get_c_name(i).into() |
| 238 |
} |
| 239 |
}) |
| 240 |
} |
| 241 |
result |
| 242 |
} |
| 243 |
|
| 244 |
pub fn port_count(&self) -> usize { |
| 245 |
self.client.as_ref().unwrap().get_midi_ports(PortFlags::PortIsInput).count() |
| 246 |
} |
| 247 |
|
| 248 |
pub fn port_name(&self, port: &MidiOutputPort) -> Result<String, PortInfoError> { |
| 249 |
Ok(port.name.to_string_lossy().into()) |
| 250 |
} |
| 251 |
|
| 252 |
fn activate_callback(&mut self) -> Box<OutputHandlerData> { |
| 253 |
let handler_data = Box::new(OutputHandlerData { |
| 254 |
port: None, |
| 255 |
buff_size: Ringbuffer::new(OUTPUT_RINGBUFFER_SIZE), |
| 256 |
buff_message: Ringbuffer::new(OUTPUT_RINGBUFFER_SIZE) |
| 257 |
}); |
| 258 |
|
| 259 |
let data_ptr = unsafe { mem::transmute_copy::<_, *mut OutputHandlerData>(&handler_data) }; |
| 260 |
|
| 261 |
self.client.as_mut().unwrap().set_process_callback(handle_output, data_ptr as *mut c_void); |
| 262 |
self.client.as_mut().unwrap().activate(); |
| 263 |
handler_data |
| 264 |
} |
| 265 |
|
| 266 |
pub fn connect(mut self, port: &MidiOutputPort, port_name: &str) -> Result<MidiOutputConnection, ConnectError<MidiOutput>> { |
| 267 |
let mut handler_data = self.activate_callback(); |
| 268 |
|
| 269 |
// Create port ... |
| 270 |
let source_port = match self.client.as_mut().unwrap().register_midi_port(port_name, PortFlags::PortIsOutput) { |
| 271 |
Ok(p) => p, |
| 272 |
Err(()) => { return Err(ConnectError::other("could not register JACK port", self)); } |
| 273 |
}; |
| 274 |
|
| 275 |
// ... and connect it to the input |
| 276 |
if let Err(_) = self.client.as_mut().unwrap().connect(source_port.get_name(), &port.name) { |
| 277 |
return Err(ConnectError::new(ConnectErrorKind::InvalidPort, self)); |
| 278 |
} |
| 279 |
|
| 280 |
handler_data.port = Some(source_port); |
| 281 |
|
| 282 |
Ok(MidiOutputConnection { |
| 283 |
handler_data: handler_data, |
| 284 |
client: self.client.take() |
| 285 |
}) |
| 286 |
} |
| 287 |
|
| 288 |
pub fn create_virtual( |
| 289 |
mut self, port_name: &str |
| 290 |
) -> Result<MidiOutputConnection, ConnectError<Self>> { |
| 291 |
let mut handler_data = self.activate_callback(); |
| 292 |
|
| 293 |
// Create port |
| 294 |
let port = match self.client.as_mut().unwrap().register_midi_port(port_name, PortFlags::PortIsOutput) { |
| 295 |
Ok(p) => p, |
| 296 |
Err(()) => { return Err(ConnectError::other("could not register JACK port", self)); } |
| 297 |
}; |
| 298 |
|
| 299 |
handler_data.port = Some(port); |
| 300 |
|
| 301 |
Ok(MidiOutputConnection { |
| 302 |
handler_data: handler_data, |
| 303 |
client: self.client.take() |
| 304 |
}) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
impl MidiOutputConnection { |
| 309 |
pub fn send(&mut self, message: &[u8]) -> Result<(), SendError> { |
| 310 |
let nbytes = message.len(); |
| 311 |
|
| 312 |
// Write full message to buffer |
| 313 |
let written = self.handler_data.buff_message.write(message); |
| 314 |
debug_assert!(written == nbytes, "not enough bytes written to ALSA ringbuffer `message`"); |
| 315 |
let nbytes_slice = unsafe { slice::from_raw_parts(&nbytes as *const usize as *const u8, mem::size_of_val(&nbytes)) }; |
| 316 |
let written = self.handler_data.buff_size.write(nbytes_slice); |
| 317 |
debug_assert!(written == mem::size_of_val(&nbytes), "not enough bytes written to ALSA ringbuffer `size`"); |
| 318 |
Ok(()) |
| 319 |
} |
| 320 |
|
| 321 |
pub fn close(mut self) -> MidiOutput { |
| 322 |
self.close_internal(); |
| 323 |
|
| 324 |
MidiOutput { |
| 325 |
client: self.client.take(), |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
fn close_internal(&mut self) { |
| 330 |
let port = self.handler_data.port.take().unwrap(); |
| 331 |
self.client.as_mut().unwrap().unregister_midi_port(port); |
| 332 |
self.client.as_mut().unwrap().deactivate(); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
impl Drop for MidiOutputConnection { |
| 337 |
fn drop(&mut self) { |
| 338 |
if self.client.is_some() { |
| 339 |
self.close_internal(); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
extern "C" fn handle_output(nframes: jack_nframes_t, arg: *mut c_void) -> i32 { |
| 345 |
let data: &mut OutputHandlerData = unsafe { mem::transmute(arg) }; |
| 346 |
|
| 347 |
// Is port created? |
| 348 |
if let Some(ref port) = data.port { |
| 349 |
let mut space: usize = 0; |
| 350 |
|
| 351 |
let mut buff = port.get_midi_buffer(nframes); |
| 352 |
buff.clear(); |
| 353 |
|
| 354 |
while data.buff_size.get_read_space() > 0 { |
| 355 |
let read = data.buff_size.read(&mut space as *mut usize as *mut u8, mem::size_of::<usize>()); |
| 356 |
debug_assert!(read == mem::size_of::<usize>(), "not enough bytes read from `size` ringbuffer"); |
| 357 |
let midi_data = buff.event_reserve(0, space); |
| 358 |
let read = data.buff_message.read(midi_data, space); |
| 359 |
debug_assert!(read == space, "not enough bytes read from `message` ringbuffer"); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return 0; |
| 364 |
} |