wip: refactor pt.33: 33e; midi player traits

This commit is contained in:
🪞👃🪞 2024-11-15 00:44:51 +01:00
parent ce78b95d8a
commit cbbecc5aba
21 changed files with 522 additions and 487 deletions

View file

@ -1,26 +1,10 @@
use crate::*;
pub trait PhrasePoolModelApi {
pub trait HasPhrases {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>>;
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>>;
}
impl PhrasePoolModelApi for PhrasePoolModel {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
&self.phrases
}
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
&mut self.phrases
}
}
/// Contains all phrases in a project
#[derive(Debug)]
pub struct PhrasePoolModel {
/// Phrases in the pool
pub phrases: Vec<Arc<RwLock<Phrase>>>,
}
#[derive(Clone, PartialEq)]
pub enum PhrasePoolCommand {
Add(usize),
@ -34,8 +18,8 @@ pub enum PhrasePoolCommand {
SetLength(usize, usize),
}
impl Command<PhrasePoolModel> for PhrasePoolCommand {
fn execute (self, model: &mut PhrasePoolModel) -> Perhaps<Self> {
impl<T: HasPhrases> Command<T> for PhrasePoolCommand {
fn execute (self, model: &mut T) -> Perhaps<Self> {
match self {
Self::Add(index) => {
//Self::Append => { view.append_new(None, None) },

View file

@ -5,156 +5,28 @@ pub trait HasPlayer: HasJack + HasClock {
fn player_mut (&mut self) -> &mut MIDIPlayer;
}
pub trait MidiInputApi {
fn has_midi_inputs (&self) -> bool {
self.midi_inputs.len() > 0
}
fn toggle_record (&mut self) {
self.recording = !self.recording;
}
fn toggle_overdub (&mut self) {
self.overdub = !self.overdub;
}
fn record (&mut self, scope: &ProcessScope) {
let sample0 = scope.last_frame_time() as usize;
if let (true, Some((started, phrase))) = (self.is_rolling(), &self.phrase) {
let start = started.sample.get() as usize;
let quant = self.clock.quant.get();
// For highlighting keys and note repeat
let mut notes_in = self.notes_in.write().unwrap();
// Record from each input
for input in self.midi_inputs.iter() {
for (sample, event, bytes) in parse_midi_input(input.iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
if self.monitoring {
self.midi_chunk[sample].push(bytes.to_vec())
}
if self.recording {
if let Some(phrase) = phrase {
let mut phrase = phrase.write().unwrap();
let length = phrase.length;
phrase.record_event({
let sample = (sample0 + sample - start) as f64;
let pulse = self.clock.timebase().samples_to_pulse(sample);
let quantized = (pulse / quant).round() * quant;
let looped = quantized as usize % length;
looped
}, message);
}
}
update_keys(&mut notes_in, &message);
}
}
}
}
if let (true, Some((start_at, phrase))) = (self.is_rolling(), &self.next_phrase) {
// TODO switch to next phrase and record into it
}
}
}
pub trait HasMidiBuffer {
fn midi_buffer (&self) -> &mut Vec<Vec<Vec<u8>>>;
fn reset (&self) -> bool;
fn reset_mut (&mut self) -> &mut bool;
pub trait MidiOutputApi {
fn has_midi_outputs (&self) -> bool {
self.midi_outputs.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, force_reset: bool) {
for frame in &mut self.midi_chunk[0..scope.n_frames() as usize] {
for frame in &mut self.midi_buffer()[0..scope.n_frames() as usize] {
frame.clear();
}
if self.reset || force_reset {
all_notes_off(&mut self.midi_chunk); self.reset = false;
}
}
fn play (&mut self, scope: &ProcessScope) -> bool {
let mut next = false;
// Write MIDI events from currently playing phrase (if any) to MIDI output buffer
if self.is_rolling() {
let sample0 = scope.last_frame_time() as usize;
let samples = scope.n_frames() as usize;
// If no phrase is playing, prepare for switchover immediately
next = self.phrase.is_none();
if let Some((started, phrase)) = &self.phrase {
// First sample to populate. Greater than 0 means that the first
// pulse of the phrase falls somewhere in the middle of the chunk.
let sample = started.sample.get() as usize;
let sample = sample + self.clock.started.read().unwrap().unwrap().0;
let sample = sample0.saturating_sub(sample);
// Iterator that emits sample (index into output buffer at which to write MIDI event)
// paired with pulse (index into phrase from which to take the MIDI event) for each
// sample of the output buffer that corresponds to a MIDI pulse.
let pulses = self.clock.timebase().pulses_between_samples(sample, sample + samples);
// Notes active during current chunk.
let notes = &mut self.notes_out.write().unwrap();
for (sample, pulse) in pulses {
// If a next phrase is enqueued, and we're past the end of the current one,
// break the loop here (FIXME count pulse correctly)
next = self.next_phrase.is_some() && if let Some(ref phrase) = phrase {
pulse >= phrase.read().unwrap().length
} else {
true
};
if next {
break
}
// If there's a currently playing phrase, output notes from it to buffer:
if let Some(ref phrase) = phrase {
// Source phrase from which the MIDI events will be taken.
let phrase = phrase.read().unwrap();
// Current pulse index in source phrase
let pulse = pulse % phrase.length;
// 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.
self.midi_note.clear();
// TODO: support MIDI channels other than CH1.
let channel = 0.into();
// Serialize MIDI event into message buffer.
LiveEvent::Midi { channel, message: *message }
.write(&mut self.midi_note)
.unwrap();
// Append serialized message to output buffer.
self.midi_chunk[sample].push(self.midi_note.clone());
// Update the list of currently held notes.
update_keys(notes, &message);
}
}
}
}
}
next
}
fn write (&mut self, scope: &ProcessScope) {
let samples = scope.n_frames() as usize;
for port in self.midi_outputs.iter_mut() {
let writer = &mut port.writer(scope);
let output = &self.midi_chunk;
for time in 0..samples {
for event in output[time].iter() {
writer.write(&RawMidi { time: time as u32, bytes: &event })
.expect(&format!("{event:?}"));
}
}
if self.reset() || force_reset {
all_notes_off(&mut self.midi_buffer());
*self.reset_mut() = false;
}
}
}
pub trait MidiMonitorApi: MidiInputApi + MidiOutputApi {
fn monitor (&mut self, scope: &ProcessScope) {
let mut notes_in = self.notes_in.write().unwrap();
for input in self.midi_inputs.iter() {
for (sample, event, bytes) in parse_midi_input(input.iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
self.midi_chunk[sample].push(bytes.to_vec());
update_keys(&mut notes_in, &message);
}
}
}
}
}
pub trait MidiLaunchApi: MidiInputApi + MidiOutputApi {
pub trait HasPhrase {
fn phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
fn next_phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
fn switchover (&mut self, scope: &ProcessScope) {
if self.is_rolling() {
let sample0 = scope.last_frame_time() as usize;
@ -180,12 +52,6 @@ pub trait MidiLaunchApi: MidiInputApi + MidiOutputApi {
}
}
}
}
pub trait MidiPlayerApi: PlayheadApi {
fn toggle_monitor (&mut self) {
self.monitoring = !self.monitoring;
}
fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
let start = self.clock.next_launch_pulse();
self.next_phrase = Some((
@ -203,6 +69,170 @@ pub trait MidiPlayerApi: PlayheadApi {
}
}
pub trait MidiInputApi: PlayheadApi + HasMidiBuffer + HasPhrase {
fn midi_ins (&self) -> &Vec<Port<MidiIn>>;
fn midi_ins_mut (&self) -> &mut Vec<Port<MidiIn>>;
fn has_midi_ins (&self) -> bool {
self.midi_ins().len() > 0
}
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) {
let sample0 = scope.last_frame_time() as usize;
if let (true, Some((started, phrase))) = (self.is_rolling(), self.phrase()) {
let start = started.sample.get() as usize;
let quant = self.quant().get();
// For highlighting keys and note repeat
let mut notes_in = self.notes_in().write().unwrap();
// Record from each input
for input in self.midi_ins_mut().iter() {
for (sample, event, bytes) in parse_midi_input(input.iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
if self.monitoring() {
self.midi_buffer()[sample].push(bytes.to_vec())
}
if self.recording() {
if let Some(phrase) = phrase {
let mut phrase = phrase.write().unwrap();
let length = phrase.length;
phrase.record_event({
let sample = (sample0 + sample - start) as f64;
let pulse = self.timebase().samples_to_pulse(sample);
let quantized = (pulse / quant).round() * quant;
let looped = quantized as usize % length;
looped
}, message);
}
}
update_keys(&mut notes_in, &message);
}
}
}
}
if let (true, Some((start_at, phrase))) = (self.is_rolling(), &self.next_phrase()) {
// TODO switch to next phrase and record into it
}
}
fn monitor (&mut self, scope: &ProcessScope) {
let mut notes_in = self.notes_in().write().unwrap();
for input in self.midi_ins_mut().iter() {
for (sample, event, bytes) in parse_midi_input(input.iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
self.midi_buffer()[sample].push(bytes.to_vec());
update_keys(&mut notes_in, &message);
}
}
}
}
}
pub trait MidiOutputApi: PlayheadApi + HasMidiBuffer + HasPhrase + HasClock {
fn midi_outs (&self) -> &Vec<Port<MidiOut>>;
fn midi_outs_mut (&self) -> &mut Vec<Port<MidiOut>>;
fn midi_note (&mut self) -> &mut Vec<u8>;
fn notes_out (&mut self) -> &Arc<RwLock<Vec<[bool;128]>>>;
fn has_midi_outs (&self) -> bool {
self.midi_outs().len() > 0
}
fn play (&mut self, scope: &ProcessScope) -> bool {
let mut next = false;
// Write MIDI events from currently playing phrase (if any) to MIDI output buffer
if self.is_rolling() {
let sample0 = scope.last_frame_time() as usize;
let samples = scope.n_frames() as usize;
// If no phrase is playing, prepare for switchover immediately
next = self.phrase().is_none();
if let Some((started, phrase)) = &self.phrase() {
// First sample to populate. Greater than 0 means that the first
// pulse of the phrase falls somewhere in the middle of the chunk.
let sample = started.sample.get() as usize;
let sample = sample + self.clock().started.read().unwrap().unwrap().0;
let sample = sample0.saturating_sub(sample);
// Iterator that emits sample (index into output buffer at which to write MIDI event)
// paired with pulse (index into phrase from which to take the MIDI event) for each
// sample of the output buffer that corresponds to a MIDI pulse.
let pulses = self.clock().timebase().pulses_between_samples(sample, sample + samples);
// Notes active during current chunk.
let notes = &mut self.notes_out().write().unwrap();
for (sample, pulse) in pulses {
// If a next phrase is enqueued, and we're past the end of the current one,
// break the loop here (FIXME count pulse correctly)
next = self.next_phrase().is_some() && if let Some(ref phrase) = phrase {
pulse >= phrase.read().unwrap().length
} else {
true
};
if next {
break
}
// If there's a currently playing phrase, output notes from it to buffer:
if let Some(ref phrase) = phrase {
// Source phrase from which the MIDI events will be taken.
let phrase = phrase.read().unwrap();
// Current pulse index in source phrase
let pulse = pulse % phrase.length;
// 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.
self.midi_note().clear();
// TODO: support MIDI channels other than CH1.
let channel = 0.into();
// Serialize MIDI event into message buffer.
LiveEvent::Midi { channel, message: *message }
.write(&mut self.midi_note())
.unwrap();
// Append serialized message to output buffer.
self.midi_buffer()[sample].push(self.midi_note().clone());
// Update the list of currently held notes.
update_keys(&mut*notes, &message);
}
}
}
}
}
next
}
fn write (&mut self, scope: &ProcessScope) {
let samples = scope.n_frames() as usize;
for port in self.midi_outs_mut().iter_mut() {
let writer = &mut port.writer(scope);
let output = &self.midi_buffer();
for time in 0..samples {
for event in output[time].iter() {
writer.write(&RawMidi { time: time as u32, bytes: &event })
.expect(&format!("{event:?}"));
}
}
}
}
}
/// Add "all notes off" to the start of a buffer.
pub fn all_notes_off (output: &mut [Vec<Vec<u8>>]) {
let mut buf = vec![];

View file

@ -1,207 +0,0 @@
use crate::*;
impl ClockApi for Clock {
fn quant (&self) -> &Quantize {
&self.quant
}
fn sync (&self) -> &LaunchSync {
&self.sync
}
fn current (&self) -> &Instant {
&self.current
}
}
impl PlayheadApi for Clock {}
impl HasJack for TransportModel {
fn jack (&self) -> &Arc<RwLock<JackClient>> {
&self.jack
}
}
impl HasClock for TransportModel {
fn clock (&self) -> &Arc<Clock> {
&self.clock
}
}
//impl TransportModelApi for TransportModel {
//fn transport (&self) -> &jack::Transport {
//&self.transport
//}
//fn metronome (&self) -> bool {
//self.metronome
//}
//}
impl Debug for TransportModel {
fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
f.debug_struct("transport")
.field("jack", &self.jack)
.field("transport", &"(JACK transport)")
.field("clock", &self.clock)
.field("metronome", &self.metronome)
.finish()
}
}
impl HasJack for ArrangerModel {
fn jack (&self) -> &Arc<RwLock<JackClient>> {
&self.transport.jack()
}
}
impl HasClock for ArrangerModel {
fn clock (&self) -> &Arc<Clock> {
&self.transport.clock()
}
}
//impl TransportModelApi for ArrangerModel {
//fn transport (&self) -> &jack::Transport {
//&self.transport.transport()
//}
//fn metronome (&self) -> bool {
//self.transport.metronome()
//}
//}
impl PhrasePoolModelApi for ArrangerModel {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
&self.phrases
}
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
&mut self.phrases
}
}
impl ArrangerApi for ArrangerModel {
fn name (&self) -> &Arc<RwLock<String>> {
&self.name
}
fn tracks (&self) -> &Vec<ArrangerTrack> {
&self.tracks
}
fn tracks_mut (&mut self) -> &mut Vec<ArrangerTrack> {
&mut self.tracks
}
fn scenes (&self) -> &Vec<ArrangerScene> {
&self.scenes
}
fn scenes_mut (&mut self) -> &mut Vec<ArrangerScene> {
&mut self.scenes
}
}
impl ArrangerScene {
//TODO
//pub fn from_edn <'a, 'e> (args: &[Edn<'e>]) -> Usually<Self> {
//let mut name = None;
//let mut clips = vec![];
//edn!(edn in args {
//Edn::Map(map) => {
//let key = map.get(&Edn::Key(":name"));
//if let Some(Edn::Str(n)) = key {
//name = Some(*n);
//} else {
//panic!("unexpected key in scene '{name:?}': {key:?}")
//}
//},
//Edn::Symbol("_") => {
//clips.push(None);
//},
//Edn::Int(i) => {
//clips.push(Some(*i as usize));
//},
//_ => panic!("unexpected in scene '{name:?}': {edn:?}")
//});
//Ok(ArrangerScene {
//name: Arc::new(name.unwrap_or("").to_string().into()),
//color: ItemColor::random(),
//clips,
//})
//}
}
impl HasJack for SequencerModel {
fn jack (&self) -> &Arc<RwLock<JackClient>> {
self.transport.jack()
}
}
impl HasClock for SequencerModel {
fn clock (&self) -> &Arc<Clock> {
self.transport.clock()
}
}
//impl TransportModelApi for SequencerModel {
//fn transport (&self) -> &jack::Transport {
//&self.transport.transport()
//}
//fn metronome (&self) -> bool {
//self.transport.metronome()
//}
//}
impl PhrasePoolModelApi for SequencerModel {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
&self.phrases
}
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
&mut self.phrases
}
}
impl HasPlayer for SequencerModel {
fn player (&self) -> &MIDIPlayer {
&self.player
}
fn player_mut (&mut self) -> &mut MIDIPlayer {
&mut self.player
}
}
impl From<Instant> for Clock {
fn from (current: Instant) -> Self {
Self {
playing: Some(TransportState::Stopped).into(),
started: None.into(),
quant: 24.into(),
sync: (current.timebase.ppq.get() * 4.).into(),
current,
}
}
}
/// Methods used primarily by the process callback
impl MIDIPlayer {
pub fn new (
jack: &Arc<RwLock<JackClient>>,
clock: &Arc<Clock>,
name: &str
) -> Usually<Self> {
let jack = jack.read().unwrap();
Ok(Self {
clock: clock.clone(),
phrase: None,
next_phrase: None,
notes_in: Arc::new(RwLock::new([false;128])),
notes_out: Arc::new(RwLock::new([false;128])),
monitoring: false,
recording: false,
overdub: true,
reset: true,
midi_note: Vec::with_capacity(8),
midi_chunk: vec![Vec::with_capacity(16);16384],
midi_outputs: vec![
jack.client().register_port(format!("{name}_out0").as_str(), MidiOut::default())?
],
midi_inputs: vec![
jack.client().register_port(format!("{name}_in0").as_str(), MidiIn::default())?
],
})
}
}

View file

@ -15,31 +15,28 @@ submod! {
api_arranger_clip
api_arranger_scene
api_arranger_track
api_clock
api_phrase
api_playhead
api_pool
api_sequencer
//api_mixer
//api_mixer_track
api_phrase
api_playhead
//api_plugin
//api_plugin_kind
//api_plugin_lv2
api_pool
//api_sampler
//api_sampler_sample
//api_sampler_voice
api_sequencer
impls
models
model_arranger
model_arranger_scene
model_arranger_track
model_clock
model_player
model_pool
model_sequencer
model_transport
}
pub trait HasJack {

View file

@ -0,0 +1,63 @@
use crate::*;
#[derive(Debug)]
pub struct ArrangerModel {
/// State of the JACK transport.
transport: TransportModel,
/// Collection of phrases.
phrases: Vec<Arc<RwLock<Phrase>>>,
/// Collection of tracks.
tracks: Vec<ArrangerTrack>,
/// Collection of scenes.
scenes: Vec<ArrangerScene>,
/// Name of arranger
name: Arc<RwLock<String>>,
}
impl HasJack for ArrangerModel {
fn jack (&self) -> &Arc<RwLock<JackClient>> {
&self.transport.jack()
}
}
impl HasClock for ArrangerModel {
fn clock (&self) -> &Arc<Clock> {
&self.transport.clock()
}
}
//impl TransportModelApi for ArrangerModel {
//fn transport (&self) -> &jack::Transport {
//&self.transport.transport()
//}
//fn metronome (&self) -> bool {
//self.transport.metronome()
//}
//}
impl HasPhrases for ArrangerModel {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
&self.phrases
}
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
&mut self.phrases
}
}
impl ArrangerApi for ArrangerModel {
fn name (&self) -> &Arc<RwLock<String>> {
&self.name
}
fn tracks (&self) -> &Vec<ArrangerTrack> {
&self.tracks
}
fn tracks_mut (&mut self) -> &mut Vec<ArrangerTrack> {
&mut self.tracks
}
fn scenes (&self) -> &Vec<ArrangerScene> {
&self.scenes
}
fn scenes_mut (&mut self) -> &mut Vec<ArrangerScene> {
&mut self.scenes
}
}

View file

@ -0,0 +1,42 @@
use crate::*;
#[derive(Default, Debug, Clone)]
pub struct ArrangerScene {
/// Name of scene
pub name: Arc<RwLock<String>>,
/// Clips in scene, one per track
pub clips: Vec<Option<Arc<RwLock<Phrase>>>>,
/// Identifying color of scene
pub color: ItemColor,
}
impl ArrangerScene {
//TODO
//pub fn from_edn <'a, 'e> (args: &[Edn<'e>]) -> Usually<Self> {
//let mut name = None;
//let mut clips = vec![];
//edn!(edn in args {
//Edn::Map(map) => {
//let key = map.get(&Edn::Key(":name"));
//if let Some(Edn::Str(n)) = key {
//name = Some(*n);
//} else {
//panic!("unexpected key in scene '{name:?}': {key:?}")
//}
//},
//Edn::Symbol("_") => {
//clips.push(None);
//},
//Edn::Int(i) => {
//clips.push(Some(*i as usize));
//},
//_ => panic!("unexpected in scene '{name:?}': {edn:?}")
//});
//Ok(ArrangerScene {
//name: Arc::new(name.unwrap_or("").to_string().into()),
//color: ItemColor::random(),
//clips,
//})
//}
}

View file

@ -0,0 +1,13 @@
use crate::*;
#[derive(Debug)]
pub struct ArrangerTrack {
/// Name of track
pub name: Arc<RwLock<String>>,
/// Preferred width of track column
pub width: usize,
/// Identifying color of track
pub color: ItemColor,
/// The MIDI player for the track
pub player: MIDIPlayer
}

View file

@ -0,0 +1,43 @@
use crate::*;
/// A timer with starting point, current time, and quantization
#[derive(Default, Debug)]
pub struct Clock {
/// Playback state
pub playing: RwLock<Option<TransportState>>,
/// Global sample and usec at which playback started
pub started: RwLock<Option<(usize, usize)>>,
/// Current moment in time
pub current: Instant,
/// Note quantization factor
pub quant: Quantize,
/// Launch quantization factor
pub sync: LaunchSync,
}
impl ClockApi for Clock {
fn quant (&self) -> &Quantize {
&self.quant
}
fn sync (&self) -> &LaunchSync {
&self.sync
}
fn current (&self) -> &Instant {
&self.current
}
}
impl PlayheadApi for Clock {
}
impl From<Instant> for Clock {
fn from (current: Instant) -> Self {
Self {
playing: Some(TransportState::Stopped).into(),
started: None.into(),
quant: 24.into(),
sync: (current.timebase.ppq.get() * 4.).into(),
current,
}
}
}

View file

@ -0,0 +1,61 @@
use crate::*;
#[derive(Debug)]
pub struct MIDIPlayer {
/// Global timebase
pub clock: Arc<Clock>,
/// Start time and phrase being played
pub phrase: Option<(Instant, Option<Arc<RwLock<Phrase>>>)>,
/// Start time and next phrase
pub next_phrase: Option<(Instant, Option<Arc<RwLock<Phrase>>>)>,
/// Play input through output.
pub monitoring: bool,
/// Write input to sequence.
pub recording: bool,
/// Overdub input to sequence.
pub overdub: bool,
/// Send all notes off
pub reset: bool, // TODO?: after Some(nframes)
/// Record from MIDI ports to current sequence.
pub midi_inputs: Vec<Port<MidiIn>>,
/// Play from current sequence to MIDI ports
pub midi_outputs: Vec<Port<MidiOut>>,
/// MIDI output buffer
pub midi_note: Vec<u8>,
/// MIDI output buffer
pub midi_chunk: Vec<Vec<Vec<u8>>>,
/// Notes currently held at input
pub notes_in: Arc<RwLock<[bool; 128]>>,
/// Notes currently held at output
pub notes_out: Arc<RwLock<[bool; 128]>>,
}
/// Methods used primarily by the process callback
impl MIDIPlayer {
pub fn new (
jack: &Arc<RwLock<JackClient>>,
clock: &Arc<Clock>,
name: &str
) -> Usually<Self> {
let jack = jack.read().unwrap();
Ok(Self {
clock: clock.clone(),
phrase: None,
next_phrase: None,
notes_in: Arc::new(RwLock::new([false;128])),
notes_out: Arc::new(RwLock::new([false;128])),
monitoring: false,
recording: false,
overdub: true,
reset: true,
midi_note: Vec::with_capacity(8),
midi_chunk: vec![Vec::with_capacity(16);16384],
midi_outputs: vec![
jack.client().register_port(format!("{name}_out0").as_str(), MidiOut::default())?
],
midi_inputs: vec![
jack.client().register_port(format!("{name}_in0").as_str(), MidiIn::default())?
],
})
}
}

View file

@ -0,0 +1,17 @@
use crate::*;
/// Contains all phrases in a project
#[derive(Debug)]
pub struct PhrasePoolModel {
/// Phrases in the pool
pub phrases: Vec<Arc<RwLock<Phrase>>>,
}
impl HasPhrases for PhrasePoolModel {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
&self.phrases
}
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
&mut self.phrases
}
}

View file

@ -0,0 +1,49 @@
use crate::*;
pub struct SequencerModel {
/// State of the JACK transport.
transport: TransportModel,
/// State of the phrase pool.
phrases: Vec<Arc<RwLock<Phrase>>>,
/// State of the phrase player.
player: MIDIPlayer,
}
impl HasJack for SequencerModel {
fn jack (&self) -> &Arc<RwLock<JackClient>> {
self.transport.jack()
}
}
impl HasClock for SequencerModel {
fn clock (&self) -> &Arc<Clock> {
self.transport.clock()
}
}
//impl TransportModelApi for SequencerModel {
//fn transport (&self) -> &jack::Transport {
//&self.transport.transport()
//}
//fn metronome (&self) -> bool {
//self.transport.metronome()
//}
//}
impl HasPhrases for SequencerModel {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
&self.phrases
}
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
&mut self.phrases
}
}
impl HasPlayer for SequencerModel {
fn player (&self) -> &MIDIPlayer {
&self.player
}
fn player_mut (&mut self) -> &mut MIDIPlayer {
&mut self.player
}
}

View file

@ -0,0 +1,43 @@
use crate::*;
pub struct TransportModel {
jack: Arc<RwLock<JackClient>>,
/// Current sample rate, tempo, and PPQ.
clock: Arc<Clock>,
/// JACK transport handle.
transport: jack::Transport,
/// Enable metronome?
metronome: bool,
}
impl HasJack for TransportModel {
fn jack (&self) -> &Arc<RwLock<JackClient>> {
&self.jack
}
}
impl HasClock for TransportModel {
fn clock (&self) -> &Arc<Clock> {
&self.clock
}
}
//impl TransportModelApi for TransportModel {
//fn transport (&self) -> &jack::Transport {
//&self.transport
//}
//fn metronome (&self) -> bool {
//self.metronome
//}
//}
impl Debug for TransportModel {
fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
f.debug_struct("transport")
.field("jack", &self.jack)
.field("transport", &"(JACK transport)")
.field("clock", &self.clock)
.field("metronome", &self.metronome)
.finish()
}
}

View file

@ -1,101 +1 @@
use crate::*;
/// A timer with starting point, current time, and quantization
#[derive(Default, Debug)]
pub struct Clock {
/// Playback state
pub playing: RwLock<Option<TransportState>>,
/// Global sample and usec at which playback started
pub started: RwLock<Option<(usize, usize)>>,
/// Current moment in time
pub current: Instant,
/// Note quantization factor
pub quant: Quantize,
/// Launch quantization factor
pub sync: LaunchSync,
}
pub struct TransportModel {
jack: Arc<RwLock<JackClient>>,
/// Current sample rate, tempo, and PPQ.
clock: Arc<Clock>,
/// JACK transport handle.
transport: jack::Transport,
/// Enable metronome?
metronome: bool,
}
#[derive(Debug)]
pub struct ArrangerModel {
/// State of the JACK transport.
transport: TransportModel,
/// Collection of phrases.
phrases: Vec<Arc<RwLock<Phrase>>>,
/// Collection of tracks.
tracks: Vec<ArrangerTrack>,
/// Collection of scenes.
scenes: Vec<ArrangerScene>,
/// Name of arranger
name: Arc<RwLock<String>>,
}
#[derive(Default, Debug, Clone)]
pub struct ArrangerScene {
/// Name of scene
pub name: Arc<RwLock<String>>,
/// Clips in scene, one per track
pub clips: Vec<Option<Arc<RwLock<Phrase>>>>,
/// Identifying color of scene
pub color: ItemColor,
}
#[derive(Debug)]
pub struct ArrangerTrack {
/// Name of track
pub name: Arc<RwLock<String>>,
/// Preferred width of track column
pub width: usize,
/// Identifying color of track
pub color: ItemColor,
/// The MIDI player for the track
pub player: MIDIPlayer
}
pub struct SequencerModel {
/// State of the JACK transport.
transport: TransportModel,
/// State of the phrase pool.
phrases: Vec<Arc<RwLock<Phrase>>>,
/// State of the phrase player.
player: MIDIPlayer,
}
#[derive(Debug)]
pub struct MIDIPlayer {
/// Global timebase
pub clock: Arc<Clock>,
/// Start time and phrase being played
pub phrase: Option<(Instant, Option<Arc<RwLock<Phrase>>>)>,
/// Start time and next phrase
pub next_phrase: Option<(Instant, Option<Arc<RwLock<Phrase>>>)>,
/// Play input through output.
pub monitoring: bool,
/// Write input to sequence.
pub recording: bool,
/// Overdub input to sequence.
pub overdub: bool,
/// Send all notes off
pub reset: bool, // TODO?: after Some(nframes)
/// Record from MIDI ports to current sequence.
pub midi_inputs: Vec<Port<MidiIn>>,
/// Play from current sequence to MIDI ports
pub midi_outputs: Vec<Port<MidiOut>>,
/// MIDI output buffer
pub midi_note: Vec<u8>,
/// MIDI output buffer
pub midi_chunk: Vec<Vec<Vec<u8>>>,
/// Notes currently held at input
pub notes_in: Arc<RwLock<[bool; 128]>>,
/// Notes currently held at output
pub notes_out: Arc<RwLock<[bool; 128]>>,
}