support multiple midi ins in PhrasePlayer::process

This commit is contained in:
🪞👃🪞 2024-10-31 23:50:16 +02:00
parent 325492ec42
commit 02f691c494
3 changed files with 51 additions and 66 deletions

View file

@ -179,7 +179,7 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
.map(|port|port.short_name()) .map(|port|port.short_name())
.transpose()? .transpose()?
.unwrap_or("(none)".into()); .unwrap_or("(none)".into());
let input = format!("i {}", input_name); let input = format!(">{}", input_name);
col!(name, input) col!(name, input)
.min_xy(w as u16, track_title_h) .min_xy(w as u16, track_title_h)
.bg(track.color) .bg(track.color)
@ -201,7 +201,7 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
.map(|port|port.short_name()) .map(|port|port.short_name())
.transpose()? .transpose()?
.unwrap_or("(none)".into()); .unwrap_or("(none)".into());
let output = format!("o {}", output_name); let output = format!("<{}", output_name);
col!(until_next, elapsed, output) col!(until_next, elapsed, output)
.min_xy(w as u16, tracks_footer) .min_xy(w as u16, tracks_footer)
.bg(track.color) .bg(track.color)

View file

@ -423,7 +423,7 @@ impl<E: Engine> PhrasePlayer<E> {
pub fn toggle_monitor (&mut self) { self.monitoring = !self.monitoring; } pub fn toggle_monitor (&mut self) { self.monitoring = !self.monitoring; }
pub fn toggle_record (&mut self) { self.recording = !self.recording; } pub fn toggle_record (&mut self) { self.recording = !self.recording; }
pub fn toggle_overdub (&mut self) { self.overdub = !self.overdub; } pub fn toggle_overdub (&mut self) { self.overdub = !self.overdub; }
pub fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) { pub fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
let start = self.clock.next_launch_pulse(); let start = self.clock.next_launch_pulse();
self.next_phrase = Some((start.into(), phrase.map(|p|p.clone()))); self.next_phrase = Some((start.into(), phrase.map(|p|p.clone())));
self.reset = true; self.reset = true;
@ -433,6 +433,21 @@ impl<E: Engine> PhrasePlayer<E> {
.map(|(started,_)|started.load(Ordering::Relaxed)) .map(|(started,_)|started.load(Ordering::Relaxed))
.map(|started|started - self.clock.sample()) .map(|started|started - self.clock.sample())
} }
pub fn playing_phrase (&self) -> Option<(usize, Arc<RwLock<Phrase>>)> {
if let (
Some(TransportState::Rolling),
Some((start_frame, _)),
Some((_started, Some(ref phrase)))
) = (
*self.clock.playing.read().unwrap(),
self.started,
&self.phrase
) {
Some((start_frame, phrase.clone()))
} else {
None
}
}
} }
/// Displays and edits phrase length /// Displays and edits phrase length
pub struct PhraseLength<E: Engine> { pub struct PhraseLength<E: Engine> {

View file

@ -10,47 +10,37 @@ impl<E: Engine> Audio for Sequencer<E> {
impl<E: Engine> PhrasePlayer<E> { impl<E: Engine> PhrasePlayer<E> {
pub fn process ( pub fn process (
&mut self, &mut self,
input: Option<MidiIter>,
scope: &ProcessScope, scope: &ProcessScope,
(frame0, frames): (usize, usize), (frame0, frames): (usize, usize),
(_usec0, _usecs): (usize, usize), (_usec0, _usecs): (usize, usize),
period: f64, period: f64,
) { ) {
let has_midi_inputs = self.has_midi_inputs();
let has_midi_outputs = self.has_midi_outputs(); let has_midi_outputs = self.has_midi_outputs();
let quant = self.clock.quant();
if has_midi_outputs { if has_midi_outputs {
self.clear_midi_out_buf(frames); self.clear_midi_out_buf(frames);
self.reset_midi_out_buf(false /* FIXME where did force-reset come from? */); self.reset_midi_out_buf(false /* FIXME where did force-reset come from? */);
} }
if let ( let has_midi_inputs = self.has_midi_inputs();
Some(TransportState::Rolling), let quant = self.clock.quant();
Some((start_frame, _)), if let Some((start_frame, phrase)) = self.playing_phrase() {
Some((_started, Some(ref phrase)))
) = (
*self.clock.playing.read().unwrap(),
self.started,
&self.phrase
) {
phrase.read().map(|phrase|{ phrase.read().map(|phrase|{
if has_midi_outputs { if has_midi_outputs {
phrase.process_out( phrase.process_out(
&mut self.midi_out_buf, &mut self.midi_out_buf, &mut self.notes_out.write().unwrap(),
&mut self.notes_out.write().unwrap(), &self.clock.timebase, (frame0.saturating_sub(start_frame), frames, period)
&self.clock.timebase,
(frame0.saturating_sub(start_frame), frames, period)
); );
} }
}).unwrap(); }).unwrap();
let mut phrase = phrase.write().unwrap(); let mut phrase = phrase.write().unwrap();
let length = phrase.length; let length = phrase.length;
// Monitor and record input // Monitor and record input
if input.is_some() && (self.recording || self.monitoring) { if has_midi_inputs && (self.recording || self.monitoring) {
// For highlighting keys and note repeat // For highlighting keys and note repeat
let mut notes_in = self.notes_in.write().unwrap(); let mut notes_in = self.notes_in.write().unwrap();
for (frame, event, bytes) in parse_midi_input(input.unwrap()) { // Record from each input
match event { for input in self.midi_inputs.iter() {
LiveEvent::Midi { message, .. } => { for (frame, event, bytes) in parse_midi_input(input.iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
if self.monitoring { if self.monitoring {
self.midi_out_buf[frame].push(bytes.to_vec()) self.midi_out_buf[frame].push(bytes.to_vec())
} }
@ -66,37 +56,20 @@ impl<E: Engine> PhrasePlayer<E> {
looped looped
}, message); }, message);
} }
match message { update_keys(&mut notes_in, &message);
MidiMessage::NoteOn { key, .. } => { }
notes_in[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
notes_in[key.as_int() as usize] = false;
},
_ => {}
}
},
_ => {}
} }
} }
} }
} else if input.is_some() && has_midi_outputs && self.monitoring { } else if has_midi_inputs && has_midi_outputs && self.monitoring {
let mut notes_in = self.notes_in.write().unwrap(); let mut notes_in = self.notes_in.write().unwrap();
for (frame, event, bytes) in parse_midi_input(input.unwrap()) { for input in self.midi_inputs.iter() {
match event { // Monitor each input
LiveEvent::Midi { message, .. } => { for (frame, event, bytes) in parse_midi_input(input.iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
self.midi_out_buf[frame].push(bytes.to_vec()); self.midi_out_buf[frame].push(bytes.to_vec());
match message { update_keys(&mut notes_in, &message);
MidiMessage::NoteOn { key, .. } => { }
notes_in[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
notes_in[key.as_int() as usize] = false;
},
_ => {}
}
},
_ => {}
} }
} }
} }
@ -112,17 +85,11 @@ impl<E: Engine> PhrasePlayer<E> {
} }
} }
} }
pub fn has_midi_inputs (&self) -> bool { pub fn has_midi_inputs (&self) -> bool { self.midi_inputs.len() > 0 }
self.midi_inputs.len() > 0 pub fn has_midi_outputs (&self) -> bool { self.midi_outputs.len() > 0 }
}
pub fn has_midi_outputs (&self) -> bool {
self.midi_outputs.len() > 0
}
/// Clear the section of the output buffer that we will be using /// Clear the section of the output buffer that we will be using
pub fn clear_midi_out_buf (&mut self, frames: usize) { pub fn clear_midi_out_buf (&mut self, frames: usize) {
for frame in &mut self.midi_out_buf[0..frames] { for frame in &mut self.midi_out_buf[0..frames] { frame.clear(); }
frame.clear();
}
} }
/// Emit "all notes off" at start of buffer if requested /// Emit "all notes off" at start of buffer if requested
pub fn reset_midi_out_buf (&mut self, force_reset: bool) { pub fn reset_midi_out_buf (&mut self, force_reset: bool) {
@ -144,9 +111,8 @@ impl Phrase {
(frame0, frames, _): (usize, usize, f64), (frame0, frames, _): (usize, usize, f64),
) { ) {
let mut buf = Vec::with_capacity(8); let mut buf = Vec::with_capacity(8);
for (time, tick) in Ticks(timebase.pulses_per_sample()).between_samples( let ticks = Ticks(timebase.pulses_per_sample()).between_samples(frame0, frame0 + frames);
frame0, frame0 + frames for (time, tick) in ticks {
) {
let tick = tick % self.length; let tick = tick % self.length;
for message in self.notes[tick].iter() { for message in self.notes[tick].iter() {
buf.clear(); buf.clear();
@ -154,11 +120,7 @@ impl Phrase {
let message = *message; let message = *message;
LiveEvent::Midi { channel, message }.write(&mut buf).unwrap(); LiveEvent::Midi { channel, message }.write(&mut buf).unwrap();
output[time as usize].push(buf.clone()); output[time as usize].push(buf.clone());
match message { update_keys(notes_on, &message);
MidiMessage::NoteOn { key, .. } => notes_on[key.as_int() as usize] = true,
MidiMessage::NoteOff { key, .. } => notes_on[key.as_int() as usize] = false,
_ => {}
}
} }
} }
} }
@ -179,3 +141,11 @@ pub fn parse_midi_input (input: MidiIter) -> Box<dyn Iterator<Item=(usize, LiveE
bytes bytes
))) )))
} }
/// Update notes_in array
pub fn update_keys (keys: &mut[bool;128], message: &MidiMessage) {
match message {
MidiMessage::NoteOn { key, .. } => { keys[key.as_int() as usize] = true; }
MidiMessage::NoteOff { key, .. } => { keys[key.as_int() as usize] = false; },
_ => {}
}
}