wip: fixing port autoconnect

This commit is contained in:
🪞👃🪞 2024-07-11 14:15:29 +03:00
parent 6979fd67ec
commit 32dc708096
5 changed files with 98 additions and 94 deletions

View file

@ -14,7 +14,7 @@ pub struct Track {
/// Phrase selector
pub sequence: Option<usize>,
/// Output from current sequence.
pub midi_out: Port<MidiOut>,
pub midi_out: Option<Port<MidiOut>>,
midi_out_buf: Vec<Vec<Vec<u8>>>,
/// Device chain
pub devices: Vec<JackDevice>,
@ -28,25 +28,15 @@ pub struct Track {
/// Highlight keys on piano roll.
pub notes_out: [bool;128],
}
ports!(Track {
audio: {
outs: |_t|Ok(vec![]),
}
midi: {
ins: |_t|Ok(vec![]),
outs: |_t|Ok(vec![]),
}
});
impl Track {
pub fn new (
name: &str,
jack: &Client,
phrases: Option<Vec<Phrase>>,
devices: Option<Vec<JackDevice>>,
) -> Usually<Self> {
Ok(Self {
name: name.to_string(),
midi_out: jack.register_port(name, MidiOut)?,
midi_out: None,
midi_out_buf: vec![vec![];16384],
notes_in: [false;128],
notes_out: [false;128],
@ -75,9 +65,25 @@ impl Track {
pub fn first_device (&self) -> Option<RwLockReadGuard<Box<dyn Device>>> {
self.get_device(0)
}
pub fn connect_first_device (&self) -> Usually<()> {
if let (Some(port), Some(device)) = (&self.midi_out, self.devices.get(0)) {
device.client.as_client().connect_ports(&port, &device.midi_ins()?[0])?;
}
Ok(())
}
pub fn last_device (&self) -> Option<RwLockReadGuard<Box<dyn Device>>> {
self.get_device(self.devices.len().saturating_sub(1))
}
pub fn connect_last_device (&self, app: &App) -> Usually<()> {
Ok(match self.devices.get(self.devices.len().saturating_sub(1)) {
Some(device) => {
app.audio_out(0).map(|left|device.connect_audio_out(0, &left)).transpose()?;
app.audio_out(1).map(|right|device.connect_audio_out(0, &right)).transpose()?;
()
},
None => ()
})
}
pub fn add_device (&mut self, device: JackDevice) -> Usually<&mut JackDevice> {
self.devices.push(device);
let index = self.devices.len() - 1;
@ -125,7 +131,7 @@ impl Track {
}
pub fn process (
&mut self,
input: MidiIter,
input: Option<MidiIter>,
timebase: &Arc<Timebase>,
playing: Option<TransportState>,
started: Option<(usize, usize)>,
@ -161,9 +167,9 @@ impl Track {
(frame0.saturating_sub(start_frame), frames, period)
);
// Monitor and record input
if self.recording || self.monitoring {
if input.is_some() && (self.recording || self.monitoring) {
// For highlighting keys and note repeat
for (frame, event, bytes) in parse_midi_input(input) {
for (frame, event, bytes) in parse_midi_input(input.unwrap()) {
match event {
LiveEvent::Midi { message, .. } => {
if self.monitoring {
@ -195,16 +201,14 @@ impl Track {
}
}
}
} else if self.monitoring {
for (frame, event, bytes) in parse_midi_input(input) {
} else if input.is_some() && self.monitoring {
for (frame, event, bytes) in parse_midi_input(input.unwrap()) {
self.process_monitor_event(frame, &event, bytes)
}
}
write_midi_output(
&mut self.midi_out.writer(scope),
&self.midi_out_buf,
frames
);
if let Some(out) = &mut self.midi_out {
write_midi_output(&mut out.writer(scope), &self.midi_out_buf, frames);
}
}
#[inline]