wip: reenable standalone sequencer

This commit is contained in:
🪞👃🪞 2024-10-08 18:33:21 +03:00
parent 690a8e8f24
commit 0eb063db1c
7 changed files with 125 additions and 37 deletions

View file

@ -12,3 +12,5 @@ fpush:
git push -fu origin main
arranger:
cargo run --bin tek_arranger
sequencer:
cargo run --bin tek_sequencer

View file

@ -457,16 +457,7 @@ impl Scene {
}
/// Returns the pulse length of the longest phrase in the scene
pub fn pulses <E: Engine> (&self, tracks: &[ArrangementTrack<E>]) -> usize {
self.clips.iter().enumerate()
.filter_map(|(i, c)|c
.map(|c|tracks
.get(i)
.map(|track|track
.phrases
.get(c))))
.filter_map(|p|p)
.filter_map(|p|p)
.fold(0, |a, p|a.max(p.read().unwrap().length))
self.clips.iter().fold(0, |a, p|a.max(p.as_ref().map(|q|q.read().unwrap().length).unwrap_or(0)))
}
/// Returns true if all phrases in the scene are currently playing
pub fn is_playing <E: Engine> (&self, tracks: &[ArrangementTrack<E>]) -> bool {

View file

@ -36,12 +36,12 @@ impl ArrangerCli {
*arrangement.name.write().unwrap() = name.clone();
}
for _ in 0..self.tracks {
let track = arrangement.track_add(None)?;
for _ in 0..self.scenes {
track.phrases.push(
Arc::new(RwLock::new(Phrase::new("", true, PPQ * 4, None)))
);
}
//let track = arrangement.track_add(None)?;
//for _ in 0..self.scenes {
//track.phrases.push(
//Arc::new(RwLock::new(Phrase::new("", true, PPQ * 4, None)))
//);
//}
}
for _ in 0..self.scenes {
let _scene = arrangement.scene_add(None)?;

View file

@ -25,6 +25,21 @@ impl Content for Arranger<Tui> {
})
}
}
/// Focusable items in standalone arranger.
impl Focus<3, Tui> for Arranger<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.arrangement, self.editor)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.arrangement, self.editor)
}
}
/// Handle top-level events in standalone arranger.
impl Handle<Tui> for Arranger<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
@ -73,21 +88,6 @@ impl Handle<Tui> for Arranger<Tui> {
Ok(Some(true))
}
}
/// Focusable items in standalone arranger.
impl Focus<3, Tui> for Arranger<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.arrangement, self.editor)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.arrangement, self.editor)
}
}
impl Arranger<Tui> {
pub fn rename_selected (&mut self) {
let Arrangement { selected, ref name, ref tracks, ref scenes, .. } = self.arrangement;

View file

@ -13,12 +13,16 @@ pub struct Sequencer<E: Engine> {
pub phrases: Arc<RwLock<PhrasePool<E>>>,
/// Phrase editor view
pub editor: PhraseEditor<E>,
/// Which view is focused
pub focus: usize
}
/// Contains all phrases in a project
pub struct PhrasePool<E: Engine> {
_engine: PhantomData<E>,
/// Phrases in the pool
pub phrases: Vec<Arc<RwLock<Option<Phrase>>>>,
/// Whether this widget is focused
pub focused: bool,
}
/// A MIDI sequence.
#[derive(Debug)]
@ -93,7 +97,8 @@ impl<E: Engine> PhrasePool<E> {
pub fn new () -> Self {
Self {
_engine: Default::default(),
phrases: vec![Arc::new(RwLock::new(Some(Phrase::default())))]
phrases: vec![Arc::new(RwLock::new(Some(Phrase::default())))],
focused: false
}
}
}

View file

@ -19,8 +19,12 @@ pub struct SequencerCli {
impl SequencerCli {
fn run (&self) -> Usually<()> {
let seq = Sequencer {
transport: self.transport.unwrap_or(false)
.then_some(Arc::new(RwLock::new(TransportToolbar::new(None)))),
focus: 0,
editor: PhraseEditor::new(),
phrases: Arc::new(RwLock::new(PhrasePool::new())),
transport: self.transport.unwrap_or(false).then_some(
Arc::new(RwLock::new(TransportToolbar::new(None)))
),
};
if let Some(name) = self.name.as_ref() {
// TODO

View file

@ -1,4 +1,72 @@
use crate::*;
impl Content for Sequencer<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
Stack::down(move|add|{
add(&self.transport)?;
add(&self.phrases.clone()
.split(Direction::Right, 20, &self.editor as &dyn Widget<Engine = Tui>)
.min_y(20)
.fill_y())
})
}
}
/// Focusable items in standalone arranger.
impl Focus<3, Tui> for Sequencer<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.phrases, self.editor)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.phrases, self.editor)
}
}
/// Handle top-level events in standalone arranger.
impl Handle<Tui> for Sequencer<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
let focus = self.focus;
let is_first_row = false;
let is_last_row = false;
match from.event() {
key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport {
transport.write().unwrap().toggle_play()?;
} else {
return Ok(None)
}
},
key!(KeyCode::Tab) => {
self.focus_next();
},
key!(KeyCode::BackTab) => {
self.focus_prev();
},
key!(KeyCode::Down) => {
if focus == 0 {
self.focus_next();
} else if focus == 1 && is_last_row {
self.focus_next();
} else {
return self.focused_mut().handle(from)
}
},
key!(KeyCode::Up) => {
if focus == 1 && is_first_row {
self.focus_prev();
} else {
return self.focused_mut().handle(from)
}
},
_ => return self.focused_mut().handle(from)
}
Ok(Some(true))
}
}
// TODO: Display phrases always in order of appearance
impl Content for PhrasePool<Tui> {
type Engine = Tui;
@ -14,6 +82,24 @@ impl Content for PhrasePool<Tui> {
.fg(Color::Rgb(70, 80, 50))))
}
}
impl Focusable<Tui> for PhrasePool<Tui> {
fn is_focused (&self) -> bool {
self.focused
}
fn set_focused (&mut self, focused: bool) {
self.focused = focused
}
}
impl Handle<Tui> for PhrasePool<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Up) => todo!(),
key!(KeyCode::Down) => todo!(),
_ => return Ok(None),
}
return Ok(Some(true))
}
}
impl Content for PhraseEditor<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
@ -21,7 +107,7 @@ impl Content for PhraseEditor<Tui> {
let toolbar = Stack::down(move|add|{
//let name = format!("{:>9}", self.name.read().unwrap().as_str());
//add(&col!("Track:", TuiStyle::bg(name.as_str(), field_bg)))?;
if let Some(phrase) = self.phrase {
if let Some(phrase) = &self.phrase {
let phrase = phrase.read().unwrap();
let length = format!("{}q{}p", phrase.length / PPQ, phrase.length % PPQ);
let length = format!("{:>9}", &length);
@ -49,10 +135,10 @@ impl Content for PhraseEditor<Tui> {
}).fill_y(),
// playhead
CustomWidget::new(|_|Ok(Some([32,2])), |to: &mut TuiOutput|{
if let Some(phrase) = self.phrase {
if let Some(phrase) = &self.phrase {
let time_0 = self.time_axis.start;
let time_z = self.time_axis.scale;
let now = self.now % phrase.read().unwrap().length;
let now = 0; // TODO FIXME: self.now % phrase.read().unwrap().length;
let [x, y, width, _] = to.area();
let x2 = x as usize + Self::H_KEYS_OFFSET;
let x3 = x as usize + width as usize;