Skip to content

Commit

Permalink
Merge pull request #9 from Boddlnagg/buffer-rewrite
Browse files Browse the repository at this point in the history
PacketBuffer rewrite
  • Loading branch information
chris-zen authored Sep 13, 2017
2 parents 46f10d1 + 2e6b442 commit 6087394
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 248 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ keywords = ["CoreMIDI", "MIDI", "OSX", "music"]
[dependencies]
core-foundation-sys = "0.2"
core-foundation = "0.2"
coremidi-sys = "1.0.0"
# coremidi-sys = { git = "https://github.com/chris-zen/coremidi-sys", branch="fix-packed-structs" }
coremidi-sys = "2.0"
time = "0.1"
libc = "0.2"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::thread;
let client = Client::new("example-client").unwrap();
let output_port = client.output_port("example-port").unwrap();
let destination = Destinations::from_index(0).unwrap();
let note_on = PacketBuffer::from_data(0, vec![0x90, 0x40, 0x7f]);
let note_off = PacketBuffer::from_data(0, vec![0x80, 0x40, 0x7f]);
let note_on = PacketBuffer::new(0, &[0x90, 0x40, 0x7f]);
let note_off = PacketBuffer::new(0, &[0x80, 0x40, 0x7f]);
output_port.send(&destination, &note_on).unwrap();
thread::sleep(Duration::from_millis(1000));
output_port.send(&destination, &note_off).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions examples/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ fn print_destinations() {
}

fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = vec![
let data = &[
0x90 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
coremidi::PacketBuffer::from_data(0, data)
coremidi::PacketBuffer::new(0, data)
}

fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = vec![
let data = &[
0x80 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
coremidi::PacketBuffer::from_data(0, data)
coremidi::PacketBuffer::new(0, data)
}
8 changes: 4 additions & 4 deletions examples/virtual-source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ fn main() {
}

fn create_note_on(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = vec![
let data = &[
0x90 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
coremidi::PacketBuffer::from_data(0, data)
coremidi::PacketBuffer::new(0, data)
}

fn create_note_off(channel: u8, note: u8, velocity: u8) -> coremidi::PacketBuffer {
let data = vec![
let data = &[
0x80 | (channel & 0x0f),
note & 0x7f,
velocity & 0x7f];
coremidi::PacketBuffer::from_data(0, data)
coremidi::PacketBuffer::new(0, data)
}
15 changes: 6 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use core_foundation::base::{OSStatus, TCFType};

use coremidi_sys::{
MIDIClientRef, MIDIClientCreate, MIDIClientDispose, MIDINotification,
MIDIPortRef, MIDIOutputPortCreate, MIDIEndpointRef, MIDISourceCreate
};

use coremidi_sys_ext::{
MIDIPortRef, MIDIOutputPortCreate, MIDIEndpointRef, MIDISourceCreate,
MIDIPacketList, MIDIInputPortCreate, MIDIDestinationCreate
};

Expand Down Expand Up @@ -151,7 +148,7 @@ impl Client {

extern "C" fn notify_proc(
notification_ptr: *const MIDINotification,
ref_con: *mut ::libc::c_void) {
ref_con: *mut ::std::os::raw::c_void) {

let _ = ::std::panic::catch_unwind(|| unsafe {
match Notification::from(&*notification_ptr) {
Expand All @@ -165,12 +162,12 @@ impl Client {

extern "C" fn read_proc(
pktlist: *const MIDIPacketList,
read_proc_ref_con: *mut ::libc::c_void,
_: *mut ::libc::c_void) { //srcConnRefCon
read_proc_ref_con: *mut ::std::os::raw::c_void,
_: *mut ::std::os::raw::c_void) { //srcConnRefCon

let _ = ::std::panic::catch_unwind(|| unsafe {
let packet_list = PacketList(pktlist);
BoxedCallback::call_from_raw_ptr(read_proc_ref_con, &packet_list);
let packet_list = &*(pktlist as *const PacketList);
BoxedCallback::call_from_raw_ptr(read_proc_ref_con, packet_list);
});
}
}
Expand Down
79 changes: 0 additions & 79 deletions src/coremidi_sys_ext.rs

This file was deleted.

8 changes: 2 additions & 6 deletions src/endpoints/sources.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use core_foundation_sys::base::OSStatus;

use coremidi_sys::{
MIDIGetNumberOfSources, MIDIGetSource, MIDIEndpointDispose, ItemCount
};

use coremidi_sys_ext::{
MIDIReceived
MIDIGetNumberOfSources, MIDIGetSource, MIDIReceived, MIDIEndpointDispose, ItemCount
};

use std::ops::Deref;
Expand Down Expand Up @@ -100,7 +96,7 @@ impl VirtualSource {
pub fn received(&self, packet_list: &PacketList) -> Result<(), OSStatus> {
let status = unsafe { MIDIReceived(
self.endpoint.object.0,
packet_list.0)
packet_list.as_ptr())
};
if status == 0 { Ok(()) } else { Err(status) }
}
Expand Down
43 changes: 28 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::thread;
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 note_on = coremidi::PacketBuffer::from_data(0, vec![0x90, 0x40, 0x7f]);
let note_off = coremidi::PacketBuffer::from_data(0, vec![0x80, 0x40, 0x7f]);
let note_on = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]);
let note_off = coremidi::PacketBuffer::new(0, &[0x80, 0x40, 0x7f]);
output_port.send(&destination, &note_on).unwrap();
thread::sleep(Duration::from_millis(1000));
output_port.send(&destination, &note_off).unwrap();
Expand All @@ -41,16 +41,11 @@ For handling low level MIDI data you may look into:
extern crate core_foundation_sys;
extern crate core_foundation;
extern crate coremidi_sys;
extern crate libc;

use core_foundation_sys::base::OSStatus;

use coremidi_sys::{
MIDIObjectRef, MIDIFlushOutput, MIDIRestart
};

use coremidi_sys_ext::{
MIDIPacketList
MIDIObjectRef, MIDIFlushOutput, MIDIRestart, MIDIPacket, MIDIPacketList
};

/// A [MIDI Object](https://developer.apple.com/reference/coremidi/midiobjectref).
Expand Down Expand Up @@ -89,12 +84,12 @@ impl<T> BoxedCallback<T> {
BoxedCallback(::std::ptr::null_mut())
}

fn raw_ptr(&mut self) -> *mut ::libc::c_void {
self.0 as *mut ::libc::c_void
fn raw_ptr(&mut self) -> *mut ::std::os::raw::c_void {
self.0 as *mut ::std::os::raw::c_void
}

// must not be null
unsafe fn call_from_raw_ptr(raw_ptr: *mut ::libc::c_void, arg: &T) {
unsafe fn call_from_raw_ptr(raw_ptr: *mut ::std::os::raw::c_void, arg: &T) {
let callback = &mut *(raw_ptr as *mut Box<FnMut(&T)>);
callback(arg);
}
Expand Down Expand Up @@ -126,7 +121,7 @@ pub struct Port { object: Object }
/// 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::from_data(0, vec![0x90, 0x40, 0x7f]);
/// let packets = coremidi::PacketBuffer::new(0, &[0x90, 0x40, 0x7f]);
/// output_port.send(&destination, &packets).unwrap();
/// ```
#[derive(Debug)]
Expand Down Expand Up @@ -219,10 +214,28 @@ pub struct Device { object: Object }

/// A [list of MIDI events](https://developer.apple.com/reference/coremidi/midipacketlist) being received from, or being sent to, one endpoint.
///
#[derive(PartialEq)]
pub struct PacketList(*const MIDIPacketList);
#[repr(C)]
pub struct PacketList {
// NOTE: This type must only exist in the form of immutable references
// pointing to valid instances of MIDIPacketList.
// This type must NOT implement `Copy`!
inner: PacketListInner,
_do_not_construct: packets::alignment::Marker
}

mod coremidi_sys_ext;
#[repr(packed)]
struct PacketListInner {
num_packets: u32,
data: [MIDIPacket; 0]
}

impl PacketList {
/// For internal usage only.
/// Requires this instance to actually point to a valid MIDIPacketList
unsafe fn as_ptr(&self) -> *mut MIDIPacketList {
self as *const PacketList as *mut PacketList as *mut MIDIPacketList
}
}

mod object;
mod devices;
Expand Down
4 changes: 2 additions & 2 deletions src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub enum Notification {

impl Notification {
pub fn from(notification: &MIDINotification) -> Result<Notification, i32> {
match notification.messageID as ::libc::c_uint {
match notification.messageID as ::std::os::raw::c_uint {
kMIDIMsgSetupChanged => Ok(Notification::SetupChanged),
kMIDIMsgObjectAdded | kMIDIMsgObjectRemoved => Self::from_object_added_removed(notification),
kMIDIMsgPropertyChanged => Self::from_property_changed(notification),
Expand All @@ -84,7 +84,7 @@ impl Notification {
child: Object(add_remove_notification.child),
child_type: child_type.unwrap()
};
match notification.messageID as ::libc::c_uint {
match notification.messageID as ::std::os::raw::c_uint {
kMIDIMsgObjectAdded => Ok(Notification::ObjectAdded(add_remove_info)),
kMIDIMsgObjectRemoved => Ok(Notification::ObjectRemoved(add_remove_info)),
_ => Err(0) // Never reached
Expand Down
Loading

0 comments on commit 6087394

Please sign in to comment.