Music player in the sound settings: pick any calm track or automatic
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
51b7bb4804
commit
4ef7672008
22 files changed
+108
-4
No files matched your search
@@ -30,6 +30,36 @@ pub fn tracks_in(c: Category) -> Vec<usize> {
|
||||
TRACKS.iter().enumerate().filter(|(_, t)| t.1 == c).map(|(i, _)| i).collect()
|
||||
}
|
||||
|
||||
// the calm tracks the music player offers (no battle, victory or defeat music)
|
||||
pub fn player_tracks() -> Vec<usize> {
|
||||
use Category as C;
|
||||
TRACKS.iter().enumerate().filter(|(_, t)| matches!(t.1, C::Title | C::Build | C::Surface | C::Torture | C::Temple)).map(|(i, _)| i).collect()
|
||||
}
|
||||
|
||||
pub fn track_id(t: usize) -> &'static str {
|
||||
TRACKS.get(t).map_or("", |t| t.0)
|
||||
}
|
||||
|
||||
pub fn track_by_id(id: &str) -> Option<usize> {
|
||||
if id.is_empty() {
|
||||
return None;
|
||||
}
|
||||
TRACKS.iter().position(|t| t.0 == id)
|
||||
}
|
||||
|
||||
// the song title stored in the XM module (without the "DungeonCraft - " prefix)
|
||||
pub fn track_title(t: usize) -> String {
|
||||
let Some(d) = TRACKS.get(t).map(|t| t.2) else { return String::new() };
|
||||
let raw = d.get(17..37).unwrap_or(&[]);
|
||||
let name: String = raw.iter().take_while(|&&b| b != 0).map(|&b| b as char).collect();
|
||||
let name = name.trim().trim_start_matches("DungeonCraft - ").trim().to_string();
|
||||
if name.is_empty() {
|
||||
track_id(t).to_string()
|
||||
} else {
|
||||
name
|
||||
}
|
||||
}
|
||||
|
||||
const MUSIC_GAIN: f32 = 0.45; // our replayer is ~4x louder than libopenmpt; leaves headroom for effects
|
||||
const SFX_GAIN: f32 = 0.55;
|
||||
|
||||
@@ -218,6 +248,20 @@ pub extern "C" fn dc_audio_render(frames: u32) -> *const f32 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_music_player_offers_only_calm_tracks_with_titles() {
|
||||
let l = player_tracks();
|
||||
assert!(l.len() > 20);
|
||||
for t in l {
|
||||
let id = track_id(t);
|
||||
assert!(!id.starts_with("battle") && !id.starts_with("victory") && id != "defeat" && !id.starts_with("assault"), "{}", id);
|
||||
assert!(!track_title(t).is_empty());
|
||||
assert_eq!(track_by_id(id), Some(t));
|
||||
}
|
||||
assert_eq!(track_by_id(""), None);
|
||||
assert_eq!(track_title(track_by_id("build13").unwrap()), "Underground River");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn engine_crossfade_and_sfx() {
|
||||
dc_audio_init(48000.0);
|
||||
|
||||
Reference in new issue
Block a user