impl PartialEq for MidiInputPort { fn eq(&self, other: &Self) -> bool { iflet (Some(id1), Some(id2)) = (self.source.unique_id(), other.source.unique_id()) {
id1 == id2
} else { // Acording to macos docs "The system assigns unique IDs to all objects.", so I think we can ignore this case false
}
}
}
fn handle_input<T>(packets: &PacketList, handler_data: &mut HandlerData<T>) { let continue_sysex = &mut handler_data.continue_sysex; let ignore = handler_data.ignore_flags; let message = &mut handler_data.message; let data = &mut handler_data.user_data.as_mut().unwrap(); for p in packets.iter() { let pdata = p.data(); if pdata.len() == 0 { continue; }
letmut timestamp = p.timestamp(); if timestamp == 0 { // this might happen for asnychronous sysex messages (?)
timestamp = unsafe { external::AudioGetCurrentHostTime() };
}
if !*continue_sysex {
message.timestamp = unsafe { external::AudioConvertHostTimeToNanos(timestamp) } as u64 / 1000;
}
letmut cur_byte = 0; if *continue_sysex { // We have a continuing, segmented sysex message. if !ignore.contains(Ignore::Sysex) { // If we're not ignoring sysex messages, copy the entire packet.
message.bytes.extend_from_slice(pdata);
}
*continue_sysex = pdata[pdata.len() - 1] != 0xF7;
if !ignore.contains(Ignore::Sysex) && !*continue_sysex { // If we reached the end of the sysex, invoke the user callback
(handler_data.callback)(message.timestamp, &message.bytes, data);
message.bytes.clear();
}
} else { while cur_byte < pdata.len() { // We are expecting that the next byte in the packet is a status byte. let status = pdata[cur_byte]; if status & 0x80 == 0 { break; } // Determine the number of bytes in the MIDI message. let size; if status < 0xC0 { size = 3; } elseif status < 0xE0 { size = 2; } elseif status < 0xF0 { size = 3; } elseif status == 0xF0 { // A MIDI sysex if ignore.contains(Ignore::Sysex) {
size = 0;
cur_byte = pdata.len();
} else {
size = pdata.len() - cur_byte;
}
*continue_sysex = pdata[pdata.len() - 1] != 0xF7;
} elseif status == 0xF1 { // A MIDI time code message if ignore.contains(Ignore::Time) {
size = 0;
cur_byte += 2;
} else {
size = 2;
}
} elseif status == 0xF2 { size = 3; } elseif status == 0xF3 { size = 2; } elseif status == 0xF8 && ignore.contains(Ignore::Time) { // A MIDI timing tick message and we're ignoring it.
size = 0;
cur_byte += 1;
} elseif status == 0xFE && ignore.contains(Ignore::ActiveSense) { // A MIDI active sensing message and we're ignoring it.
size = 0;
cur_byte += 1;
} else { size = 1; }
// Copy the MIDI data to our vector. if size > 0 { let message_bytes = &pdata[cur_byte..(cur_byte + size)]; if !*continue_sysex { // This is either a non-sysex message or a non-segmented sysex message
(handler_data.callback)(message.timestamp, message_bytes, data);
message.bytes.clear();
} else { // This is the beginning of a segmented sysex message
message.bytes.extend_from_slice(message_bytes);
}
cur_byte += size;
}
}
}
}
}
pubstruct MidiInputConnection<T> {
client: Client, #[allow(dead_code)]
details: InputConnectionDetails, // TODO: get rid of Arc & Mutex? // synchronization is required because the borrow checker does not // know that the callback we're in here is never called concurrently // (always in sequence)
handler_data: Arc<Mutex<HandlerData<T>>>
}
/// This is all the data that is stored on the heap as long as a connection /// is opened and passed to the callback handler. /// /// It is important that `user_data` is the last field to not influence /// offsets after monomorphization. struct HandlerData<T> {
message: MidiMessage,
ignore_flags: Ignore,
continue_sysex: bool,
callback: Box<dyn FnMut(u64, &[u8], &mut T) + Send>,
user_data: Option<T>
}
impl PartialEq for MidiOutputPort { fn eq(&self, other: &Self) -> bool { iflet (Some(id1), Some(id2)) = (self.dest.unique_id(), other.dest.unique_id()) {
id1 == id2
} else { // Acording to macos docs "The system assigns unique IDs to all objects.", so I think we can ignore this case false
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.