P2a: eight-bar arrangement with four editable sections

This commit is contained in:
Emil
2026-11-01 02:15:14 +03:00
parent 9add2610d7
commit c85e3cc723
19 changed files with 628 additions and 105 deletions
+89 -24
View File
@@ -42,7 +42,8 @@ pub struct ProjectSnapshot<'a> {
pub revision: u64,
pub bpm: f32,
pub gains: &'a [f32; VEIN_COUNT],
pub pattern: &'a Pattern,
pub sections: &'a [Pattern],
pub active_section: u8,
pub cores: &'a [CoreSummary],
pub journal: &'a [JournalEntry],
pub history: &'a [ProjectEdit],
@@ -70,6 +71,8 @@ impl ProjectStore {
bpm REAL NOT NULL,
gains TEXT NOT NULL,
pattern TEXT NOT NULL,
arrangement TEXT NOT NULL DEFAULT '[]',
active_section INTEGER NOT NULL DEFAULT 0,
history TEXT NOT NULL DEFAULT '[]',
redo TEXT NOT NULL DEFAULT '[]',
saved_at INTEGER NOT NULL
@@ -91,15 +94,21 @@ impl ProjectStore {
command TEXT NOT NULL
);",
)?;
let _ = conn.execute("ALTER TABLE cores ADD COLUMN faces TEXT", []);
let _ = conn.execute(
"ALTER TABLE project ADD COLUMN history TEXT NOT NULL DEFAULT '[]'",
[],
);
let _ = conn.execute(
"ALTER TABLE project ADD COLUMN redo TEXT NOT NULL DEFAULT '[]'",
[],
);
ensure_column(&conn, "cores", "faces", "TEXT")?;
ensure_column(&conn, "project", "history", "TEXT NOT NULL DEFAULT '[]'")?;
ensure_column(&conn, "project", "redo", "TEXT NOT NULL DEFAULT '[]'")?;
ensure_column(
&conn,
"project",
"arrangement",
"TEXT NOT NULL DEFAULT '[]'",
)?;
ensure_column(
&conn,
"project",
"active_section",
"INTEGER NOT NULL DEFAULT 0",
)?;
conn.execute(
"INSERT INTO meta (key, value) VALUES ('schema', ?1)
ON CONFLICT(key) DO UPDATE SET value = excluded.value",
@@ -120,7 +129,7 @@ impl ProjectStore {
let row = self
.conn
.query_row(
"SELECT name, revision, bpm, gains, pattern, history, redo FROM project WHERE id = 1",
"SELECT name, revision, bpm, gains, pattern, arrangement, active_section, history, redo FROM project WHERE id = 1",
[],
|row| {
Ok((
@@ -130,12 +139,24 @@ impl ProjectStore {
row.get::<_, String>(3)?,
row.get::<_, String>(4)?,
row.get::<_, String>(5)?,
row.get::<_, String>(6)?,
row.get::<_, i64>(6)?,
row.get::<_, String>(7)?,
row.get::<_, String>(8)?,
))
},
)
.optional()?;
let Some((name, revision, bpm, gains_json, pattern_json, history_json, redo_json)) = row
let Some((
name,
revision,
bpm,
gains_json,
pattern_json,
arrangement_json,
active_section,
history_json,
redo_json,
)) = row
else {
return Ok(None);
};
@@ -145,7 +166,13 @@ impl ProjectStore {
for (slot, value) in gains.iter_mut().zip(gains_value.iter()) {
*slot = *value;
}
let mut pattern: Pattern = serde_json::from_str(&pattern_json)?;
let pattern: Pattern = serde_json::from_str(&pattern_json)?;
let mut sections: Vec<Pattern> =
serde_json::from_str(&arrangement_json).unwrap_or_default();
if sections.is_empty() {
sections = vec![pattern; 4];
}
let active_section = (active_section.max(0) as usize).min(sections.len() - 1) as u8;
let mut warnings = Vec::new();
let history: Vec<ProjectEdit> =
serde_json::from_str(&history_json).unwrap_or_else(|error| {
@@ -210,7 +237,7 @@ impl ProjectStore {
}
}
for row in &mut pattern.rows {
for row in sections.iter_mut().flat_map(|section| &mut section.rows) {
let missing = row.face.as_ref().and_then(|face| {
let valid = cores.iter().any(|core| {
core.summary.id == face.core
@@ -274,7 +301,8 @@ impl ProjectStore {
revision: revision as u64,
bpm: bpm as f32,
gains,
pattern,
sections,
active_section,
cores,
journal,
seq,
@@ -287,20 +315,30 @@ impl ProjectStore {
pub fn save(&mut self, snapshot: &ProjectSnapshot<'_>) -> Result<(), ProjectError> {
let gains = serde_json::to_string(snapshot.gains)?;
let pattern = serde_json::to_string(snapshot.pattern)?;
let active = snapshot
.sections
.get(snapshot.active_section as usize)
.or_else(|| snapshot.sections.first())
.cloned()
.unwrap_or_else(zhila_core::default_pattern);
let pattern = serde_json::to_string(&active)?;
let arrangement = serde_json::to_string(snapshot.sections)?;
let active_section = snapshot.active_section as i64;
let history = serde_json::to_string(snapshot.history)?;
let redo = serde_json::to_string(snapshot.redo)?;
let saved_at = now_ms();
let tx = self.conn.transaction()?;
tx.execute(
"INSERT INTO project (id, name, revision, bpm, gains, pattern, history, redo, saved_at)
VALUES (1, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
"INSERT INTO project (id, name, revision, bpm, gains, pattern, arrangement, active_section, history, redo, saved_at)
VALUES (1, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
ON CONFLICT(id) DO UPDATE SET
name = excluded.name,
revision = excluded.revision,
bpm = excluded.bpm,
gains = excluded.gains,
pattern = excluded.pattern,
arrangement = excluded.arrangement,
active_section = excluded.active_section,
history = excluded.history,
redo = excluded.redo,
saved_at = excluded.saved_at",
@@ -310,6 +348,8 @@ impl ProjectStore {
snapshot.bpm as f64,
gains,
pattern,
arrangement,
active_section,
history,
redo,
saved_at as i64
@@ -445,6 +485,28 @@ impl ProjectStore {
}
}
fn ensure_column(
conn: &Connection,
table: &str,
column: &str,
definition: &str,
) -> Result<(), ProjectError> {
let exists = conn
.query_row(
"SELECT 1 FROM pragma_table_info(?1) WHERE name = ?2",
params![table, column],
|_| Ok(true),
)
.optional()?;
if exists.is_none() {
conn.execute(
&format!("ALTER TABLE {table} ADD COLUMN {column} {definition}"),
[],
)?;
}
Ok(())
}
fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
@@ -519,7 +581,8 @@ mod tests {
let history = vec![ProjectEdit {
bpm: 120.0,
gains,
pattern: pattern.clone(),
sections: vec![pattern.clone()],
active_section: 0,
cores: vec![CoreEdit {
core: core.summary.id.clone(),
boundaries: core.face_boundaries(),
@@ -534,7 +597,8 @@ mod tests {
revision: 4,
bpm: 132.0,
gains: &gains,
pattern: &pattern,
sections: std::slice::from_ref(&pattern),
active_section: 0,
cores: std::slice::from_ref(&core.summary),
journal: &journal,
history: &history,
@@ -553,10 +617,11 @@ mod tests {
assert_eq!(loaded.data.cores[0].summary.id, core.summary.id);
assert_eq!(loaded.data.cores[0].faces.len(), 16);
assert!((loaded.data.cores[0].summary.faces[7].end_sec - expected_edge).abs() < 1e-4);
assert_eq!(loaded.data.pattern.rows.len(), 5);
assert_eq!(loaded.data.pattern.rows[0].velocities[0], 96);
assert_eq!(loaded.data.sections.len(), 1);
assert_eq!(loaded.data.sections[0].rows.len(), 5);
assert_eq!(loaded.data.sections[0].rows[0].velocities[0], 96);
assert_eq!(
loaded.data.pattern.rows[0].face.as_ref().unwrap().core,
loaded.data.sections[0].rows[0].face.as_ref().unwrap().core,
core.summary.id
);
assert_eq!(loaded.data.journal.len(), 1);