Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/backend/jack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use crate::{Ignore, MidiMessage};

const OUTPUT_RINGBUFFER_SIZE: usize = 16384;

type CallbackFn<T> = dyn FnMut(u64, &[u8], &mut T) + Send;

struct InputHandlerData<T> {
port: Option<MidiPort>,
ignore_flags: Ignore,
callback: Box<dyn FnMut(u64, &[u8], &mut T) + Send>,
callback: Box<CallbackFn<T>>,
user_data: Option<T>,
}

Expand Down Expand Up @@ -135,19 +137,20 @@ impl MidiInput {
};

// ... and connect it to the output
if let Err(_) = self
if self
.client
.as_mut()
.unwrap()
.connect(&port.name, dest_port.get_name())
.is_err()
{
return Err(ConnectError::new(ConnectErrorKind::InvalidPort, self));
}

handler_data.port = Some(dest_port);

Ok(MidiInputConnection {
handler_data: handler_data,
handler_data,
client: self.client.take(),
})
}
Expand Down Expand Up @@ -179,7 +182,7 @@ impl MidiInput {
handler_data.port = Some(port);

Ok(MidiInputConnection {
handler_data: handler_data,
handler_data,
client: self.client.take(),
})
}
Expand Down Expand Up @@ -234,7 +237,7 @@ extern "C" fn handle_input<T>(nframes: jack_nframes_t, arg: *mut c_void) -> i32
for i in 0..event.size {
message
.bytes
.push(unsafe { *event.buffer.offset(i as isize) });
.push(unsafe { *event.buffer.add(i) });
}

message.timestamp = Client::get_time(); // this is in microseconds
Expand All @@ -246,7 +249,7 @@ extern "C" fn handle_input<T>(nframes: jack_nframes_t, arg: *mut c_void) -> i32
}
}

return 0;
0
}

struct OutputHandlerData {
Expand Down Expand Up @@ -356,19 +359,19 @@ impl MidiOutput {
};

// ... and connect it to the input
if let Err(_) = self
if self
.client
.as_mut()
.unwrap()
.connect(source_port.get_name(), &port.name)
.connect(source_port.get_name(), &port.name).is_err()
{
return Err(ConnectError::new(ConnectErrorKind::InvalidPort, self));
}

handler_data.port = Some(source_port);

Ok(MidiOutputConnection {
handler_data: handler_data,
handler_data,
client: self.client.take(),
})
}
Expand All @@ -395,7 +398,7 @@ impl MidiOutput {
handler_data.port = Some(port);

Ok(MidiOutputConnection {
handler_data: handler_data,
handler_data,
client: self.client.take(),
})
}
Expand Down Expand Up @@ -449,7 +452,7 @@ impl Drop for MidiOutputConnection {
}

extern "C" fn handle_output(nframes: jack_nframes_t, arg: *mut c_void) -> i32 {
let data: &mut OutputHandlerData = unsafe { mem::transmute(arg) };
let data: &mut OutputHandlerData = unsafe { &mut *(arg as *mut OutputHandlerData) };

// Is port created?
if let Some(ref port) = data.port {
Expand All @@ -475,5 +478,5 @@ extern "C" fn handle_output(nframes: jack_nframes_t, arg: *mut c_void) -> i32 {
}
}

return 0;
0
}
5 changes: 1 addition & 4 deletions src/backend/jack/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl Client {

pub fn open(name: &str, options: JackOpenOptions) -> Result<Client, ()> {
let c_name = CString::new(name)
.ok()
.expect("client name must not contain null bytes");
let result = unsafe { jack_client_open(c_name.as_ptr(), options.bits(), ptr::null_mut()) };
if result.is_null() {
Expand All @@ -66,7 +65,7 @@ impl Client {
}
}

pub fn get_midi_ports(&self, flags: PortFlags) -> PortInfos {
pub fn get_midi_ports(&self, flags: PortFlags) -> PortInfos<'_> {
let raw: *mut *const c_char = unsafe {
jack_get_ports(
self.p,
Expand All @@ -91,7 +90,6 @@ impl Client {

pub fn register_midi_port(&mut self, name: &str, flags: PortFlags) -> Result<MidiPort, ()> {
let c_name = CString::new(name)
.ok()
.expect("port name must not contain null bytes");
let result = unsafe {
jack_port_register(
Expand Down Expand Up @@ -172,7 +170,6 @@ impl<'a> Index<usize> for PortInfos<'a> {
fn index(&self, index: usize) -> &Self::Output {
let slice = self.get_c_name(index).to_bytes();
str::from_utf8(slice)
.ok()
.expect("Error converting port name to UTF8")
}
}
Expand Down