big ass refactor (rip client)

This commit is contained in:
🪞👃🪞 2024-07-03 14:51:48 +03:00
parent 94c1f83ef2
commit 8c3cf53c67
56 changed files with 2232 additions and 1891 deletions

194
src/view/chain.rs Normal file
View file

@ -0,0 +1,194 @@
use crate::core::*;
use crate::view::*;
use crate::model::*;
pub enum ChainViewMode {
Hidden,
Compact,
Row,
Column,
}
pub struct ChainView<'a> {
pub focused: bool,
pub chain: Option<&'a Chain>,
}
impl<'a> Render for ChainView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let style = Some(Style::default().green().dim());
if self.focused {
let Rect { x, y, width, height} = area;
lozenge_left(buf, x, y, height, style);
lozenge_right(buf, x + width - 1, y, height, style);
};
let (area, _plugins) = if let Some(ref chain) = self.chain {
draw_as_row(&*chain, buf, area, style)?
} else {
(area, vec![])
};
Ok(area)
}
}
pub fn render (state: &Chain, buf: &mut Buffer, area: Rect)
-> Usually<Rect>
{
let Rect { x, y, .. } = area;
let selected = Some(if state.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
});
let result = match state.view {
ChainViewMode::Hidden => Rect { x, y, width: 0, height: 0 },
ChainViewMode::Compact => {
let area = Rect { x, y, width: (state.name.len() + 4) as u16, height: 3 };
buf.set_string(area.x + 2, area.y + 1, &state.name, Style::default());
draw_box_styled(buf, area, selected)
},
ChainViewMode::Row => {
draw_box_styled(buf, area, selected);
let (area, _) = Row::draw(buf, area, &state.items, 0)?;
area
},
ChainViewMode::Column => {
draw_as_column(state, buf, area, selected)?
},
};
Ok(result)
}
pub fn draw_as_row (
state: &Chain, buf: &mut Buffer, area: Rect, _: Option<Style>
) -> Usually<(Rect, Vec<Rect>)> {
let Rect { mut x, y, width, height } = area;
let mut h = 0u16;
let mut frames = vec![];
for (i, device) in state.items.iter().enumerate() {
let x2 = 0u16;
let _y2 = 1u16;
//for port in device.midi_ins()?.iter() {
//port.blit(buf, x, y + y2, Some(Style::default()));
//x2 = x2.max(port.len() as u16);
//y2 = y2 + 1;
//}
//for port in device.audio_ins()?.iter() {
//port.blit(buf, x, y + y2, Some(Style::default()));
//x2 = x2.max(port.len() as u16);
//y2 = y2 + 1;
//}
let width = width.saturating_sub(x).saturating_sub(x2);
let style = Some(if i == state.focus {
if state.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
}
} else {
Style::default().dim()
});
lozenge_left(buf, x + x2, y, height, style);
let frame = device.render(buf, Rect { x: x + x2, y, width, height })?;
lozenge_right(buf, x + x2 + frame.width - 1, y, height, style);
//let mut y2 = 1u16;
//for port in device.midi_outs()?.iter() {
//port.blit(buf, x + x2 + frame.width, y + y2, Some(Style::default()));
//x2 = x2.max(port.len() as u16);
//y2 = y2 + 1;
//}
//for port in device.audio_outs()?.iter() {
//port.blit(buf, x + x2 + frame.width, y + y2, Some(Style::default()));
//x2 = x2.max(port.len() as u16);
//y2 = y2 + 1;
//}
frames.push(frame);
h = h.max(frame.height);
x = x + frame.width;
}
Ok((Rect { x: area.x, y: area.y, width, height: h }, frames))
}
pub fn draw_as_column (
state: &Chain, buf: &mut Buffer, area: Rect, selected: Option<Style>
) -> Usually<Rect> {
//let area = Rect { x, y, width: 40, height: 30 };
//let mut y = area.y;
//buf.set_string(area.x, y, "│", Style::default().black());
//buf.set_string(area.x + area.width - 1, y, "│", Style::default().black());
//buf.set_string(area.x + 2, y, "Input...", Style::default().dim());
//let mut x = 0u16;
//for (i, device) in state.items.iter().enumerate() {
//let result = device.render(buf, Rect {
//x: area.x,
//y,
//width: area.width,
//height: 21//area.height.saturating_sub(y)
//})?;
//if i == state.focus {
//if state.focused {
//draw_box_styled(buf, result, Some(Style::default().green().not_dim()))
//} else {
//draw_box_styled_dotted(buf, result, Some(Style::default().green().dim()))
//};
//};
////let result = Rect { x: 0, y: 0, width: result.width, height: 21 };
//x = x.max(result.width);
//y = y + result.height;
////y = y + 1;
//buf.set_string(area.x, y, "│", Style::default().black());
//buf.set_string(area.x + area.width - 1, y, "│", Style::default().black());
//buf.set_string(area.x + 2, y, " Patch in ┐ │ └ Patch out", Style::default().dim());
//y = y + 1;
////buf.set_string(area.x, y, format!("{y}---BOT---"), Style::default().red());
////buf.set_string(area.x + area.width - 1, area.y + 1, "│", Style::default().black());
////buf.set_string(area.x + 2, y, "Patch...", Style::default().dim());
//}
//Ok(draw_box(buf, Rect {
//x: area.x,
//y: area.y,
//width: area.width,
//height: y - area.y,
//}))
let Rect { x, mut y, width, height } = area;
//let (area, areas) = Column::draw(buf, area, &state.items, 0)?;
let mut w = 0u16;
let mut frames = vec![];
for device in state.items.iter() {
let style_midi = Style::default().black().bold().on_green();
let style_audio = Style::default().black().bold().on_red();
let midi_ins = device.midi_ins()?;
let midi_outs = device.midi_outs()?;
let audio_ins = device.audio_ins()?;
let audio_outs = device.audio_outs()?;
y = y + midi_ins.len() as u16;
let frame = device.render(buf, Rect {
x, y, width, height: height.saturating_sub(y)
})?;
frames.push(frame);
w = w.max(frame.width);
y = y - midi_ins.len() as u16;
for port in midi_ins.iter() {
buf.set_string(x + frame.width - 10, y, &format!(" <i> MIDI {port} "), style_midi);
y = y + 1;
}
y = y - audio_ins.len() as u16;
for port in audio_ins.iter() {
buf.set_string(x + frame.width - 10, y, &format!(" <i> MIDI {port} "), style_audio);
y = y + 1;
}
y = y + frame.height - 1;
y = y + midi_outs.len() as u16;
for port in midi_outs.iter() {
buf.set_string(x + 2, y, &format!(" <o> MIDI {port} "), style_midi);
y = y + 1;
}
y = y + audio_outs.len() as u16;
for port in audio_outs.iter() {
buf.set_string(x + 2, y, &format!(" <o> Audio {port} "), style_audio);
y = y + 1;
}
}
draw_box_styled(buf, frames[state.focus], selected);
Ok(area)
}

168
src/view/grid.rs Normal file
View file

@ -0,0 +1,168 @@
use crate::core::*;
use crate::model::*;
use crate::view::*;
pub struct SceneGridView<'a> {
pub buf: &'a mut Buffer,
pub area: Rect,
pub name: &'a str,
pub focused: bool,
pub scenes: &'a[Scene],
pub tracks: &'a[Track],
pub cursor: &'a(usize, usize),
}
impl<'a> SceneGridView<'a> {
pub fn new (
buf: &'a mut Buffer,
area: Rect,
name: &'a str,
focused: bool,
scenes: &'a[Scene],
tracks: &'a[Track],
cursor: &'a(usize, usize),
) -> Self {
Self {
buf,
area,
name,
focused,
scenes,
tracks,
cursor,
}
}
pub fn draw (&mut self) -> Usually<Rect> {
self.area.height = self.scenes.len() as u16 + 2;
let style = Some(Style::default().green().dim());
if self.focused {
let Rect { x, y, width, height } = self.area;
lozenge_left(self.buf, x, y, height, style);
lozenge_right(self.buf, x + width - 1, y, height, style);
}
let columns = self.column_names();
let mut x = self.area.x;
for (i, w) in self.column_widths().iter().enumerate() {
if x >= self.area.x + self.area.width {
break
}
self.separator_v(x, i == self.cursor.0);
x = x + w;
self.separator_v(x, i == self.cursor.0);
}
let (mut x, y) = (self.area.x, self.area.y);
for (i, title) in columns.iter().enumerate() {
if x >= self.area.x + self.area.width {
break
}
title.blit(
self.buf, x+1, y, Some(self.highlight(i == self.cursor.0).bold())
);
if i == 0 {
self.scenes(x+1, y + 1);
} else if i < columns.len() {
self.clips(x+1, y + 1, i - 1);
}
let w = (title.len() as u16).max(10) + 3;
x = x + w;
}
"Add track…".blit(self.buf, x + 2, y, Some(Style::default().dim()));
Ok(self.area)
}
fn column_names (&self) -> Vec<&'a str> {
let mut column_names = vec![self.name];
for track in self.tracks.iter() {
column_names.push(track.name.as_str());
}
column_names
}
fn column_widths (&self) -> Vec<u16> {
self.column_names().iter().map(|name|(name.len() as u16).max(10) + 3).collect()
}
fn scenes (&mut self, x: u16, y: u16) -> u16 {
let mut index = 0usize;
loop {
if index >= self.scenes.len() {
break
}
if y + index as u16 >= self.area.height {
break
}
if let Some(scene) = self.scenes.get(index) {
let style = Some(self.highlight(
(0 == self.cursor.0) && (index + 1 == self.cursor.1)
).bold());
"".blit(self.buf, x, y + index as u16, style);
scene.name.blit(self.buf, x+1, y + index as u16, style);
}
index = index + 1;
}
let hi = (0 == self.cursor.0) &&
(self.scenes.len() + 1 == self.cursor.1);
"Add scene…".blit(self.buf, x, y + index as u16, Some(if hi {
self.highlight(true)
} else {
Style::default().dim()
}));
index as u16
}
fn clips (&mut self, x: u16, y: u16, track: usize) -> u16 {
let mut index = 0;
loop {
if index >= self.scenes.len() {
break
}
if y + index as u16 >= self.area.height {
break
}
if let Some(scene) = self.scenes.get(index) {
let hi = (track + 1 == self.cursor.0) &&
(index + 1 == self.cursor.1);
let style = Some(self.highlight(hi));
let clip = scene.clips.get(track);
let index = index as u16;
let label = if let Some(Some(clip)) = clip {
if let Some(phrase) = self.tracks[track].sequencer.phrases.get(*clip) {
format!("{}", phrase.name)
} else {
format!("????")
}
} else {
format!(" ·········")
};
label.blit(self.buf, x, y + index, style);
}
index = index + 1;
}
let hi = (track + 1 == self.cursor.0) &&
(self.scenes.len() + 1 == self.cursor.1);
" + Add clip".blit(self.buf, x, y + index as u16, Some(if hi {
self.highlight(true)
} else {
Style::default().dim()
}));
index as u16
}
fn highlight (&self, highlight: bool) -> Style {
if highlight {
if self.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
}
} else {
Style::default()
}
}
fn separator_v (&mut self, x: u16, highlight: bool) {
let style = Some(self.highlight(highlight));
for y in self.area.y+1..self.area.y+self.area.height-1 {
"".blit(self.buf, x, y, style);
}
}
}

50
src/view/launcher.rs Normal file
View file

@ -0,0 +1,50 @@
use crate::{core::*, model::*, view::*};
pub fn render (state: &Launcher, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
//area.width = 80; // DOS mode
//area.height = 25;
let Rect { x, mut y, width, height } = area;
y = y + TransportView {
timebase: &state.timebase,
playing: state.playing,
record: state.sequencer().map(|s|s.recording).unwrap_or(false),
overdub: state.sequencer().map(|s|s.overdub).unwrap_or(false),
monitor: state.sequencer().map(|s|s.monitoring).unwrap_or(false),
frame: state.current_frame
}.render(buf, area)?.height;
y = y + SceneGridView {
buf,
area: Rect { x, y, width, height },
name: &state.name,
focused: state.view.is_tracks(),
scenes: &state.scenes,
tracks: &state.tracks,
cursor: &state.cursor
}.draw()?.height;
if let Some(chain) = state.chain() {
y = y + ChainView {
focused: state.view.is_chains(),
chain: Some(&*chain),
}.render(buf, Rect { x, y, width, height: height/3 })?.height
}
let track = state.track().map(|t|t.1);
y = y + SequencerView {
focused: state.view.is_sequencer(),
ppq: state.timebase.ppq() as usize,
track: track,
phrase: track.unwrap().sequencer.phrases.get(state.phrase_id().unwrap())
}.render(buf, Rect { x, y, width, height: height - y })?.height;
area.height = y;
if state.show_help {
let style = Some(Style::default().bold().white().not_dim().on_black().italic());
let hide = "[Tab] Mode [Arrows] Move [.,] Value [F1] Toggle help ";
hide.blit(buf, x + (width - hide.len() as u16) / 2, height - 1, style);
}
Ok(area)
}

View file

@ -0,0 +1,42 @@
pub enum Collected<'a> {
Box(Box<dyn Render + 'a>),
Ref(&'a (dyn Render + 'a)),
None
}
impl<'a> Render for Collected<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
match self {
Self::Box(item) => (*item).render(buf, area),
Self::Ref(item) => (*item).render(buf, area),
Self::None => Ok(area),
}
}
}
pub struct Collector<'a>(pub Vec<Collected<'a>>);
impl<'a, R: Render + 'a> FnOnce<(R)> for Collector<'a> {
type Output = ();
extern "rust-call" fn call_once (self, (device, ): (R,)) -> Self::Output {
self.add(widget.into_collected());
}
}
impl<'a> Collector<'a> {
pub fn collect (collect: impl Fn(&mut Collector<'a>)) -> Self {
let mut items = Self(vec![]);
collect(&mut items);
items
}
fn add (mut self, widget: Collected<'a>) -> Self {
self.0.push(widget);
self
}
}
pub trait Collection<'a, T, U> {
fn add (self, widget: impl Render + 'a) -> Self;
}

View file

@ -0,0 +1,73 @@
use crate::core::*;
pub struct Stack<'a>(pub &'a[Box<dyn Render + Sync>]);
impl<'a> Render for Stack<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let mut area2 = area.clone();
for layer in self.0.iter() {
area2 = layer.render(buf, area2)?;
}
Ok(area)
}
}
pub struct Column(pub Vec<Box<dyn Device>>);
pub struct Row(pub Vec<Box<dyn Device>>);
impl Column {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (_i, device) in items.iter().enumerate() {
let y = area.y + h;
let rect = Rect { x: area.x, y, width: area.width, height: area.height - h };
let result = device.render(buf, rect)?;
rects.push(result);
w = w.max(result.width);
h = ((h + result.height) as i16 + gap).max(0) as u16;
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}
}
impl Render for Column {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(Column::draw(buf, area, &self.0, 0)?.0)
}
}
impl Row {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (_i, device) in items.iter().enumerate() {
let x = area.x + w;
let rect = Rect { x, y: area.y, width: area.width - w, height: area.height };
let result = device.render(buf, rect)?;
rects.push(result);
w = ((w + result.width) as i16 + gap).max(0) as u16;
h = h.max(result.height);
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}
}
impl Render for Row {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(Row::draw(buf, area, &self.0, 0)?.0)
}
}

0
src/view/layout/focus.rs Normal file
View file

View file

@ -0,0 +1,35 @@
use crate::core::*;
const LOZENGE: [[&'static str;3];3] = [
["", "", ""],
["", " ", ""],
["", "", ""],
];
pub fn lozenge_left (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<Style>) {
let y2 = y1 + h;
let y3 = y2.saturating_sub(1);
for y in y1..y2 {
if y == y1 {
LOZENGE[0][0]
} else if y == y3 {
LOZENGE[2][0]
} else {
LOZENGE[1][0]
}.blit(buf, x, y, style)
}
}
pub fn lozenge_right (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<Style>) {
let y2 = y1 + h;
let y3 = y2.saturating_sub(1);
for y in y1..y2 {
if y == y1 {
LOZENGE[0][2]
} else if y == y3 {
LOZENGE[2][2]
} else {
LOZENGE[1][2]
}.blit(buf, x, y, style)
}
}

53
src/view/layout/mod.rs Normal file
View file

@ -0,0 +1,53 @@
mod focus; pub use self::focus::*;
mod container; pub use self::container::*;
mod scroll; pub use self::scroll::*;
mod table; pub use self::table::*;
mod lozenge; pub use self::lozenge::*;
use crate::core::*;
pub trait Modal<T>: Device {
fn handle_with_state (&self, state: &mut T, event: &AppEvent) -> Usually<bool>;
}
pub trait MaxHeight: Device {
fn max_height (&self) -> u16 {
u16::MAX
}
}
impl<T: Device> MaxHeight for T {}
pub fn draw_box (buffer: &mut Buffer, area: Rect) -> Rect {
draw_box_styled(buffer, area, Some(Style::default().gray().dim()))
}
pub fn draw_box_styled (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
if area.width < 1 || area.height < 1 {
return area
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y, style);
for y in (area.y + 1)..(area.y + area.height - 1) {
"".blit(buffer, area.x, y, style);
"".blit(buffer, area.x + area.width - 1, y, style);
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y + area.height - 1, style);
area
}
pub fn draw_box_styled_dotted (buffer: &mut Buffer, area: Rect, style: Option<Style>) -> Rect {
if area.width < 1 || area.height < 1 {
return area
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y, style);
for y in (area.y + 1)..(area.y + area.height - 1) {
"".blit(buffer, area.x, y, style);
"".blit(buffer, area.x + area.width - 1, y, style);
}
format!("{}", "".repeat((area.width - 2).into()))
.blit(buffer, area.x, area.y + area.height - 1, style);
area
}

View file

@ -0,0 +1,5 @@
pub struct ScrollY;
pub struct ScrollX;
pub struct ScrollXY;

33
src/view/layout/table.rs Normal file
View file

@ -0,0 +1,33 @@
use crate::core::*;
pub struct Cell<T> {
text: String,
style: Option<Style>,
width: u16,
height: u16,
data: T
}
impl<T> Cell<T> {
pub fn new (text: &str, data: T) -> Self {
Self { text: text.to_string(), style: None, width: text.len() as u16, height: 1, data }
}
pub fn draw (&self, buf: &mut Buffer, x: u16, y: u16) {
self.text.blit(buf, x, y, self.style)
}
}
pub struct Table<T> {
columns: Vec<Vec<Cell<T>>>,
row: usize,
col: usize,
}
impl<T> Table<T> {
pub fn new (columns: Vec<Vec<Cell<T>>>) -> Self {
Self { columns, row: 0, col: 0 }
}
pub fn set (&mut self, col: usize, row: usize, cell: Cell<T>) {
self.columns[col][row] = cell;
}
}

68
src/view/mixer.rs Normal file
View file

@ -0,0 +1,68 @@
use crate::core::*;
use crate::view::*;
use crate::model::*;
pub fn render (state: &Mixer, buf: &mut Buffer, mut area: Rect)
-> Usually<Rect>
{
if area.height < 2 {
return Ok(area)
}
area.x = area.width.saturating_sub(80) / 2;
area.width = area.width.min(80);
area.height = state.tracks.len() as u16 + 2;
draw_box(buf, area);
let x = area.x + 1;
let y = area.y + 1;
let _h = area.height - 2;
for (i, track) in state.tracks.iter().enumerate() {
//buf.set_string(
//x, y + index as u16,
//&track.name, Style::default().bold().not_dim()
//);
for (j, (column, field)) in [
(0, format!(" {:10} ", track.name)),
(12, format!(" {:.1}dB ", track.gain)),
(22, format!(" [ ] ")),
(30, format!(" C ")),
(35, format!(" {:.1}dB ", track.level)),
(45, format!(" [ ] ")),
(51, format!(" {:7} ", track.route)),
].into_iter().enumerate() {
buf.set_string(
x + column as u16,
y + i as u16,
field,
if state.selected_track == i && state.selected_column == j {
Style::default().white().bold().not_dim()
} else {
Style::default().not_dim()
}
);
//stdout.queue(move_to(column, row))?;
//if state.selected_track == i && state.selected_column == j {
//stdout.queue(PrintStyledContent(field.to_string().bold().reverse()))?;
//} else {
//stdout.queue(PrintStyledContent(field.to_string().bold()))?;
//}
//fn render_meters (
//state: &mut Mixer,
//stdout: &mut Stdout,
//offset: Rect
//) -> Result<(), Box<dyn Error>> {
//let move_to = |col, row| crossterm::cursor::MoveTo(offset.0 + col, offset.1 + row);
//for (i, track) in state.tracks.iter().enumerate() {
//let row = (i + 1) as u16;
//stdout
//.queue(move_to(10, row))?.queue(PrintStyledContent("▁".green()))?
//.queue(move_to(20, row))?.queue(PrintStyledContent("▁".green()))?
//.queue(move_to(28, row))?.queue(PrintStyledContent("▁".green()))?
//.queue(move_to(43, row))?.queue(PrintStyledContent("▁".green()))?;
//}
//Ok(())
//}
}
}
Ok(area)
}

48
src/view/plugin.rs Normal file
View file

@ -0,0 +1,48 @@
use crate::{core::*, model::*};
pub fn render (state: &Plugin, buf: &mut Buffer, area: Rect)
-> Usually<Rect>
{
let Rect { x, y, height, .. } = area;
let mut width = 20u16;
match &state.plugin {
Some(PluginKind::LV2(LV2Plugin { port_list, instance, .. })) => {
let start = state.selected.saturating_sub((height as usize / 2).saturating_sub(1));
let end = start + height as usize - 2;
//draw_box(buf, Rect { x, y, width, height });
for i in start..end {
if let Some(port) = port_list.get(i) {
let value = if let Some(value) = instance.control_input(port.index) {
value
} else {
port.default_value
};
//let label = &format!("C·· M·· {:25} = {value:.03}", port.name);
let label = &format!("{:25} = {value:.03}", port.name);
width = width.max(label.len() as u16 + 4);
label.blit(buf, x + 2, y + 1 + i as u16 - start as u16, if i == state.selected {
Some(Style::default().green())
} else {
None
});
} else {
break
}
}
},
_ => {}
};
draw_header(state, buf, area.x, area.y, width)?;
Ok(Rect { width, ..area })
}
fn draw_header (state: &Plugin, buf: &mut Buffer, x: u16, y: u16, w: u16) -> Usually<Rect> {
let style = Style::default().gray();
let label1 = format!(" {}", state.name);
label1.blit(buf, x + 1, y, Some(style.white().bold()));
if let Some(ref path) = state.path {
let label2 = format!("{}", &path[..((w as usize - 10).min(path.len()))]);
label2.blit(buf, x + 2 + label1.len() as u16, y, Some(style.not_dim()));
}
Ok(Rect { x, y, width: w, height: 1 })
}

78
src/view/sampler.rs Normal file
View file

@ -0,0 +1,78 @@
use crate::core::*;
use crate::model::*;
pub fn render (state: &Sampler, buf: &mut Buffer, Rect { x, y, height, .. }: Rect)
-> Usually<Rect>
{
let style = Style::default().gray();
let title = format!(" {} ({})", state.name, state.voices.len());
title.blit(buf, x+1, y, Some(style.white().bold().not_dim()));
let mut width = title.len() + 2;
for (i, (note, sample)) in state.samples.iter().enumerate() {
let style = if i == state.cursor.0 {
Style::default().green()
} else {
Style::default()
};
let i = i as u16;
let y1 = y+1+i;
if y1 >= y + height {
break
}
if i as usize == state.cursor.0 {
"".blit(buf, x+1, y1, Some(style.bold()));
}
let label1 = format!("{note:3} {:8}", sample.name);
let label2 = format!("{:>6} {:>6}", sample.start, sample.end);
label1.blit(buf, x+2, y1, Some(style.bold()));
label2.blit(buf, x+3+label1.len()as u16, y1, Some(style));
width = width.max(label1.len() + label2.len() + 4);
}
let height = ((1 + state.samples.len()) as u16).min(height);
Ok(Rect { x, y, width: width as u16, height })
}
//fn render_table (
//state: &mut Sampler,
//stdout: &mut Stdout,
//offset: (u16, u16),
//) -> Result<(), Box<dyn Error>> {
//let move_to = |col, row| crossterm::cursor::MoveTo(offset.0 + col, offset.1 + row);
//stdout.queue(move_to(0, 3))?.queue(
//Print(" Name Rate Trigger Route")
//)?;
//for (i, sample) in state.samples.lock().unwrap().iter().enumerate() {
//let row = 4 + i as u16;
//for (j, (column, field)) in [
//(0, format!(" {:7} ", sample.name)),
//(9, format!(" {:.1}Hz ", sample.rate)),
//(18, format!(" MIDI C{} {} ", sample.trigger.0, sample.trigger.1)),
//(33, format!(" {:.1}dB -> Output ", sample.gain)),
//(50, format!(" {} ", sample.playing.unwrap_or(0))),
//].into_iter().enumerate() {
//stdout.queue(move_to(column, row))?;
//if state.selected_sample == i && state.selected_column == j {
//stdout.queue(PrintStyledContent(field.to_string().bold().reverse()))?;
//} else {
//stdout.queue(PrintStyledContent(field.to_string().bold()))?;
//}
//}
//}
//Ok(())
//}
//fn render_meters (
//state: &mut Sampler,
//stdout: &mut Stdout,
//offset: (u16, u16),
//) -> Result<(), Box<dyn Error>> {
//let move_to = |col, row| crossterm::cursor::MoveTo(offset.0 + col, offset.1 + row);
//for (i, sample) in state.samples.lock().iter().enumerate() {
//let row = 4 + i as u16;
//stdout.queue(move_to(32, row))?.queue(
//PrintStyledContent("▁".green())
//)?;
//}
//Ok(())
//}

104
src/view/sequencer.rs Normal file
View file

@ -0,0 +1,104 @@
use crate::{core::*,model::*,view::*};
pub mod horizontal;
pub mod vertical;
pub struct SequencerView<'a> {
pub focused: bool,
pub phrase: Option<&'a Phrase>,
pub track: Option<&'a Track>,
pub ppq: usize,
}
impl<'a> Render for SequencerView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, width, height } = area;
let style = Some(Style::default().green().dim());
if self.focused {
lozenge_left(buf, x, y, height, style);
lozenge_right(buf, x + width - 1, y, height, style);
}
if let Some(ref track) = self.track {
self::horizontal::draw(
buf,
area,
self.phrase,
self.ppq,
track.sequencer.time_cursor,
track.sequencer.time_start,
track.sequencer.time_zoom,
track.sequencer.note_cursor,
track.sequencer.note_start,
Some(if self.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
})
)?;
}
Ok(area)
}
}
pub fn render (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, width, height } = area;
let header = draw_header(s, buf, area)?;
let piano = match s.view {
SequencerMode::Tiny => Rect { x, y, width, height: 0 },
SequencerMode::Compact => Rect { x, y, width, height: 0 },
SequencerMode::Vertical => self::vertical::draw(s, buf, Rect {
x, y: y + header.height, width, height,
})?,
SequencerMode::Horizontal => self::horizontal::draw(
buf,
Rect { x, y: y + header.height, width, height, },
s.phrase(),
s.timebase.ppq() as usize,
s.time_cursor,
s.time_start,
s.time_zoom,
s.note_cursor,
s.note_start,
None
)?,
};
Ok(draw_box(buf, Rect {
x, y,
width: header.width.max(piano.width),
height: header.height + piano.height
}))
}
pub fn draw_header (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, width, .. } = area;
let style = Style::default().gray();
crate::view::TransportView {
timebase: &s.timebase,
playing: s.playing,
record: s.recording,
overdub: s.overdub,
monitor: s.monitoring,
frame: 0
}.render(buf, area)?;
let separator = format!("{}", "-".repeat((width - 2).into()));
separator.blit(buf, x, y + 2, Some(style.dim()));
let _ = draw_clips(s, buf, area)?;
Ok(Rect { x, y, width, height: 3 })
}
pub fn draw_clips (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area;
let style = Style::default().gray();
for (i, sequence) in s.phrases.iter().enumerate() {
let label = format!("{}", &sequence.name);
label.blit(buf, x + 2, y + 3 + (i as u16)*2, Some(if Some(i) == s.sequence {
match s.playing {
TransportState::Rolling => style.white().bold(),
_ => style.not_dim().bold()
}
} else {
style.dim()
}));
}
Ok(Rect { x, y, width: 14, height: 14 })
}

View file

@ -0,0 +1,177 @@
use crate::core::*;
use super::*;
pub fn draw (
buf: &mut Buffer,
area: Rect,
phrase: Option<&Phrase>,
ppq: usize,
time: usize,
time0: usize,
time_z: usize,
note: usize,
note0: usize,
style: Option<Style>,
) -> Usually<Rect> {
let now = 0;
let notes = &[];
match time_z {
1 => "1/384",
2 => "1/192",
3 => "1/128",
4 => "1/96",
6 => "1/64",
8 => "1/48",
12 => "1/32",
16 => "1/24",
24 => "1/16",
32 => "1/12",
48 => "1/8",
64 => "1/6",
96 => "1/4",
128 => "1/3",
192 => "1/2",
384 => "1/1",
_ => ""
}.blit(buf, area.x, area.y, Some(Style::default().dim()));
keys(buf, area, note0, notes)?;
timer(buf, area, time0, now);
if let Some(phrase) = phrase {
lanes(buf, area, phrase, ppq, time_z, time0, note0);
}
let style = style.unwrap_or_else(||{Style::default().green().not_dim()});
cursor(buf, area, style, time, note);
//footer(buf, area, note0, note, time0, time, time_z);
Ok(area)
}
pub fn timer (buf: &mut Buffer, area: Rect, time0: usize, now: usize) {
let x = area.x + 5;
for step in time0..(time0+area.width as usize).saturating_sub(5) {
buf.set_string(x + step as u16, area.y, &"-", if step == now {
Style::default().yellow().bold().not_dim()
} else {
Style::default()
});
}
}
pub fn keys (buf: &mut Buffer, area: Rect, note0: usize, _notes: &[bool])
-> Usually<Rect>
{
let bw = Style::default().dim();
let Rect { x, y, width, height } = area;
let h = height.saturating_sub(2);
for index in 0..h {
let y = y + h - index;
let key = KEYS_VERTICAL[(index % 6) as usize];
key.blit(buf, x + 1, y, Some(bw));
"".blit(buf, x + 2, y, Some(bw));
"|---".repeat(width.saturating_sub(6) as usize).blit(buf, x + 5, y, Some(bw.black()));
let note_a = note0 + (index * 2) as usize;
if note_a % 12 == 0 {
let octave = format!("C{}", (note_a / 12) as i8 - 2);
octave.blit(buf, x + 3, y, None);
continue
}
let note_b = note0 + (index * 2) as usize;
if note_b % 12 == 0 {
let octave = format!("C{}", (note_b / 12) as i8 - 2);
octave.blit(buf, x + 3, y, None);
continue
}
}
Ok(area)
}
pub fn lanes (
buf: &mut Buffer,
area: Rect,
phrase: &Phrase,
ppq: usize,
time_z: usize,
_time0: usize,
note0: usize,
) {
let Rect { x, y, width, height } = area;
//let time0 = time0 / time_z;
//let time1 = time0 + width as usize;
//let note1 = note0 + height as usize;
let bg = Style::default();
let (bw, wh) = (bg.dim(), bg.white());
let offset = 5;
for x in x+offset..x+width-offset {
let step = (x-offset) as usize * time_z;
if step % ppq == 0 {
"|".blit(buf, x as u16, y, Some(Style::default().dim()));
}
let bar = 4 * ppq;
if step % bar == 0 {
format!("{}", (step/bar)+1)
.blit(buf, x as u16, y, Some(Style::default().bold().not_dim()));
}
let (a, b) = (step, step + time_z);
for index in 0..height-2 {
let note_a = note0 + index as usize * 2;
let note_b = note0 + index as usize * 2 + 1;
let (character, style) = match (
phrase.contains_note_on(u7::from_int_lossy(note_a as u8), a, b),
phrase.contains_note_on(u7::from_int_lossy(note_b as u8), a, b),
) {
(true, true) => ("", wh),
(false, true) => ("", wh),
(true, false) => ("", wh),
(false, false) => ("·", bw),
};
let y = y + height.saturating_sub(index+2) as u16;
character.blit(buf, x, y, Some(style));
}
}
}
pub fn cursor (
buf: &mut Buffer,
area: Rect,
style: Style,
time: usize,
note: usize
) {
let x = area.x + 5 + time as u16;
let y = area.y + 1 + note as u16 / 2;
let c = if note % 2 == 0 { "" } else { "" };
c.blit(buf, x, y, Some(style));
}
pub fn footer (
buf: &mut Buffer,
area: Rect,
note0: usize,
note: usize,
time0: usize,
time: usize,
time_z: usize,
) {
let Rect { mut x, y, width, height } = area;
buf.set_string(x, y + height, format!("{}", "-".repeat((width - 2).into())),
Style::default().dim());
buf.set_string(x, y + height + 2, format!("{}", "-".repeat((width - 2).into())),
Style::default().dim());
x = x + 2;
{
for (_, [letter, title, value]) in [
["S", &format!("ync"), &format!("<4/4>")],
["Q", &format!("uant"), &format!("<1/{}>", 4 * time_z)],
["N", &format!("ote"), &format!("{} ({}-{})", note0 + note, note0, "X")],
["T", &format!("ime"), &format!("{} ({}-{})", time0 + time, time0 + 1, "X")],
].iter().enumerate() {
buf.set_string(x, y + height + 1, letter, Style::default().bold().yellow().dim());
x = x + 1;
buf.set_string(x, y + height + 1, &title, Style::default().bold().dim());
x = x + title.len() as u16 + 1;
buf.set_string(x, y + height + 1, &value, Style::default().not_dim());
x = x + value.len() as u16;
buf.set_string(x, y + height + 1, " ", Style::default().dim());
x = x + 2;
}
}
}

View file

@ -0,0 +1,98 @@
use super::*;
pub fn draw (
s: &Sequencer,
buf: &mut Buffer,
mut area: Rect,
) -> Usually<Rect> {
area.x = area.x + 13;
keys(s, buf, area, 0);
steps(s, buf, area, 0);
playhead(s, buf, area.x, area.y);
Ok(area)
}
pub fn steps (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) {
if s.sequence.is_none() {
return
}
let ppq = s.timebase.ppq() as usize;
let bg = Style::default();
let bw = bg.dim();
let wh = bg.white();
let Rect { x, y, .. } = area;
for step in s.time_start..s.time_start+area.height as usize {
let y = y - (s.time_start + step / 2) as u16;
let step = step as usize;
//buf.set_string(x + 5, y, &" ".repeat(32.max(note1-s.note_start)as usize), bg);
if step % s.time_zoom == 0 {
buf.set_string(x + 2, y, &format!("{:2} ", step + 1), Style::default());
}
for k in s.note_start..s.note_start+area.width as usize {
let key = ::midly::num::u7::from_int_lossy(k as u8);
if step % 2 == 0 {
let (a, b, c) = (
(step + 0) as usize * ppq / s.time_zoom as usize,
(step + 1) as usize * ppq / s.time_zoom as usize,
(step + 2) as usize * ppq / s.time_zoom as usize,
);
let phrase = &s.phrases[s.sequence.unwrap()];
let (character, style) = match (
phrase.contains_note_on(key, a, b),
phrase.contains_note_on(key, b, c),
) {
(true, true) => ("", wh),
(true, false) => ("", wh),
(false, true) => ("", wh),
(false, false) => ("·", bw),
};
character.blit(buf, x + (5 + k - s.note_start) as u16, y, Some(style));
}
}
if beat == step as usize {
buf.set_string(x + 4, y, if beat % 2 == 0 { "" } else { "" }, Style::default().yellow());
for key in s.note_start..s.note_start+area.width as usize {
let _color = if s.notes_on[key as usize] {
Style::default().red()
} else {
KEY_STYLE[key as usize % 12]
};
}
}
}
}
pub fn playhead (s: &Sequencer, buf: &mut Buffer, x: u16, y: u16) {
let x = x + 5 + s.note_cursor as u16;
let y = y + s.time_cursor as u16 / 2;
let c = if s.time_cursor % 2 == 0 { "" } else { "" };
buf.set_string(x, y, c, Style::default());
}
pub fn keys (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) {
let ppq = s.timebase.ppq() as usize;
let Rect { x, y, .. } = area;
for key in s.note_start..s.note_start+area.width as usize {
let x = x + (5 + key - s.note_start) as u16;
if key % 12 == 0 {
let octave = format!("C{}", (key / 12) as i8 - 4);
buf.set_string(x, y, &octave, Style::default());
}
let mut color = KEY_STYLE[key as usize % 12];
let mut is_on = s.notes_on[key as usize];
let step = beat;
let (a, b, c) = (
(step + 0) as usize * ppq / s.time_zoom as usize,
(step + 1) as usize * ppq / s.time_zoom as usize,
(step + 2) as usize * ppq / s.time_zoom as usize,
);
let key = ::midly::num::u7::from(key as u8);
let phrase = &s.phrases[s.sequence.unwrap()];
is_on = is_on || phrase.contains_note_on(key, a, b);
is_on = is_on || phrase.contains_note_on(key, b, c);
if is_on {
color = Style::default().red();
}
buf.set_string(x, y - 1, &format!(""), color);
}
}

95
src/view/transport.rs Normal file
View file

@ -0,0 +1,95 @@
use crate::core::*;
pub struct TransportView<'a> {
pub timebase: &'a Arc<Timebase>,
pub playing: TransportState,
pub record: bool,
pub overdub: bool,
pub monitor: bool,
pub frame: usize,
}
impl<'a> Render for TransportView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, width, .. } = area;
draw_play_stop(buf, x + 1, y, &self.playing);
draw_rec(buf, x + 12, y, self.record);
draw_dub(buf, x + 19, y, self.overdub);
draw_mon(buf, x + 26, y, self.monitor);
draw_bpm(buf, x + 33, y, self.timebase.bpm() as usize);
draw_timer(buf, x + width - 1, y, &self.timebase, self.frame);
Ok(Rect { x, y, width, height: 1 })
}
}
pub fn draw_timer (buf: &mut Buffer, x: u16, y: u16, timebase: &Arc<Timebase>, frame: usize) {
let ppq = timebase.ppq() as usize;
let pulse = timebase.frames_pulses(frame as f64) as usize;
let (beats, pulses) = (pulse / ppq, pulse % ppq);
let (bars, beats) = (beats / 4, beats % 4);
let usecs = timebase.frames_usecs(frame as f64) as usize;
let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000);
let (minutes, seconds) = (seconds / 60, seconds % 60);
let timer = format!("{minutes}:{seconds:02}:{msecs:03} {}.{}.{pulses:02}",
bars as usize + 1,
beats as usize + 1
);
timer.blit(buf, x - timer.len() as u16, y, Some(Style::default().not_dim()));
}
pub fn draw_play_stop (buf: &mut Buffer, x: u16, y: u16, state: &TransportState) {
let style = Style::default().gray();
match state {
TransportState::Rolling => "▶ PLAYING",
TransportState::Starting => "READY ...",
TransportState::Stopped => "⏹ STOPPED",
}.blit(buf, x, y, Some(match state {
TransportState::Stopped => style.dim().bold(),
TransportState::Starting => style.not_dim().bold(),
TransportState::Rolling => style.not_dim().white().bold()
}));
}
pub fn draw_rec (buf: &mut Buffer, x: u16, y: u16, on: bool) {
"⏺ REC".blit(buf, x, y, Some(if on {
Style::default().bold().red()
} else {
Style::default().bold().dim()
}))
}
pub fn draw_dub (buf: &mut Buffer, x: u16, y: u16, on: bool) {
"⏺ DUB".blit(buf, x, y, Some(if on {
Style::default().bold().yellow()
} else {
Style::default().bold().dim()
}))
}
pub fn draw_mon (buf: &mut Buffer, x: u16, y: u16, on: bool) {
"⏺ MON".blit(buf, x, y, Some(if on {
Style::default().bold().green()
} else {
Style::default().bold().dim()
}))
}
pub fn draw_bpm (buf: &mut Buffer, x: u16, y: u16, bpm: usize) {
let style = Style::default().not_dim();
"BPM"
.blit(buf, x, y, Some(style));
format!("{}.{:03}", bpm as usize, bpm % 1)
.blit(buf, x + 4, y, Some(style.bold()));
"SYNC"
.blit(buf, x + 13, y, Some(style));
"4/4"
.blit(buf, x + 18, y, Some(style.bold()));
"QUANT"
.blit(buf, x + 23, y, Some(style));
"1/16"
.blit(buf, x + 29, y, Some(style.bold()));
}