insert duplicate phrase

This commit is contained in:
🪞👃🪞 2024-10-11 10:12:02 +03:00
parent 69a81106fc
commit db2a2efa63
4 changed files with 46 additions and 30 deletions

View file

@ -178,12 +178,12 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
let name = &(phrase as &Arc<RwLock<Phrase>>).read().unwrap().name; let name = &(phrase as &Arc<RwLock<Phrase>>).read().unwrap().name;
let name = format!("{}", name.read().unwrap()); let name = format!("{}", name.read().unwrap());
add(&name.as_str().push_x(1).fixed_x(w))?; add(&name.as_str().push_x(1).fixed_x(w))?;
color = COLOR_BG1; color = (phrase as &Arc<RwLock<Phrase>>).read().unwrap().color;
if let Some(playing_phrase) = &track.player.phrase { //if let Some(playing_phrase) = &track.player.phrase {
if *playing_phrase.read().unwrap() == *phrase.read().unwrap() { //if *playing_phrase.read().unwrap() == *phrase.read().unwrap() {
color = COLOR_PLAYING //color = COLOR_PLAYING
} //}
} //}
}, },
_ => {} _ => {}
}; };

View file

@ -5,6 +5,8 @@ pub(crate) use tek_core::crossterm::event::KeyCode;
pub(crate) use tek_core::midly::{num::u7, live::LiveEvent, MidiMessage}; pub(crate) use tek_core::midly::{num::u7, live::LiveEvent, MidiMessage};
pub(crate) use tek_core::jack::*; pub(crate) use tek_core::jack::*;
pub(crate) use std::sync::{Arc, RwLock}; pub(crate) use std::sync::{Arc, RwLock};
pub(crate) use rand::{thread_rng, prelude::*};
pub(crate) use palette::{*, convert::*, okhsl::*};
submod! { submod! {
arranger arranger_tui arranger arranger_tui

View file

@ -32,7 +32,7 @@ pub struct PhrasePool<E: Engine> {
pub focused: bool, pub focused: bool,
} }
/// A MIDI sequence. /// A MIDI sequence.
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Phrase { pub struct Phrase {
pub uuid: uuid::Uuid, pub uuid: uuid::Uuid,
/// Name of phrase /// Name of phrase
@ -153,30 +153,28 @@ impl<E: Engine> PhraseEditor<E> {
} }
} }
} }
impl Default for Phrase { pub fn random_color () -> Color {
fn default () -> Self { Self::new("", false, 0, None) } let mut rng = thread_rng();
let color: Okhsl<f32> = Okhsl::new(
rng.gen::<f32>() * 360f32 - 180f32,
rng.gen::<f32>() * 0.5 + 0.25,
rng.gen::<f32>() * 0.5 + 0.25,
);
let color: Srgb<f32> = Srgb::from_color_unclamped(color);
Color::Rgb(
(color.red * 255.0) as u8,
(color.green * 255.0) as u8,
(color.blue * 255.0) as u8,
)
} }
impl Phrase { impl Phrase {
pub fn new ( pub fn new (
name: &str, name: &str,
loop_on: bool, loop_on: bool,
length: usize, length: usize,
notes: Option<PhraseData> notes: Option<PhraseData>,
color: Option<Color>,
) -> Self { ) -> Self {
use rand::{thread_rng, prelude::*};
use palette::{*, convert::*, okhsl::*};
let mut rng = thread_rng();
let color: Okhsl<f32> = Okhsl::new(
rng.gen::<f32>() * 360f32 - 180f32,
rng.gen::<f32>() * 0.5 + 0.25,
rng.gen::<f32>() * 0.5 + 0.25,
);
let color: Srgb<f32> = Srgb::from_color_unclamped(color);
let color = Color::Rgb(
(color.red * 255.0) as u8,
(color.green * 255.0) as u8,
(color.blue * 255.0) as u8,
);
Self { Self {
uuid: uuid::Uuid::new_v4(), uuid: uuid::Uuid::new_v4(),
name: Arc::new(RwLock::new(name.into())), name: Arc::new(RwLock::new(name.into())),
@ -187,7 +185,7 @@ impl Phrase {
loop_start: 0, loop_start: 0,
loop_length: length, loop_length: length,
percussive: true, percussive: true,
color color: color.unwrap_or_else(random_color)
} }
} }
pub fn toggle_loop (&mut self) { pub fn toggle_loop (&mut self) {
@ -244,6 +242,9 @@ impl Phrase {
} }
} }
} }
impl Default for Phrase {
fn default () -> Self { Self::new("", false, 0, None, Some(Color::Rgb(0, 0, 0))) }
}
impl std::cmp::PartialEq for Phrase { impl std::cmp::PartialEq for Phrase {
fn eq (&self, other: &Self) -> bool { fn eq (&self, other: &Self) -> bool {
self.uuid == other.uuid self.uuid == other.uuid

View file

@ -83,13 +83,26 @@ impl Handle<Tui> for PhrasePool<Tui> {
key!(KeyCode::Down) => { key!(KeyCode::Down) => {
self.phrase = (self.phrase + 1) % self.phrases.len() self.phrase = (self.phrase + 1) % self.phrases.len()
}, },
key!(KeyCode::Char('i')) => { key!(KeyCode::Char('a')) => { // append new
self.phrases.insert(self.phrase, Arc::new(RwLock::new(Phrase::default()))); let mut phrase = Phrase::default();
phrase.color = random_color();
self.phrases.push(Arc::new(RwLock::new(phrase)));
self.phrase = self.phrases.len() - 1;
},
key!(KeyCode::Char('i')) => { // insert new
let mut phrase = Phrase::default();
phrase.color = random_color();
self.phrases.insert(self.phrase, Arc::new(RwLock::new(phrase)));
self.phrase += 1; self.phrase += 1;
}, },
key!(KeyCode::Char('a')) => { key!(KeyCode::Char('d')) => { // insert duplicate
self.phrases.push(Arc::new(RwLock::new(Phrase::default()))); let phrase = (*self.phrases[self.phrase].read().unwrap()).clone();
self.phrase = self.phrases.len() - 1; self.phrases.insert(self.phrase, Arc::new(RwLock::new(phrase)));
self.phrase += 1;
},
key!(KeyCode::Char('c')) => { // change color
let mut phrase = self.phrases[self.phrase].write().unwrap();
phrase.color = random_color();
}, },
_ => return Ok(None), _ => return Ok(None),
} }