Skip to content

Commit

Permalink
Start implementing beatmap module, plus refinements
Browse files Browse the repository at this point in the history
- Make all types implement `Clone` and some `Copy
- Wrap long doc comments
- Rename non-self-explanatory fields
  • Loading branch information
valentinegb committed Aug 18, 2024
1 parent 75d6d67 commit 84e030c
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 70 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ Documentation can be found on [docs.rs](https://docs.rs/beat_saber_map).
- [ ] Create structures for map files
- [x] [`Info`](https://docs.rs/beat_saber_map/latest/beat_saber_map/info/struct.Info.html)
- [x] [`Audio`](https://docs.rs/beat_saber_map/latest/beat_saber_map/audio/struct.Audio.html)
- [ ] `Beatmap`
- [ ] [`Beatmap`](https://docs.rs/beat_saber_map/latest/beat_saber_map/beatmap/struct.Beatmap.html)
- [ ] `Lightshow`
- [ ] Create utility methods for map file structures and [`BeatSaberMap`](https://docs.rs/beat_saber_map/latest/beat_saber_map/struct.BeatSaberMap.html)
- [x] [`BeatSaberMap::from_dir()`](https://docs.rs/beat_saber_map/latest/beat_saber_map/struct.BeatSaberMap.html#method.from_dir)
- [x] [`Info::from_file()`](https://docs.rs/beat_saber_map/latest/beat_saber_map/info/struct.Info.html#method.from_file)
- [x] [`Audio::from_file()`](https://docs.rs/beat_saber_map/latest/beat_saber_map/audio/struct.Audio.html#method.from_file)
- [x] [`Beatmap::from_file()`](https://docs.rs/beat_saber_map/latest/beat_saber_map/beatmap/struct.Beatmap.html#method.from_file)
- [ ] More...
111 changes: 111 additions & 0 deletions sample/Normal.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"version": "4.0.0",
"colorNotes": [
{
"b": 10,
"r": 0,
"i": 0
}
],
"colorNotesData": [
{
"x": 1,
"y": 0,
"c": 0,
"d": 1,
"a": 0
},
{
"x": 2,
"y": 2,
"c": 0,
"d": 0,
"a": 0
}
],
"bombNotes": [
{
"b": 10,
"r": 0,
"i": 0
}
],
"bombNotesData": [
{
"x": 1,
"y": 0
}
],
"obstacles": [
{
"b": 10,
"r": 0,
"i": 0
}
],
"obstaclesData": [
{
"d": 5,
"x": 1,
"y": 0,
"w": 1,
"h": 5
}
],
"arcs": [
{
"hb": 10,
"tb": 15,
"hr": 0,
"tr": 0,
"hi": 0,
"ti": 1,
"ai": 0
}
],
"arcsData": [
{
"m": 1,
"tm": 1,
"a": 0
}
],
"chains": [
{
"hb": 10,
"tb": 15,
"hr": 0,
"tr": 0,
"i": 0,
"ci": 0
}
],
"chainsData": [
{
"tx": 2,
"ty": 2,
"c": 3,
"s": 0.5
}
],
"spawnRotations": [
{
"b": 10,
"i": 0
},
{
"b": 15,
"i": 1
}
],
"spawnRotationsData": [
{
"t": 0,
"r": 15
},
{
"t": 1,
"r": 15
}
]
}
67 changes: 43 additions & 24 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,31 @@ use crate::Error;

/// Information regarding how audio file should be processed.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html) for language-agnostic documentation.
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html)
/// for language-agnostic documentation.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct Audio {
/// Should be "4.0.0", that's the currently supported schema version.
pub version: String,
/// Used for verifying internal relationships and leaderboard integrity.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format.html#checksums) for language-agnostic documentation.
/// Refer to the
/// [BSMG Wiki](https://bsmg.wiki/mapping/map-format.html#checksums) for
/// language-agnostic documentation.
pub song_checksum: String,
/// Measures duration of audio file in samples.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#sample-count) for language-agnostic documentation.
/// Refer to the
/// [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#sample-count)
/// for language-agnostic documentation.
pub song_sample_count: u32,
/// Caches quality level of audio file.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#song-frequency) for language-agnostic documentation.
/// Refer to the
/// [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#song-frequency)
/// for language-agnostic documentation.
pub song_frequency: u32,
/// See [`BpmData`].
pub bpm_data: Vec<BpmData>,
Expand All @@ -47,40 +54,52 @@ impl Default for Audio {
}

impl Audio {
/// Instatiates an [`Audio`] from an audio file, typically named `BPMInfo.dat`.
/// Instatiates an [`Audio`] from an audio file, typically named
/// `BPMInfo.dat`.
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
}
}

/// Alters BPM of specified region.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#bpm-regions) for language-agnostic documentation.
#[derive(Debug, PartialEq, Eq, Default, Deserialize, Serialize)]
/// Refer to the
/// [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#bpm-regions) for
/// language-agnostic documentation.
#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct BpmData {
/// Start sample index.
pub si: usize,
#[serde(rename = "si")]
pub start_index: usize,
/// End sample index.
pub ei: usize,
#[serde(rename = "ei")]
pub end_index: usize,
/// Start beat.
pub sb: usize,
#[serde(rename = "sb")]
pub start_beat: usize,
/// End beat.
pub eb: usize,
#[serde(rename = "eb")]
pub end_beat: usize,
}

/// Applies normalization to loudness of audio file within specified region.
///
/// Refer to the [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#lufs-data) for language-agnostic documentation.
#[derive(Debug, PartialEq, Eq, Default, Deserialize, Serialize)]
/// Refer to the
/// [BSMG Wiki](https://bsmg.wiki/mapping/map-format/audio.html#lufs-data) for
/// language-agnostic documentation.
#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct LufsData {
/// Start sample index.
pub si: usize,
#[serde(rename = "si")]
pub start_index: usize,
/// End sample index.
pub ei: usize,
#[serde(rename = "ei")]
pub end_index: usize,
/// Loudness.
pub l: usize,
#[serde(rename = "l")]
pub loudness: usize,
}

#[cfg(test)]
Expand All @@ -100,15 +119,15 @@ mod tests {
song_sample_count: 1149214,
song_frequency: 44100,
bpm_data: vec![BpmData {
si: 0,
ei: 1149214,
sb: 0,
eb: 26,
start_index: 0,
end_index: 1149214,
start_beat: 0,
end_beat: 26,
}],
lufs_data: vec![LufsData {
si: 0,
ei: 1149214,
l: 0,
start_index: 0,
end_index: 1149214,
loudness: 0,
}],
}
}
Expand Down
Loading

0 comments on commit 84e030c

Please sign in to comment.