optimize track callback

This commit is contained in:
🪞👃🪞 2024-07-07 00:17:49 +03:00
parent e98c110dbe
commit acb952736e
2 changed files with 52 additions and 29 deletions

View file

@ -141,40 +141,61 @@ impl Track {
} else if reset { } else if reset {
all_notes_off(&mut self.midi_out_buf); all_notes_off(&mut self.midi_out_buf);
} }
// Play from phrase into output buffer // For highlighting keys and note repeat
// Currently playing phrase
let phrase = &mut self.sequence.map(|id|self.phrases.get_mut(id)).flatten(); let phrase = &mut self.sequence.map(|id|self.phrases.get_mut(id)).flatten();
if playing == Some(TransportState::Rolling) { if playing == Some(TransportState::Rolling) {
if let Some((start_frame, start_usec)) = started { if let (Some(phrase), Some((start_frame, _))) = (phrase, started) {
if let Some(phrase) = phrase { // Output playback from sequencer
phrase.process_out( phrase.process_out(
&mut self.midi_out_buf, &mut self.midi_out_buf,
&mut self.notes_on, &mut self.notes_on,
timebase, timebase,
(frame0.saturating_sub(start_frame), frames, period) (frame0.saturating_sub(start_frame), frames, period)
); );
// Monitor and record input
if self.recording || self.monitoring {
let notes_on = &mut self.notes_on;
for (frame, event, bytes) in parse_midi_input(input) {
match event {
LiveEvent::Midi { message, .. } => {
if monitoring {
self.midi_out_buf[frame].push(bytes.to_vec())
}
if recording {
phrase.record_event({
let pulse = timebase.frame_to_pulse(
(frame0 + frame - start_frame) as f64
);
let quantized = (
pulse / quant as f64
).round() as usize * quant;
let looped = quantized % phrase.length;
looped
}, message);
}
match message {
MidiMessage::NoteOn { key, .. } => {
notes_on[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
notes_on[key.as_int() as usize] = false;
},
_ => {}
}
},
_ => {}
}
}
} }
} }
} } else if self.monitoring {
// Monitor and record input // For highlighting keys and note repeat
if self.recording || self.monitoring {
// For highlighting keys
let notes_on = &mut self.notes_on; let notes_on = &mut self.notes_on;
for (frame, event, bytes) in parse_midi_input(input) { for (frame, event, bytes) in parse_midi_input(input) {
match event { match event {
LiveEvent::Midi { message, .. } => { LiveEvent::Midi { message, .. } => {
if monitoring { self.midi_out_buf[frame].push(bytes.to_vec());
self.midi_out_buf[frame].push(bytes.to_vec())
}
if recording {
if let Some((start_frame, _)) = started {
phrase.as_mut().map(|p|p.record_event({
let pulse = timebase.frame_to_pulse((frame0 + frame - start_frame) as f64);
let quantized = (pulse / quant as f64).round() as usize * quant;
let looped = quantized % p.length;
looped
}, message));
}
}
match message { match message {
MidiMessage::NoteOn { key, .. } => { MidiMessage::NoteOn { key, .. } => {
notes_on[key.as_int() as usize] = true; notes_on[key.as_int() as usize] = true;

View file

@ -106,15 +106,17 @@ mod horizontal {
cell_lo.set_char('▄'); cell_lo.set_char('▄');
cell_lo.set_style(dim); cell_lo.set_style(dim);
let keys = [&cell_lo, &cell_lo, &cell_full, &cell_hi, &cell_hi, &cell_hi]; let cell_keys = [&cell_lo, &cell_lo, &cell_full, &cell_hi, &cell_hi, &cell_hi];
let Rect { x, y, width, height } = area; let Rect { x, y, width, height } = area;
let height = height.min(128); let height = height.min(128);
let h = height.saturating_sub(2); let h = height.saturating_sub(2);
for index in 0..h { for index in 0..h {
let y = y + h - index; let y = y + h - index;
*buf.get_mut(x + 1, y) = keys[(index % 6) as usize].clone(); let key1 = buf.get_mut(x + 1, y);
*buf.get_mut(x + 2, y) = cell_full.clone(); *key1 = cell_keys[(index % 6) as usize].clone();
let key2 = buf.get_mut(x + 2, y);
*key2 = cell_full.clone();
for x in x+5..x+width-1 { for x in x+5..x+width-1 {
*buf.get_mut(x, y) = cell_bg.clone(); *buf.get_mut(x, y) = cell_bg.clone();
} }