Skip to content

Commit 71873ca

Browse files
Merge pull request #190 from FastTrackStudios/feat/bar-one
feat(structure): the song's bar 1, where a count-in ends, as structure every view reads
2 parents 40210e0 + a8ec37a commit 71873ca

4 files changed

Lines changed: 108 additions & 2 deletions

File tree

‎crates/session/src/doc.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl SessionDoc {
151151
self.doc
152152
.get_map(TEMPO)
153153
.insert("map", tempo_to_loro(&session.tempo))?;
154+
write_bar_one(&self.doc.get_map(TEMPO), session.bar_one)?;
154155
let rulers = self.doc.get_map(RULERS);
155156
let wanted: BTreeMap<String, &String> = session
156157
.rulers
@@ -198,6 +199,7 @@ impl SessionDoc {
198199
{
199200
session.tempo = tempo;
200201
}
202+
session.bar_one = self.bar_one();
201203
self.doc.get_map(RULERS).for_each(|k, v| {
202204
if let (Ok(i), ValueOrContainer::Value(LoroValue::String(name))) = (k.parse::<u32>(), v)
203205
{
@@ -350,6 +352,25 @@ impl SessionDoc {
350352
Ok(())
351353
}
352354

355+
/// Where the song's bar 1 is ([`Session::bar_one`]); `None`: the
356+
/// session's start.
357+
#[must_use]
358+
pub fn bar_one(&self) -> Option<Time> {
359+
value_of(&self.doc.get_map(TEMPO), "bar_one").and_then(|v| time_from_loro(&v))
360+
}
361+
362+
/// Sets where the song's bar 1 is (`None`: the session's start). One
363+
/// value, last writer wins, with the tempo map it belongs to.
364+
///
365+
/// # Errors
366+
///
367+
/// Loro refused the write.
368+
pub fn set_bar_one(&self, at: Option<Time>) -> DocResult<()> {
369+
write_bar_one(&self.doc.get_map(TEMPO), at)?;
370+
self.doc.commit();
371+
Ok(())
372+
}
373+
353374
fn put<R>(
354375
&self,
355376
root: &str,
@@ -697,6 +718,15 @@ pub fn value_from_loro(v: &LoroValue) -> Value {
697718
}
698719
}
699720

721+
fn write_bar_one(tempo: &LoroMap, at: Option<Time>) -> DocResult<()> {
722+
match at {
723+
Some(at) => tempo.insert("bar_one", time_to_loro(at))?,
724+
None if tempo.get("bar_one").is_some() => tempo.delete("bar_one")?,
725+
None => {}
726+
}
727+
Ok(())
728+
}
729+
700730
fn time_to_loro(t: Time) -> LoroValue {
701731
match t {
702732
Time::Seconds(s) => LoroValue::from(vec![LoroValue::from("s"), LoroValue::Double(s)]),

‎crates/session/src/service.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::sync::{Arc, PoisonError, RwLock};
3737
use crate::doc::{DocError, SessionDoc};
3838
use crate::ext::ExtensionData;
3939
use crate::id::{ExtensionId, ItemId, LaneId, MarkId};
40-
use crate::timeline::{Item, Lane, Marker, Region, TempoMap};
40+
use crate::timeline::{Item, Lane, Marker, Region, TempoMap, Time};
4141
use crate::value::Value;
4242

4343
/// What the host granted the caller beyond its own namespace.
@@ -91,6 +91,9 @@ pub enum Request {
9191
RemoveItem { id: ItemId },
9292
/// Sets the tempo map (needs [`Grants::structure`]).
9393
SetTempo { tempo: TempoMapData },
94+
/// Sets where the song's bar 1 is (`None`: the session's start; needs
95+
/// [`Grants::structure`]).
96+
SetBarOne { at: Option<Time> },
9497
/// Puts a marker (needs [`Grants::structure`]).
9598
PutMarker { id: MarkId, marker: Marker },
9699
/// Puts a region (needs [`Grants::structure`]).
@@ -184,6 +187,10 @@ pub enum Response {
184187
tempo: TempoMapData,
185188
markers: BTreeMap<MarkId, Marker>,
186189
regions: BTreeMap<MarkId, Region>,
190+
/// Where the song's bar 1 is (`crate::Session::bar_one`); `None`:
191+
/// the session's start. Absent from an older host's answer.
192+
#[facet(default)]
193+
bar_one: Option<Time>,
187194
},
188195
/// What changed since the version asked about: `whole` when that
189196
/// version is unknown (read everything again), else `changes`. The
@@ -243,6 +250,7 @@ pub const fn action_for(request: &Request) -> &'static str {
243250
| Request::ReadStructure
244251
| Request::ReadChanges { .. } => READ,
245252
Request::SetTempo { .. }
253+
| Request::SetBarOne { .. }
246254
| Request::PutMarker { .. }
247255
| Request::PutRegion { .. }
248256
| Request::RemoveMark { .. } => STRUCTURE,
@@ -335,13 +343,15 @@ pub fn handle(
335343
tempo: TempoMapData::of(&doc.tempo()),
336344
markers: doc.markers(),
337345
regions: doc.regions(),
346+
bar_one: doc.bar_one(),
338347
}),
339348
Request::ReadTimeline
340349
| Request::PutLane { .. }
341350
| Request::RemoveLane { .. }
342351
| Request::PutItem { .. }
343352
| Request::RemoveItem { .. } => timeline(doc, caller, request),
344353
Request::SetTempo { .. }
354+
| Request::SetBarOne { .. }
345355
| Request::PutMarker { .. }
346356
| Request::PutRegion { .. }
347357
| Request::RemoveMark { .. } => {
@@ -522,6 +532,10 @@ fn song_structure(doc: &SessionDoc, request: Request) -> Result<Response, Servic
522532
doc.set_tempo(&tempo_map(&tempo)?)?;
523533
Ok(Response::Done)
524534
}
535+
Request::SetBarOne { at } => {
536+
doc.set_bar_one(at)?;
537+
Ok(Response::Done)
538+
}
525539
Request::PutMarker { id, marker } => {
526540
doc.put_marker(&id, &marker)?;
527541
Ok(Response::Done)

‎crates/session/src/session.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
44

55
use crate::ext::ExtensionData;
66
use crate::id::{ExtensionId, ItemId, LaneId, MarkId};
7-
use crate::timeline::{Item, Lane, Marker, Region, TempoMap};
7+
use crate::timeline::{Item, Lane, Marker, Region, TempoMap, Time};
88

99
/// One session, as plain data.
1010
///
@@ -18,6 +18,12 @@ pub struct Session {
1818
pub name: String,
1919
/// Tempo and meter.
2020
pub tempo: TempoMap,
21+
/// Where the song's bar 1 is: what a chart, a cue sheet or a band
22+
/// calls bar 1, when the session starts earlier (a count-in, a click
23+
/// intro before the verse). `None`: the session's own start. A view
24+
/// that numbers bars as the music does reads its bars from here
25+
/// (the session's bar at this point is the song's bar 1).
26+
pub bar_one: Option<Time>,
2127
/// The rulers markers and regions sit on, by index: their names ("Song",
2228
/// "Sections"). Index `0` is the default ruler.
2329
pub rulers: BTreeMap<u32, String>,

‎crates/session/tests/session/service.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ fn everyone_reads_the_structure_with_its_ramps() {
326326
tempo: read,
327327
markers,
328328
regions,
329+
..
329330
} = ask(&doc, "ignition", Request::ReadStructure).unwrap()
330331
else {
331332
panic!("not the structure")
@@ -339,3 +340,58 @@ fn everyone_reads_the_structure_with_its_ramps() {
339340
assert!((qn - Time::Seconds(7.25).quarter_notes(&doc.tempo())).abs() < 1e-12);
340341
assert!((map.bpm_at(11.0) - 120.0).abs() < 1e-12);
341342
}
343+
344+
/// The song's bar 1 (a count-in before it) is structure: set with the
345+
/// structure grant only, read back by everyone, kept with the session.
346+
#[test]
347+
fn the_songs_bar_one_is_structure_every_view_reads() {
348+
let doc = SessionDoc::new();
349+
// Four bars of 4/4 at 160 BPM: the verse starts 6 s in.
350+
let verse = Time::Seconds(6.0);
351+
assert!(refused(ask(
352+
&doc,
353+
"keyflow",
354+
Request::SetBarOne { at: Some(verse) }
355+
)));
356+
let granted = Grants { structure: true };
357+
let version = match handle(
358+
&doc,
359+
&ext("daw"),
360+
Grants::default(),
361+
Request::ReadChanges { since: None },
362+
)
363+
.unwrap()
364+
{
365+
Response::Changes { version, .. } => version,
366+
other => panic!("{other:?}"),
367+
};
368+
handle(
369+
&doc,
370+
&ext("daw"),
371+
granted,
372+
Request::SetBarOne { at: Some(verse) },
373+
)
374+
.unwrap();
375+
let Response::Structure { bar_one, .. } =
376+
ask(&doc, "ignition", Request::ReadStructure).unwrap()
377+
else {
378+
panic!("not the structure");
379+
};
380+
assert_eq!(bar_one, Some(verse));
381+
assert_eq!(doc.read().bar_one, Some(verse));
382+
// A view following changes hears it as the tempo map's.
383+
let Response::Changes { changes, .. } = ask(
384+
&doc,
385+
"keyflow",
386+
Request::ReadChanges {
387+
since: Some(version),
388+
},
389+
)
390+
.unwrap() else {
391+
panic!("not changes");
392+
};
393+
assert_eq!(changes, [session::changes::Change::Tempo]);
394+
// Cleared: the session's start again.
395+
handle(&doc, &ext("daw"), granted, Request::SetBarOne { at: None }).unwrap();
396+
assert_eq!(doc.bar_one(), None);
397+
}

0 commit comments

Comments
 (0)