wip: remodularize 2

This commit is contained in:
🪞👃🪞 2025-01-08 19:19:35 +01:00
parent 3b6ff81dad
commit d38dc14e84
27 changed files with 564 additions and 563 deletions

View file

@ -1,6 +1,5 @@
use crate::*;
mod arranger_audio; pub(crate) use self::arranger_audio::*;
mod arranger_command; pub(crate) use self::arranger_command::*;
mod arranger_scene; pub(crate) use self::arranger_scene::*;
mod arranger_select; pub(crate) use self::arranger_select::*;
@ -27,6 +26,42 @@ pub struct Arranger {
pub perf: PerfModel,
pub compact: bool,
}
audio!(|self: Arranger, client, scope|{
// Start profiling cycle
let t0 = self.perf.get_t0();
// Update transport clock
//if Control::Quit == ClockAudio(self).process(client, scope) {
//return Control::Quit
//}
//// Update MIDI sequencers
//let tracks = &mut self.tracks;
//let note_buf = &mut self.note_buf;
//let midi_buf = &mut self.midi_buf;
//if Control::Quit == TracksAudio(tracks, note_buf, midi_buf).process(client, scope) {
//return Control::Quit
//}
// FIXME: one of these per playing track
//self.now.set(0.);
//if let ArrangerSelection::Clip(t, s) = self.selected {
//let phrase = self.scenes.get(s).map(|scene|scene.clips.get(t));
//if let Some(Some(Some(phrase))) = phrase {
//if let Some(track) = self.tracks().get(t) {
//if let Some((ref started_at, Some(ref playing))) = track.player.play_phrase {
//let phrase = phrase.read().unwrap();
//if *playing.read().unwrap() == *phrase {
//let pulse = self.current().pulse.get();
//let start = started_at.pulse.get();
//let now = (pulse - start) % phrase.length as f64;
//self.now.set(now);
//}
//}
//}
//}
//}
// End profiling cycle
self.perf.update(t0, scope);
return Control::Continue
});
has_clock!(|self: Arranger|&self.clock);
has_phrases!(|self: Arranger|self.pool.phrases);
has_editor!(|self: Arranger|self.editor);

View file

@ -1,38 +0,0 @@
use crate::*;
audio!(|self: Arranger, client, scope|{
// Start profiling cycle
let t0 = self.perf.get_t0();
// Update transport clock
//if Control::Quit == ClockAudio(self).process(client, scope) {
//return Control::Quit
//}
//// Update MIDI sequencers
//let tracks = &mut self.tracks;
//let note_buf = &mut self.note_buf;
//let midi_buf = &mut self.midi_buf;
//if Control::Quit == TracksAudio(tracks, note_buf, midi_buf).process(client, scope) {
//return Control::Quit
//}
// FIXME: one of these per playing track
//self.now.set(0.);
//if let ArrangerSelection::Clip(t, s) = self.selected {
//let phrase = self.scenes.get(s).map(|scene|scene.clips.get(t));
//if let Some(Some(Some(phrase))) = phrase {
//if let Some(track) = self.tracks().get(t) {
//if let Some((ref started_at, Some(ref playing))) = track.player.play_phrase {
//let phrase = phrase.read().unwrap();
//if *playing.read().unwrap() == *phrase {
//let pulse = self.current().pulse.get();
//let start = started_at.pulse.get();
//let now = (pulse - start) % phrase.length as f64;
//self.now.set(now);
//}
//}
//}
//}
//}
// End profiling cycle
self.perf.update(t0, scope);
return Control::Continue
});

View file

@ -1,4 +1,3 @@
mod groovebox_audio; pub use self::groovebox_audio::*;
mod groovebox_command; pub use self::groovebox_command::*;
mod groovebox_tui; pub use self::groovebox_tui::*;
@ -25,6 +24,39 @@ pub struct Groovebox {
pub perf: PerfModel,
}
audio!(|self: Groovebox, client, scope|{
let t0 = self.perf.get_t0();
if Control::Quit == ClockAudio(&mut self.player).process(client, scope) {
return Control::Quit
}
if Control::Quit == PlayerAudio(
&mut self.player, &mut self.note_buf, &mut self.midi_buf
).process(client, scope) {
return Control::Quit
}
if Control::Quit == SamplerAudio(&mut self.sampler).process(client, scope) {
return Control::Quit
}
// TODO move these to editor and sampler:
for RawMidi { time, bytes } in self.player.midi_ins[0].iter(scope) {
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
match message {
MidiMessage::NoteOn { ref key, .. } => {
self.editor.set_note_point(key.as_int() as usize);
},
MidiMessage::Controller { controller, value } => {
if let Some(sample) = &self.sampler.mapped[self.editor.note_point()] {
sample.write().unwrap().handle_cc(controller, value)
}
}
_ => {}
}
}
}
self.perf.update(t0, scope);
Control::Continue
});
has_clock!(|self: Groovebox|self.player.clock());
impl EdnViewData<TuiOut> for &Groovebox {

View file

@ -1,35 +0,0 @@
use crate::*;
use super::*;
audio!(|self: Groovebox, client, scope|{
let t0 = self.perf.get_t0();
if Control::Quit == ClockAudio(&mut self.player).process(client, scope) {
return Control::Quit
}
if Control::Quit == PlayerAudio(
&mut self.player, &mut self.note_buf, &mut self.midi_buf
).process(client, scope) {
return Control::Quit
}
if Control::Quit == SamplerAudio(&mut self.sampler).process(client, scope) {
return Control::Quit
}
// TODO move these to editor and sampler:
for RawMidi { time, bytes } in self.player.midi_ins[0].iter(scope) {
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
match message {
MidiMessage::NoteOn { ref key, .. } => {
self.editor.set_note_point(key.as_int() as usize);
},
MidiMessage::Controller { controller, value } => {
if let Some(sample) = &self.sampler.mapped[self.editor.note_point()] {
sample.write().unwrap().handle_cc(controller, value)
}
}
_ => {}
}
}
}
self.perf.update(t0, scope);
Control::Continue
});

View file

@ -5,6 +5,20 @@
#![feature(impl_trait_in_assoc_type)]
#![feature(associated_type_defaults)]
pub mod arranger; pub use self::arranger::*;
pub mod file; pub use self::file::*;
pub mod focus; pub use self::focus::*;
pub mod groovebox; pub use self::groovebox::*;
pub mod meter; pub use self::meter::*;
pub mod mixer; pub use self::mixer::*;
pub mod plugin; pub use self::plugin::*;
pub mod pool; pub use self::pool::*;
pub mod sampler; pub use self::sampler::*;
pub mod sequencer; pub use self::sequencer::*;
pub use ::tek_time; pub(crate) use ::tek_time::*;
pub use ::tek_jack; pub(crate) use ::tek_jack::{*, jack::{*, contrib::*}};
pub use ::tek_midi; pub(crate) use ::tek_midi::{*, midly::{*, num::*, live::*}};
pub use ::tek_tui::{self, tek_edn, tek_input, tek_output};
pub(crate) use ::tek_tui::{
*,
@ -24,8 +38,6 @@ pub(crate) use ::tek_tui::{
buffer::Cell,
}
};
pub use ::tek_jack;
pub(crate) use ::tek_jack::{*, jack::{*, contrib::*}};
pub(crate) use std::cmp::{Ord, Eq, PartialEq};
pub(crate) use std::collections::BTreeMap;
@ -41,55 +53,235 @@ pub(crate) use std::sync::{Arc, Mutex, RwLock};
pub(crate) use std::thread::{spawn, JoinHandle};
pub(crate) use std::time::Duration;
pub mod arranger; pub use self::arranger::*;
pub mod file; pub use self::file::*;
pub mod focus; pub use self::focus::*;
pub mod groovebox; pub use self::groovebox::*;
pub mod meter; pub use self::meter::*;
pub mod mixer; pub use self::mixer::*;
pub mod piano; pub use self::piano::*;
pub mod plugin; pub use self::plugin::*;
pub mod pool; pub use self::pool::*;
pub mod sampler; pub use self::sampler::*;
pub mod sequencer; pub use self::sequencer::*;
//#[cfg(test)] mod test_focus {
//use super::focus::*;
//#[test] fn test_focus () {
pub use ::midly::{self, num::u7};
pub(crate) use ::midly::{
Smf,
MidiMessage,
TrackEventKind,
live::LiveEvent,
};
//struct FocusTest {
//focused: char,
//cursor: (usize, usize)
//}
testmod! { test }
//impl HasFocus for FocusTest {
//type Item = char;
//fn focused (&self) -> Self::Item {
//self.focused
//}
//fn set_focused (&mut self, to: Self::Item) {
//self.focused = to
//}
//}
/// Define test modules.
#[macro_export] macro_rules! testmod {
($($name:ident)*) => { $(#[cfg(test)] mod $name;)* };
}
//impl FocusGrid for FocusTest {
//fn focus_cursor (&self) -> (usize, usize) {
//self.cursor
//}
//fn focus_cursor_mut (&mut self) -> &mut (usize, usize) {
//&mut self.cursor
//}
//fn focus_layout (&self) -> &[&[Self::Item]] {
//&[
//&['a', 'a', 'a', 'b', 'b', 'd'],
//&['a', 'a', 'a', 'b', 'b', 'd'],
//&['a', 'a', 'a', 'c', 'c', 'd'],
//&['a', 'a', 'a', 'c', 'c', 'd'],
//&['e', 'e', 'e', 'e', 'e', 'e'],
//]
//}
//}
#[derive(Default)]
pub struct BigBuffer {
pub width: usize,
pub height: usize,
pub content: Vec<Cell>
}
//let mut tester = FocusTest { focused: 'a', cursor: (0, 0) };
impl BigBuffer {
pub fn new (width: usize, height: usize) -> Self {
Self { width, height, content: vec![Cell::default(); width*height] }
}
pub fn get (&self, x: usize, y: usize) -> Option<&Cell> {
let i = self.index_of(x, y);
self.content.get(i)
}
pub fn get_mut (&mut self, x: usize, y: usize) -> Option<&mut Cell> {
let i = self.index_of(x, y);
self.content.get_mut(i)
}
pub fn index_of (&self, x: usize, y: usize) -> usize {
y * self.width + x
}
}
//tester.focus_right();
//assert_eq!(tester.cursor.0, 3);
//assert_eq!(tester.focused, 'b');
from!(|size:(usize, usize)| BigBuffer = Self::new(size.0, size.1));
//tester.focus_down();
//assert_eq!(tester.cursor.1, 2);
//assert_eq!(tester.focused, 'c');
//}
//}
//use crate::*;
//struct TestEngine([u16;4], Vec<Vec<char>>);
//impl Engine for TestEngine {
//type Unit = u16;
//type Size = [Self::Unit;2];
//type Area = [Self::Unit;4];
//type Input = Self;
//type Handled = bool;
//fn exited (&self) -> bool {
//true
//}
//}
//#[derive(Copy, Clone)]
//struct TestArea(u16, u16);
//impl Render<TestEngine> for TestArea {
//fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
//Ok(Some([to[0], to[1], self.0, self.1]))
//}
//fn render (&self, to: &mut TestEngine) -> Perhaps<[u16;4]> {
//if let Some(layout) = self.layout(to.area())? {
//for y in layout.y()..layout.y()+layout.h()-1 {
//for x in layout.x()..layout.x()+layout.w()-1 {
//to.1[y as usize][x as usize] = '*';
//}
//}
//Ok(Some(layout))
//} else {
//Ok(None)
//}
//}
//}
//#[test]
//fn test_plus_minus () -> Usually<()> {
//let area = [0, 0, 10, 10];
//let engine = TestEngine(area, vec![vec![' ';10];10]);
//let test = TestArea(4, 4);
//assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
//assert_eq!(Push::X(1, test).layout(area)?, Some([1, 0, 4, 4]));
//Ok(())
//}
//#[test]
//fn test_outset_align () -> Usually<()> {
//let area = [0, 0, 10, 10];
//let engine = TestEngine(area, vec![vec![' ';10];10]);
//let test = TestArea(4, 4);
//assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
//assert_eq!(Margin::X(1, test).layout(area)?, Some([0, 0, 6, 4]));
//assert_eq!(Align::X(test).layout(area)?, Some([3, 0, 4, 4]));
//assert_eq!(Align::X(Margin::X(1, test)).layout(area)?, Some([2, 0, 6, 4]));
//assert_eq!(Margin::X(1, Align::X(test)).layout(area)?, Some([2, 0, 6, 4]));
//Ok(())
//}
////#[test]
////fn test_misc () -> Usually<()> {
////let area: [u16;4] = [0, 0, 10, 10];
////let test = TestArea(4, 4);
////assert_eq!(test.layout(area)?,
////Some([0, 0, 4, 4]));
////assert_eq!(Align::Center(test).layout(area)?,
////Some([3, 3, 4, 4]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&test)?;
////add(&test)
////})).layout(area)?,
////Some([3, 1, 4, 8]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&test)
////})).layout(area)?,
////Some([2, 0, 6, 10]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&Padding::XY(2, 2, test))
////})).layout(area)?,
////Some([2, 1, 6, 8]));
////assert_eq!(Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&Padding::XY(2, 2, test))
////}).layout(area)?,
////Some([0, 0, 6, 8]));
////assert_eq!(Stack::right(|add|{
////add(&Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&Padding::XY(2, 2, test))
////}))?;
////add(&Align::Center(TestArea(2 ,2)))
////}).layout(area)?,
////Some([0, 0, 8, 8]));
////Ok(())
////}
////#[test]
////fn test_offset () -> Usually<()> {
////let area: [u16;4] = [50, 50, 100, 100];
////let test = TestArea(3, 3);
////assert_eq!(Push::X(1, test).layout(area)?, Some([51, 50, 3, 3]));
////assert_eq!(Push::Y(1, test).layout(area)?, Some([50, 51, 3, 3]));
////assert_eq!(Push::XY(1, 1, test).layout(area)?, Some([51, 51, 3, 3]));
////Ok(())
////}
////#[test]
////fn test_outset () -> Usually<()> {
////let area: [u16;4] = [50, 50, 100, 100];
////let test = TestArea(3, 3);
////assert_eq!(Margin::X(1, test).layout(area)?, Some([49, 50, 5, 3]));
////assert_eq!(Margin::Y(1, test).layout(area)?, Some([50, 49, 3, 5]));
////assert_eq!(Margin::XY(1, 1, test).layout(area)?, Some([49, 49, 5, 5]));
////Ok(())
////}
////#[test]
////fn test_padding () -> Usually<()> {
////let area: [u16;4] = [50, 50, 100, 100];
////let test = TestArea(3, 3);
////assert_eq!(Padding::X(1, test).layout(area)?, Some([51, 50, 1, 3]));
////assert_eq!(Padding::Y(1, test).layout(area)?, Some([50, 51, 3, 1]));
////assert_eq!(Padding::XY(1, 1, test).layout(area)?, Some([51, 51, 1, 1]));
////Ok(())
////}
////#[test]
////fn test_stuff () -> Usually<()> {
////let area: [u16;4] = [0, 0, 100, 100];
////assert_eq!("1".layout(area)?,
////Some([0, 0, 1, 1]));
////assert_eq!("333".layout(area)?,
////Some([0, 0, 3, 1]));
////assert_eq!(Layers::new(|add|{add(&"1")?;add(&"333")}).layout(area)?,
////Some([0, 0, 3, 1]));
////assert_eq!(Stack::down(|add|{add(&"1")?;add(&"333")}).layout(area)?,
////Some([0, 0, 3, 2]));
////assert_eq!(Stack::right(|add|{add(&"1")?;add(&"333")}).layout(area)?,
////Some([0, 0, 4, 1]));
////assert_eq!(Stack::down(|add|{
////add(&Stack::right(|add|{add(&"1")?;add(&"333")}))?;
////add(&"55555")
////}).layout(area)?,
////Some([0, 0, 5, 2]));
////let area: [u16;4] = [1, 1, 100, 100];
////assert_eq!(Margin::X(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
////Some([0, 1, 6, 1]));
////assert_eq!(Margin::Y(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
////Some([1, 0, 4, 3]));
////assert_eq!(Margin::XY(1, 1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
////Some([0, 0, 6, 3]));
////assert_eq!(Stack::down(|add|{
////add(&Margin::XY(1, 1, "1"))?;
////add(&Margin::XY(1, 1, "333"))
////}).layout(area)?,
////Some([1, 1, 5, 6]));
////let area: [u16;4] = [1, 1, 95, 100];
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Margin::XY(1, 1, "1"))?;
////add(&Margin::XY(1, 1, "333"))
////})).layout(area)?,
////Some([46, 48, 5, 6]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Layers::new(|add|{
//////add(&Margin::XY(1, 1, Background(Color::Rgb(0,128,0))))?;
////add(&Margin::XY(1, 1, "1"))?;
////add(&Margin::XY(1, 1, "333"))?;
//////add(&Background(Color::Rgb(0,128,0)))?;
////Ok(())
////}))?;
////add(&Layers::new(|add|{
//////add(&Margin::XY(1, 1, Background(Color::Rgb(0,0,128))))?;
////add(&Margin::XY(1, 1, "555"))?;
////add(&Margin::XY(1, 1, "777777"))?;
//////add(&Background(Color::Rgb(0,0,128)))?;
////Ok(())
////}))
////})).layout(area)?,
////Some([46, 48, 5, 6]));
////Ok(())
////}

View file

@ -1,47 +0,0 @@
use crate::*;
use super::*;
mod piano_h; pub(crate) use self::piano_h::*;
mod piano_h_cursor; pub(crate) use self::piano_h_cursor::*;
mod piano_h_keys; pub(crate) use self::piano_h_keys::*;
mod piano_h_notes; pub(crate) use self::piano_h_notes::*;
mod piano_h_time; pub(crate) use self::piano_h_time::*;
/// A phrase, rendered as a horizontal piano roll.
pub struct PianoHorizontal {
phrase: Option<Arc<RwLock<MidiClip>>>,
/// Buffer where the whole phrase is rerendered on change
buffer: Arc<RwLock<BigBuffer>>,
/// Size of actual notes area
size: Measure<TuiOut>,
/// The display window
range: MidiRangeModel,
/// The note cursor
point: MidiPointModel,
/// The highlight color palette
color: ItemPalette,
/// Width of the keyboard
keys_width: u16,
}
impl PianoHorizontal {
pub fn new (phrase: Option<&Arc<RwLock<MidiClip>>>) -> Self {
let size = Measure::new();
let mut range = MidiRangeModel::from((24, true));
range.time_axis = size.x.clone();
range.note_axis = size.y.clone();
let mut piano = Self {
keys_width: 5,
size,
range,
buffer: RwLock::new(Default::default()).into(),
point: MidiPointModel::default(),
phrase: phrase.cloned(),
color: phrase.as_ref()
.map(|p|p.read().unwrap().color)
.unwrap_or(ItemPalette::from(ItemColor::from(TuiTheme::g(64)))),
};
piano.redraw();
piano
}
}

View file

@ -1,161 +0,0 @@
use crate::*;
use super::*;
pub(crate) fn note_y_iter (note_lo: usize, note_hi: usize, y0: u16) -> impl Iterator<Item=(usize, u16, usize)> {
(note_lo..=note_hi).rev().enumerate().map(move|(y, n)|(y, y0 + y as u16, n))
}
render!(TuiOut: (self: PianoHorizontal) => Bsp::s(
Fixed::y(1, Bsp::e(
Fixed::x(self.keys_width, ""),
Fill::x(PianoHorizontalTimeline(self)),
)),
Fill::xy(Bsp::e(
Fixed::x(self.keys_width, PianoHorizontalKeys(self)),
Fill::xy(self.size.of(lay!(
Fill::xy(PianoHorizontalNotes(self)),
Fill::xy(PianoHorizontalCursor(self)),
))),
)),
));
impl PianoHorizontal {
/// Draw the piano roll foreground using full blocks on note on and half blocks on legato: █▄ █▄ █▄
fn draw_bg (buf: &mut BigBuffer, phrase: &MidiClip, zoom: usize, note_len: usize) {
for (y, note) in (0..=127).rev().enumerate() {
for (x, time) in (0..buf.width).map(|x|(x, x*zoom)) {
let cell = buf.get_mut(x, y).unwrap();
cell.set_bg(phrase.color.darkest.rgb);
cell.set_fg(phrase.color.darker.rgb);
cell.set_char(if time % 384 == 0 {
'│'
} else if time % 96 == 0 {
'╎'
} else if time % note_len == 0 {
'┊'
} else if (127 - note) % 12 == 0 {
'='
} else if (127 - note) % 6 == 0 {
'—'
} else {
'·'
});
}
}
}
/// Draw the piano roll background using full blocks on note on and half blocks on legato: █▄ █▄ █▄
fn draw_fg (buf: &mut BigBuffer, phrase: &MidiClip, zoom: usize) {
let style = Style::default().fg(phrase.color.base.rgb);//.bg(Color::Rgb(0, 0, 0));
let mut notes_on = [false;128];
for (x, time_start) in (0..phrase.length).step_by(zoom).enumerate() {
for (y, note) in (0..=127).rev().enumerate() {
if let Some(cell) = buf.get_mut(x, note) {
if notes_on[note] {
cell.set_char('▂');
cell.set_style(style);
}
}
}
let time_end = time_start + zoom;
for time in time_start..time_end.min(phrase.length) {
for event in phrase.notes[time].iter() {
match event {
MidiMessage::NoteOn { key, .. } => {
let note = key.as_int() as usize;
let cell = buf.get_mut(x, note).unwrap();
cell.set_char('█');
cell.set_style(style);
notes_on[note] = true
},
MidiMessage::NoteOff { key, .. } => {
notes_on[key.as_int() as usize] = false
},
_ => {}
}
}
}
}
}
}
has_size!(<TuiOut>|self:PianoHorizontal|&self.size);
impl TimeRange for PianoHorizontal {
fn time_len (&self) -> &AtomicUsize { self.range.time_len() }
fn time_zoom (&self) -> &AtomicUsize { self.range.time_zoom() }
fn time_lock (&self) -> &AtomicBool { self.range.time_lock() }
fn time_start (&self) -> &AtomicUsize { self.range.time_start() }
fn time_axis (&self) -> &AtomicUsize { self.range.time_axis() }
}
impl NoteRange for PianoHorizontal {
fn note_lo (&self) -> &AtomicUsize { self.range.note_lo() }
fn note_axis (&self) -> &AtomicUsize { self.range.note_axis() }
}
impl NotePoint for PianoHorizontal {
fn note_len (&self) -> usize { self.point.note_len() }
fn set_note_len (&self, x: usize) { self.point.set_note_len(x) }
fn note_point (&self) -> usize { self.point.note_point() }
fn set_note_point (&self, x: usize) { self.point.set_note_point(x) }
}
impl TimePoint for PianoHorizontal {
fn time_point (&self) -> usize { self.point.time_point() }
fn set_time_point (&self, x: usize) { self.point.set_time_point(x) }
}
impl MidiViewer for PianoHorizontal {
fn phrase (&self) -> &Option<Arc<RwLock<MidiClip>>> {
&self.phrase
}
fn phrase_mut (&mut self) -> &mut Option<Arc<RwLock<MidiClip>>> {
&mut self.phrase
}
/// Determine the required space to render the phrase.
fn buffer_size (&self, phrase: &MidiClip) -> (usize, usize) {
(phrase.length / self.range.time_zoom().get(), 128)
}
fn redraw (&self) {
let buffer = if let Some(phrase) = self.phrase.as_ref() {
let phrase = phrase.read().unwrap();
let buf_size = self.buffer_size(&phrase);
let mut buffer = BigBuffer::from(buf_size);
let note_len = self.note_len();
let time_zoom = self.time_zoom().get();
self.time_len().set(phrase.length);
PianoHorizontal::draw_bg(&mut buffer, &phrase, time_zoom, note_len);
PianoHorizontal::draw_fg(&mut buffer, &phrase, time_zoom);
buffer
} else {
Default::default()
};
*self.buffer.write().unwrap() = buffer
}
fn set_phrase (&mut self, phrase: Option<&Arc<RwLock<MidiClip>>>) {
*self.phrase_mut() = phrase.cloned();
self.color = phrase.map(|p|p.read().unwrap().color)
.unwrap_or(ItemPalette::from(ItemColor::from(TuiTheme::g(64))));
self.redraw();
}
}
impl std::fmt::Debug for PianoHorizontal {
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let buffer = self.buffer.read().unwrap();
f.debug_struct("PianoHorizontal")
.field("time_zoom", &self.range.time_zoom)
.field("buffer", &format!("{}x{}", buffer.width, buffer.height))
.finish()
}
}
// Update sequencer playhead indicator
//self.now().set(0.);
//if let Some((ref started_at, Some(ref playing))) = self.player.play_phrase {
//let phrase = phrase.read().unwrap();
//if *playing.read().unwrap() == *phrase {
//let pulse = self.current().pulse.get();
//let start = started_at.pulse.get();
//let now = (pulse - start) % phrase.length as f64;
//self.now().set(now);
//}
//}

View file

@ -1,33 +0,0 @@
use crate::*;
use super::*;
pub struct PianoHorizontalCursor<'a>(pub(crate) &'a PianoHorizontal);
render!(TuiOut: |self: PianoHorizontalCursor<'a>, render|{
let style = Some(Style::default().fg(self.0.color.lightest.rgb));
let note_hi = self.0.note_hi();
let note_len = self.0.note_len();
let note_lo = self.0.note_lo().get();
let note_point = self.0.note_point();
let time_point = self.0.time_point();
let time_start = self.0.time_start().get();
let time_zoom = self.0.time_zoom().get();
let [x0, y0, w, _] = render.area().xywh();
for (area_y, screen_y, note) in note_y_iter(note_lo, note_hi, y0) {
if note == note_point {
for x in 0..w {
let screen_x = x0 + x;
let time_1 = time_start + x as usize * time_zoom;
let time_2 = time_1 + time_zoom;
if time_1 <= time_point && time_point < time_2 {
render.blit(&"", screen_x, screen_y, style);
let tail = note_len as u16 / time_zoom as u16;
for x_tail in (screen_x + 1)..(screen_x + tail) {
render.blit(&"", x_tail, screen_y, style);
}
break
}
}
break
}
}
});

View file

@ -1,59 +0,0 @@
use crate::*;
use super::*;
pub struct PianoHorizontalKeys<'a>(pub(crate) &'a PianoHorizontal);
render!(TuiOut: |self: PianoHorizontalKeys<'a>, to|{
let state = self.0;
let color = state.color;
let note_lo = state.note_lo().get();
let note_hi = state.note_hi();
let note_point = state.note_point();
let [x, y0, w, h] = to.area().xywh();
let key_style = Some(Style::default().fg(Color::Rgb(192, 192, 192)).bg(Color::Rgb(0, 0, 0)));
let off_style = Some(Style::default().fg(TuiTheme::g(160)));
let on_style = Some(Style::default().fg(TuiTheme::g(255)).bg(color.base.rgb).bold());
for (area_y, screen_y, note) in note_y_iter(note_lo, note_hi, y0) {
to.blit(&to_key(note), x, screen_y, key_style);
if note > 127 {
continue
}
if note == note_point {
to.blit(&format!("{:<5}", Note::pitch_to_name(note)), x, screen_y, on_style)
} else {
to.blit(&Note::pitch_to_name(note), x, screen_y, off_style)
};
}
});
has_color!(|self: PianoHorizontalKeys<'a>|self.0.color.base);
impl<'a> NoteRange for PianoHorizontalKeys<'a> {
fn note_lo (&self) -> &AtomicUsize { &self.0.note_lo() }
fn note_axis (&self) -> &AtomicUsize { &self.0.note_axis() }
}
impl<'a> NotePoint for PianoHorizontalKeys<'a> {
fn note_len (&self) -> usize { self.0.note_len() }
fn set_note_len (&self, x: usize) { self.0.set_note_len(x) }
fn note_point (&self) -> usize { self.0.note_point() }
fn set_note_point (&self, x: usize) { self.0.set_note_point(x) }
}
fn to_key (note: usize) -> &'static str {
match note % 12 {
11 => "████▌",
10 => " ",
9 => "████▌",
8 => " ",
7 => "████▌",
6 => " ",
5 => "████▌",
4 => "████▌",
3 => " ",
2 => "████▌",
1 => " ",
0 => "████▌",
_ => unreachable!(),
}
}

View file

@ -1,35 +0,0 @@
use crate::*;
use super::*;
pub struct PianoHorizontalNotes<'a>(pub(crate) &'a PianoHorizontal);
render!(TuiOut: |self: PianoHorizontalNotes<'a>, render|{
let time_start = self.0.time_start().get();
let note_axis = self.0.note_axis().get();
let note_lo = self.0.note_lo().get();
let note_hi = self.0.note_hi();
let note_point = self.0.note_point();
let source = self.0.buffer.read().unwrap();
let [x0, y0, w, h] = render.area().xywh();
if h as usize != note_axis {
panic!("area height mismatch: {h} <> {note_axis}");
}
for (area_x, screen_x) in (x0..x0+w).enumerate() {
for (area_y, screen_y, note) in note_y_iter(note_lo, note_hi, y0) {
let source_x = time_start + area_x;
let source_y = note_hi - area_y;
// TODO: enable loop rollover:
//let source_x = (time_start + area_x) % source.width.max(1);
//let source_y = (note_hi - area_y) % source.height.max(1);
let is_in_x = source_x < source.width;
let is_in_y = source_y < source.height;
if is_in_x && is_in_y {
if let Some(source_cell) = source.get(source_x, source_y) {
if let Some(cell) = render.buffer.cell_mut(ratatui::prelude::Position::from((screen_x, screen_y))) {
*cell = source_cell.clone();
}
}
}
}
}
});

View file

@ -1,15 +0,0 @@
use crate::*;
use super::*;
pub struct PianoHorizontalTimeline<'a>(pub(crate) &'a PianoHorizontal);
render!(TuiOut: |self: PianoHorizontalTimeline<'a>, render|{
let [x, y, w, h] = render.area();
let style = Some(Style::default().dim());
let length = self.0.phrase.as_ref().map(|p|p.read().unwrap().length).unwrap_or(1);
for (area_x, screen_x) in (0..w).map(|d|(d, d+x)) {
let t = area_x as usize * self.0.time_zoom().get();
if t < length {
render.blit(&"|", screen_x, y, style);
}
}
});

View file

@ -1,73 +1 @@
use crate::*;
pub struct ClipSelected {
pub(crate) title: &'static str,
pub(crate) name: Arc<str>,
pub(crate) color: ItemPalette,
pub(crate) time: Arc<str>,
}
render!(TuiOut: (self: ClipSelected) =>
FieldV(self.color, self.title, format!("{} {}", self.time, self.name)));
impl ClipSelected {
/// Shows currently playing phrase with beats elapsed
pub fn play_phrase <T: HasPlayPhrase + HasClock> (state: &T) -> Self {
let (name, color) = if let Some((_, Some(phrase))) = state.play_phrase() {
let MidiClip { ref name, color, .. } = *phrase.read().unwrap();
(name.clone().into(), color)
} else {
("".to_string().into(), TuiTheme::g(64).into())
};
Self {
title: "Now",
name,
color,
time: state.pulses_since_start_looped()
.map(|(times, time)|format!("{:>3}x {:>}",
times+1.0,
state.clock().timebase.format_beats_1(time)))
.unwrap_or_else(||String::from(" ")).into()
}
}
/// Shows next phrase with beats remaining until switchover
pub fn next_phrase <T: HasPlayPhrase> (state: &T) -> Self {
let mut time: Arc<str> = String::from("--.-.--").into();
let mut name: Arc<str> = String::from("").into();
let mut color = ItemPalette::from(TuiTheme::g(64));
if let Some((t, Some(phrase))) = state.next_phrase() {
let phrase = phrase.read().unwrap();
name = phrase.name.clone();
color = phrase.color.clone();
time = {
let target = t.pulse.get();
let current = state.clock().playhead.pulse.get();
if target > current {
let remaining = target - current;
format!("-{:>}", state.clock().timebase.format_beats_1(remaining))
} else {
String::new()
}
}.into()
} else if let Some((t, Some(phrase))) = state.play_phrase() {
let phrase = phrase.read().unwrap();
if phrase.looped {
name = phrase.name.clone();
color = phrase.color.clone();
let target = t.pulse.get() + phrase.length as f64;
let current = state.clock().playhead.pulse.get();
if target > current {
time = format!("-{:>}", state.clock().timebase.format_beats_0(
target - current
)).into()
}
} else {
name = "Stop".to_string().into();
}
};
Self { title: "Next", time, name, color, }
}
}

View file

@ -1,232 +0,0 @@
//#[cfg(test)] mod test_focus {
//use super::focus::*;
//#[test] fn test_focus () {
//struct FocusTest {
//focused: char,
//cursor: (usize, usize)
//}
//impl HasFocus for FocusTest {
//type Item = char;
//fn focused (&self) -> Self::Item {
//self.focused
//}
//fn set_focused (&mut self, to: Self::Item) {
//self.focused = to
//}
//}
//impl FocusGrid for FocusTest {
//fn focus_cursor (&self) -> (usize, usize) {
//self.cursor
//}
//fn focus_cursor_mut (&mut self) -> &mut (usize, usize) {
//&mut self.cursor
//}
//fn focus_layout (&self) -> &[&[Self::Item]] {
//&[
//&['a', 'a', 'a', 'b', 'b', 'd'],
//&['a', 'a', 'a', 'b', 'b', 'd'],
//&['a', 'a', 'a', 'c', 'c', 'd'],
//&['a', 'a', 'a', 'c', 'c', 'd'],
//&['e', 'e', 'e', 'e', 'e', 'e'],
//]
//}
//}
//let mut tester = FocusTest { focused: 'a', cursor: (0, 0) };
//tester.focus_right();
//assert_eq!(tester.cursor.0, 3);
//assert_eq!(tester.focused, 'b');
//tester.focus_down();
//assert_eq!(tester.cursor.1, 2);
//assert_eq!(tester.focused, 'c');
//}
//}
//use crate::*;
//struct TestEngine([u16;4], Vec<Vec<char>>);
//impl Engine for TestEngine {
//type Unit = u16;
//type Size = [Self::Unit;2];
//type Area = [Self::Unit;4];
//type Input = Self;
//type Handled = bool;
//fn exited (&self) -> bool {
//true
//}
//}
//#[derive(Copy, Clone)]
//struct TestArea(u16, u16);
//impl Render<TestEngine> for TestArea {
//fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
//Ok(Some([to[0], to[1], self.0, self.1]))
//}
//fn render (&self, to: &mut TestEngine) -> Perhaps<[u16;4]> {
//if let Some(layout) = self.layout(to.area())? {
//for y in layout.y()..layout.y()+layout.h()-1 {
//for x in layout.x()..layout.x()+layout.w()-1 {
//to.1[y as usize][x as usize] = '*';
//}
//}
//Ok(Some(layout))
//} else {
//Ok(None)
//}
//}
//}
//#[test]
//fn test_plus_minus () -> Usually<()> {
//let area = [0, 0, 10, 10];
//let engine = TestEngine(area, vec![vec![' ';10];10]);
//let test = TestArea(4, 4);
//assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
//assert_eq!(Push::X(1, test).layout(area)?, Some([1, 0, 4, 4]));
//Ok(())
//}
//#[test]
//fn test_outset_align () -> Usually<()> {
//let area = [0, 0, 10, 10];
//let engine = TestEngine(area, vec![vec![' ';10];10]);
//let test = TestArea(4, 4);
//assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
//assert_eq!(Margin::X(1, test).layout(area)?, Some([0, 0, 6, 4]));
//assert_eq!(Align::X(test).layout(area)?, Some([3, 0, 4, 4]));
//assert_eq!(Align::X(Margin::X(1, test)).layout(area)?, Some([2, 0, 6, 4]));
//assert_eq!(Margin::X(1, Align::X(test)).layout(area)?, Some([2, 0, 6, 4]));
//Ok(())
//}
////#[test]
////fn test_misc () -> Usually<()> {
////let area: [u16;4] = [0, 0, 10, 10];
////let test = TestArea(4, 4);
////assert_eq!(test.layout(area)?,
////Some([0, 0, 4, 4]));
////assert_eq!(Align::Center(test).layout(area)?,
////Some([3, 3, 4, 4]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&test)?;
////add(&test)
////})).layout(area)?,
////Some([3, 1, 4, 8]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&test)
////})).layout(area)?,
////Some([2, 0, 6, 10]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&Padding::XY(2, 2, test))
////})).layout(area)?,
////Some([2, 1, 6, 8]));
////assert_eq!(Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&Padding::XY(2, 2, test))
////}).layout(area)?,
////Some([0, 0, 6, 8]));
////assert_eq!(Stack::right(|add|{
////add(&Stack::down(|add|{
////add(&Margin::XY(2, 2, test))?;
////add(&Padding::XY(2, 2, test))
////}))?;
////add(&Align::Center(TestArea(2 ,2)))
////}).layout(area)?,
////Some([0, 0, 8, 8]));
////Ok(())
////}
////#[test]
////fn test_offset () -> Usually<()> {
////let area: [u16;4] = [50, 50, 100, 100];
////let test = TestArea(3, 3);
////assert_eq!(Push::X(1, test).layout(area)?, Some([51, 50, 3, 3]));
////assert_eq!(Push::Y(1, test).layout(area)?, Some([50, 51, 3, 3]));
////assert_eq!(Push::XY(1, 1, test).layout(area)?, Some([51, 51, 3, 3]));
////Ok(())
////}
////#[test]
////fn test_outset () -> Usually<()> {
////let area: [u16;4] = [50, 50, 100, 100];
////let test = TestArea(3, 3);
////assert_eq!(Margin::X(1, test).layout(area)?, Some([49, 50, 5, 3]));
////assert_eq!(Margin::Y(1, test).layout(area)?, Some([50, 49, 3, 5]));
////assert_eq!(Margin::XY(1, 1, test).layout(area)?, Some([49, 49, 5, 5]));
////Ok(())
////}
////#[test]
////fn test_padding () -> Usually<()> {
////let area: [u16;4] = [50, 50, 100, 100];
////let test = TestArea(3, 3);
////assert_eq!(Padding::X(1, test).layout(area)?, Some([51, 50, 1, 3]));
////assert_eq!(Padding::Y(1, test).layout(area)?, Some([50, 51, 3, 1]));
////assert_eq!(Padding::XY(1, 1, test).layout(area)?, Some([51, 51, 1, 1]));
////Ok(())
////}
////#[test]
////fn test_stuff () -> Usually<()> {
////let area: [u16;4] = [0, 0, 100, 100];
////assert_eq!("1".layout(area)?,
////Some([0, 0, 1, 1]));
////assert_eq!("333".layout(area)?,
////Some([0, 0, 3, 1]));
////assert_eq!(Layers::new(|add|{add(&"1")?;add(&"333")}).layout(area)?,
////Some([0, 0, 3, 1]));
////assert_eq!(Stack::down(|add|{add(&"1")?;add(&"333")}).layout(area)?,
////Some([0, 0, 3, 2]));
////assert_eq!(Stack::right(|add|{add(&"1")?;add(&"333")}).layout(area)?,
////Some([0, 0, 4, 1]));
////assert_eq!(Stack::down(|add|{
////add(&Stack::right(|add|{add(&"1")?;add(&"333")}))?;
////add(&"55555")
////}).layout(area)?,
////Some([0, 0, 5, 2]));
////let area: [u16;4] = [1, 1, 100, 100];
////assert_eq!(Margin::X(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
////Some([0, 1, 6, 1]));
////assert_eq!(Margin::Y(1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
////Some([1, 0, 4, 3]));
////assert_eq!(Margin::XY(1, 1, Stack::right(|add|{add(&"1")?;add(&"333")})).layout(area)?,
////Some([0, 0, 6, 3]));
////assert_eq!(Stack::down(|add|{
////add(&Margin::XY(1, 1, "1"))?;
////add(&Margin::XY(1, 1, "333"))
////}).layout(area)?,
////Some([1, 1, 5, 6]));
////let area: [u16;4] = [1, 1, 95, 100];
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Margin::XY(1, 1, "1"))?;
////add(&Margin::XY(1, 1, "333"))
////})).layout(area)?,
////Some([46, 48, 5, 6]));
////assert_eq!(Align::Center(Stack::down(|add|{
////add(&Layers::new(|add|{
//////add(&Margin::XY(1, 1, Background(Color::Rgb(0,128,0))))?;
////add(&Margin::XY(1, 1, "1"))?;
////add(&Margin::XY(1, 1, "333"))?;
//////add(&Background(Color::Rgb(0,128,0)))?;
////Ok(())
////}))?;
////add(&Layers::new(|add|{
//////add(&Margin::XY(1, 1, Background(Color::Rgb(0,0,128))))?;
////add(&Margin::XY(1, 1, "555"))?;
////add(&Margin::XY(1, 1, "777777"))?;
//////add(&Background(Color::Rgb(0,0,128)))?;
////Ok(())
////}))
////})).layout(area)?,
////Some([46, 48, 5, 6]));
////Ok(())
////}