mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-02-01 00:36:40 +01:00
198 lines
5.9 KiB
Rust
198 lines
5.9 KiB
Rust
use crate::*;
|
|
|
|
#[derive(Debug)] pub struct JackMidiOut {
|
|
/// Handle to JACK client, for receiving reconnect events.
|
|
jack: Jack,
|
|
/// Port name
|
|
name: Arc<str>,
|
|
/// Port handle.
|
|
port: Port<MidiOut>,
|
|
/// List of ports to connect to.
|
|
conn: Vec<PortConnect>,
|
|
/// List of currently held notes.
|
|
held: Arc<RwLock<[bool;128]>>,
|
|
/// Buffer
|
|
note_buffer: Vec<u8>,
|
|
/// Buffer
|
|
output_buffer: Vec<Vec<Vec<u8>>>,
|
|
}
|
|
|
|
has!(Jack: |self: JackMidiOut|self.jack);
|
|
|
|
impl JackMidiOut {
|
|
pub fn new (jack: &Jack, name: impl AsRef<str>, connect: &[PortConnect])
|
|
-> Usually<Self>
|
|
{
|
|
let jack = jack.clone();
|
|
let port = jack.register_port::<MidiOut>(name.as_ref())?;
|
|
let name = name.as_ref().into();
|
|
let conn = connect.to_vec();
|
|
let port = Self {
|
|
jack,
|
|
port,
|
|
name,
|
|
conn,
|
|
held: Arc::new([false;128].into()),
|
|
note_buffer: vec![0;8],
|
|
output_buffer: vec![vec![];65536],
|
|
};
|
|
port.connect_to_matching()?;
|
|
Ok(port)
|
|
}
|
|
pub fn name (&self) -> &Arc<str> {
|
|
&self.name
|
|
}
|
|
pub fn port (&self) -> &Port<MidiOut> {
|
|
&self.port
|
|
}
|
|
pub fn port_mut (&mut self) -> &mut Port<MidiOut> {
|
|
&mut self.port
|
|
}
|
|
pub fn into_port (self) -> Port<MidiOut> {
|
|
self.port
|
|
}
|
|
pub fn close (self) -> Usually<()> {
|
|
let Self { jack, port, .. } = self;
|
|
Ok(jack.with_client(|client|client.unregister_port(port))?)
|
|
}
|
|
/// Clear the section of the output buffer that we will be using,
|
|
/// emitting "all notes off" at start of buffer if requested.
|
|
pub fn buffer_clear (&mut self, scope: &ProcessScope, reset: bool) {
|
|
let n_frames = (scope.n_frames() as usize).min(self.output_buffer.len());
|
|
for frame in &mut self.output_buffer[0..n_frames] {
|
|
frame.clear();
|
|
}
|
|
if reset {
|
|
all_notes_off(&mut self.output_buffer);
|
|
}
|
|
}
|
|
/// Write a note to the output buffer
|
|
pub fn buffer_write <'a> (
|
|
&'a mut self,
|
|
sample: usize,
|
|
event: LiveEvent,
|
|
) {
|
|
self.note_buffer.fill(0);
|
|
event.write(&mut self.note_buffer).expect("failed to serialize MIDI event");
|
|
self.output_buffer[sample].push(self.note_buffer.clone());
|
|
// Update the list of currently held notes.
|
|
if let LiveEvent::Midi { ref message, .. } = event {
|
|
update_keys(&mut*self.held.write().unwrap(), message);
|
|
}
|
|
}
|
|
/// Write a chunk of MIDI data from the output buffer to the output port.
|
|
pub fn buffer_emit (&mut self, scope: &ProcessScope) {
|
|
let samples = scope.n_frames() as usize;
|
|
let mut writer = self.port.writer(scope);
|
|
for (time, events) in self.output_buffer.iter().enumerate().take(samples) {
|
|
for bytes in events.iter() {
|
|
writer.write(&RawMidi { time: time as u32, bytes }).unwrap_or_else(|_|{
|
|
panic!("Failed to write MIDI data: {bytes:?}");
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AsRef<Port<MidiOut>> for JackMidiOut {
|
|
fn as_ref (&self) -> &Port<MidiOut> {
|
|
&self.port
|
|
}
|
|
}
|
|
|
|
impl JackPort for JackMidiOut {
|
|
type Port = MidiOut;
|
|
type Pair = MidiIn;
|
|
fn port (&self) -> &Port<MidiOut> { &self.port }
|
|
}
|
|
|
|
impl JackPortConnect<&str> for JackMidiOut {
|
|
fn connect_to (&self, to: &str) -> Usually<PortConnectStatus> {
|
|
self.with_client(|c|if let Some(ref port) = c.port_by_name(to.as_ref()) {
|
|
self.connect_to(port)
|
|
} else {
|
|
Ok(Missing)
|
|
})
|
|
}
|
|
}
|
|
|
|
impl JackPortConnect<&Port<Unowned>> for JackMidiOut {
|
|
fn connect_to (&self, port: &Port<Unowned>) -> Usually<PortConnectStatus> {
|
|
self.with_client(|c|Ok(if let Ok(_) = c.connect_ports(&self.port, port) {
|
|
Connected
|
|
} else if let Ok(_) = c.connect_ports(port, &self.port) {
|
|
Connected
|
|
} else {
|
|
Mismatch
|
|
}))
|
|
}
|
|
}
|
|
|
|
impl JackPortConnect<&Port<MidiIn>> for JackMidiOut {
|
|
fn connect_to (&self, port: &Port<MidiIn>) -> Usually<PortConnectStatus> {
|
|
self.with_client(|c|Ok(if let Ok(_) = c.connect_ports(&self.port, port) {
|
|
Connected
|
|
} else if let Ok(_) = c.connect_ports(port, &self.port) {
|
|
Connected
|
|
} else {
|
|
Mismatch
|
|
}))
|
|
}
|
|
}
|
|
|
|
impl JackPortAutoconnect for JackMidiOut {
|
|
fn conn (&self) -> &[PortConnect] {
|
|
&self.conn
|
|
}
|
|
}
|
|
|
|
#[tengri_proc::command(JackMidiOut)]
|
|
impl MidiOutputCommand {
|
|
}
|
|
|
|
impl<T: Has<Vec<JackMidiOut>>> HasMidiOuts for T {
|
|
fn midi_outs (&self) -> &Vec<JackMidiOut> {
|
|
self.get()
|
|
}
|
|
fn midi_outs_mut (&mut self) -> &mut Vec<JackMidiOut> {
|
|
self.get_mut()
|
|
}
|
|
}
|
|
|
|
|
|
/// Trait for thing that may output MIDI.
|
|
pub trait HasMidiOuts {
|
|
fn midi_outs (&self) -> &Vec<JackMidiOut>;
|
|
fn midi_outs_mut (&mut self) -> &mut Vec<JackMidiOut>;
|
|
fn midi_outs_with_sizes <'a> (&'a self) ->
|
|
impl Iterator<Item=(usize, &Arc<str>, &[PortConnect], usize, usize)> + Send + Sync + 'a
|
|
{
|
|
let mut y = 0;
|
|
self.midi_outs().iter().enumerate().map(move|(i, output)|{
|
|
let height = 1 + output.conn().len();
|
|
let data = (i, output.name(), output.conn(), y, y + height);
|
|
y += height;
|
|
data
|
|
})
|
|
}
|
|
fn midi_outs_emit (&mut self, scope: &ProcessScope) {
|
|
for port in self.midi_outs_mut().iter_mut() {
|
|
port.buffer_emit(scope)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Trail for thing that may gain new MIDI ports.
|
|
impl<T: HasMidiOuts + HasJack> AddMidiOut for T {
|
|
fn midi_out_add (&mut self) -> Usually<()> {
|
|
let index = self.midi_outs().len();
|
|
let port = JackMidiOut::new(self.jack(), &format!("{index}/M"), &[])?;
|
|
self.midi_outs_mut().push(port);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// May create new MIDI output ports.
|
|
pub trait AddMidiOut {
|
|
fn midi_out_add (&mut self) -> Usually<()>;
|
|
}
|