P2a: eight-bar arrangement with four editable sections
This commit is contained in:
+198
-23
@@ -126,7 +126,7 @@ pub struct AudioCore {
|
||||
/// Immutable material view shared with the audio thread.
|
||||
#[derive(Clone)]
|
||||
pub struct MaterialSnapshot {
|
||||
pub pattern: Pattern,
|
||||
pub sections: Vec<Pattern>,
|
||||
pub cores: Vec<AudioCore>,
|
||||
}
|
||||
|
||||
@@ -137,7 +137,8 @@ pub struct ProjectData {
|
||||
pub revision: u64,
|
||||
pub bpm: f32,
|
||||
pub gains: [f32; VEIN_COUNT],
|
||||
pub pattern: Pattern,
|
||||
pub sections: Vec<Pattern>,
|
||||
pub active_section: u8,
|
||||
pub cores: Vec<Core>,
|
||||
pub journal: Vec<JournalEntry>,
|
||||
pub seq: u64,
|
||||
@@ -155,11 +156,13 @@ pub struct CoreEdit {
|
||||
pub struct ProjectEdit {
|
||||
pub bpm: f32,
|
||||
pub gains: [f32; VEIN_COUNT],
|
||||
pub pattern: Pattern,
|
||||
pub sections: Vec<Pattern>,
|
||||
pub active_section: u8,
|
||||
pub cores: Vec<CoreEdit>,
|
||||
}
|
||||
|
||||
const MAX_HISTORY: usize = 64;
|
||||
pub const ARRANGEMENT_SECTIONS: usize = 4;
|
||||
|
||||
/// The audio engine as seen by the session. Implementations must be real-time
|
||||
/// safe on the audio thread and non-blocking on these methods.
|
||||
@@ -191,6 +194,8 @@ pub enum CoreError {
|
||||
UnknownFace,
|
||||
#[error("face boundaries must be sixteen ordered cuts inside the core")]
|
||||
InvalidFaces,
|
||||
#[error("section must be 0..3")]
|
||||
InvalidSection,
|
||||
#[error("this command only exists on the binary import path")]
|
||||
UnsupportedCommand,
|
||||
}
|
||||
@@ -229,7 +234,8 @@ pub struct Session {
|
||||
bpm: f32,
|
||||
playing: bool,
|
||||
gains: [f32; VEIN_COUNT],
|
||||
pattern: Pattern,
|
||||
sections: Vec<Pattern>,
|
||||
active_section: u8,
|
||||
cores: Vec<Core>,
|
||||
journal: Vec<JournalEntry>,
|
||||
project_name: String,
|
||||
@@ -250,7 +256,8 @@ impl Session {
|
||||
bpm: 128.0,
|
||||
playing: false,
|
||||
gains: INITIAL_GAINS,
|
||||
pattern,
|
||||
sections: vec![pattern; ARRANGEMENT_SECTIONS],
|
||||
active_section: 0,
|
||||
cores: Vec::new(),
|
||||
journal: Vec::new(),
|
||||
project_name: "descent-04".into(),
|
||||
@@ -269,7 +276,8 @@ impl Session {
|
||||
bpm: data.bpm,
|
||||
playing: false,
|
||||
gains: data.gains,
|
||||
pattern: data.pattern,
|
||||
sections: data.sections,
|
||||
active_section: data.active_section,
|
||||
cores: data.cores,
|
||||
journal: data.journal,
|
||||
project_name: data.name,
|
||||
@@ -315,7 +323,11 @@ impl Session {
|
||||
}
|
||||
|
||||
fn sanitize_faces(&mut self) {
|
||||
for row in &mut self.pattern.rows {
|
||||
for row in self
|
||||
.sections
|
||||
.iter_mut()
|
||||
.flat_map(|section| &mut section.rows)
|
||||
{
|
||||
let Some(face) = &row.face else { continue };
|
||||
let exists = self.cores.iter().any(|core| {
|
||||
core.summary.id == face.core
|
||||
@@ -334,7 +346,8 @@ impl Session {
|
||||
ProjectEdit {
|
||||
bpm: self.bpm,
|
||||
gains: self.gains,
|
||||
pattern: self.pattern.clone(),
|
||||
sections: self.sections.clone(),
|
||||
active_section: self.active_section,
|
||||
cores: self
|
||||
.cores
|
||||
.iter()
|
||||
@@ -357,7 +370,8 @@ impl Session {
|
||||
fn apply_edit(&mut self, edit: ProjectEdit) {
|
||||
self.bpm = edit.bpm;
|
||||
self.gains = edit.gains;
|
||||
self.pattern = edit.pattern;
|
||||
self.sections = edit.sections;
|
||||
self.active_section = edit.active_section;
|
||||
for core_edit in &edit.cores {
|
||||
if let Some(core) = self
|
||||
.cores
|
||||
@@ -381,7 +395,15 @@ impl Session {
|
||||
}
|
||||
|
||||
pub fn pattern(&self) -> &Pattern {
|
||||
&self.pattern
|
||||
&self.sections[self.active_section as usize]
|
||||
}
|
||||
|
||||
pub fn sections(&self) -> &[Pattern] {
|
||||
&self.sections
|
||||
}
|
||||
|
||||
pub fn active_section(&self) -> u8 {
|
||||
self.active_section
|
||||
}
|
||||
|
||||
pub fn journal(&self) -> &[JournalEntry] {
|
||||
@@ -399,7 +421,7 @@ impl Session {
|
||||
playing: self.playing,
|
||||
bpm: self.bpm,
|
||||
bar: meter.bar,
|
||||
loop_bars: LOOP_BARS,
|
||||
loop_bars: self.sections.len().max(1) as f32 * LOOP_BARS,
|
||||
},
|
||||
veins: VeinId::ALL
|
||||
.iter()
|
||||
@@ -410,7 +432,9 @@ impl Session {
|
||||
gain: self.gains[index],
|
||||
})
|
||||
.collect(),
|
||||
pattern: self.pattern.clone(),
|
||||
pattern: self.pattern().clone(),
|
||||
arrangement: self.sections.clone(),
|
||||
active_section: self.active_section,
|
||||
cores: self.cores.iter().map(|core| core.summary.clone()).collect(),
|
||||
audio: self.engine.info(),
|
||||
journal_len: self.journal.len(),
|
||||
@@ -560,6 +584,9 @@ impl Session {
|
||||
Command::SetCoreFaces { core, boundaries } => {
|
||||
self.set_core_faces(&core, &boundaries, actor)
|
||||
}
|
||||
Command::SelectSection { index } => self.select_section(index, actor),
|
||||
Command::ClearSection { index } => self.clear_section(index, actor),
|
||||
Command::CopySection { from, to } => self.copy_section(from, to, actor),
|
||||
Command::Undo => {
|
||||
let Some(edit) = self.history.pop() else {
|
||||
return Ok((
|
||||
@@ -616,7 +643,7 @@ impl Session {
|
||||
|
||||
pub fn material_snapshot(&self) -> MaterialSnapshot {
|
||||
MaterialSnapshot {
|
||||
pattern: self.pattern.clone(),
|
||||
sections: self.sections.clone(),
|
||||
cores: self
|
||||
.cores
|
||||
.iter()
|
||||
@@ -631,6 +658,109 @@ impl Session {
|
||||
}
|
||||
|
||||
/// Called by the daemon after the binary import and the analysis.
|
||||
pub fn select_section(
|
||||
&mut self,
|
||||
index: u8,
|
||||
actor: Actor,
|
||||
) -> Result<(CommandResult, Vec<Event>), CoreError> {
|
||||
if index as usize >= self.sections.len() {
|
||||
return Err(CoreError::InvalidSection);
|
||||
}
|
||||
if index == self.active_section {
|
||||
return Ok((
|
||||
CommandResult::State {
|
||||
state: self.snapshot(),
|
||||
},
|
||||
Vec::new(),
|
||||
));
|
||||
}
|
||||
self.active_section = index;
|
||||
self.revision += 1;
|
||||
self.record(actor, Command::SelectSection { index });
|
||||
Ok((
|
||||
CommandResult::State {
|
||||
state: self.snapshot(),
|
||||
},
|
||||
vec![Event::Arrangement {
|
||||
sections: self.sections.clone(),
|
||||
active: index,
|
||||
}],
|
||||
))
|
||||
}
|
||||
|
||||
pub fn clear_section(
|
||||
&mut self,
|
||||
index: u8,
|
||||
actor: Actor,
|
||||
) -> Result<(CommandResult, Vec<Event>), CoreError> {
|
||||
if index as usize >= self.sections.len() {
|
||||
return Err(CoreError::InvalidSection);
|
||||
}
|
||||
if self.sections[index as usize]
|
||||
.rows
|
||||
.iter()
|
||||
.all(|row| row.velocities.iter().all(|velocity| *velocity == 0) && row.face.is_none())
|
||||
{
|
||||
return Ok((
|
||||
CommandResult::State {
|
||||
state: self.snapshot(),
|
||||
},
|
||||
Vec::new(),
|
||||
));
|
||||
}
|
||||
self.push_history();
|
||||
for row in &mut self.sections[index as usize].rows {
|
||||
row.velocities.fill(0);
|
||||
row.face = None;
|
||||
}
|
||||
self.revision += 1;
|
||||
self.record(actor, Command::ClearSection { index });
|
||||
self.sync_material();
|
||||
Ok((
|
||||
CommandResult::State {
|
||||
state: self.snapshot(),
|
||||
},
|
||||
vec![Event::Arrangement {
|
||||
sections: self.sections.clone(),
|
||||
active: self.active_section,
|
||||
}],
|
||||
))
|
||||
}
|
||||
|
||||
pub fn copy_section(
|
||||
&mut self,
|
||||
from: u8,
|
||||
to: u8,
|
||||
actor: Actor,
|
||||
) -> Result<(CommandResult, Vec<Event>), CoreError> {
|
||||
if from as usize >= self.sections.len() || to as usize >= self.sections.len() {
|
||||
return Err(CoreError::InvalidSection);
|
||||
}
|
||||
if from == to || self.sections[from as usize] == self.sections[to as usize] {
|
||||
return Ok((
|
||||
CommandResult::State {
|
||||
state: self.snapshot(),
|
||||
},
|
||||
Vec::new(),
|
||||
));
|
||||
}
|
||||
self.push_history();
|
||||
let source = self.sections[from as usize].clone();
|
||||
self.sections[to as usize] = source;
|
||||
self.revision += 1;
|
||||
self.record(actor, Command::CopySection { from, to });
|
||||
self.sync_material();
|
||||
Ok((
|
||||
CommandResult::State {
|
||||
state: self.snapshot(),
|
||||
},
|
||||
vec![Event::Arrangement {
|
||||
sections: self.sections.clone(),
|
||||
active: self.active_section,
|
||||
}],
|
||||
))
|
||||
}
|
||||
|
||||
pub fn set_core_faces(
|
||||
&mut self,
|
||||
core_id: &str,
|
||||
@@ -703,15 +833,15 @@ impl Session {
|
||||
velocity: u8,
|
||||
actor: Actor,
|
||||
) -> Result<(CommandResult, Vec<Event>), CoreError> {
|
||||
if row as usize >= self.pattern.rows.len() {
|
||||
let section = self.active_section as usize;
|
||||
if row as usize >= self.sections[section].rows.len() {
|
||||
return Err(CoreError::InvalidRow);
|
||||
}
|
||||
if step as usize >= LOOP_STEPS {
|
||||
return Err(CoreError::InvalidStep);
|
||||
}
|
||||
let velocity = velocity.min(127);
|
||||
let unchanged = self
|
||||
.pattern
|
||||
let unchanged = self.sections[section]
|
||||
.rows
|
||||
.get(row as usize)
|
||||
.is_some_and(|row| row.velocities[step as usize] == velocity);
|
||||
@@ -724,7 +854,7 @@ impl Session {
|
||||
));
|
||||
}
|
||||
self.push_history();
|
||||
let Some(row_ref) = self.pattern.rows.get_mut(row as usize) else {
|
||||
let Some(row_ref) = self.sections[section].rows.get_mut(row as usize) else {
|
||||
return Err(CoreError::InvalidRow);
|
||||
};
|
||||
row_ref.velocities[step as usize] = velocity;
|
||||
@@ -757,7 +887,8 @@ impl Session {
|
||||
face: u8,
|
||||
actor: Actor,
|
||||
) -> Result<(CommandResult, Vec<Event>), CoreError> {
|
||||
if row as usize >= self.pattern.rows.len() {
|
||||
let section = self.active_section as usize;
|
||||
if row as usize >= self.sections[section].rows.len() {
|
||||
return Err(CoreError::InvalidRow);
|
||||
}
|
||||
self.find_face(core, face)?;
|
||||
@@ -765,8 +896,7 @@ impl Session {
|
||||
core: core.to_string(),
|
||||
face,
|
||||
};
|
||||
let unchanged = self
|
||||
.pattern
|
||||
let unchanged = self.sections[section]
|
||||
.rows
|
||||
.get(row as usize)
|
||||
.and_then(|row| row.face.as_ref())
|
||||
@@ -780,7 +910,7 @@ impl Session {
|
||||
));
|
||||
}
|
||||
self.push_history();
|
||||
let Some(row_ref) = self.pattern.rows.get_mut(row as usize) else {
|
||||
let Some(row_ref) = self.sections[section].rows.get_mut(row as usize) else {
|
||||
return Err(CoreError::InvalidRow);
|
||||
};
|
||||
row_ref.face = Some(reference);
|
||||
@@ -1119,7 +1249,7 @@ mod tests {
|
||||
let (session, _) = session();
|
||||
let state = session.snapshot();
|
||||
assert_eq!(state.transport.bar, 2.25);
|
||||
assert_eq!(state.transport.loop_bars, LOOP_BARS);
|
||||
assert_eq!(state.transport.loop_bars, 8.0);
|
||||
assert_eq!(state.veins.len(), VEIN_COUNT);
|
||||
assert_eq!(state.pattern.rows.len(), VEIN_COUNT);
|
||||
assert_eq!(state.audio.device, "test");
|
||||
@@ -1233,7 +1363,8 @@ mod tests {
|
||||
revision: 7,
|
||||
bpm: 120.0,
|
||||
gains: [0.5; VEIN_COUNT],
|
||||
pattern: default_pattern(),
|
||||
sections: vec![default_pattern(); ARRANGEMENT_SECTIONS],
|
||||
active_section: 0,
|
||||
cores: vec![test_core()],
|
||||
journal: Vec::new(),
|
||||
seq: 0,
|
||||
@@ -1251,6 +1382,50 @@ mod tests {
|
||||
assert!(!state.can_undo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrangement_sections_are_independent() {
|
||||
let (mut session, engine) = session();
|
||||
engine.clear();
|
||||
let state = session.snapshot();
|
||||
assert_eq!(state.arrangement.len(), ARRANGEMENT_SECTIONS);
|
||||
assert_eq!(state.active_section, 0);
|
||||
assert_eq!(state.transport.loop_bars, 8.0);
|
||||
|
||||
session
|
||||
.apply(Command::SelectSection { index: 1 }, Actor::Hand)
|
||||
.unwrap();
|
||||
assert_eq!(session.active_section(), 1);
|
||||
session.set_pattern_step(0, 0, 0, Actor::Hand).unwrap();
|
||||
let state = session.snapshot();
|
||||
assert_eq!(state.pattern.rows[0].velocities[0], 0);
|
||||
assert_eq!(state.arrangement[0].rows[0].velocities[0], 112);
|
||||
assert_eq!(state.arrangement[1].rows[0].velocities[0], 0);
|
||||
|
||||
session
|
||||
.apply(Command::CopySection { from: 0, to: 2 }, Actor::Hand)
|
||||
.unwrap();
|
||||
let state = session.snapshot();
|
||||
assert_eq!(state.arrangement[2], state.arrangement[0]);
|
||||
session
|
||||
.apply(Command::ClearSection { index: 2 }, Actor::Hand)
|
||||
.unwrap();
|
||||
assert!(
|
||||
session.snapshot().arrangement[2].rows.iter().all(|row| row
|
||||
.velocities
|
||||
.iter()
|
||||
.all(|velocity| *velocity == 0)
|
||||
&& row.face.is_none())
|
||||
);
|
||||
session.apply(Command::Undo, Actor::Hand).unwrap();
|
||||
assert_eq!(
|
||||
session.snapshot().arrangement[2],
|
||||
session.snapshot().arrangement[0]
|
||||
);
|
||||
|
||||
assert_eq!(session.material_snapshot().sections.len(), 4);
|
||||
assert!(engine.calls().iter().any(|call| call.contains("material")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manual_faces_are_validated_and_undoable() {
|
||||
let (mut session, _) = session();
|
||||
|
||||
Reference in New Issue
Block a user