wip: nomralize

This commit is contained in:
okay stopped screaming 2026-03-21 23:29:20 +02:00
parent 35197fb826
commit 244e2b388e
16 changed files with 1880 additions and 1866 deletions

View file

@ -1,7 +1,9 @@
use crate::*;
use ::std::sync::{Arc, RwLock};
use ::tengri::{space::east, color::ItemTheme};
use ::tengri::{draw::*, term::*};
use crate::device::{MidiInput, MidiOutput, AudioInput, AudioOutput};
impl HasJack<'static> for Arrangement { fn jack (&self) -> &Jack<'static> { &self.jack } }
/// Arranger.
///
@ -48,6 +50,27 @@ use crate::device::{MidiInput, MidiOutput, AudioInput, AudioOutput};
#[cfg(feature = "scene")] pub scene_scroll: usize,
}
impl_has!(Jack<'static>: |self: Arrangement| self.jack);
impl_has!(Measure<Tui>: |self: Arrangement| self.size);
impl_has!(Vec<Track>: |self: Arrangement| self.tracks);
impl_has!(Vec<Scene>: |self: Arrangement| self.scenes);
impl_has!(Vec<MidiInput>: |self: Arrangement| self.midi_ins);
impl_has!(Vec<MidiOutput>: |self: Arrangement| self.midi_outs);
impl_has!(Clock: |self: Arrangement| self.clock);
impl_has!(Selection: |self: Arrangement| self.selection);
impl_as_ref_opt!(MidiEditor: |self: Arrangement| self.editor.as_ref());
impl_as_mut_opt!(MidiEditor: |self: Arrangement| self.editor.as_mut());
impl_as_ref_opt!(Track: |self: Arrangement| self.selected_track());
impl_as_mut_opt!(Track: |self: Arrangement| self.selected_track_mut());
impl <T: AsRef<Selection>+AsMut<Selection>> HasSelection for T {}
impl <T: AsRef<Vec<Scene>>+AsMut<Vec<Scene>>> HasScenes for T {}
impl <T: AsRef<Vec<Track>>+AsMut<Vec<Track>>> HasTracks for T {}
impl <T: AsRefOpt<Scene>+AsMutOpt<Scene>+Send+Sync> HasScene for T {}
impl <T: AsRefOpt<Track>+AsMutOpt<Track>+Send+Sync> HasTrack for T {}
impl <T: ScenesView+HasMidiIns+HasMidiOuts+HasTrackScroll+Measured<Tui>> TracksView for T {}
impl <T: TracksView+ScenesView+Send+Sync> ClipsView for T {}
pub trait ClipsView: TracksView + ScenesView {
fn view_scenes_clips <'a> (&'a self)
@ -674,247 +697,277 @@ impl Selection {
}
}
}
impl_has!(Jack<'static>: |self: Arrangement| self.jack);
impl_has!(Measure<Tui>: |self: Arrangement| self.size);
impl_has!(Vec<Track>: |self: Arrangement| self.tracks);
impl_has!(Vec<Scene>: |self: Arrangement| self.scenes);
impl_has!(Vec<MidiInput>: |self: Arrangement| self.midi_ins);
impl_has!(Vec<MidiOutput>: |self: Arrangement| self.midi_outs);
impl_has!(Clock: |self: Arrangement| self.clock);
impl_has!(Selection: |self: Arrangement| self.selection);
impl_as_ref_opt!(MidiEditor: |self: Arrangement| self.editor.as_ref());
impl_as_mut_opt!(MidiEditor: |self: Arrangement| self.editor.as_mut());
impl_as_ref_opt!(Track: |self: Arrangement| self.selected_track());
impl_as_mut_opt!(Track: |self: Arrangement| self.selected_track_mut());
impl Arrangement {
/// Create a new arrangement.
pub fn new (
jack: &Jack<'static>,
name: Option<Arc<str>>,
clock: Clock,
tracks: Vec<Track>,
scenes: Vec<Scene>,
midi_ins: Vec<MidiInput>,
midi_outs: Vec<MidiOutput>,
) -> Self {
Self {
clock, tracks, scenes, midi_ins, midi_outs,
jack: jack.clone(),
name: name.unwrap_or_default(),
color: ItemTheme::random(),
selection: Selection::TrackClip { track: 0, scene: 0 },
..Default::default()
impl Arrangement {
/// Create a new arrangement.
pub fn new (
jack: &Jack<'static>,
name: Option<Arc<str>>,
clock: Clock,
tracks: Vec<Track>,
scenes: Vec<Scene>,
midi_ins: Vec<MidiInput>,
midi_outs: Vec<MidiOutput>,
) -> Self {
Self {
clock, tracks, scenes, midi_ins, midi_outs,
jack: jack.clone(),
name: name.unwrap_or_default(),
color: ItemTheme::random(),
selection: Selection::TrackClip { track: 0, scene: 0 },
..Default::default()
}
}
/// Width of display
pub fn w (&self) -> u16 {
self.size.w() as u16
}
/// Width allocated for sidebar.
pub fn w_sidebar (&self, is_editing: bool) -> u16 {
self.w() / if is_editing { 16 } else { 8 } as u16
}
/// Width available to display tracks.
pub fn w_tracks_area (&self, is_editing: bool) -> u16 {
self.w().saturating_sub(self.w_sidebar(is_editing))
}
/// Height of display
pub fn h (&self) -> u16 {
self.size.h() as u16
}
/// Height taken by visible device slots.
pub fn h_devices (&self) -> u16 {
2
//1 + self.devices_with_sizes().last().map(|(_, _, _, _, y)|y as u16).unwrap_or(0)
}
/// Add multiple tracks
#[cfg(feature = "track")] pub fn tracks_add (
&mut self,
count: usize, width: Option<usize>,
mins: &[Connect], mouts: &[Connect],
) -> Usually<()> {
let track_color_1 = ItemColor::random();
let track_color_2 = ItemColor::random();
for i in 0..count {
let color = track_color_1.mix(track_color_2, i as f32 / count as f32).into();
let track = self.track_add(None, Some(color), mins, mouts)?.1;
if let Some(width) = width {
track.width = width;
}
}
/// Width of display
pub fn w (&self) -> u16 {
self.size.w() as u16
Ok(())
}
/// Add a track
#[cfg(feature = "track")] pub fn track_add (
&mut self,
name: Option<&str>, color: Option<ItemTheme>,
mins: &[Connect], mouts: &[Connect],
) -> Usually<(usize, &mut Track)> {
let name: Arc<str> = name.map_or_else(
||format!("trk{:02}", self.track_last).into(),
|x|x.to_string().into()
);
self.track_last += 1;
let track = Track {
width: (name.len() + 2).max(12),
color: color.unwrap_or_else(ItemTheme::random),
sequencer: Sequencer::new(
&format!("{name}"),
self.jack(),
Some(self.clock()),
None,
mins,
mouts
)?,
name,
..Default::default()
};
self.tracks_mut().push(track);
let len = self.tracks().len();
let index = len - 1;
for scene in self.scenes_mut().iter_mut() {
while scene.clips.len() < len {
scene.clips.push(None);
}
}
/// Width allocated for sidebar.
pub fn w_sidebar (&self, is_editing: bool) -> u16 {
self.w() / if is_editing { 16 } else { 8 } as u16
}
/// Width available to display tracks.
pub fn w_tracks_area (&self, is_editing: bool) -> u16 {
self.w().saturating_sub(self.w_sidebar(is_editing))
}
/// Height of display
pub fn h (&self) -> u16 {
self.size.h() as u16
}
/// Height taken by visible device slots.
pub fn h_devices (&self) -> u16 {
2
//1 + self.devices_with_sizes().last().map(|(_, _, _, _, y)|y as u16).unwrap_or(0)
}
/// Add multiple tracks
#[cfg(feature = "track")] pub fn tracks_add (
&mut self,
count: usize, width: Option<usize>,
mins: &[Connect], mouts: &[Connect],
) -> Usually<()> {
let track_color_1 = ItemColor::random();
let track_color_2 = ItemColor::random();
for i in 0..count {
let color = track_color_1.mix(track_color_2, i as f32 / count as f32).into();
let track = self.track_add(None, Some(color), mins, mouts)?.1;
if let Some(width) = width {
track.width = width;
Ok((index, &mut self.tracks_mut()[index]))
}
#[cfg(feature = "track")] pub fn view_inputs (&self, _theme: ItemTheme) -> impl Draw<Tui> + '_ {
south(
h_exact(1, self.view_inputs_header()),
Thunk::new(|to: &mut Tui|{
for (index, port) in self.midi_ins().iter().enumerate() {
to.place(&x_push(index as u16 * 10, h_exact(1, self.view_inputs_row(port))))
}
}
Ok(())
}
/// Add a track
#[cfg(feature = "track")] pub fn track_add (
&mut self,
name: Option<&str>, color: Option<ItemTheme>,
mins: &[Connect], mouts: &[Connect],
) -> Usually<(usize, &mut Track)> {
let name: Arc<str> = name.map_or_else(
||format!("trk{:02}", self.track_last).into(),
|x|x.to_string().into()
);
self.track_last += 1;
let track = Track {
width: (name.len() + 2).max(12),
color: color.unwrap_or_else(ItemTheme::random),
sequencer: Sequencer::new(
&format!("{name}"),
self.jack(),
Some(self.clock()),
None,
mins,
mouts
)?,
name,
..Default::default()
};
self.tracks_mut().push(track);
let len = self.tracks().len();
let index = len - 1;
for scene in self.scenes_mut().iter_mut() {
while scene.clips.len() < len {
scene.clips.push(None);
}
}
Ok((index, &mut self.tracks_mut()[index]))
}
#[cfg(feature = "track")] pub fn view_inputs (&self, _theme: ItemTheme) -> impl Draw<Tui> + '_ {
south(
h_exact(1, self.view_inputs_header()),
Thunk::new(|to: &mut Tui|{
for (index, port) in self.midi_ins().iter().enumerate() {
to.place(&x_push(index as u16 * 10, h_exact(1, self.view_inputs_row(port))))
}
})
)
}
#[cfg(feature = "track")] fn view_inputs_header (&self) -> impl Draw<Tui> + '_ {
east(w_exact(20, origin_w(button_3("i", "nput ", format!("{}", self.midi_ins.len()), false))),
west(w_exact(4, button_2("I", "+", false)), Thunk::new(move|to: &mut Tui|for (_index, track, x1, _x2) in self.tracks_with_sizes() {
#[cfg(feature = "track")]
to.place(&x_push(x1 as u16, Tui::bg(track.color.dark.rgb, origin_w(w_exact(track.width as u16, east!(
either(track.sequencer.monitoring, Tui::fg(Green, "mon "), "mon "),
either(track.sequencer.recording, Tui::fg(Red, "rec "), "rec "),
either(track.sequencer.overdub, Tui::fg(Yellow, "dub "), "dub "),
))))))
})))
}
#[cfg(feature = "track")] fn view_inputs_row (&self, port: &MidiInput) -> impl Draw<Tui> {
east(w_exact(20, origin_w(east("", Tui::bold(true, Tui::fg(Rgb(255,255,255), port.port_name()))))),
west(w_exact(4, ()), Thunk::new(move|to: &mut Tui|for (_index, track, _x1, _x2) in self.tracks_with_sizes() {
#[cfg(feature = "track")]
to.place(&Tui::bg(track.color.darker.rgb, origin_w(w_exact(track.width as u16, east!(
either(track.sequencer.monitoring, Tui::fg(Green, ""), " · "),
either(track.sequencer.recording, Tui::fg(Red, ""), " · "),
either(track.sequencer.overdub, Tui::fg(Yellow, ""), " · "),
)))))
})))
}
#[cfg(feature = "track")] pub fn view_outputs (&self, theme: ItemTheme) -> impl Draw<Tui> {
let mut h = 1;
for output in self.midi_outs().iter() {
h += 1 + output.connections.len();
}
let h = h as u16;
let list = south(
h_exact(1, w_full(origin_w(button_3("o", "utput", format!("{}", self.midi_outs.len()), false)))),
h_exact(h - 1, wh_full(origin_nw(Thunk::new(|to: &mut Tui|{
for (_index, port) in self.midi_outs().iter().enumerate() {
to.place(&h_exact(1,w_full(east(
origin_w(east("", Tui::fg(Rgb(255,255,255),Tui::bold(true, port.port_name())))),
w_full(origin_e(format!("{}/{} ",
port.port().get_connections().len(),
port.connections.len())))))));
for (index, conn) in port.connections.iter().enumerate() {
to.place(&h_exact(1, w_full(origin_w(format!(" c{index:02}{}", conn.info())))));
}
}
})))));
h_exact(h, view_track_row_section(theme, list, button_2("O", "+", false),
Tui::bg(theme.darker.rgb, origin_w(w_full(
Thunk::new(|to: &mut Tui|{
for (index, track, _x1, _x2) in self.tracks_with_sizes() {
to.place(&w_exact(track_width(index, track),
Thunk::new(|to: &mut Tui|{
to.place(&h_exact(1, origin_w(east(
either(true, Tui::fg(Green, "play "), "play "),
either(false, Tui::fg(Yellow, "solo "), "solo "),
))));
for (_index, port) in self.midi_outs().iter().enumerate() {
to.place(&h_exact(1, origin_w(east(
either(true, Tui::fg(Green, ""), " · "),
either(false, Tui::fg(Yellow, ""), " · "),
))));
for (_index, _conn) in port.connections.iter().enumerate() {
to.place(&h_exact(1, w_full("")));
}
}})))}}))))))
}
#[cfg(feature = "track")] pub fn view_track_devices (&self, theme: ItemTheme) -> impl Draw<Tui> {
let mut h = 2u16;
for track in self.tracks().iter() {
h = h.max(track.devices.len() as u16 * 2);
}
view_track_row_section(theme,
button_3("d", "evice", format!("{}", self.track().map(|t|t.devices.len()).unwrap_or(0)), false),
button_2("D", "+", false),
Thunk::new(move|to: &mut Tui|for (index, track, _x1, _x2) in self.tracks_with_sizes() {
to.place(&wh_exact(track_width(index, track), h + 1,
Tui::bg(track.color.dark.rgb, origin_nw(iter_south(2, move||0..h,
|_, _index|wh_exact(track.width as u16, 2,
Tui::fg_bg(
ItemTheme::G[32].lightest.rgb,
ItemTheme::G[32].dark.rgb,
origin_nw(format!(" · {}", "--")))))))));
}))
}
/// Put a clip in a slot
#[cfg(feature = "clip")] pub fn clip_put (
&mut self, track: usize, scene: usize, clip: Option<Arc<RwLock<MidiClip>>>
) -> Option<Arc<RwLock<MidiClip>>> {
let old = self.scenes[scene].clips[track].clone();
self.scenes[scene].clips[track] = clip;
old
}
/// Change the color of a clip, returning the previous one
#[cfg(feature = "clip")] pub fn clip_set_color (&self, track: usize, scene: usize, color: ItemTheme)
-> Option<ItemTheme>
{
self.scenes[scene].clips[track].as_ref().map(|clip|{
let mut clip = clip.write().unwrap();
let old = clip.color.clone();
clip.color = color.clone();
panic!("{color:?} {old:?}");
//old
})
)
}
#[cfg(feature = "track")] fn view_inputs_header (&self) -> impl Draw<Tui> + '_ {
east(w_exact(20, origin_w(button_3("i", "nput ", format!("{}", self.midi_ins.len()), false))),
west(w_exact(4, button_2("I", "+", false)), Thunk::new(move|to: &mut Tui|for (_index, track, x1, _x2) in self.tracks_with_sizes() {
#[cfg(feature = "track")]
to.place(&x_push(x1 as u16, Tui::bg(track.color.dark.rgb, origin_w(w_exact(track.width as u16, east!(
either(track.sequencer.monitoring, Tui::fg(Green, "mon "), "mon "),
either(track.sequencer.recording, Tui::fg(Red, "rec "), "rec "),
either(track.sequencer.overdub, Tui::fg(Yellow, "dub "), "dub "),
))))))
})))
}
#[cfg(feature = "track")] fn view_inputs_row (&self, port: &MidiInput) -> impl Draw<Tui> {
east(w_exact(20, origin_w(east("", Tui::bold(true, Tui::fg(Rgb(255,255,255), port.port_name()))))),
west(w_exact(4, ()), Thunk::new(move|to: &mut Tui|for (_index, track, _x1, _x2) in self.tracks_with_sizes() {
#[cfg(feature = "track")]
to.place(&Tui::bg(track.color.darker.rgb, origin_w(w_exact(track.width as u16, east!(
either(track.sequencer.monitoring, Tui::fg(Green, ""), " · "),
either(track.sequencer.recording, Tui::fg(Red, ""), " · "),
either(track.sequencer.overdub, Tui::fg(Yellow, ""), " · "),
)))))
})))
}
#[cfg(feature = "track")] pub fn view_outputs (&self, theme: ItemTheme) -> impl Draw<Tui> {
let mut h = 1;
for output in self.midi_outs().iter() {
h += 1 + output.connections.len();
}
/// Toggle looping for the active clip
#[cfg(feature = "clip")] pub fn toggle_loop (&mut self) {
if let Some(clip) = self.selected_clip() {
clip.write().unwrap().toggle_loop()
}
let h = h as u16;
let list = south(
h_exact(1, w_full(origin_w(button_3("o", "utput", format!("{}", self.midi_outs.len()), false)))),
h_exact(h - 1, wh_full(origin_nw(Thunk::new(|to: &mut Tui|{
for (_index, port) in self.midi_outs().iter().enumerate() {
to.place(&h_exact(1,w_full(east(
origin_w(east("", Tui::fg(Rgb(255,255,255),Tui::bold(true, port.port_name())))),
w_full(origin_e(format!("{}/{} ",
port.port().get_connections().len(),
port.connections.len())))))));
for (index, conn) in port.connections.iter().enumerate() {
to.place(&h_exact(1, w_full(origin_w(format!(" c{index:02}{}", conn.info())))));
}
}
})))));
h_exact(h, view_track_row_section(theme, list, button_2("O", "+", false),
Tui::bg(theme.darker.rgb, origin_w(w_full(
Thunk::new(|to: &mut Tui|{
for (index, track, _x1, _x2) in self.tracks_with_sizes() {
to.place(&w_exact(track_width(index, track),
Thunk::new(|to: &mut Tui|{
to.place(&h_exact(1, origin_w(east(
either(true, Tui::fg(Green, "play "), "play "),
either(false, Tui::fg(Yellow, "solo "), "solo "),
))));
for (_index, port) in self.midi_outs().iter().enumerate() {
to.place(&h_exact(1, origin_w(east(
either(true, Tui::fg(Green, ""), " · "),
either(false, Tui::fg(Yellow, ""), " · "),
))));
for (_index, _conn) in port.connections.iter().enumerate() {
to.place(&h_exact(1, w_full("")));
}
}})))}}))))))
}
#[cfg(feature = "track")] pub fn view_track_devices (&self, theme: ItemTheme) -> impl Draw<Tui> {
let mut h = 2u16;
for track in self.tracks().iter() {
h = h.max(track.devices.len() as u16 * 2);
}
/// Get the first sampler of the active track
#[cfg(feature = "sampler")] pub fn sampler (&self) -> Option<&Sampler> {
self.selected_track()?.sampler(0)
}
/// Get the first sampler of the active track
#[cfg(feature = "sampler")] pub fn sampler_mut (&mut self) -> Option<&mut Sampler> {
self.selected_track_mut()?.sampler_mut(0)
view_track_row_section(theme,
button_3("d", "evice", format!("{}", self.track().map(|t|t.devices.len()).unwrap_or(0)), false),
button_2("D", "+", false),
Thunk::new(move|to: &mut Tui|for (index, track, _x1, _x2) in self.tracks_with_sizes() {
to.place(&wh_exact(track_width(index, track), h + 1,
Tui::bg(track.color.dark.rgb, origin_nw(iter_south(2, move||0..h,
|_, _index|wh_exact(track.width as u16, 2,
Tui::fg_bg(
ItemTheme::G[32].lightest.rgb,
ItemTheme::G[32].dark.rgb,
origin_nw(format!(" · {}", "--")))))))));
}))
}
/// Put a clip in a slot
#[cfg(feature = "clip")] pub fn clip_put (
&mut self, track: usize, scene: usize, clip: Option<Arc<RwLock<MidiClip>>>
) -> Option<Arc<RwLock<MidiClip>>> {
let old = self.scenes[scene].clips[track].clone();
self.scenes[scene].clips[track] = clip;
old
}
/// Change the color of a clip, returning the previous one
#[cfg(feature = "clip")] pub fn clip_set_color (&self, track: usize, scene: usize, color: ItemTheme)
-> Option<ItemTheme>
{
self.scenes[scene].clips[track].as_ref().map(|clip|{
let mut clip = clip.write().unwrap();
let old = clip.color.clone();
clip.color = color.clone();
panic!("{color:?} {old:?}");
//old
})
}
/// Toggle looping for the active clip
#[cfg(feature = "clip")] pub fn toggle_loop (&mut self) {
if let Some(clip) = self.selected_clip() {
clip.write().unwrap().toggle_loop()
}
}
impl ScenesView for Arrangement {
fn h_scenes (&self) -> u16 {
(self.measure_height() as u16).saturating_sub(20)
}
fn w_side (&self) -> u16 {
(self.measure_width() as u16 * 2 / 10).max(20)
}
fn w_mid (&self) -> u16 {
(self.measure_width() as u16).saturating_sub(2 * self.w_side()).max(40)
}
/// Get the first sampler of the active track
#[cfg(feature = "sampler")] pub fn sampler (&self) -> Option<&Sampler> {
self.selected_track()?.sampler(0)
}
impl HasClipsSize for Arrangement {
fn clips_size (&self) -> &Measure<Tui> { &self.size_inner }
/// Get the first sampler of the active track
#[cfg(feature = "sampler")] pub fn sampler_mut (&mut self) -> Option<&mut Sampler> {
self.selected_track_mut()?.sampler_mut(0)
}
}
impl ScenesView for Arrangement {
fn h_scenes (&self) -> u16 {
(self.measure_height() as u16).saturating_sub(20)
}
fn w_side (&self) -> u16 {
(self.measure_width() as u16 * 2 / 10).max(20)
}
fn w_mid (&self) -> u16 {
(self.measure_width() as u16).saturating_sub(2 * self.w_side()).max(40)
}
}
impl HasClipsSize for Arrangement {
fn clips_size (&self) -> &Measure<Tui> { &self.size_inner }
}
pub type SceneWith<'a, T> =
(usize, &'a Scene, usize, usize, T);
def_command!(SceneCommand: |scene: Scene| {
SetSize { size: usize } => { todo!() },
SetZoom { size: usize } => { todo!() },
SetName { name: Arc<str> } =>
swap_value(&mut scene.name, name, |name|Self::SetName{name}),
SetColor { color: ItemTheme } =>
swap_value(&mut scene.color, color, |color|Self::SetColor{color}),
});
def_command!(TrackCommand: |track: Track| {
Stop => { track.sequencer.enqueue_next(None); Ok(None) },
SetMute { mute: Option<bool> } => todo!(),
SetSolo { solo: Option<bool> } => todo!(),
SetSize { size: usize } => todo!(),
SetZoom { zoom: usize } => todo!(),
SetName { name: Arc<str> } =>
swap_value(&mut track.name, name, |name|Self::SetName { name }),
SetColor { color: ItemTheme } =>
swap_value(&mut track.color, color, |color|Self::SetColor { color }),
SetRec { rec: Option<bool> } =>
toggle_bool(&mut track.sequencer.recording, rec, |rec|Self::SetRec { rec }),
SetMon { mon: Option<bool> } =>
toggle_bool(&mut track.sequencer.monitoring, mon, |mon|Self::SetMon { mon }),
});
def_command!(ClipCommand: |clip: MidiClip| {
SetColor { color: Option<ItemTheme> } => {
//(SetColor [t: usize, s: usize, c: ItemTheme]
//clip.clip_set_color(t, s, c).map(|o|Self::SetColor(t, s, o)))));
//("color" [a: usize, b: usize] Some(Self::SetColor(a.unwrap(), b.unwrap(), ItemTheme::random())))
todo!()
},
SetLoop { looping: Option<bool> } => {
//(SetLoop [t: usize, s: usize, l: bool] cmd_todo!("\n\rtodo: {self:?}"))
//("loop" [a: usize, b: usize, c: bool] Some(Self::SetLoop(a.unwrap(), b.unwrap(), c.unwrap())))
todo!()
}
});