refactor midi player api

This commit is contained in:
🪞👃🪞 2024-11-27 00:15:43 +01:00
parent a26a1f2967
commit 71e19c9800
11 changed files with 159 additions and 183 deletions

View file

@ -129,22 +129,6 @@ impl ClockModel {
pub fn is_rolling (&self) -> bool {
*self.playing.read().unwrap() == Some(TransportState::Rolling)
}
//fn toggle_play (&self) -> Usually<()> {
//let playing = self.playing.read().unwrap().expect("1st sample has not been processed yet");
//let playing = match playing {
//TransportState::Stopped => {
//self.transport.start()?;
//Some(TransportState::Starting)
//},
//_ => {
//self.transport.stop()?;
//self.transport.locate(0)?;
//Some(TransportState::Stopped)
//},
//};
//*self.playing.write().unwrap() = playing;
//Ok(())
//}
}
/// Hosts the JACK callback for updating the temporal pointer and playback status.

View file

@ -3,3 +3,20 @@ use crate::*;
pub trait JackApi {
fn jack (&self) -> &Arc<RwLock<JackClient>>;
}
pub trait HasMidiIns {
fn midi_ins (&self) -> &Vec<Port<MidiIn>>;
fn midi_ins_mut (&mut self) -> &mut Vec<Port<MidiIn>>;
fn has_midi_ins (&self) -> bool {
self.midi_ins().len() > 0
}
}
pub trait HasMidiOuts {
fn midi_outs (&self) -> &Vec<Port<MidiOut>>;
fn midi_outs_mut (&mut self) -> &mut Vec<Port<MidiOut>>;
fn midi_note (&mut self) -> &mut Vec<u8>;
fn has_midi_outs (&self) -> bool {
self.midi_outs().len() > 0
}
}

View file

@ -1,32 +1,19 @@
use crate::*;
pub trait HasPlayer: JackApi {
pub trait HasPlayer {
fn player (&self) -> &impl MidiPlayerApi;
fn player_mut (&mut self) -> &mut impl MidiPlayerApi;
}
pub trait MidiPlayerApi: MidiInputApi + MidiOutputApi + Send + Sync {}
pub trait MidiPlayerApi: MidiRecordApi + MidiPlaybackApi + Send + Sync {}
pub trait HasPhrase: HasClock {
pub trait HasPlayPhrase: HasClock {
fn reset (&self) -> bool;
fn reset_mut (&mut self) -> &mut bool;
fn play_phrase (&self)
-> &Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn play_phrase_mut (&mut self)
-> &mut Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn next_phrase (&self)
-> &Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn next_phrase_mut (&mut self)
-> &mut Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
let start = self.clock().next_launch_pulse() as f64;
let instant = Instant::from_pulse(&self.clock().timebase(), start);
let phrase = phrase.map(|p|p.clone());
*self.next_phrase_mut() = Some((instant, phrase));
*self.reset_mut() = true;
}
fn play_phrase (&self) -> &Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn play_phrase_mut (&mut self) -> &mut Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn next_phrase (&self) -> &Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn next_phrase_mut (&mut self) -> &mut Option<(Instant, Option<Arc<RwLock<Phrase>>>)>;
fn pulses_since_start (&self) -> Option<f64> {
if let Some((started, Some(_))) = self.play_phrase().as_ref() {
Some(self.clock().current.pulse.get() - started.pulse.get())
@ -34,39 +21,24 @@ pub trait HasPhrase: HasClock {
None
}
}
fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
let start = self.clock().next_launch_pulse() as f64;
let instant = Instant::from_pulse(&self.clock().timebase(), start);
let phrase = phrase.map(|p|p.clone());
*self.next_phrase_mut() = Some((instant, phrase));
*self.reset_mut() = true;
}
}
pub trait MidiInputApi: HasPhrase {
fn midi_ins (&self) -> &Vec<Port<MidiIn>>;
fn midi_ins_mut (&mut self) -> &mut Vec<Port<MidiIn>>;
fn has_midi_ins (&self) -> bool {
self.midi_ins().len() > 0
}
pub trait MidiRecordApi: HasClock + HasPlayPhrase + HasMidiIns {
fn notes_in (&self) -> &Arc<RwLock<[bool;128]>>;
fn recording (&self) -> bool;
fn recording_mut (&mut self) -> &mut bool;
fn toggle_record (&mut self) {
*self.recording_mut() = !self.recording();
}
fn monitoring (&self) -> bool;
fn monitoring_mut (&mut self) -> &mut bool;
fn toggle_monitor (&mut self) {
*self.monitoring_mut() = !self.monitoring();
}
fn overdub (&self) -> bool;
fn overdub_mut (&mut self) -> &mut bool;
fn toggle_overdub (&mut self) {
*self.overdub_mut() = !self.overdub();
}
fn notes_in (&self) -> &Arc<RwLock<[bool;128]>>;
fn record (
&mut self,
scope: &ProcessScope,
midi_buf: &mut Vec<Vec<Vec<u8>>>,
) {
fn record (&mut self, scope: &ProcessScope, midi_buf: &mut Vec<Vec<Vec<u8>>>) {
let sample0 = scope.last_frame_time() as usize;
// For highlighting keys and note repeat
let notes_in = self.notes_in().clone();
@ -107,11 +79,12 @@ pub trait MidiInputApi: HasPhrase {
}
}
fn monitor (
&mut self,
scope: &ProcessScope,
midi_buf: &mut Vec<Vec<Vec<u8>>>,
) {
fn monitoring (&self) -> bool;
fn monitoring_mut (&mut self) -> &mut bool;
fn toggle_monitor (&mut self) {
*self.monitoring_mut() = !self.monitoring();
}
fn monitor (&mut self, scope: &ProcessScope, midi_buf: &mut Vec<Vec<Vec<u8>>>) {
// For highlighting keys and note repeat
let notes_in = self.notes_in().clone();
for input in self.midi_ins_mut().iter() {
@ -124,42 +97,33 @@ pub trait MidiInputApi: HasPhrase {
}
}
fn overdub (&self) -> bool;
fn overdub_mut (&mut self) -> &mut bool;
fn toggle_overdub (&mut self) {
*self.overdub_mut() = !self.overdub();
}
}
pub trait MidiOutputApi: HasPhrase {
fn midi_outs (&self) -> &Vec<Port<MidiOut>>;
fn midi_outs_mut (&mut self) -> &mut Vec<Port<MidiOut>>;
fn midi_note (&mut self) -> &mut Vec<u8>;
pub trait MidiPlaybackApi: HasPlayPhrase + HasClock + HasMidiOuts {
fn notes_out (&self) -> &Arc<RwLock<[bool;128]>>;
fn has_midi_outs (&self) -> bool {
self.midi_outs().len() > 0
}
/// Clear the section of the output buffer that we will be using,
/// emitting "all notes off" at start of buffer if requested.
fn clear (
&mut self,
scope: &ProcessScope,
midi_buf: &mut Vec<Vec<Vec<u8>>>,
reset: bool
&mut self, scope: &ProcessScope, out_buf: &mut Vec<Vec<Vec<u8>>>, reset: bool
) {
for frame in &mut midi_buf[0..scope.n_frames() as usize] {
for frame in &mut out_buf[0..scope.n_frames() as usize] {
frame.clear();
}
if reset {
all_notes_off(midi_buf);
all_notes_off(out_buf);
}
}
/// Output notes from phrase to MIDI output ports.
fn play (
&mut self,
scope: &ProcessScope,
note_buffer: &mut Vec<u8>,
output_buffer: &mut Vec<Vec<Vec<u8>>>
&mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out_buf: &mut Vec<Vec<Vec<u8>>>
) -> bool {
let mut next = false;
// Write MIDI events from currently playing phrase (if any) to MIDI output buffer
@ -207,15 +171,15 @@ pub trait MidiOutputApi: HasPhrase {
// Output each MIDI event from phrase at appropriate frames of output buffer:
for message in phrase.notes[pulse].iter() {
// Clear output buffer for this MIDI event.
note_buffer.clear();
note_buf.clear();
// TODO: support MIDI channels other than CH1.
let channel = 0.into();
// Serialize MIDI event into message buffer.
LiveEvent::Midi { channel, message: *message }
.write(note_buffer)
.write(note_buf)
.unwrap();
// Append serialized message to output buffer.
output_buffer[sample].push(note_buffer.clone());
out_buf[sample].push(note_buf.clone());
// Update the list of currently held notes.
update_keys(&mut*notes, &message);
}
@ -227,11 +191,9 @@ pub trait MidiOutputApi: HasPhrase {
next
}
/// Handle switchover from current to next playing phrase.
fn switchover (
&mut self,
scope: &ProcessScope,
note_buffer: &mut Vec<u8>,
output_buffer: &mut Vec<Vec<Vec<u8>>>
&mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out_buf: &mut Vec<Vec<Vec<u8>>>
) {
if self.clock().is_rolling() {
let sample0 = scope.last_frame_time() as usize;
@ -251,19 +213,22 @@ pub trait MidiOutputApi: HasPhrase {
}
// TODO fill in remaining ticks of chunk from next phrase.
// ?? just call self.play(scope) again, since enqueuement is off ???
self.play(scope, note_buffer, output_buffer);
self.play(scope, note_buf, out_buf);
// ?? or must it be with modified scope ??
// likely not because start time etc
}
}
}
fn write (&mut self, scope: &ProcessScope, output_buffer: &Vec<Vec<Vec<u8>>>) {
/// Write a chunk of MIDI notes to the output buffer.
fn write (
&mut self, scope: &ProcessScope, out_buf: &Vec<Vec<Vec<u8>>>
) {
let samples = scope.n_frames() as usize;
for port in self.midi_outs_mut().iter_mut() {
let writer = &mut port.writer(scope);
for time in 0..samples {
for event in output_buffer[time].iter() {
for event in out_buf[time].iter() {
writer.write(&RawMidi { time: time as u32, bytes: &event })
.expect(&format!("{event:?}"));
}

View file

@ -78,7 +78,7 @@ pub trait ArrangerSceneApi: Sized {
Some(clip) => tracks
.get(track_index)
.map(|track|{
if let Some((_, Some(phrase))) = track.play_phrase() {
if let Some((_, Some(phrase))) = track.player().play_phrase() {
*phrase.read().unwrap() == *clip.read().unwrap()
} else {
false

View file

@ -33,7 +33,7 @@ pub enum ArrangerTrackCommand {
SetZoom(usize),
}
pub trait ArrangerTrackApi: MidiPlayerApi + Send + Sync + Sized {
pub trait ArrangerTrackApi: HasPlayer + Send + Sync + Sized {
/// Name of track
fn name (&self) -> &Arc<RwLock<String>>;
/// Preferred width of track column
@ -78,7 +78,7 @@ impl<'a, T: ArrangerTrackApi, H: HasTracks<T>> Audio for TracksAudio<'a, T, H> {
let note_buffer = &mut self.1;
let output_buffer = &mut self.2;
for track in model.tracks_mut().iter_mut() {
if PlayerAudio(track, note_buffer, output_buffer).process(client, scope) == Control::Quit {
if PlayerAudio(track.player_mut(), note_buffer, output_buffer).process(client, scope) == Control::Quit {
return Control::Quit
}
}