mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
move Phrase::process_in logic to Track::process
This commit is contained in:
parent
4b909ffdc3
commit
163ecaaed6
6 changed files with 107 additions and 105 deletions
|
|
@ -77,61 +77,6 @@ impl Phrase {
|
|||
}
|
||||
}
|
||||
}
|
||||
/// Read a chunk of MIDI events from an input port.
|
||||
pub fn process_in (
|
||||
&mut self,
|
||||
input: ::jack::MidiIter,
|
||||
notes_on: &mut Vec<bool>,
|
||||
mut monitor: Option<&mut MIDIChunk>,
|
||||
record: bool,
|
||||
timebase: &Arc<Timebase>,
|
||||
frame0: usize,
|
||||
) {
|
||||
for RawMidi { time, bytes } in input {
|
||||
let time = time as usize;
|
||||
let pulse = timebase.frames_pulses((frame0 + time) as f64) as usize;
|
||||
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
|
||||
if let MidiMessage::NoteOn { key, vel: _ } = message {
|
||||
notes_on[key.as_int() as usize] = true;
|
||||
if let Some(ref mut monitor) = monitor {
|
||||
if monitor[time].is_none() {
|
||||
monitor[time] = Some(vec![]);
|
||||
}
|
||||
if let Some(Some(frame)) = monitor.get_mut(time) {
|
||||
frame.push(bytes.into())
|
||||
}
|
||||
}
|
||||
if record {
|
||||
let contains = self.notes.contains_key(&pulse);
|
||||
if contains {
|
||||
self.notes.get_mut(&pulse).unwrap().push(message.clone());
|
||||
} else {
|
||||
self.notes.insert(pulse, vec![message.clone()]);
|
||||
}
|
||||
}
|
||||
} else if let midly::MidiMessage::NoteOff { key, vel: _ } = message {
|
||||
notes_on[key.as_int() as usize] = false;
|
||||
if let Some(ref mut monitor) = monitor {
|
||||
if monitor[time].is_none() {
|
||||
monitor[time] = Some(vec![]);
|
||||
}
|
||||
if let Some(Some(frame)) = monitor.get_mut(time) {
|
||||
frame.push(bytes.into())
|
||||
}
|
||||
}
|
||||
if record {
|
||||
let contains = self.notes.contains_key(&pulse);
|
||||
if contains {
|
||||
self.notes.get_mut(&pulse).unwrap().push(message.clone());
|
||||
} else {
|
||||
self.notes.insert(pulse, vec![message.clone()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! phrase {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,15 @@ pub struct Track {
|
|||
/// Device selector
|
||||
pub device: usize,
|
||||
}
|
||||
|
||||
ports!(Track {
|
||||
audio: {
|
||||
outs: |_t|Ok(vec![]),
|
||||
}
|
||||
midi: {
|
||||
ins: |_t|Ok(vec![]),
|
||||
outs: |_t|Ok(vec![]),
|
||||
}
|
||||
});
|
||||
impl Track {
|
||||
pub fn new (
|
||||
name: &str,
|
||||
|
|
@ -43,6 +51,75 @@ impl Track {
|
|||
device: 0,
|
||||
})
|
||||
}
|
||||
pub fn process (
|
||||
&mut self,
|
||||
input: MidiIter,
|
||||
timebase: &Arc<Timebase>,
|
||||
playing: Option<TransportState>,
|
||||
scope: &ProcessScope,
|
||||
frame0: usize,
|
||||
frames: usize,
|
||||
panic: bool,
|
||||
) {
|
||||
// Need to be borrowed outside the conditional
|
||||
let recording = self.recording;
|
||||
let monitoring = self.monitoring;
|
||||
// Output buffer
|
||||
let mut output: Vec<Option<Vec<Vec<u8>>>> = vec![None;frames];
|
||||
// For highlighting keys
|
||||
let mut notes_on = vec![false;128];
|
||||
if panic { all_notes_off(&mut output); }
|
||||
// Play from phrase into output buffer
|
||||
if let Some(phrase) = self.phrase() {
|
||||
if playing == Some(TransportState::Rolling) {
|
||||
phrase.process_out(
|
||||
&mut output,
|
||||
&mut notes_on,
|
||||
timebase,
|
||||
frame0,
|
||||
frames
|
||||
);
|
||||
}
|
||||
}
|
||||
// Monitor and record input
|
||||
let phrase = &mut self.phrase_mut();
|
||||
for (time, event, bytes) in parse_midi_input(input) {
|
||||
let pulse = timebase.frames_pulses((frame0 + time) as f64) as usize;
|
||||
match event {
|
||||
LiveEvent::Midi { message, .. } => {
|
||||
if monitoring {
|
||||
if let Some(Some(frame)) = output.get_mut(time) {
|
||||
frame.push(bytes.into())
|
||||
} else {
|
||||
output[time] = Some(vec![bytes.into()]);
|
||||
}
|
||||
}
|
||||
if recording {
|
||||
if let Some(phrase) = phrase {
|
||||
let contains = phrase.notes.contains_key(&pulse);
|
||||
if contains {
|
||||
phrase.notes.get_mut(&pulse).unwrap().push(message.clone());
|
||||
} else {
|
||||
phrase.notes.insert(pulse, vec![message.clone()]);
|
||||
}
|
||||
};
|
||||
}
|
||||
match message {
|
||||
MidiMessage::NoteOn { key, .. } => {
|
||||
notes_on[key.as_int() as usize] = true;
|
||||
}
|
||||
MidiMessage::NoteOff { key, .. } => {
|
||||
notes_on[key.as_int() as usize] = false;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
self.notes_on = notes_on;
|
||||
write_midi_output(&mut self.midi_out.writer(scope), &output, frames);
|
||||
}
|
||||
pub fn add_device (
|
||||
&mut self, device: JackDevice
|
||||
) -> &mut JackDevice {
|
||||
|
|
@ -84,13 +161,3 @@ impl Track {
|
|||
&mut self.phrases[index]
|
||||
}
|
||||
}
|
||||
|
||||
ports!(Track {
|
||||
audio: {
|
||||
outs: |_t|Ok(vec![]),
|
||||
}
|
||||
midi: {
|
||||
ins: |_t|Ok(vec![]),
|
||||
outs: |_t|Ok(vec![]),
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue