| 1 |
#![cfg(any(target_os = "macos", target_os = "ios"))] |
| 2 |
|
| 3 |
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types)] |
| 4 |
|
| 5 |
extern crate core_foundation_sys; |
| 6 |
|
| 7 |
use core_foundation_sys::string::*; |
| 8 |
use core_foundation_sys::data::*; |
| 9 |
use core_foundation_sys::dictionary::*; |
| 10 |
use core_foundation_sys::propertylist::*; |
| 11 |
|
| 12 |
use std::{ptr, mem}; |
| 13 |
|
| 14 |
include!("generated.rs"); |
| 15 |
|
| 16 |
#[inline] |
| 17 |
pub unsafe fn MIDIPacketNext(pkt: *const MIDIPacket) -> *const MIDIPacket { |
| 18 |
// Get pointer to potentially unaligned data without triggering undefined behavior |
| 19 |
// addr_of does not require creating an intermediate reference to unaligned data. |
| 20 |
let ptr = ptr::addr_of!((*pkt).data) as *const u8; |
| 21 |
let offset = (*pkt).length as isize; |
| 22 |
if cfg!(any(target_arch = "arm", target_arch = "aarch64")) { |
| 23 |
// MIDIPacket must be 4-byte aligned on ARM |
| 24 |
((ptr.offset(offset + 3) as usize) & !(3usize)) as *const MIDIPacket |
| 25 |
} else { |
| 26 |
ptr.offset(offset) as *const MIDIPacket |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
#[inline] |
| 31 |
pub unsafe fn MIDIEventPacketNext(pkt: *const MIDIEventPacket) -> *const MIDIEventPacket { |
| 32 |
// Get pointer to potentially unaligned data without triggering undefined behavior |
| 33 |
// addr_of does not require creating an intermediate reference to unaligned data. |
| 34 |
let ptr = ptr::addr_of!((*pkt).words) as *const u8; |
| 35 |
let offset = (((*pkt).wordCount as usize) * mem::size_of::<u32>()) as isize; |
| 36 |
if cfg!(any(target_arch = "arm", target_arch = "aarch64")) { |
| 37 |
// MIDIEventPacket must be 4-byte aligned on ARM |
| 38 |
((ptr.offset(offset + 3) as usize) & !(3usize)) as *const MIDIEventPacket |
| 39 |
} else { |
| 40 |
ptr.offset(offset) as *const MIDIEventPacket |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
#[allow(dead_code)] |
| 45 |
mod static_test { |
| 46 |
/// Statically assert the correct size of `MIDIPacket` and `MIDIPacketList`, |
| 47 |
/// which require non-default alignment. |
| 48 |
unsafe fn assert_sizes() { |
| 49 |
use super::{MIDIPacket, MIDIPacketList}; |
| 50 |
use std::mem::{transmute, zeroed}; |
| 51 |
|
| 52 |
let p: MIDIPacket = zeroed(); |
| 53 |
transmute::<_, [u8; 268]>(p); |
| 54 |
|
| 55 |
let p: MIDIPacketList = zeroed(); |
| 56 |
transmute::<_, [u8; 272]>(p); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
#[cfg(test)] |
| 61 |
mod tests { |
| 62 |
use super::*; |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn midi_packet_next() { |
| 66 |
const BUFFER_SIZE: usize = 65536; |
| 67 |
let buffer: &mut [u8] = &mut [0; BUFFER_SIZE]; |
| 68 |
let pkt_list_ptr = buffer.as_mut_ptr() as *mut MIDIPacketList; |
| 69 |
|
| 70 |
let packets = vec![ |
| 71 |
(1, vec![0x90, 0x40, 0x7f]), // tuple of (time, [midi bytes]) |
| 72 |
(2, vec![0x90, 0x41, 0x7f]), |
| 73 |
]; |
| 74 |
|
| 75 |
unsafe { |
| 76 |
let mut pkt_ptr = MIDIPacketListInit(pkt_list_ptr); |
| 77 |
for pkt in &packets { |
| 78 |
pkt_ptr = MIDIPacketListAdd( |
| 79 |
pkt_list_ptr, |
| 80 |
BUFFER_SIZE as ByteCount, |
| 81 |
pkt_ptr, |
| 82 |
pkt.0, |
| 83 |
pkt.1.len() as ByteCount, |
| 84 |
pkt.1.as_ptr(), |
| 85 |
); |
| 86 |
assert!(!pkt_ptr.is_null()); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
unsafe { |
| 91 |
let first_packet = &(*pkt_list_ptr).packet as *const MIDIPacket; // get pointer to first midi packet in the list |
| 92 |
let len = (*first_packet).length as usize; |
| 93 |
assert_eq!( |
| 94 |
&(*first_packet).data[0..len], |
| 95 |
&[0x90, 0x40, 0x7f] |
| 96 |
); |
| 97 |
|
| 98 |
let second_packet = MIDIPacketNext(first_packet); |
| 99 |
let len = (*second_packet).length as usize; |
| 100 |
assert_eq!( |
| 101 |
&(*second_packet).data[0..len], |
| 102 |
&[0x90, 0x41, 0x7f] |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn midi_event_packet_next() { |
| 109 |
const BUFFER_SIZE: usize = 65536; |
| 110 |
let buffer: &mut [u8] = &mut [0; BUFFER_SIZE]; |
| 111 |
let pkt_list_ptr = buffer.as_mut_ptr() as *mut MIDIEventList; |
| 112 |
|
| 113 |
let packets = vec![ |
| 114 |
(1, vec![10u32, 20]), // tuple of (time, [midi words]) |
| 115 |
(2, vec![30u32, 40, 50]), |
| 116 |
]; |
| 117 |
|
| 118 |
unsafe { |
| 119 |
let mut pkt_ptr = MIDIEventListInit(pkt_list_ptr, kMIDIProtocol_2_0 as MIDIProtocolID); |
| 120 |
for pkt in &packets { |
| 121 |
pkt_ptr = MIDIEventListAdd( |
| 122 |
pkt_list_ptr, |
| 123 |
BUFFER_SIZE as ByteCount, |
| 124 |
pkt_ptr, |
| 125 |
pkt.0, |
| 126 |
pkt.1.len() as ByteCount, |
| 127 |
pkt.1.as_ptr(), |
| 128 |
); |
| 129 |
assert!(!pkt_ptr.is_null()); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
unsafe { |
| 134 |
let first_packet = &(*pkt_list_ptr).packet as *const MIDIEventPacket; // get pointer to first midi packet in the list |
| 135 |
let len = (*first_packet).wordCount as usize; |
| 136 |
assert_eq!( |
| 137 |
&(*first_packet).words[0..len], |
| 138 |
&[10, 20] |
| 139 |
); |
| 140 |
|
| 141 |
let second_packet = MIDIEventPacketNext(first_packet); |
| 142 |
let len = (*second_packet).wordCount as usize; |
| 143 |
assert_eq!( |
| 144 |
&(*second_packet).words[0..len], |
| 145 |
&[30, 40, 50] |
| 146 |
); |
| 147 |
} |
| 148 |
} |
| 149 |
} |