mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-10 13:46:42 +01:00
167 lines
5.1 KiB
Rust
167 lines
5.1 KiB
Rust
//! MIDI sequencer
|
|
use crate::*;
|
|
|
|
pub trait HasSequencer {
|
|
fn sequencer (&self) -> &impl MidiSequencer;
|
|
fn sequencer_mut (&mut self) -> &mut impl MidiSequencer;
|
|
}
|
|
|
|
#[macro_export] macro_rules! has_sequencer {
|
|
(|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => {
|
|
impl $(<$($L),*$($T $(: $U)?),*>)? HasSequencer for $Struct $(<$($L),*$($T),*>)? {
|
|
fn sequencer (&$self) -> &impl MidiSequencer { &$cb }
|
|
fn sequencer_mut (&mut $self) -> &mut impl MidiSequencer { &mut$cb }
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Contains state for playing a clip
|
|
pub struct Sequencer {
|
|
/// State of clock and playhead
|
|
pub clock: Clock,
|
|
/// Start time and clip being played
|
|
pub play_clip: Option<(Moment, Option<Arc<RwLock<MidiClip>>>)>,
|
|
/// Start time and next clip
|
|
pub next_clip: Option<(Moment, Option<Arc<RwLock<MidiClip>>>)>,
|
|
/// 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_ins: Vec<JackMidiIn>,
|
|
/// Play from current sequence to MIDI ports
|
|
pub midi_outs: Vec<JackMidiOut>,
|
|
/// Notes currently held at input
|
|
pub notes_in: Arc<RwLock<[bool; 128]>>,
|
|
/// Notes currently held at output
|
|
pub notes_out: Arc<RwLock<[bool; 128]>>,
|
|
/// MIDI output buffer
|
|
pub note_buf: Vec<u8>,
|
|
}
|
|
|
|
impl Default for Sequencer {
|
|
fn default () -> Self {
|
|
Self {
|
|
play_clip: None,
|
|
next_clip: None,
|
|
recording: false,
|
|
monitoring: false,
|
|
overdub: false,
|
|
|
|
notes_in: RwLock::new([false;128]).into(),
|
|
notes_out: RwLock::new([false;128]).into(),
|
|
note_buf: vec![0;8],
|
|
reset: true,
|
|
|
|
midi_ins: vec![],
|
|
midi_outs: vec![],
|
|
clock: Clock::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Sequencer {
|
|
pub fn new (
|
|
name: impl AsRef<str>,
|
|
jack: &Jack,
|
|
clock: Option<&Clock>,
|
|
clip: Option<&Arc<RwLock<MidiClip>>>,
|
|
midi_from: &[PortConnect],
|
|
midi_to: &[PortConnect],
|
|
) -> Usually<Self> {
|
|
let _name = name.as_ref();
|
|
let clock = clock.cloned().unwrap_or_default();
|
|
Ok(Self {
|
|
midi_ins: vec![JackMidiIn::new(jack, format!("M/{}", name.as_ref()), midi_from)?,],
|
|
midi_outs: vec![JackMidiOut::new(jack, format!("{}/M", name.as_ref()), midi_to)?, ],
|
|
play_clip: clip.map(|clip|(Moment::zero(&clock.timebase), Some(clip.clone()))),
|
|
clock,
|
|
note_buf: vec![0;8],
|
|
reset: true,
|
|
recording: false,
|
|
monitoring: false,
|
|
overdub: false,
|
|
next_clip: None,
|
|
notes_in: RwLock::new([false;128]).into(),
|
|
notes_out: RwLock::new([false;128]).into(),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for Sequencer {
|
|
fn fmt (&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
f.debug_struct("Sequencer")
|
|
.field("clock", &self.clock)
|
|
.field("play_clip", &self.play_clip)
|
|
.field("next_clip", &self.next_clip)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
has_clock!(|self: Sequencer|self.clock);
|
|
|
|
impl HasMidiIns for Sequencer {
|
|
fn midi_ins (&self) -> &Vec<JackMidiIn> { &self.midi_ins }
|
|
fn midi_ins_mut (&mut self) -> &mut Vec<JackMidiIn> { &mut self.midi_ins }
|
|
}
|
|
|
|
impl HasMidiOuts for Sequencer {
|
|
fn midi_outs (&self) -> &Vec<JackMidiOut> { &self.midi_outs }
|
|
fn midi_outs_mut (&mut self) -> &mut Vec<JackMidiOut> { &mut self.midi_outs }
|
|
fn midi_note (&mut self) -> &mut Vec<u8> { &mut self.note_buf }
|
|
}
|
|
|
|
impl MidiRecorder for Sequencer {
|
|
fn recording (&self) -> bool {
|
|
self.recording
|
|
}
|
|
fn recording_mut (&mut self) -> &mut bool {
|
|
&mut self.recording
|
|
}
|
|
fn monitoring (&self) -> bool {
|
|
self.monitoring
|
|
}
|
|
fn monitoring_mut (&mut self) -> &mut bool {
|
|
&mut self.monitoring
|
|
}
|
|
fn overdub (&self) -> bool {
|
|
self.overdub
|
|
}
|
|
fn overdub_mut (&mut self) -> &mut bool {
|
|
&mut self.overdub
|
|
}
|
|
fn notes_in (&self) -> &Arc<RwLock<[bool; 128]>> {
|
|
&self.notes_in
|
|
}
|
|
}
|
|
|
|
impl MidiPlayer for Sequencer {
|
|
fn notes_out (&self) -> &Arc<RwLock<[bool; 128]>> {
|
|
&self.notes_out
|
|
}
|
|
}
|
|
|
|
impl HasPlayClip for Sequencer {
|
|
fn reset (&self) -> bool {
|
|
self.reset
|
|
}
|
|
fn reset_mut (&mut self) -> &mut bool {
|
|
&mut self.reset
|
|
}
|
|
fn play_clip (&self) -> &Option<(Moment, Option<Arc<RwLock<MidiClip>>>)> {
|
|
&self.play_clip
|
|
}
|
|
fn play_clip_mut (&mut self) -> &mut Option<(Moment, Option<Arc<RwLock<MidiClip>>>)> {
|
|
&mut self.play_clip
|
|
}
|
|
fn next_clip (&self) -> &Option<(Moment, Option<Arc<RwLock<MidiClip>>>)> {
|
|
&self.next_clip
|
|
}
|
|
fn next_clip_mut (&mut self) -> &mut Option<(Moment, Option<Arc<RwLock<MidiClip>>>)> {
|
|
&mut self.next_clip
|
|
}
|
|
}
|