fix timer of horizontal sequencer

This commit is contained in:
🪞👃🪞 2024-07-03 20:03:00 +03:00
parent 2601592d17
commit 1259176576
6 changed files with 293 additions and 254 deletions

View file

@ -11,70 +11,92 @@ pub enum ChainViewMode {
pub struct ChainView<'a> {
pub focused: bool,
pub chain: Option<&'a Track>,
pub track: Option<&'a Track>,
}
impl<'a> Render for ChainView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let style = Some(if self.focused {
Style::default().green()
} else {
Style::default().green().dim()
} else {
Style::default().dim()
});
let (area, _plugins) = if let Some(ref chain) = self.chain {
draw_as_row(&*chain, buf, area, style)?
let Rect { x, y, width, height } = area;
if self.track.is_none() {
let label = "No track selected";
label.blit(
buf,
x + (width - label.len() as u16) / 2,
y + height / 2,
Some(Style::default().dim().bold())
);
}
lozenge_left(buf, x, y, height, style);
let (area, _plugins) = if let Some(ref chain) = self.track {
self.draw_as_row(buf, area, style)?
} else {
(area, vec![])
};
let Rect { x, y, width, height} = area;
lozenge_left(buf, x, y, height, style);
lozenge_right(buf, x + width - 1, y, height, style);
Ok(area)
}
}
pub fn draw_as_row (
state: &Track, buf: &mut Buffer, area: Rect, style: Option<Style>
) -> Usually<(Rect, Vec<Rect>)> {
let Rect { mut x, y, width, height } = area;
let mut h = 3u16;
let mut frames = vec![];
for (i, device) in state.devices.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 frame = device.render(buf, Rect { x: x + x2, y, width, height })?;
lozenge_left(buf, x + x2, y, frame.height, style);
lozenge_right(buf, x + x2 + frame.width - 1, y, frame.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;
impl<'a> ChainView<'a> {
pub fn draw_as_row (
&self, buf: &mut Buffer, area: Rect, style: Option<Style>
) -> Usually<(Rect, Vec<Rect>)> {
let Rect { mut x, y, width, height } = area;
let mut h = 3u16;
let mut frames = vec![];
let track = self.track.as_ref().unwrap();
for (i, device) in track.devices.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 frame = device.render(buf, Rect { x: x + x2, y, width, height })?;
let style = if i == track.device {
Some(if self.focused {
Style::default().green().bold().not_dim()
} else {
Style::default().green().dim()
})
} else {
style
};
lozenge_left(buf, x + x2, y, frame.height, style);
lozenge_right(buf, x + x2 + frame.width - 1, y, frame.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;
}
let label = "[Add device…]";
label.blit(buf, x + 1, area.y + 1, Some(Style::default().dim()));
x = x + 1 + label.len() as u16 + 1;
Ok((Rect { x: area.x, y: area.y, width: x, height: h }, frames))
}
let label = "[Add device…]";
label.blit(buf, x + 1, area.y + 1, Some(Style::default().dim()));
x = x + 1 + label.len() as u16 + 1;
Ok((Rect { x: area.x, y: area.y, width: x, height: h }, frames))
}
pub fn draw_as_column (

View file

@ -17,6 +17,9 @@ pub struct SequencerView<'a> {
pub time_start: usize,
/// Position of cursor within time range
pub time_cursor: usize,
/// Current time
pub now: usize
}
impl<'a> Render for SequencerView<'a> {
@ -27,82 +30,70 @@ impl<'a> Render for SequencerView<'a> {
lozenge_left(buf, x, y, height, style);
lozenge_right(buf, x + width - 1, y, height, style);
}
self::horizontal::draw(
buf,
area,
self.phrase,
self.ppq,
self.time_cursor,
self.time_start,
self.time_zoom,
self.note_cursor,
self.note_start,
Some(if self.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
})
)?;
self.draw_horizontal(buf, area)?;
Ok(area)
}
}
impl<'a> SequencerView<'a> {
fn draw_horizontal (&self, buf: &mut Buffer, area: Rect) -> Usually<()> {
let style = Some(if self.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
});
let notes = &[];
pulse_to_note_length(self.time_zoom)
.blit(buf, area.x, area.y, Some(Style::default().dim()));
self::horizontal::keys(buf, area, self.note_start, notes)?;
if let Some(phrase) = self.phrase {
self::horizontal::timer(buf, area, self.time_start, self.time_zoom, self.now % phrase.length);
self::horizontal::lanes(buf, area, phrase, self.ppq, self.time_zoom, self.time_start, self.note_start);
}
let style = style.unwrap_or_else(||{Style::default().green().not_dim()});
self::horizontal::cursor(buf, area, style, self.time_cursor, self.note_cursor);
Ok(())
}
}
fn pulse_to_note_length (time_z: usize) -> &'static str {
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",
_ => ""
}
}
mod horizontal {
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 {
pub fn timer (buf: &mut Buffer, area: Rect, time0: usize, time_z: usize, now: usize) {
let Rect { x, width, .. } = area;
let offset = 5;
for x in x+offset..x+width-offset {
let step = (time0 + (x-offset) as usize) * time_z;
let next_step = (time0 + (x-offset) as usize + 1) * time_z;
let style = if step <= now && now < next_step {
Style::default().yellow().bold().not_dim()
} else {
Style::default()
});
};
"-".blit(buf, x, area.y, Some(style))
}
}
@ -164,16 +155,19 @@ mod horizontal {
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 (
let (character, mut 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),
(false, false) => (if step % ppq == 0 { "|" } else { "·" }, bw),
};
let y = y + height.saturating_sub(index+2) as u16;
if step > phrase.length {
style = Style::default().gray()
}
character.blit(buf, x, y, Some(style));
}
}