mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: refactor pt.34 (35e) more traits, fewer structs
This commit is contained in:
parent
cbbecc5aba
commit
beca1a6ade
19 changed files with 361 additions and 379 deletions
|
|
@ -1,140 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum ArrangerCommand {
|
|
||||||
Clear,
|
|
||||||
Export,
|
|
||||||
Import,
|
|
||||||
StopAll,
|
|
||||||
Scene(ArrangerSceneCommand),
|
|
||||||
Track(ArrangerTrackCommand),
|
|
||||||
Clip(ArrangerClipCommand),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ArrangerApi: HasJack + HasClock {
|
|
||||||
fn name (&self) -> &Arc<RwLock<String>>;
|
|
||||||
|
|
||||||
fn tracks (&self) -> &Vec<ArrangerTrack>;
|
|
||||||
fn tracks_mut (&mut self) -> &mut Vec<ArrangerTrack>;
|
|
||||||
|
|
||||||
fn scenes (&self) -> &Vec<ArrangerScene>;
|
|
||||||
fn scenes_mut (&mut self) -> &mut Vec<ArrangerScene>;
|
|
||||||
|
|
||||||
fn track_default_name (&self) -> String {
|
|
||||||
format!("Track {}", self.tracks().len() + 1)
|
|
||||||
}
|
|
||||||
fn track_add (
|
|
||||||
&mut self, name: Option<&str>, color: Option<ItemColor>
|
|
||||||
) -> Usually<&mut ArrangerTrack> {
|
|
||||||
let name = name.map_or_else(||self.track_default_name(), |x|x.to_string());
|
|
||||||
let track = ArrangerTrack {
|
|
||||||
width: name.len() + 2,
|
|
||||||
color: color.unwrap_or_else(||ItemColor::random()),
|
|
||||||
player: MIDIPlayer::new(&self.jack(), &self.clock(), name.as_str())?,
|
|
||||||
name: Arc::new(name.into()),
|
|
||||||
};
|
|
||||||
self.tracks_mut().push(track);
|
|
||||||
let index = self.tracks().len() - 1;
|
|
||||||
Ok(&mut self.tracks_mut()[index])
|
|
||||||
}
|
|
||||||
fn track_del (&mut self, index: usize) {
|
|
||||||
self.tracks_mut().remove(index);
|
|
||||||
for scene in self.scenes_mut().iter_mut() {
|
|
||||||
scene.clips.remove(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn scene_default_name (&self) -> String {
|
|
||||||
format!("Scene {}", self.scenes().len() + 1)
|
|
||||||
}
|
|
||||||
fn scene_add (
|
|
||||||
&mut self, name: Option<&str>, color: Option<ItemColor>
|
|
||||||
) -> Usually<&mut ArrangerScene> {
|
|
||||||
let name = name.map_or_else(||self.scene_default_name(), |x|x.to_string());
|
|
||||||
let scene = ArrangerScene {
|
|
||||||
name: Arc::new(name.into()),
|
|
||||||
clips: vec![None;self.tracks().len()],
|
|
||||||
color: color.unwrap_or_else(||ItemColor::random()),
|
|
||||||
};
|
|
||||||
self.scenes_mut().push(scene);
|
|
||||||
let index = self.scenes().len() - 1;
|
|
||||||
Ok(&mut self.scenes_mut()[index])
|
|
||||||
}
|
|
||||||
fn scene_del (&mut self, index: usize) {
|
|
||||||
self.scenes_mut().remove(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Command<ArrangerModel> for ArrangerCommand {
|
|
||||||
fn execute (self, state: &mut ArrangerModel) -> Perhaps<Self> {
|
|
||||||
match self {
|
|
||||||
Self::Scene(command) => { return Ok(command.execute(state)?.map(Self::Scene)) },
|
|
||||||
Self::Track(command) => { return Ok(command.execute(state)?.map(Self::Track)) },
|
|
||||||
Self::Clip(command) => { return Ok(command.execute(state)?.map(Self::Clip)) },
|
|
||||||
_ => todo!()
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//impl Command<ArrangerModel> for ArrangerSceneCommand {
|
|
||||||
//}
|
|
||||||
//Edit(phrase) => { state.state.phrase = phrase.clone() },
|
|
||||||
//ToggleViewMode => { state.state.mode.to_next(); },
|
|
||||||
//Delete => { state.state.delete(); },
|
|
||||||
//Activate => { state.state.activate(); },
|
|
||||||
//ZoomIn => { state.state.zoom_in(); },
|
|
||||||
//ZoomOut => { state.state.zoom_out(); },
|
|
||||||
//MoveBack => { state.state.move_back(); },
|
|
||||||
//MoveForward => { state.state.move_forward(); },
|
|
||||||
//RandomColor => { state.state.randomize_color(); },
|
|
||||||
//Put => { state.state.phrase_put(); },
|
|
||||||
//Get => { state.state.phrase_get(); },
|
|
||||||
//AddScene => { state.state.scene_add(None, None)?; },
|
|
||||||
//AddTrack => { state.state.track_add(None, None)?; },
|
|
||||||
//ToggleLoop => { state.state.toggle_loop() },
|
|
||||||
//pub fn zoom_in (&mut self) {
|
|
||||||
//if let ArrangerEditorMode::Vertical(factor) = self.mode {
|
|
||||||
//self.mode = ArrangerEditorMode::Vertical(factor + 1)
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
//pub fn zoom_out (&mut self) {
|
|
||||||
//if let ArrangerEditorMode::Vertical(factor) = self.mode {
|
|
||||||
//self.mode = ArrangerEditorMode::Vertical(factor.saturating_sub(1))
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
//pub fn move_back (&mut self) {
|
|
||||||
//match self.selected {
|
|
||||||
//ArrangerEditorFocus::Scene(s) => {
|
|
||||||
//if s > 0 {
|
|
||||||
//self.scenes.swap(s, s - 1);
|
|
||||||
//self.selected = ArrangerEditorFocus::Scene(s - 1);
|
|
||||||
//}
|
|
||||||
//},
|
|
||||||
//ArrangerEditorFocus::Track(t) => {
|
|
||||||
//if t > 0 {
|
|
||||||
//self.tracks.swap(t, t - 1);
|
|
||||||
//self.selected = ArrangerEditorFocus::Track(t - 1);
|
|
||||||
//// FIXME: also swap clip order in scenes
|
|
||||||
//}
|
|
||||||
//},
|
|
||||||
//_ => todo!("arrangement: move forward")
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
//pub fn move_forward (&mut self) {
|
|
||||||
//match self.selected {
|
|
||||||
//ArrangerEditorFocus::Scene(s) => {
|
|
||||||
//if s < self.scenes.len().saturating_sub(1) {
|
|
||||||
//self.scenes.swap(s, s + 1);
|
|
||||||
//self.selected = ArrangerEditorFocus::Scene(s + 1);
|
|
||||||
//}
|
|
||||||
//},
|
|
||||||
//ArrangerEditorFocus::Track(t) => {
|
|
||||||
//if t < self.tracks.len().saturating_sub(1) {
|
|
||||||
//self.tracks.swap(t, t + 1);
|
|
||||||
//self.selected = ArrangerEditorFocus::Track(t + 1);
|
|
||||||
//// FIXME: also swap clip order in scenes
|
|
||||||
//}
|
|
||||||
//},
|
|
||||||
//_ => todo!("arrangement: move forward")
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum ArrangerTrackCommand {
|
|
||||||
Add,
|
|
||||||
Delete(usize),
|
|
||||||
RandomColor,
|
|
||||||
Stop,
|
|
||||||
Swap(usize, usize),
|
|
||||||
SetSize(usize),
|
|
||||||
SetZoom(usize),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: ArrangerApi> Command<T> for ArrangerTrackCommand {
|
|
||||||
fn execute (self, state: &mut T) -> Perhaps<Self> {
|
|
||||||
match self {
|
|
||||||
Self::Delete(index) => { state.track_del(index); },
|
|
||||||
_ => todo!()
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ArrangerTrackApi: Sized {
|
|
||||||
/// Name of track
|
|
||||||
fn name (&self) -> Arc<RwLock<String>>;
|
|
||||||
/// Preferred width of track column
|
|
||||||
fn width (&self) -> usize;
|
|
||||||
/// Preferred width of track column
|
|
||||||
fn width_mut (&mut self) -> &mut usize;
|
|
||||||
/// Identifying color of track
|
|
||||||
fn color (&self) -> ItemColor;
|
|
||||||
/// The MIDI player for the track
|
|
||||||
fn player (&self) -> MIDIPlayer;
|
|
||||||
|
|
||||||
fn longest_name (tracks: &[Self]) -> usize {
|
|
||||||
tracks.iter().map(|s|s.name().read().unwrap().len()).fold(0, usize::max)
|
|
||||||
}
|
|
||||||
|
|
||||||
const MIN_WIDTH: usize = 3;
|
|
||||||
|
|
||||||
fn width_inc (&mut self) {
|
|
||||||
*self.width_mut() += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn width_dec (&mut self) {
|
|
||||||
if self.width() > Self::MIN_WIDTH {
|
|
||||||
*self.width_mut() -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -10,11 +10,11 @@ pub enum ArrangerClipCommand {
|
||||||
RandomColor,
|
RandomColor,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ArrangerApi> Command<T> for ArrangerClipCommand {
|
//impl<T: ArrangerApi> Command<T> for ArrangerClipCommand {
|
||||||
fn execute (self, state: &mut T) -> Perhaps<Self> {
|
//fn execute (self, state: &mut T) -> Perhaps<Self> {
|
||||||
match self {
|
//match self {
|
||||||
_ => todo!()
|
//_ => todo!()
|
||||||
}
|
//}
|
||||||
Ok(None)
|
//Ok(None)
|
||||||
}
|
//}
|
||||||
}
|
//}
|
||||||
10
crates/tek_api/src/api_jack.rs
Normal file
10
crates/tek_api/src/api_jack.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
pub trait HasJack {
|
||||||
|
fn jack (&self) -> &impl JackApi;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait JackApi {
|
||||||
|
fn jack (&self) -> &Arc<RwLock<JackClient>>;
|
||||||
|
fn transport (&self) -> &RwLock<Option<TransportState>>;
|
||||||
|
}
|
||||||
|
|
@ -1,84 +1 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
/// A MIDI sequence.
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Phrase {
|
|
||||||
pub uuid: uuid::Uuid,
|
|
||||||
/// Name of phrase
|
|
||||||
pub name: String,
|
|
||||||
/// Temporal resolution in pulses per quarter note
|
|
||||||
pub ppq: usize,
|
|
||||||
/// Length of phrase in pulses
|
|
||||||
pub length: usize,
|
|
||||||
/// Notes in phrase
|
|
||||||
pub notes: PhraseData,
|
|
||||||
/// Whether to loop the phrase or play it once
|
|
||||||
pub loop_on: bool,
|
|
||||||
/// Start of loop
|
|
||||||
pub loop_start: usize,
|
|
||||||
/// Length of loop
|
|
||||||
pub loop_length: usize,
|
|
||||||
/// All notes are displayed with minimum length
|
|
||||||
pub percussive: bool,
|
|
||||||
/// Identifying color of phrase
|
|
||||||
pub color: ItemColorTriplet,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// MIDI message structural
|
|
||||||
pub type PhraseData = Vec<Vec<MidiMessage>>;
|
|
||||||
|
|
||||||
impl Phrase {
|
|
||||||
pub fn new (
|
|
||||||
name: impl AsRef<str>,
|
|
||||||
loop_on: bool,
|
|
||||||
length: usize,
|
|
||||||
notes: Option<PhraseData>,
|
|
||||||
color: Option<ItemColorTriplet>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
uuid: uuid::Uuid::new_v4(),
|
|
||||||
name: name.as_ref().to_string(),
|
|
||||||
ppq: PPQ,
|
|
||||||
length,
|
|
||||||
notes: notes.unwrap_or(vec![Vec::with_capacity(16);length]),
|
|
||||||
loop_on,
|
|
||||||
loop_start: 0,
|
|
||||||
loop_length: length,
|
|
||||||
percussive: true,
|
|
||||||
color: color.unwrap_or_else(ItemColorTriplet::random)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn duplicate (&self) -> Self {
|
|
||||||
let mut clone = self.clone();
|
|
||||||
clone.uuid = uuid::Uuid::new_v4();
|
|
||||||
clone
|
|
||||||
}
|
|
||||||
pub fn toggle_loop (&mut self) { self.loop_on = !self.loop_on; }
|
|
||||||
pub fn record_event (&mut self, pulse: usize, message: MidiMessage) {
|
|
||||||
if pulse >= self.length { panic!("extend phrase first") }
|
|
||||||
self.notes[pulse].push(message);
|
|
||||||
}
|
|
||||||
/// Check if a range `start..end` contains MIDI Note On `k`
|
|
||||||
pub fn contains_note_on (&self, k: u7, start: usize, end: usize) -> bool {
|
|
||||||
//panic!("{:?} {start} {end}", &self);
|
|
||||||
for events in self.notes[start.max(0)..end.min(self.notes.len())].iter() {
|
|
||||||
for event in events.iter() {
|
|
||||||
if let MidiMessage::NoteOn {key,..} = event { if *key == k { return true } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Default for Phrase {
|
|
||||||
fn default () -> Self {
|
|
||||||
Self::new("(empty)", false, 0, None, Some(ItemColor::from(Color::Rgb(0, 0, 0)).into()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for Phrase {
|
|
||||||
fn eq (&self, other: &Self) -> bool {
|
|
||||||
self.uuid == other.uuid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for Phrase {}
|
|
||||||
|
|
|
||||||
|
|
@ -35,18 +35,23 @@ impl<T: PlayheadApi> Command<T> for PlayheadCommand {
|
||||||
|
|
||||||
pub trait PlayheadApi: ClockApi {
|
pub trait PlayheadApi: ClockApi {
|
||||||
|
|
||||||
fn playing (&self) -> &Arc<RwLock<TransportState>>;
|
fn transport (&self) -> &RwLock<Option<TransportState>>;
|
||||||
|
|
||||||
fn transport (&self) -> Transport;
|
fn playing (&self) -> &RwLock<Option<TransportState>>;
|
||||||
|
|
||||||
|
/// Global sample and usec at which playback started
|
||||||
|
fn started (&self) -> &RwLock<Option<(usize, usize)>>;
|
||||||
|
|
||||||
fn pulse (&self) -> f64 {
|
fn pulse (&self) -> f64 {
|
||||||
self.current().pulse.get()
|
self.current().pulse.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_launch_pulse (&self) -> usize {
|
fn next_launch_pulse (&self) -> usize {
|
||||||
let sync = self.sync().get() as usize;
|
let sync = self.sync().get() as usize;
|
||||||
let pulse = self.pulse() as usize;
|
let pulse = self.pulse() as usize;
|
||||||
if pulse % sync == 0 { pulse } else { (pulse / sync + 1) * sync }
|
if pulse % sync == 0 { pulse } else { (pulse / sync + 1) * sync }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toggle_play (&self) -> Usually<()> {
|
fn toggle_play (&self) -> Usually<()> {
|
||||||
let playing = self.playing().read().unwrap().expect("1st sample has not been processed yet");
|
let playing = self.playing().read().unwrap().expect("1st sample has not been processed yet");
|
||||||
let playing = match playing {
|
let playing = match playing {
|
||||||
|
|
@ -63,10 +68,12 @@ pub trait PlayheadApi: ClockApi {
|
||||||
*self.playing().write().unwrap() = playing;
|
*self.playing().write().unwrap() = playing;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_stopped (&self) -> bool {
|
fn is_stopped (&self) -> bool {
|
||||||
*self.playing().read().unwrap() == Some(TransportState::Stopped)
|
*self.playing().read().unwrap() == Some(TransportState::Stopped)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_rolling (&self) -> bool {
|
fn is_rolling (&self) -> bool {
|
||||||
*self.clock.playing.read().unwrap() == Some(TransportState::Rolling)
|
*self.playing().read().unwrap() == Some(TransportState::Rolling)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,30 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
|
pub trait HasScenes<S: ArrangerSceneApi> {
|
||||||
|
fn scenes (&self) -> &Vec<S>;
|
||||||
|
fn scenes_mut (&mut self) -> &mut Vec<S>;
|
||||||
|
|
||||||
|
fn scene_default_name (&self) -> String {
|
||||||
|
format!("Scene {}", self.scenes().len() + 1)
|
||||||
|
}
|
||||||
|
fn scene_add (&mut self, name: Option<&str>, color: Option<ItemColor>)
|
||||||
|
-> Usually<&mut S>
|
||||||
|
{
|
||||||
|
let name = name.map_or_else(||self.scene_default_name(), |x|x.to_string());
|
||||||
|
let scene = ArrangerScene {
|
||||||
|
name: Arc::new(name.into()),
|
||||||
|
clips: vec![None;self.tracks().len()],
|
||||||
|
color: color.unwrap_or_else(||ItemColor::random()),
|
||||||
|
};
|
||||||
|
self.scenes_mut().push(scene);
|
||||||
|
let index = self.scenes().len() - 1;
|
||||||
|
Ok(&mut self.scenes_mut()[index])
|
||||||
|
}
|
||||||
|
fn scene_del (&mut self, index: usize) {
|
||||||
|
self.scenes_mut().remove(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum ArrangerSceneCommand {
|
pub enum ArrangerSceneCommand {
|
||||||
Add,
|
Add,
|
||||||
|
|
@ -11,15 +36,15 @@ pub enum ArrangerSceneCommand {
|
||||||
SetZoom(usize),
|
SetZoom(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ArrangerApi> Command<T> for ArrangerSceneCommand {
|
//impl<T: ArrangerApi> Command<T> for ArrangerSceneCommand {
|
||||||
fn execute (self, state: &mut T) -> Perhaps<Self> {
|
//fn execute (self, state: &mut T) -> Perhaps<Self> {
|
||||||
match self {
|
//match self {
|
||||||
Self::Delete(index) => { state.scene_del(index); },
|
//Self::Delete(index) => { state.scene_del(index); },
|
||||||
_ => todo!()
|
//_ => todo!()
|
||||||
}
|
//}
|
||||||
Ok(None)
|
//Ok(None)
|
||||||
}
|
//}
|
||||||
}
|
//}
|
||||||
|
|
||||||
pub trait ArrangerSceneApi: Sized {
|
pub trait ArrangerSceneApi: Sized {
|
||||||
fn name () -> Arc<RwLock<String>>;
|
fn name () -> Arc<RwLock<String>>;
|
||||||
|
|
@ -24,25 +24,26 @@ pub trait HasMidiBuffer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HasPhrase {
|
pub trait HasPhrase: PlayheadApi + HasClock + HasMidiBuffer {
|
||||||
fn phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
|
fn phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
|
||||||
fn next_phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
|
fn next_phrase (&self) -> &Option<(Instant, Arc<RwLock<Phrase>>)>;
|
||||||
|
fn next_phrase_mut (&mut self) -> &mut Option<(Instant, Arc<RwLock<Phrase>>)>;
|
||||||
fn switchover (&mut self, scope: &ProcessScope) {
|
fn switchover (&mut self, scope: &ProcessScope) {
|
||||||
if self.is_rolling() {
|
if self.is_rolling() {
|
||||||
let sample0 = scope.last_frame_time() as usize;
|
let sample0 = scope.last_frame_time() as usize;
|
||||||
//let samples = scope.n_frames() as usize;
|
//let samples = scope.n_frames() as usize;
|
||||||
if let Some((start_at, phrase)) = &self.next_phrase {
|
if let Some((start_at, phrase)) = &self.next_phrase() {
|
||||||
let start = start_at.sample.get() as usize;
|
let start = start_at.sample.get() as usize;
|
||||||
let sample = self.clock.started.read().unwrap().unwrap().0;
|
let sample = self.clock().started.read().unwrap().unwrap().0;
|
||||||
// If it's time to switch to the next phrase:
|
// If it's time to switch to the next phrase:
|
||||||
if start <= sample0.saturating_sub(sample) {
|
if start <= sample0.saturating_sub(sample) {
|
||||||
// Samples elapsed since phrase was supposed to start
|
// Samples elapsed since phrase was supposed to start
|
||||||
let skipped = sample0 - start;
|
let skipped = sample0 - start;
|
||||||
// Switch over to enqueued phrase
|
// Switch over to enqueued phrase
|
||||||
let started = Instant::from_sample(&self.clock.timebase(), start as f64);
|
let started = Instant::from_sample(&self.clock().timebase(), start as f64);
|
||||||
self.phrase = Some((started, phrase.clone()));
|
self.phrase = Some((started, phrase.clone()));
|
||||||
// Unset enqueuement (TODO: where to implement looping?)
|
// Unset enqueuement (TODO: where to implement looping?)
|
||||||
self.next_phrase = None
|
*self.next_phrase_mut() = None
|
||||||
}
|
}
|
||||||
// TODO fill in remaining ticks of chunk from next phrase.
|
// TODO fill in remaining ticks of chunk from next phrase.
|
||||||
// ?? just call self.play(scope) again, since enqueuement is off ???
|
// ?? just call self.play(scope) again, since enqueuement is off ???
|
||||||
|
|
@ -53,16 +54,16 @@ pub trait HasPhrase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
|
fn enqueue_next (&mut self, phrase: Option<&Arc<RwLock<Phrase>>>) {
|
||||||
let start = self.clock.next_launch_pulse();
|
let start = self.clock().next_launch_pulse();
|
||||||
self.next_phrase = Some((
|
self.next_phrase = Some((
|
||||||
Instant::from_pulse(&self.clock.timebase(), start as f64),
|
Instant::from_pulse(&self.clock().timebase(), start as f64),
|
||||||
phrase.map(|p|p.clone())
|
phrase.map(|p|p.clone())
|
||||||
));
|
));
|
||||||
self.reset = true;
|
self.reset = true;
|
||||||
}
|
}
|
||||||
fn pulses_since_start (&self) -> Option<f64> {
|
fn pulses_since_start (&self) -> Option<f64> {
|
||||||
if let Some((started, Some(_))) = self.phrase.as_ref() {
|
if let Some((started, Some(_))) = self.phrase.as_ref() {
|
||||||
Some(self.clock.current.pulse.get() - started.pulse.get())
|
Some(self.clock().current.pulse.get() - started.pulse.get())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
||||||
80
crates/tek_api/src/api_track.rs
Normal file
80
crates/tek_api/src/api_track.rs
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
pub trait HasTracks<T: ArrangerTrackApi> {
|
||||||
|
fn tracks (&self) -> &Vec<T>;
|
||||||
|
fn tracks_mut (&mut self) -> &mut Vec<T>;
|
||||||
|
|
||||||
|
fn track_default_name (&self) -> String {
|
||||||
|
format!("Track {}", self.tracks().len() + 1)
|
||||||
|
}
|
||||||
|
fn track_add (&mut self, name: Option<&str>, color: Option<ItemColor>)
|
||||||
|
-> Usually<&mut T>
|
||||||
|
{
|
||||||
|
let name = name.map_or_else(||self.track_default_name(), |x|x.to_string());
|
||||||
|
let track = ArrangerTrack {
|
||||||
|
width: name.len() + 2,
|
||||||
|
color: color.unwrap_or_else(||ItemColor::random()),
|
||||||
|
player: MIDIPlayer::new(&self.jack(), &self.clock(), name.as_str())?,
|
||||||
|
name: Arc::new(name.into()),
|
||||||
|
};
|
||||||
|
self.tracks_mut().push(track);
|
||||||
|
let index = self.tracks().len() - 1;
|
||||||
|
Ok(&mut self.tracks_mut()[index])
|
||||||
|
}
|
||||||
|
fn track_del (&mut self, index: usize) {
|
||||||
|
self.tracks_mut().remove(index);
|
||||||
|
for scene in self.scenes_mut().iter_mut() {
|
||||||
|
scene.clips.remove(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum ArrangerTrackCommand {
|
||||||
|
Add,
|
||||||
|
Delete(usize),
|
||||||
|
RandomColor,
|
||||||
|
Stop,
|
||||||
|
Swap(usize, usize),
|
||||||
|
SetSize(usize),
|
||||||
|
SetZoom(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
//impl<T: ArrangerApi> Command<T> for ArrangerTrackCommand {
|
||||||
|
//fn execute (self, state: &mut T) -> Perhaps<Self> {
|
||||||
|
//match self {
|
||||||
|
//Self::Delete(index) => { state.track_del(index); },
|
||||||
|
//_ => todo!()
|
||||||
|
//}
|
||||||
|
//Ok(None)
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub trait ArrangerTrackApi: Sized {
|
||||||
|
/// Name of track
|
||||||
|
fn name (&self) -> Arc<RwLock<String>>;
|
||||||
|
/// Preferred width of track column
|
||||||
|
fn width (&self) -> usize;
|
||||||
|
/// Preferred width of track column
|
||||||
|
fn width_mut (&mut self) -> &mut usize;
|
||||||
|
/// Identifying color of track
|
||||||
|
fn color (&self) -> ItemColor;
|
||||||
|
/// The MIDI player for the track
|
||||||
|
fn player (&self) -> MIDIPlayer;
|
||||||
|
|
||||||
|
fn longest_name (tracks: &[Self]) -> usize {
|
||||||
|
tracks.iter().map(|s|s.name().read().unwrap().len()).fold(0, usize::max)
|
||||||
|
}
|
||||||
|
|
||||||
|
const MIN_WIDTH: usize = 3;
|
||||||
|
|
||||||
|
fn width_inc (&mut self) {
|
||||||
|
*self.width_mut() += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn width_dec (&mut self) {
|
||||||
|
if self.width() > Self::MIN_WIDTH {
|
||||||
|
*self.width_mut() -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,17 +11,16 @@ pub(crate) use tek_core::jack::{
|
||||||
submod! {
|
submod! {
|
||||||
//api_jack
|
//api_jack
|
||||||
|
|
||||||
api_arranger
|
api_clip
|
||||||
api_arranger_clip
|
api_scene
|
||||||
api_arranger_scene
|
api_track
|
||||||
api_arranger_track
|
|
||||||
api_clock
|
api_clock
|
||||||
api_phrase
|
api_jack
|
||||||
api_playhead
|
api_playhead
|
||||||
api_pool
|
api_pool
|
||||||
api_sequencer
|
api_sequencer
|
||||||
//api_mixer
|
//api_mixer
|
||||||
//api_mixer_track
|
//api_channel
|
||||||
//api_plugin
|
//api_plugin
|
||||||
//api_plugin_kind
|
//api_plugin_kind
|
||||||
//api_plugin_lv2
|
//api_plugin_lv2
|
||||||
|
|
@ -29,16 +28,75 @@ submod! {
|
||||||
//api_sampler_sample
|
//api_sampler_sample
|
||||||
//api_sampler_voice
|
//api_sampler_voice
|
||||||
|
|
||||||
model_arranger
|
model_scene
|
||||||
model_arranger_scene
|
model_track
|
||||||
model_arranger_track
|
|
||||||
model_clock
|
model_clock
|
||||||
|
model_phrase
|
||||||
model_player
|
model_player
|
||||||
model_pool
|
model_pool
|
||||||
model_sequencer
|
model_sequencer
|
||||||
model_transport
|
model_transport
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait HasJack {
|
//impl Command<ArrangerModel> for ArrangerSceneCommand {
|
||||||
fn jack (&self) -> &Arc<RwLock<JackClient>>;
|
//}
|
||||||
}
|
//Edit(phrase) => { state.state.phrase = phrase.clone() },
|
||||||
|
//ToggleViewMode => { state.state.mode.to_next(); },
|
||||||
|
//Delete => { state.state.delete(); },
|
||||||
|
//Activate => { state.state.activate(); },
|
||||||
|
//ZoomIn => { state.state.zoom_in(); },
|
||||||
|
//ZoomOut => { state.state.zoom_out(); },
|
||||||
|
//MoveBack => { state.state.move_back(); },
|
||||||
|
//MoveForward => { state.state.move_forward(); },
|
||||||
|
//RandomColor => { state.state.randomize_color(); },
|
||||||
|
//Put => { state.state.phrase_put(); },
|
||||||
|
//Get => { state.state.phrase_get(); },
|
||||||
|
//AddScene => { state.state.scene_add(None, None)?; },
|
||||||
|
//AddTrack => { state.state.track_add(None, None)?; },
|
||||||
|
//ToggleLoop => { state.state.toggle_loop() },
|
||||||
|
//pub fn zoom_in (&mut self) {
|
||||||
|
//if let ArrangerEditorMode::Vertical(factor) = self.mode {
|
||||||
|
//self.mode = ArrangerEditorMode::Vertical(factor + 1)
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//pub fn zoom_out (&mut self) {
|
||||||
|
//if let ArrangerEditorMode::Vertical(factor) = self.mode {
|
||||||
|
//self.mode = ArrangerEditorMode::Vertical(factor.saturating_sub(1))
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//pub fn move_back (&mut self) {
|
||||||
|
//match self.selected {
|
||||||
|
//ArrangerEditorFocus::Scene(s) => {
|
||||||
|
//if s > 0 {
|
||||||
|
//self.scenes.swap(s, s - 1);
|
||||||
|
//self.selected = ArrangerEditorFocus::Scene(s - 1);
|
||||||
|
//}
|
||||||
|
//},
|
||||||
|
//ArrangerEditorFocus::Track(t) => {
|
||||||
|
//if t > 0 {
|
||||||
|
//self.tracks.swap(t, t - 1);
|
||||||
|
//self.selected = ArrangerEditorFocus::Track(t - 1);
|
||||||
|
//// FIXME: also swap clip order in scenes
|
||||||
|
//}
|
||||||
|
//},
|
||||||
|
//_ => todo!("arrangement: move forward")
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//pub fn move_forward (&mut self) {
|
||||||
|
//match self.selected {
|
||||||
|
//ArrangerEditorFocus::Scene(s) => {
|
||||||
|
//if s < self.scenes.len().saturating_sub(1) {
|
||||||
|
//self.scenes.swap(s, s + 1);
|
||||||
|
//self.selected = ArrangerEditorFocus::Scene(s + 1);
|
||||||
|
//}
|
||||||
|
//},
|
||||||
|
//ArrangerEditorFocus::Track(t) => {
|
||||||
|
//if t < self.tracks.len().saturating_sub(1) {
|
||||||
|
//self.tracks.swap(t, t + 1);
|
||||||
|
//self.selected = ArrangerEditorFocus::Track(t + 1);
|
||||||
|
//// FIXME: also swap clip order in scenes
|
||||||
|
//}
|
||||||
|
//},
|
||||||
|
//_ => todo!("arrangement: move forward")
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
|
||||||
|
|
@ -1,63 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -28,6 +28,13 @@ impl ClockApi for Clock {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlayheadApi for Clock {
|
impl PlayheadApi for Clock {
|
||||||
|
fn playing (&self) -> &RwLock<Option<TransportState>> {
|
||||||
|
&self.playing
|
||||||
|
}
|
||||||
|
/// Global sample and usec at which playback started
|
||||||
|
fn started (&self) -> &RwLock<Option<(usize, usize)>> {
|
||||||
|
&self.started
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Instant> for Clock {
|
impl From<Instant> for Clock {
|
||||||
|
|
|
||||||
84
crates/tek_api/src/model_phrase.rs
Normal file
84
crates/tek_api/src/model_phrase.rs
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
/// A MIDI sequence.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Phrase {
|
||||||
|
pub uuid: uuid::Uuid,
|
||||||
|
/// Name of phrase
|
||||||
|
pub name: String,
|
||||||
|
/// Temporal resolution in pulses per quarter note
|
||||||
|
pub ppq: usize,
|
||||||
|
/// Length of phrase in pulses
|
||||||
|
pub length: usize,
|
||||||
|
/// Notes in phrase
|
||||||
|
pub notes: PhraseData,
|
||||||
|
/// Whether to loop the phrase or play it once
|
||||||
|
pub loop_on: bool,
|
||||||
|
/// Start of loop
|
||||||
|
pub loop_start: usize,
|
||||||
|
/// Length of loop
|
||||||
|
pub loop_length: usize,
|
||||||
|
/// All notes are displayed with minimum length
|
||||||
|
pub percussive: bool,
|
||||||
|
/// Identifying color of phrase
|
||||||
|
pub color: ItemColorTriplet,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// MIDI message structural
|
||||||
|
pub type PhraseData = Vec<Vec<MidiMessage>>;
|
||||||
|
|
||||||
|
impl Phrase {
|
||||||
|
pub fn new (
|
||||||
|
name: impl AsRef<str>,
|
||||||
|
loop_on: bool,
|
||||||
|
length: usize,
|
||||||
|
notes: Option<PhraseData>,
|
||||||
|
color: Option<ItemColorTriplet>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
uuid: uuid::Uuid::new_v4(),
|
||||||
|
name: name.as_ref().to_string(),
|
||||||
|
ppq: PPQ,
|
||||||
|
length,
|
||||||
|
notes: notes.unwrap_or(vec![Vec::with_capacity(16);length]),
|
||||||
|
loop_on,
|
||||||
|
loop_start: 0,
|
||||||
|
loop_length: length,
|
||||||
|
percussive: true,
|
||||||
|
color: color.unwrap_or_else(ItemColorTriplet::random)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn duplicate (&self) -> Self {
|
||||||
|
let mut clone = self.clone();
|
||||||
|
clone.uuid = uuid::Uuid::new_v4();
|
||||||
|
clone
|
||||||
|
}
|
||||||
|
pub fn toggle_loop (&mut self) { self.loop_on = !self.loop_on; }
|
||||||
|
pub fn record_event (&mut self, pulse: usize, message: MidiMessage) {
|
||||||
|
if pulse >= self.length { panic!("extend phrase first") }
|
||||||
|
self.notes[pulse].push(message);
|
||||||
|
}
|
||||||
|
/// Check if a range `start..end` contains MIDI Note On `k`
|
||||||
|
pub fn contains_note_on (&self, k: u7, start: usize, end: usize) -> bool {
|
||||||
|
//panic!("{:?} {start} {end}", &self);
|
||||||
|
for events in self.notes[start.max(0)..end.min(self.notes.len())].iter() {
|
||||||
|
for event in events.iter() {
|
||||||
|
if let MidiMessage::NoteOn {key,..} = event { if *key == k { return true } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Default for Phrase {
|
||||||
|
fn default () -> Self {
|
||||||
|
Self::new("(empty)", false, 0, None, Some(ItemColor::from(Color::Rgb(0, 0, 0)).into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Phrase {
|
||||||
|
fn eq (&self, other: &Self) -> bool {
|
||||||
|
self.uuid == other.uuid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Phrase {}
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
|
|
@ -6,8 +6,11 @@ pub enum NextPrev {
|
||||||
Prev,
|
Prev,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait Execute<T> {
|
||||||
|
fn command (&mut self, command: T) -> Perhaps<T>;
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Command<S>: Send + Sync + Sized {
|
pub trait Command<S>: Send + Sync + Sized {
|
||||||
fn translate (self, _: &S) -> Self { self }
|
|
||||||
fn execute (self, state: &mut S) -> Perhaps<Self>;
|
fn execute (self, state: &mut S) -> Perhaps<Self>;
|
||||||
}
|
}
|
||||||
pub fn delegate <B, C: Command<S>, S> (
|
pub fn delegate <B, C: Command<S>, S> (
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,9 @@ pub type ArrangerAppCommand = AppViewCommand<ArrangerViewCommand>;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum ArrangerViewCommand {
|
pub enum ArrangerViewCommand {
|
||||||
|
Scene(ArrangerSceneCommand),
|
||||||
|
Track(ArrangerTrackCommand),
|
||||||
|
Clip(ArrangerClipCommand),
|
||||||
Edit(ArrangerCommand),
|
Edit(ArrangerCommand),
|
||||||
Select(ArrangerSelection),
|
Select(ArrangerSelection),
|
||||||
Zoom(usize),
|
Zoom(usize),
|
||||||
|
|
@ -217,6 +220,9 @@ impl Command<ArrangerApp<Tui>> for ArrangerViewCommand {
|
||||||
fn execute (self, state: &mut ArrangerApp<Tui>) -> Perhaps<Self> {
|
fn execute (self, state: &mut ArrangerApp<Tui>) -> Perhaps<Self> {
|
||||||
use ArrangerViewCommand::*;
|
use ArrangerViewCommand::*;
|
||||||
match self {
|
match self {
|
||||||
|
Scene(cmd) => { delegate(cmd, Scene, &mut state.app) },
|
||||||
|
Track(cmd) => { delegate(cmd, Track, &mut state.app) },
|
||||||
|
Clip(cmd) => { delegate(cmd, Clip, &mut state.app) },
|
||||||
Phrases(cmd) => { delegate(cmd, Phrases, &mut state.app) },
|
Phrases(cmd) => { delegate(cmd, Phrases, &mut state.app) },
|
||||||
Editor(cmd) => { delegate(cmd, Editor, &mut state.app) },
|
Editor(cmd) => { delegate(cmd, Editor, &mut state.app) },
|
||||||
Transport(cmd) => { delegate(cmd, Transport, &mut state.app) },
|
Transport(cmd) => { delegate(cmd, Transport, &mut state.app) },
|
||||||
|
|
@ -256,6 +262,45 @@ pub struct ArrangerView<E: Engine> {
|
||||||
size: Measure<E>,
|
size: Measure<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HasJack for ArrangerView<Tui> {
|
||||||
|
fn jack (&self) -> &Arc<RwLock<JackClient>> {
|
||||||
|
&self.transport.jack()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasClock for ArrangerView<Tui> {
|
||||||
|
fn clock (&self) -> &Arc<Clock> {
|
||||||
|
&self.transport.clock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasPhrases for ArrangerView<Tui> {
|
||||||
|
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>> {
|
||||||
|
&self.phrases
|
||||||
|
}
|
||||||
|
fn phrases_mut (&mut self) -> &mut Vec<Arc<RwLock<Phrase>>> {
|
||||||
|
&mut self.phrases
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasTracks<ArrangerTrack> for ArrangerView<Tui> {
|
||||||
|
fn tracks (&self) -> &Vec<ArrangerTrack> {
|
||||||
|
&self.tracks
|
||||||
|
}
|
||||||
|
fn tracks_mut (&mut self) -> &mut Vec<ArrangerTrack> {
|
||||||
|
&mut self.tracks
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasScenes<ArrangerScene> for ArrangerView<Tui> {
|
||||||
|
fn scenes (&self) -> &Vec<ArrangerScene> {
|
||||||
|
&self.scenes
|
||||||
|
}
|
||||||
|
fn scenes_mut (&mut self) -> &mut Vec<ArrangerScene> {
|
||||||
|
&mut self.scenes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Display mode of arranger
|
/// Display mode of arranger
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub enum ArrangerMode {
|
pub enum ArrangerMode {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue