mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
remove some allocations from process callback
This commit is contained in:
parent
2e26fc2eaa
commit
e83802e1fd
3 changed files with 109 additions and 113 deletions
|
|
@ -7,18 +7,15 @@ pub type MIDIMessage =
|
||||||
Vec<u8>;
|
Vec<u8>;
|
||||||
|
|
||||||
pub type MIDIChunk =
|
pub type MIDIChunk =
|
||||||
[Option<Vec<MIDIMessage>>];
|
[Vec<MIDIMessage>];
|
||||||
|
|
||||||
/// Add "all notes off" to the start of a buffer.
|
/// Add "all notes off" to the start of a buffer.
|
||||||
pub fn all_notes_off (output: &mut MIDIChunk) {
|
pub fn all_notes_off (output: &mut MIDIChunk) {
|
||||||
output[0] = Some(vec![]);
|
|
||||||
if let Some(Some(frame)) = output.get_mut(0) {
|
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
let msg = MidiMessage::Controller { controller: 123.into(), value: 0.into() };
|
let msg = MidiMessage::Controller { controller: 123.into(), value: 0.into() };
|
||||||
let evt = LiveEvent::Midi { channel: 0.into(), message: msg };
|
let evt = LiveEvent::Midi { channel: 0.into(), message: msg };
|
||||||
evt.write(&mut buf).unwrap();
|
evt.write(&mut buf).unwrap();
|
||||||
frame.push(buf);
|
output[0].push(buf);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_midi_input (input: MidiIter) -> Box<dyn Iterator<Item=(usize, LiveEvent, &[u8])> + '_> {
|
pub fn parse_midi_input (input: MidiIter) -> Box<dyn Iterator<Item=(usize, LiveEvent, &[u8])> + '_> {
|
||||||
|
|
@ -32,11 +29,9 @@ pub fn parse_midi_input (input: MidiIter) -> Box<dyn Iterator<Item=(usize, LiveE
|
||||||
/// Write to JACK port from output buffer (containing notes from sequence and/or monitor)
|
/// Write to JACK port from output buffer (containing notes from sequence and/or monitor)
|
||||||
pub fn write_midi_output (writer: &mut ::jack::MidiWriter, output: &MIDIChunk, frames: usize) {
|
pub fn write_midi_output (writer: &mut ::jack::MidiWriter, output: &MIDIChunk, frames: usize) {
|
||||||
for time in 0..frames {
|
for time in 0..frames {
|
||||||
if let Some(Some(frame)) = output.get(time) {
|
for event in output[time].iter() {
|
||||||
for event in frame.iter() {
|
|
||||||
writer.write(&::jack::RawMidi { time: time as u32, bytes: &event })
|
writer.write(&::jack::RawMidi { time: time as u32, bytes: &event })
|
||||||
.expect(&format!("{event:?}"));
|
.expect(&format!("{event:?}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,12 +68,7 @@ impl Phrase {
|
||||||
}
|
}
|
||||||
LiveEvent::Midi { channel, message }.write(&mut buf).unwrap();
|
LiveEvent::Midi { channel, message }.write(&mut buf).unwrap();
|
||||||
let t = *time as usize;
|
let t = *time as usize;
|
||||||
if output[t].is_none() {
|
output[t].push(buf);
|
||||||
output[t] = Some(vec![]);
|
|
||||||
}
|
|
||||||
if let Some(Some(frame)) = output.get_mut(t) {
|
|
||||||
frame.push(buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ pub struct Track {
|
||||||
pub sequence: Option<usize>,
|
pub sequence: Option<usize>,
|
||||||
/// Output from current sequence.
|
/// Output from current sequence.
|
||||||
pub midi_out: Port<MidiOut>,
|
pub midi_out: Port<MidiOut>,
|
||||||
|
midi_out_buf: Vec<Vec<Vec<u8>>>,
|
||||||
/// Red keys on piano roll.
|
/// Red keys on piano roll.
|
||||||
pub notes_on: Vec<bool>,
|
pub notes_on: Vec<bool>,
|
||||||
/// Device chain
|
/// Device chain
|
||||||
|
|
@ -41,7 +42,8 @@ impl Track {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
midi_out: jack.register_port(name, MidiOut)?,
|
midi_out: jack.register_port(name, MidiOut)?,
|
||||||
notes_on: vec![],
|
midi_out_buf: vec![vec![];1024],
|
||||||
|
notes_on: vec![false;128],
|
||||||
monitoring: false,
|
monitoring: false,
|
||||||
recording: false,
|
recording: false,
|
||||||
overdub: true,
|
overdub: true,
|
||||||
|
|
@ -51,77 +53,17 @@ impl Track {
|
||||||
device: 0,
|
device: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn process (
|
pub fn device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
&mut self,
|
self.get_device(self.device)
|
||||||
input: MidiIter,
|
|
||||||
timebase: &Arc<Timebase>,
|
|
||||||
playing: Option<TransportState>,
|
|
||||||
scope: &ProcessScope,
|
|
||||||
frame0: usize,
|
|
||||||
frames: usize,
|
|
||||||
panic: bool,
|
|
||||||
) {
|
|
||||||
// Need to be borrowed outside the conditionals?
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
pub fn first_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
|
self.get_device(0)
|
||||||
}
|
}
|
||||||
// Monitor and record input
|
pub fn last_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
if self.recording || self.monitoring {
|
self.get_device(self.devices.len().saturating_sub(1))
|
||||||
let phrase = &mut self.phrase_mut();
|
|
||||||
for (time, event, bytes) in parse_midi_input(input) {
|
|
||||||
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()]);
|
|
||||||
}
|
}
|
||||||
}
|
pub fn get_device (&self, i: usize) -> Option<MutexGuard<Box<dyn Device>>> {
|
||||||
if recording {
|
self.devices.get(i).map(|d|d.state.lock().unwrap())
|
||||||
if let Some(phrase) = phrase {
|
|
||||||
let pulse = timebase.frames_pulses((frame0 + time) as f64) as usize;
|
|
||||||
let pulse = pulse % phrase.length;
|
|
||||||
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) -> Usually<&mut JackDevice> {
|
pub fn add_device (&mut self, device: JackDevice) -> Usually<&mut JackDevice> {
|
||||||
self.devices.push(device);
|
self.devices.push(device);
|
||||||
|
|
@ -138,18 +80,6 @@ impl Track {
|
||||||
let index = self.devices.len() - 1;
|
let index = self.devices.len() - 1;
|
||||||
Ok(&mut self.devices[index])
|
Ok(&mut self.devices[index])
|
||||||
}
|
}
|
||||||
pub fn get_device (&self, i: usize) -> Option<MutexGuard<Box<dyn Device>>> {
|
|
||||||
self.devices.get(i).map(|d|d.state.lock().unwrap())
|
|
||||||
}
|
|
||||||
pub fn device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
|
||||||
self.get_device(self.device)
|
|
||||||
}
|
|
||||||
pub fn first_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
|
||||||
self.get_device(0)
|
|
||||||
}
|
|
||||||
pub fn last_device (&self) -> Option<MutexGuard<Box<dyn Device>>> {
|
|
||||||
self.get_device(self.devices.len().saturating_sub(1))
|
|
||||||
}
|
|
||||||
pub fn phrase (&self) -> Option<&Phrase> {
|
pub fn phrase (&self) -> Option<&Phrase> {
|
||||||
if let Some(phrase) = self.sequence {
|
if let Some(phrase) = self.sequence {
|
||||||
return self.phrases.get(phrase)
|
return self.phrases.get(phrase)
|
||||||
|
|
@ -180,4 +110,80 @@ impl Track {
|
||||||
pub fn toggle_overdub (&mut self) {
|
pub fn toggle_overdub (&mut self) {
|
||||||
self.overdub = !self.overdub;
|
self.overdub = !self.overdub;
|
||||||
}
|
}
|
||||||
|
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 conditionals?
|
||||||
|
let recording = self.recording;
|
||||||
|
let monitoring = self.monitoring;
|
||||||
|
// Clear the section of the output buffer that we will be using
|
||||||
|
for frame in &mut self.midi_out_buf[0..frames] {
|
||||||
|
frame.clear();
|
||||||
|
}
|
||||||
|
// Emit "all notes off" at start of buffer if requested
|
||||||
|
if panic {
|
||||||
|
all_notes_off(&mut self.midi_out_buf);
|
||||||
|
}
|
||||||
|
// Play from phrase into output buffer
|
||||||
|
if let Some(Some(phrase)) = self.sequence.map(|id|self.phrases.get(id)) {
|
||||||
|
if playing == Some(TransportState::Rolling) {
|
||||||
|
phrase.process_out(
|
||||||
|
&mut self.midi_out_buf,
|
||||||
|
&mut self.notes_on,
|
||||||
|
timebase,
|
||||||
|
frame0,
|
||||||
|
frames
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Monitor and record input
|
||||||
|
if self.recording || self.monitoring {
|
||||||
|
// For highlighting keys
|
||||||
|
let notes_on = &mut self.notes_on;
|
||||||
|
let phrase = &mut self.sequence.map(|id|self.phrases.get_mut(id));
|
||||||
|
for (time, event, bytes) in parse_midi_input(input) {
|
||||||
|
match event {
|
||||||
|
LiveEvent::Midi { message, .. } => {
|
||||||
|
if monitoring {
|
||||||
|
self.midi_out_buf[time].push(bytes.to_vec())
|
||||||
|
}
|
||||||
|
if recording {
|
||||||
|
if let Some(Some(phrase)) = phrase {
|
||||||
|
let pulse = timebase.frames_pulses((frame0 + time) as f64) as usize;
|
||||||
|
let pulse = pulse % phrase.length;
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write_midi_output(
|
||||||
|
&mut self.midi_out.writer(scope),
|
||||||
|
&self.midi_out_buf,
|
||||||
|
frames
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue