mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: port: make device
This commit is contained in:
parent
447638ee71
commit
cb7e4f7a95
31 changed files with 602 additions and 865 deletions
|
|
@ -1,152 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
//impl_port!(MidiInput: MidiOut -> MidiIn |j, n|j.register_port::<MidiOut>(n));
|
||||
|
||||
#[derive(Debug)] pub struct MidiInput<'j> {
|
||||
/// Handle to JACK client, for receiving reconnect events.
|
||||
jack: Jack<'j>,
|
||||
/// Port name
|
||||
name: Arc<str>,
|
||||
/// Port handle.
|
||||
port: Port<MidiOut>,
|
||||
/// List of ports to connect to.
|
||||
connections: Vec<Connect>
|
||||
}
|
||||
impl<'j> AsRef<Port<MidiOut>> for MidiInput<'j> {
|
||||
fn as_ref (&self) -> &Port<MidiOut> { &self.port }
|
||||
}
|
||||
impl<'j> MidiInput<'j> {
|
||||
pub fn new (jack: &Jack, name: impl AsRef<str>, connect: &[Connect])
|
||||
-> Usually<Self>
|
||||
{
|
||||
let port = Self {
|
||||
port: jack.register_port::<MidiIn>(name.as_ref())?,
|
||||
jack,
|
||||
name: name.into(),
|
||||
connections: connect.to_vec()
|
||||
};
|
||||
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))?)
|
||||
}
|
||||
pub fn parsed <'a> (&'a self, scope: &'a ProcessScope) -> impl Iterator<Item=(usize, LiveEvent<'a>, &'a [u8])> {
|
||||
parse_midi_input(self.port().iter(scope))
|
||||
}
|
||||
}
|
||||
impl<'j> HasJack<'j> for MidiInput<'j> {
|
||||
fn jack (&self) -> &'j Jack<'j> { &self.jack }
|
||||
}
|
||||
impl<'j> JackPort<'j> for MidiInput<'j> {
|
||||
type Port = MidiIn;
|
||||
type Pair = MidiOut;
|
||||
fn port (&self) -> &Port<MidiOut> { &self.port }
|
||||
}
|
||||
//impl<'j, T: AsRef<str>> ConnectTo<'j, T> for MidiInput<'j> {
|
||||
//fn connect_to (&self, to: &T) -> Usually<ConnectStatus> {
|
||||
//self.with_client(|c|if let Some(ref port) = c.port_by_name(to.as_ref()) {
|
||||
//self.connect_to(port)
|
||||
//} else {
|
||||
//Ok(Missing)
|
||||
//})
|
||||
//}
|
||||
//}
|
||||
connect_to!(<'j>|self: MidiInput<'j>, port: &str|{
|
||||
self.with_client(|c|if let Some(ref port) = c.port_by_name(port.as_ref()) {
|
||||
self.connect_to(port)
|
||||
} else {
|
||||
Ok(Missing)
|
||||
})
|
||||
});
|
||||
connect_to!(<'j>|self: MidiInput<'j>, port: Port<MidiOut>|{
|
||||
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
|
||||
}))
|
||||
});
|
||||
connect_to!(<'j>|self: MidiInput<'j>, port: Port<Unowned>|{
|
||||
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<'j> ConnectAuto<'j> for MidiInput<'j> {
|
||||
fn connections (&self) -> &[Connect] {
|
||||
&self.connections
|
||||
}
|
||||
}
|
||||
|
||||
#[tengri_proc::command(MidiInput)]
|
||||
impl MidiInputCommand {
|
||||
//fn _todo_ (_port: &mut MidiInput) -> Perhaps<Self> { Ok(None) }
|
||||
}
|
||||
|
||||
impl<T: Has<Vec<MidiInput>>> HasMidiIns for T {
|
||||
fn midi_ins (&self) -> &Vec<MidiInput> {
|
||||
self.get()
|
||||
}
|
||||
fn midi_ins_mut (&mut self) -> &mut Vec<MidiInput> {
|
||||
self.get_mut()
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for thing that may receive MIDI.
|
||||
pub trait HasMidiIns {
|
||||
fn midi_ins (&self) -> &Vec<MidiInput>;
|
||||
fn midi_ins_mut (&mut self) -> &mut Vec<MidiInput>;
|
||||
/// Collect MIDI input from app ports (TODO preallocate large buffers)
|
||||
fn midi_input_collect <'a> (&'a self, scope: &'a ProcessScope) -> CollectedMidiInput<'a> {
|
||||
self.midi_ins().iter()
|
||||
.map(|port|port.port().iter(scope)
|
||||
.map(|RawMidi { time, bytes }|(time, LiveEvent::parse(bytes)))
|
||||
.collect::<Vec<_>>())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
fn midi_ins_with_sizes <'a> (&'a self) ->
|
||||
impl Iterator<Item=(usize, &Arc<str>, &[Connect], usize, usize)> + Send + Sync + 'a
|
||||
{
|
||||
let mut y = 0;
|
||||
self.midi_ins().iter().enumerate().map(move|(i, input)|{
|
||||
let height = 1 + input.conn().len();
|
||||
let data = (i, input.name(), input.conn(), y, y + height);
|
||||
y += height;
|
||||
data
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub type CollectedMidiInput<'a> = Vec<Vec<(u32, Result<LiveEvent<'a>, MidiError>)>>;
|
||||
|
||||
impl<'j, T: HasMidiIns + HasJack<'j>> AddMidiIn for T {
|
||||
fn midi_in_add (&mut self) -> Usually<()> {
|
||||
let index = self.midi_ins().len();
|
||||
let port = MidiInput::new(self.jack(), &format!("M/{index}"), &[])?;
|
||||
self.midi_ins_mut().push(port);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// May create new MIDI input ports.
|
||||
pub trait AddMidiIn {
|
||||
fn midi_in_add (&mut self) -> Usually<()>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue