Merge remote-tracking branch 'desktop/parity-audio' into t3/achieve-full-parity
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
//! Replays `tools/test_ambient_sound_parity.py` scripts through Horizon's
|
||||
//! ambient sound handlers over its `SoundEngine`:
|
||||
//! `ambient_fixture ROOT INPUT OUTPUT`.
|
||||
#[path = "audio_common/mod.rs"]
|
||||
mod common;
|
||||
use common::{Fake, d, f, flush};
|
||||
use horizon_client::ambience::{Ambience, AmbientContext, AmbientSounds, Mood, bubble_column_in};
|
||||
use horizon_client::sound_engine::{Bound, SoundEngine, TickContext, Ticking};
|
||||
use horizon_client::sound_events::{SoundRegistry, normalize};
|
||||
use horizon_core::random::RandomSource;
|
||||
use std::io::Write;
|
||||
|
||||
/// The oracle's scripted brightness.
|
||||
fn light(mode: i32, x: i32, y: i32, z: i32) -> (u8, u8) {
|
||||
let h = x.wrapping_mul(73856093) ^ y.wrapping_mul(19349663) ^ z.wrapping_mul(83492791);
|
||||
let u = h as u32;
|
||||
match mode {
|
||||
0 => (15, 0),
|
||||
1 => (0, 0),
|
||||
2 => (0, ((u >> 4) & 15) as u8),
|
||||
_ => (
|
||||
if h & 3 == 0 { ((u >> 8) & 15) as u8 } else { 0 },
|
||||
((u >> 12) & 15) as u8,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn sounds(a: &[&str]) -> AmbientSounds {
|
||||
AmbientSounds {
|
||||
r#loop: (a[1] != "-").then(|| normalize(a[1])),
|
||||
mood: (a[2] != "-").then(|| {
|
||||
let m: Vec<&str> = a[2].split(',').collect();
|
||||
Mood {
|
||||
sound: normalize(m[0]),
|
||||
tick_delay: m[1].parse().expect("delay"),
|
||||
block_search_extent: m[2].parse().expect("extent"),
|
||||
offset: d(m[3]),
|
||||
}
|
||||
}),
|
||||
additions: a[3..]
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let p: Vec<&str> = s.split(',').collect();
|
||||
(normalize(p[0]), d(p[1]))
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let mut engine = SoundEngine::new(SoundRegistry::load(std::path::Path::new(&args[1])));
|
||||
engine.listening = true;
|
||||
let mut backend = Fake::default();
|
||||
let mut ambience = Ambience::new(0);
|
||||
let mut current = AmbientSounds::default();
|
||||
let (mut position, mut eye, mut eye_in_water, mut spectator, mut removed, mut mode) =
|
||||
([0.0f64; 3], 0.0f32, false, false, false, 0);
|
||||
let mut height = 1.8f32;
|
||||
let mut columns: Vec<([i32; 3], bool)> = Vec::new();
|
||||
let input = std::fs::read_to_string(&args[2]).expect("input");
|
||||
let mut out = std::io::BufWriter::new(std::fs::File::create(&args[3]).expect("output"));
|
||||
for line in input.lines() {
|
||||
writeln!(out, "{line}").expect("write");
|
||||
let a: Vec<&str> = line.split(' ').collect();
|
||||
match a[0] {
|
||||
"start" => {
|
||||
engine.stop_all(&mut backend);
|
||||
ambience.clear(&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
});
|
||||
ambience = Ambience::new(a[1].parse().expect("seed"));
|
||||
current = AmbientSounds::default();
|
||||
(position, eye, eye_in_water, spectator, removed, mode) =
|
||||
([0.0; 3], 0.0, false, false, false, 0);
|
||||
height = 1.8;
|
||||
columns.clear();
|
||||
}
|
||||
"sounds" => current = sounds(&a),
|
||||
"player" => {
|
||||
position = [d(a[1]), d(a[2]), d(a[3])];
|
||||
eye = f(a[4]);
|
||||
eye_in_water = a[5] == "1";
|
||||
spectator = a[6] == "1";
|
||||
removed = a[7] == "1";
|
||||
mode = a[8].parse().expect("mode");
|
||||
height = f(a[9]);
|
||||
columns = a[10..]
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let p: Vec<&str> = c.split(',').collect();
|
||||
let n = |i: usize| p[i].parse::<i32>().expect("column");
|
||||
([n(0), n(1), n(2)], p[3] == "1")
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
"volume" => {
|
||||
let source: usize = a[1].parse().expect("source");
|
||||
engine.source_volumes[source] = f(a[2]);
|
||||
engine.refresh_category_volume(source, &mut backend);
|
||||
}
|
||||
"tick" => {
|
||||
let light = |b: [i32; 3]| light(mode, b[0], b[1], b[2]);
|
||||
let ctx = AmbientContext {
|
||||
sounds: ¤t,
|
||||
position,
|
||||
eye_y: position[1] + f64::from(eye),
|
||||
underwater: eye_in_water,
|
||||
spectator,
|
||||
light: &light,
|
||||
bubble_column: bubble_column_in(
|
||||
[
|
||||
position[0] - f64::from(0.3f32),
|
||||
position[1],
|
||||
position[2] - f64::from(0.3f32),
|
||||
position[0] + f64::from(0.3f32),
|
||||
position[1] + f64::from(height),
|
||||
position[2] + f64::from(0.3f32),
|
||||
],
|
||||
|b| columns.iter().find(|c| c.0 == b).map(|c| c.1),
|
||||
),
|
||||
};
|
||||
ambience.tick(
|
||||
&ctx,
|
||||
&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
},
|
||||
);
|
||||
let tick = TickContext {
|
||||
player_underwater: ambience.was_underwater(),
|
||||
player_removed: removed,
|
||||
};
|
||||
engine.tick(false, &tick, &mut backend);
|
||||
}
|
||||
_ => panic!("{line}"),
|
||||
}
|
||||
writeln!(
|
||||
out,
|
||||
" rand={} mood={:08x} underwater={} delay={}",
|
||||
ambience.random.clone().next_long(),
|
||||
ambience.moodiness.to_bits(),
|
||||
ambience.was_underwater(),
|
||||
ambience.underwater_delay()
|
||||
)
|
||||
.expect("write");
|
||||
for (event, id) in ambience.loops() {
|
||||
let i = engine.instance(*id).expect("loop");
|
||||
let Ticking::BiomeLoop { fade, direction } = i.ticking else {
|
||||
panic!("loop kind");
|
||||
};
|
||||
writeln!(
|
||||
out,
|
||||
" loop {event} fade={fade} direction={direction} stopped={} active={}",
|
||||
i.stopped,
|
||||
engine.is_active(*id)
|
||||
)
|
||||
.expect("write");
|
||||
}
|
||||
let mut playing: Vec<String> = engine
|
||||
.active_instances()
|
||||
.into_iter()
|
||||
.map(|id| {
|
||||
let i = engine.instance(id).expect("instance");
|
||||
format!(
|
||||
" playing {} volume={:08x} looping={} relative={} at={:016x},{:016x},{:016x} stopped={}",
|
||||
i.identifier,
|
||||
i.volume.to_bits(),
|
||||
i.looping,
|
||||
i.relative,
|
||||
i.position[0].to_bits(),
|
||||
i.position[1].to_bits(),
|
||||
i.position[2].to_bits(),
|
||||
i.tickable() && i.stopped
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
playing.sort();
|
||||
for p in playing {
|
||||
writeln!(out, "{p}").expect("write");
|
||||
}
|
||||
flush(&mut engine, &mut out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
//! Audio environment attributes from Horizon's `MusicTables` and
|
||||
//! `AmbientTables` for `tools/test_audio_attributes_parity.py`:
|
||||
//! `audio_attributes_fixture ROOT INPUT OUTPUT`.
|
||||
use horizon_client::ambience::AmbientTables;
|
||||
use horizon_client::music::{Music, MusicTables};
|
||||
use horizon_client::sound_events::normalize;
|
||||
use std::io::Write;
|
||||
|
||||
fn music(m: Option<&Music>) -> String {
|
||||
m.map_or("-".to_owned(), |m| {
|
||||
format!(
|
||||
"{},{},{},{}",
|
||||
normalize(&m.sound),
|
||||
m.min_delay,
|
||||
m.max_delay,
|
||||
m.replace_current_music
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let root = std::path::Path::new(&args[1]);
|
||||
let tables = MusicTables::load(root);
|
||||
let ambient = AmbientTables::load(root);
|
||||
let input = std::fs::read_to_string(&args[2]).expect("input");
|
||||
let mut out = std::io::BufWriter::new(std::fs::File::create(&args[3]).expect("output"));
|
||||
for (name, m) in [
|
||||
("MENU", Music::menu()),
|
||||
("CREDITS", Music::credits()),
|
||||
("END_BOSS", Music::end_boss()),
|
||||
("CREATIVE", Music::creative()),
|
||||
("END", Music::end()),
|
||||
("UNDER_WATER", Music::under_water()),
|
||||
("GAME", Music::game()),
|
||||
] {
|
||||
writeln!(out, "{name} {}", music(Some(&m))).expect("write");
|
||||
}
|
||||
for line in input.lines() {
|
||||
let f: Vec<&str> = line.split(' ').collect();
|
||||
let (dimension, biome) = (f[0], f[1]);
|
||||
let bg = tables.background(dimension, biome);
|
||||
let mut s = format!(
|
||||
"{line} default={} creative={} underwater={}",
|
||||
music(bg.default.as_ref()),
|
||||
music(bg.creative.as_ref()),
|
||||
music(bg.underwater.as_ref())
|
||||
);
|
||||
for creative in [false, true] {
|
||||
for underwater in [false, true] {
|
||||
s.push_str(&format!(
|
||||
" select={}",
|
||||
music(bg.select(creative, underwater))
|
||||
));
|
||||
}
|
||||
}
|
||||
s.push_str(&format!(
|
||||
" volume={:08x}",
|
||||
tables.volume(dimension, biome).to_bits()
|
||||
));
|
||||
let a = ambient.get(dimension, biome);
|
||||
s.push_str(&format!(
|
||||
" loop={}",
|
||||
a.r#loop.as_deref().map_or("-".to_owned(), normalize)
|
||||
));
|
||||
s.push_str(&format!(
|
||||
" mood={}",
|
||||
a.mood.as_ref().map_or("-".to_owned(), |m| format!(
|
||||
"{},{},{},{:016x}",
|
||||
normalize(&m.sound),
|
||||
m.tick_delay,
|
||||
m.block_search_extent,
|
||||
m.offset.to_bits()
|
||||
))
|
||||
));
|
||||
for (sound, chance) in &a.additions {
|
||||
s.push_str(&format!(
|
||||
" addition={},{:016x}",
|
||||
normalize(sound),
|
||||
chance.to_bits()
|
||||
));
|
||||
}
|
||||
writeln!(out, "{s}").expect("write");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
//! Shared by the audio fixtures: a backend recording what an OpenAL source
|
||||
//! would hold, and the per-step dump the Java oracles print.
|
||||
#![allow(dead_code)]
|
||||
use horizon_client::sound_engine::{Backend, ChannelId, InstanceId, PlayResult, SoundEngine};
|
||||
use horizon_client::sound_events::Sound;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
|
||||
const INITIAL: u32 = 0x1011;
|
||||
const PLAYING: u32 = 0x1012;
|
||||
const PAUSED: u32 = 0x1013;
|
||||
const STOPPED: u32 = 0x1014;
|
||||
|
||||
/// An OpenAL source's state as `alGetSource*` reports it.
|
||||
#[derive(Clone)]
|
||||
pub struct Source {
|
||||
pub state: u32,
|
||||
pub gain: f32,
|
||||
pub pitch: f32,
|
||||
pub position: [f32; 3],
|
||||
pub relative: bool,
|
||||
pub looping: bool,
|
||||
pub model: u32,
|
||||
pub max: f32,
|
||||
pub rolloff: f32,
|
||||
pub reference: f32,
|
||||
}
|
||||
impl Default for Source {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: INITIAL,
|
||||
gain: 1.0,
|
||||
pitch: 1.0,
|
||||
position: [0.0; 3],
|
||||
relative: false,
|
||||
looping: false,
|
||||
model: 0xD002,
|
||||
max: f32::MAX,
|
||||
rolloff: 1.0,
|
||||
reference: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Buffers and streams are ready at once (silence in the oracle); stopping an
|
||||
/// initial source leaves it initial, as in OpenAL Soft.
|
||||
#[derive(Default)]
|
||||
pub struct Fake {
|
||||
pub sources: HashMap<ChannelId, Source>,
|
||||
next: ChannelId,
|
||||
}
|
||||
impl Backend for Fake {
|
||||
fn create(&mut self, _streaming: bool) -> Option<ChannelId> {
|
||||
self.next += 1;
|
||||
self.sources.insert(self.next, Source::default());
|
||||
Some(self.next)
|
||||
}
|
||||
fn destroy(&mut self, channel: ChannelId) {
|
||||
self.sources.remove(&channel);
|
||||
}
|
||||
fn set_pitch(&mut self, c: ChannelId, pitch: f32) {
|
||||
self.sources.get_mut(&c).expect("source").pitch = pitch;
|
||||
}
|
||||
fn set_volume(&mut self, c: ChannelId, volume: f32) {
|
||||
self.sources.get_mut(&c).expect("source").gain = volume;
|
||||
}
|
||||
fn set_attenuation(&mut self, c: ChannelId, linear: Option<f32>) {
|
||||
let s = self.sources.get_mut(&c).expect("source");
|
||||
match linear {
|
||||
Some(max) => {
|
||||
s.model = 0xD003;
|
||||
s.max = max;
|
||||
s.rolloff = 1.0;
|
||||
s.reference = 0.0;
|
||||
}
|
||||
None => s.model = 0,
|
||||
}
|
||||
}
|
||||
fn set_looping(&mut self, c: ChannelId, looping: bool) {
|
||||
self.sources.get_mut(&c).expect("source").looping = looping;
|
||||
}
|
||||
fn set_position(&mut self, c: ChannelId, p: [f64; 3]) {
|
||||
self.sources.get_mut(&c).expect("source").position =
|
||||
[p[0] as f32, p[1] as f32, p[2] as f32];
|
||||
}
|
||||
fn set_relative(&mut self, c: ChannelId, relative: bool) {
|
||||
self.sources.get_mut(&c).expect("source").relative = relative;
|
||||
}
|
||||
fn start(&mut self, c: ChannelId, _sound: &Sound, _looping: bool) {
|
||||
self.sources.get_mut(&c).expect("source").state = PLAYING;
|
||||
}
|
||||
fn stop(&mut self, c: ChannelId) {
|
||||
if let Some(s) = self.sources.get_mut(&c)
|
||||
&& s.state != INITIAL
|
||||
{
|
||||
s.state = STOPPED;
|
||||
}
|
||||
}
|
||||
fn pause(&mut self, c: ChannelId) {
|
||||
let s = self.sources.get_mut(&c).expect("source");
|
||||
if s.state == PLAYING {
|
||||
s.state = PAUSED;
|
||||
}
|
||||
}
|
||||
fn unpause(&mut self, c: ChannelId) {
|
||||
let s = self.sources.get_mut(&c).expect("source");
|
||||
if s.state == PAUSED {
|
||||
s.state = PLAYING;
|
||||
}
|
||||
}
|
||||
fn stopped(&mut self, c: ChannelId) -> bool {
|
||||
self.sources.get(&c).is_none_or(|s| s.state == STOPPED)
|
||||
}
|
||||
}
|
||||
pub fn bits(v: f32) -> String {
|
||||
format!("{:08x}", v.to_bits())
|
||||
}
|
||||
pub fn f(hex: &str) -> f32 {
|
||||
f32::from_bits(u32::from_str_radix(hex, 16).expect("float"))
|
||||
}
|
||||
pub fn d(hex: &str) -> f64 {
|
||||
f64::from_bits(u64::from_str_radix(hex, 16).expect("double"))
|
||||
}
|
||||
/// The shared per-step dump (instances, notifications, pool counts).
|
||||
pub fn describe(engine: &SoundEngine, backend: &Fake, name: &str, id: InstanceId) -> String {
|
||||
let mut s = format!(" {name} active={}", engine.is_active(id));
|
||||
match engine.channel(id).and_then(|c| backend.sources.get(&c)) {
|
||||
None => s.push_str(" channel=-"),
|
||||
Some(src) => s.push_str(&format!(
|
||||
" state={:x} gain={} pitch={} pos={},{},{} rel={} loop={} model={:x} max={} rolloff={} ref={}",
|
||||
src.state,
|
||||
bits(src.gain),
|
||||
bits(src.pitch),
|
||||
bits(src.position[0]),
|
||||
bits(src.position[1]),
|
||||
bits(src.position[2]),
|
||||
u8::from(src.relative),
|
||||
u8::from(src.looping),
|
||||
src.model,
|
||||
bits(src.max),
|
||||
bits(src.rolloff),
|
||||
bits(src.reference)
|
||||
)),
|
||||
}
|
||||
s
|
||||
}
|
||||
pub fn flush(engine: &mut SoundEngine, out: &mut impl Write) {
|
||||
for n in std::mem::take(&mut engine.notifications) {
|
||||
writeln!(
|
||||
out,
|
||||
"notify {} {} {} {} {:016x} {:016x} {:016x}",
|
||||
n.identifier,
|
||||
n.subtitle.as_deref().unwrap_or("-"),
|
||||
bits(n.range),
|
||||
n.looping,
|
||||
n.position[0].to_bits(),
|
||||
n.position[1].to_bits(),
|
||||
n.position[2].to_bits()
|
||||
)
|
||||
.expect("write");
|
||||
}
|
||||
let [used, streaming] = engine.used_channels();
|
||||
writeln!(
|
||||
out,
|
||||
" Sounds: {used}/{} + {streaming}/{}",
|
||||
engine.limits[0], engine.limits[1]
|
||||
)
|
||||
.expect("write");
|
||||
}
|
||||
|
||||
pub fn result_name(r: PlayResult) -> &'static str {
|
||||
match r {
|
||||
PlayResult::Started => "STARTED",
|
||||
PlayResult::StartedSilently => "STARTED_SILENTLY",
|
||||
PlayResult::NotStarted => "NOT_STARTED",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
//! Replays `tools/test_music_parity.py` scripts through Horizon's
|
||||
//! `MusicManager` over its `SoundEngine`: `music_fixture ROOT INPUT OUTPUT`.
|
||||
#[path = "audio_common/mod.rs"]
|
||||
mod common;
|
||||
use common::{Fake, describe, f, flush};
|
||||
use horizon_client::music::{Music, MusicFrequency, MusicManager};
|
||||
use horizon_client::sound_engine::{Bound, MUSIC, SoundEngine, TickContext};
|
||||
use horizon_client::sound_events::SoundRegistry;
|
||||
use std::io::Write;
|
||||
|
||||
fn music(spec: &str) -> Option<Music> {
|
||||
if spec == "-" {
|
||||
return None;
|
||||
}
|
||||
let p: Vec<&str> = spec.split(',').collect();
|
||||
Some(Music::new(
|
||||
&horizon_client::sound_events::normalize(p[0]),
|
||||
p[1].parse().expect("min"),
|
||||
p[2].parse().expect("max"),
|
||||
p[3] == "1",
|
||||
))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let mut engine = SoundEngine::new(SoundRegistry::load(std::path::Path::new(&args[1])));
|
||||
engine.listening = true;
|
||||
let mut backend = Fake::default();
|
||||
let mut manager = MusicManager::new(0);
|
||||
let (mut situation, mut volume) = (None::<Music>, 1.0f32);
|
||||
let input = std::fs::read_to_string(&args[2]).expect("input");
|
||||
let mut out = std::io::BufWriter::new(std::fs::File::create(&args[3]).expect("output"));
|
||||
for line in input.lines() {
|
||||
writeln!(out, "{line}").expect("write");
|
||||
let a: Vec<&str> = line.split(' ').collect();
|
||||
match a[0] {
|
||||
"start" => {
|
||||
if let Some(id) = manager.current_instance() {
|
||||
engine.release(id);
|
||||
}
|
||||
manager = MusicManager::new(a[1].parse().expect("seed"));
|
||||
manager.frequency = MusicFrequency::parse(a[2]);
|
||||
engine.stop_all(&mut backend);
|
||||
}
|
||||
"situation" => {
|
||||
situation = music(a[1]);
|
||||
volume = f(a[2]);
|
||||
}
|
||||
"tick" => {
|
||||
let mut sounds = Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
};
|
||||
manager.tick(situation.as_ref(), volume, a[1] == "1", &mut sounds);
|
||||
engine.tick(a[2] == "1", &TickContext::default(), &mut backend);
|
||||
}
|
||||
"finish" => {
|
||||
if let Some(c) = manager.current_instance().and_then(|id| engine.channel(id)) {
|
||||
horizon_client::sound_engine::Backend::stop(&mut backend, c);
|
||||
}
|
||||
}
|
||||
"stop" => manager.stop_playing(
|
||||
situation.as_ref(),
|
||||
&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
},
|
||||
),
|
||||
"stopmusic" => {
|
||||
let m = music(a[1]).expect("music");
|
||||
manager.stop_playing_music(
|
||||
&m,
|
||||
situation.as_ref(),
|
||||
&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
},
|
||||
);
|
||||
}
|
||||
"freq" => manager.set_frequency(MusicFrequency::parse(a[1]), situation.as_ref()),
|
||||
"toast" => manager.show_now_playing_toast_if_needed(),
|
||||
"volume" => {
|
||||
let source: usize = a[1].parse().expect("source");
|
||||
engine.source_volumes[source] = f(a[2]);
|
||||
engine.refresh_category_volume(source, &mut backend);
|
||||
}
|
||||
"stopall" => engine.stop_all(&mut backend),
|
||||
_ => panic!("{line}"),
|
||||
}
|
||||
writeln!(
|
||||
out,
|
||||
" current={} delay={} gain={:08x} toastShown={} musicGain={:08x}",
|
||||
manager.current(&engine).unwrap_or("-"),
|
||||
manager.next_song_delay(),
|
||||
manager.current_gain().to_bits(),
|
||||
manager.toast_shown(),
|
||||
engine.category_gain(MUSIC).to_bits()
|
||||
)
|
||||
.expect("write");
|
||||
for show in std::mem::take(&mut manager.toast_requests) {
|
||||
writeln!(out, " toast {}", if show { "show" } else { "hide" }).expect("write");
|
||||
}
|
||||
if let Some(id) = manager.current_instance() {
|
||||
writeln!(out, "{}", describe(&engine, &backend, "music", id)).expect("write");
|
||||
}
|
||||
flush(&mut engine, &mut out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//! `getSituationalMusic`/`getMusicVolume` through Horizon's `Situation` for
|
||||
//! `tools/test_situational_music_parity.py`:
|
||||
//! `situational_music_fixture ROOT INPUT OUTPUT`.
|
||||
use horizon_client::music::{BackgroundMusic, Music, MusicTables, Situation};
|
||||
use horizon_client::sound_events::normalize;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let tables = MusicTables::load(std::path::Path::new(&args[1]));
|
||||
let input = std::fs::read_to_string(&args[2]).expect("input");
|
||||
let mut out = std::io::BufWriter::new(std::fs::File::create(&args[3]).expect("output"));
|
||||
for line in input.lines() {
|
||||
let f: Vec<&str> = line.split(' ').collect();
|
||||
let in_world = f[1] == "1";
|
||||
let (background, probe_volume) = if in_world {
|
||||
(tables.background(f[2], f[3]), tables.volume(f[2], f[3]))
|
||||
} else {
|
||||
(BackgroundMusic::default(), 1.0)
|
||||
};
|
||||
let situation = Situation {
|
||||
screen_music: (f[0] == "win").then(Music::credits),
|
||||
in_world,
|
||||
in_end: f[2] == "minecraft:the_end",
|
||||
boss_music: f[4].contains("music"),
|
||||
background: &background,
|
||||
probe_volume,
|
||||
creative: f[5] == "1" && f[6] == "1",
|
||||
underwater: f[7] == "1",
|
||||
};
|
||||
let music = situation.music().map_or("-".to_owned(), |m| {
|
||||
format!(
|
||||
"{},{},{},{}",
|
||||
normalize(&m.sound),
|
||||
m.min_delay,
|
||||
m.max_delay,
|
||||
m.replace_current_music
|
||||
)
|
||||
});
|
||||
writeln!(
|
||||
out,
|
||||
"{line} music={music} volume={:08x}",
|
||||
situation.volume().to_bits()
|
||||
)
|
||||
.expect("write");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
//! Replays `tools/test_sound_engine_parity.py` scripts through Horizon's
|
||||
//! `SoundEngine` with a backend recording what an OpenAL source would hold:
|
||||
//! `sound_engine_fixture ROOT INPUT OUTPUT`.
|
||||
#[path = "audio_common/mod.rs"]
|
||||
mod common;
|
||||
use common::{Fake, d, describe, f, flush, result_name};
|
||||
use horizon_client::sound_engine::{
|
||||
Attenuation, Backend, InstanceId, SoundEngine, SoundInstance, TickContext,
|
||||
};
|
||||
use horizon_client::sound_events::SoundRegistry;
|
||||
use horizon_core::random::LegacyRandom;
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let mut engine = SoundEngine::new(SoundRegistry::load(std::path::Path::new(&args[1])));
|
||||
engine.listening = true;
|
||||
let mut backend = Fake::default();
|
||||
let mut ctx = TickContext::default();
|
||||
let mut instances: BTreeMap<String, InstanceId> = BTreeMap::new();
|
||||
let input = std::fs::read_to_string(&args[2]).expect("input");
|
||||
let mut out = std::io::BufWriter::new(std::fs::File::create(&args[3]).expect("output"));
|
||||
for line in input.lines() {
|
||||
writeln!(out, "{line}").expect("write");
|
||||
let a: Vec<&str> = line.split(' ').collect();
|
||||
let source = |i: usize| a[i].parse::<usize>().expect("source");
|
||||
match a[0] {
|
||||
"play" => {
|
||||
let event = horizon_client::sound_events::normalize(a[3]);
|
||||
let random = LegacyRandom::new(a[14].parse().expect("seed"));
|
||||
let instance = match a[2] {
|
||||
"simple" => SoundInstance::new(
|
||||
&event,
|
||||
source(4),
|
||||
f(a[5]),
|
||||
f(a[6]),
|
||||
random,
|
||||
a[10] == "1",
|
||||
a[11].parse().expect("delay"),
|
||||
if a[12] == "linear" {
|
||||
Attenuation::Linear
|
||||
} else {
|
||||
Attenuation::None
|
||||
},
|
||||
[d(a[7]), d(a[8]), d(a[9])],
|
||||
a[13] == "1",
|
||||
),
|
||||
"loop" => SoundInstance::biome_loop(&event, random),
|
||||
"uloop" => SoundInstance::under_liquid_loop(&event, random),
|
||||
"usub" => SoundInstance::under_liquid_sub(&event, random),
|
||||
_ => panic!("{line}"),
|
||||
};
|
||||
if let Some(old) = instances.remove(a[1]) {
|
||||
engine.release(old);
|
||||
}
|
||||
let (id, result) = engine.play_new(instance, true, &mut backend);
|
||||
instances.insert(a[1].to_owned(), id);
|
||||
writeln!(out, " result {}", result_name(result)).expect("write");
|
||||
}
|
||||
"replay" => {
|
||||
let result = engine.play(instances[a[1]], &mut backend);
|
||||
writeln!(out, " result {}", result_name(result)).expect("write");
|
||||
}
|
||||
"queue" => engine.queue_ticking(instances[a[1]]),
|
||||
"delayed" => engine.play_delayed(instances[a[1]], a[2].parse().expect("delay")),
|
||||
"fade" => {
|
||||
let i = engine.instance_mut(instances[a[1]]).expect("instance");
|
||||
if a[2] == "in" {
|
||||
i.fade_in();
|
||||
} else {
|
||||
i.fade_out();
|
||||
}
|
||||
}
|
||||
"stop" => engine.stop(instances[a[1]], &mut backend),
|
||||
"stopmatch" => {
|
||||
let sound = (a[1] != "-").then(|| horizon_client::sound_events::normalize(a[1]));
|
||||
let src = (a[2] != "-").then(|| source(2));
|
||||
engine.stop_matching(sound.as_deref(), src, &mut backend);
|
||||
}
|
||||
"finish" => {
|
||||
if let Some(c) = engine.channel(instances[a[1]]) {
|
||||
backend.stop(c);
|
||||
}
|
||||
}
|
||||
"gain" => engine.update_category_volume(source(1), f(a[2]), &mut backend),
|
||||
"volume" => engine.source_volumes[source(1)] = f(a[2]),
|
||||
"refresh" => engine.refresh_category_volume(source(1), &mut backend),
|
||||
"pause" => engine.pause_all_except(&[1, 10], &mut backend),
|
||||
"resume" => engine.resume(&mut backend),
|
||||
"player" => {
|
||||
ctx.player_underwater = a[1] == "1";
|
||||
ctx.player_removed = a[2] == "1";
|
||||
}
|
||||
"tick" => engine.tick(a[1] == "1", &ctx, &mut backend),
|
||||
"forget" => {
|
||||
if let Some(id) = instances.remove(a[1]) {
|
||||
engine.release(id);
|
||||
}
|
||||
}
|
||||
_ => panic!("{line}"),
|
||||
}
|
||||
for (name, id) in &instances {
|
||||
writeln!(out, "{}", describe(&engine, &backend, name, *id)).expect("write");
|
||||
}
|
||||
flush(&mut engine, &mut out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
//! Resolves sound instances through Horizon's sound event registry for
|
||||
//! `tools/test_sound_event_parity.py`: `sound_event_fixture ROOT INPUT OUTPUT [NAMES]`.
|
||||
use horizon_client::sound_engine::{Attenuation, MASTER, SoundEngine, SoundInstance};
|
||||
use horizon_client::sound_events::{Sentinel, SoundRegistry};
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
use std::io::Write;
|
||||
|
||||
fn bits(v: f32) -> String {
|
||||
format!("{:08x}", v.to_bits())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let registry = SoundRegistry::load(std::path::Path::new(&args[1]));
|
||||
let mut engine = SoundEngine::new(registry);
|
||||
let input = std::fs::read_to_string(&args[2]).expect("input");
|
||||
let mut out = std::io::BufWriter::new(std::fs::File::create(&args[3]).expect("output"));
|
||||
let mut names: Vec<String> = engine.registry.names().cloned().collect();
|
||||
names.sort();
|
||||
for name in names {
|
||||
writeln!(out, "registered {name}").expect("write");
|
||||
}
|
||||
for line in input.lines() {
|
||||
let f: Vec<&str> = line.split(' ').collect();
|
||||
let seed: i64 = f[1].parse().expect("seed");
|
||||
let volume = f32::from_bits(u32::from_str_radix(f[2], 16).expect("volume"));
|
||||
let pitch = f32::from_bits(u32::from_str_radix(f[3], 16).expect("pitch"));
|
||||
let event = horizon_client::sound_events::normalize(f[0]);
|
||||
let instance = SoundInstance::new(
|
||||
&event,
|
||||
MASTER,
|
||||
volume,
|
||||
pitch,
|
||||
LegacyRandom::new(seed),
|
||||
false,
|
||||
0,
|
||||
Attenuation::Linear,
|
||||
[0.0; 3],
|
||||
false,
|
||||
);
|
||||
let id = engine.add(instance, true);
|
||||
engine.resolve(id);
|
||||
let weight = engine.registry.weight(&event);
|
||||
let i = engine.instance_mut(id).expect("instance");
|
||||
let mut s = line.to_owned();
|
||||
match &i.event {
|
||||
None => s.push_str(" event=none"),
|
||||
Some(subtitle) => s.push_str(&format!(
|
||||
" event weight={weight} subtitle={}",
|
||||
subtitle.as_deref().unwrap_or("-")
|
||||
)),
|
||||
}
|
||||
let v = i.get_volume();
|
||||
let p = i.get_pitch();
|
||||
let sound = i.sound.clone().expect("resolved");
|
||||
let (namespace, path) = sound.path();
|
||||
let sentinel = match sound.sentinel {
|
||||
Sentinel::None => "-",
|
||||
Sentinel::Empty => "empty",
|
||||
Sentinel::IntentionallyEmpty => "intentionally_empty",
|
||||
};
|
||||
s.push_str(&format!(
|
||||
" sound={} path={namespace}:{path} sentinel={sentinel} weight={} type={} stream={} preload={} attenuation={} volume={} pitch={} range={} next={}",
|
||||
sound.location,
|
||||
sound.weight,
|
||||
if sound.event { "SOUND_EVENT" } else { "FILE" },
|
||||
sound.stream,
|
||||
sound.preload,
|
||||
sound.attenuation_distance,
|
||||
bits(v),
|
||||
bits(p),
|
||||
bits(sound.attenuation_distance(v)),
|
||||
i.random.next_long()
|
||||
));
|
||||
writeln!(out, "{s}").expect("write");
|
||||
engine.release(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
//! The player's ambient sound handlers: `BiomeAmbientSoundsHandler` (biome
|
||||
//! loops with 40-tick cross-fades, random additions, cave mood) driven by the
|
||||
//! `minecraft:audio/ambient_sounds` attribute, `UnderwaterAmbientSoundHandler`
|
||||
//! and the underwater loop (`UnderLiquidAmbientSoundInstance`).
|
||||
//! and `LocalPlayer.updateIsUnderwater` (enter/exit sounds and the
|
||||
//! `UnderLiquidAmbientSoundInstance` loop), played through the sound engine.
|
||||
use crate::sound_engine::{AMBIENT, InstanceId, PLAYERS, SoundEngine, SoundInstance, Sounds};
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
use serde_json::Value;
|
||||
use std::{collections::HashMap, fs, path::Path};
|
||||
@@ -80,87 +82,83 @@ impl AmbientTables {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
/// The sound engine side of the handlers.
|
||||
pub trait AmbientOutput {
|
||||
/// A relative tickable instance; None when it could not start.
|
||||
fn play_tracked(&mut self, event: &str, volume: f32, looping: bool) -> Option<u64>;
|
||||
fn set_volume(&mut self, id: u64, volume: f32);
|
||||
fn stop(&mut self, id: u64);
|
||||
fn is_active(&self, id: u64) -> bool;
|
||||
/// A positional one-shot (`SimpleSoundInstance`, linear attenuation).
|
||||
fn play_at(&mut self, event: &str, at: [f64; 3]);
|
||||
}
|
||||
impl AmbientOutput for crate::audio::Audio {
|
||||
fn play_tracked(&mut self, event: &str, volume: f32, looping: bool) -> Option<u64> {
|
||||
crate::audio::Audio::play_tracked(self, event, volume, looping)
|
||||
}
|
||||
fn set_volume(&mut self, id: u64, volume: f32) {
|
||||
self.set_tracked_volume(id, volume);
|
||||
}
|
||||
fn stop(&mut self, id: u64) {
|
||||
self.stop_tracked(id);
|
||||
}
|
||||
fn is_active(&self, id: u64) -> bool {
|
||||
self.tracked_active(id)
|
||||
}
|
||||
fn play_at(&mut self, event: &str, at: [f64; 3]) {
|
||||
self.play(event, Some(at), 1.0, 1.0);
|
||||
}
|
||||
}
|
||||
/// `BiomeAmbientSoundsHandler.LoopSoundInstance`.
|
||||
struct LoopSound {
|
||||
id: Option<u64>,
|
||||
fade: i32,
|
||||
direction: i32,
|
||||
stopped: bool,
|
||||
}
|
||||
impl LoopSound {
|
||||
fn tick(&mut self) -> f32 {
|
||||
if self.fade < 0 {
|
||||
self.stopped = true;
|
||||
}
|
||||
self.fade += self.direction;
|
||||
(self.fade as f32 / 40.0).clamp(0.0, 1.0)
|
||||
}
|
||||
fn fade_out(&mut self) {
|
||||
self.fade = self.fade.min(40);
|
||||
self.direction = -1;
|
||||
}
|
||||
fn fade_in(&mut self) {
|
||||
self.fade = self.fade.max(0);
|
||||
self.direction = 1;
|
||||
}
|
||||
}
|
||||
/// What the handlers read about the player and level each tick.
|
||||
pub struct AmbientContext<'a> {
|
||||
/// `AMBIENT_SOUNDS` at the player's position.
|
||||
pub sounds: &'a AmbientSounds,
|
||||
/// Feet and eye position.
|
||||
/// The player's position (feet) and eye height.
|
||||
pub position: [f64; 3],
|
||||
pub eye_y: f64,
|
||||
/// `isEyeInFluid(WATER)` this tick (`Entity.updateIsUnderwater`).
|
||||
pub underwater: bool,
|
||||
/// Raw (sky, block) light at a block.
|
||||
pub spectator: bool,
|
||||
/// `Level.getBrightness` (sky, block) at a block.
|
||||
pub light: &'a dyn Fn([i32; 3]) -> (u8, u8),
|
||||
/// The bubble column in the player's box (its `drag` property), from
|
||||
/// `bubble_column_in`.
|
||||
pub bubble_column: Option<bool>,
|
||||
}
|
||||
/// `BubbleColumnAmbientSoundHandler`'s lookup: the first bubble column
|
||||
/// (x fastest, then y, then z) in the player's bounding box inflated by
|
||||
/// (0, -0.4F, 0) and deflated by 1.0E-6, as `drag_down`. `state` gives the
|
||||
/// bubble column at a block (None for anything else).
|
||||
pub fn bubble_column_in(bb: [f64; 6], state: impl Fn([i32; 3]) -> Option<bool>) -> Option<bool> {
|
||||
// AABB keeps min <= max on every construction.
|
||||
let make = |a: [f64; 6]| {
|
||||
[
|
||||
a[0].min(a[3]),
|
||||
a[1].min(a[4]),
|
||||
a[2].min(a[5]),
|
||||
a[0].max(a[3]),
|
||||
a[1].max(a[4]),
|
||||
a[2].max(a[5]),
|
||||
]
|
||||
};
|
||||
let inflate = |b: [f64; 6], x: f64, y: f64, z: f64| {
|
||||
make([b[0] - x, b[1] - y, b[2] - z, b[3] + x, b[4] + y, b[5] + z])
|
||||
};
|
||||
let b = inflate(make(bb), 0.0, f64::from(-0.4f32), 0.0);
|
||||
let b = inflate(b, -1.0E-6, -1.0E-6, -1.0E-6);
|
||||
let (x0, y0, z0) = (
|
||||
b[0].floor() as i32,
|
||||
b[1].floor() as i32,
|
||||
b[2].floor() as i32,
|
||||
);
|
||||
let (x1, y1, z1) = (
|
||||
b[3].floor() as i32,
|
||||
b[4].floor() as i32,
|
||||
b[5].floor() as i32,
|
||||
);
|
||||
for z in z0..=z1 {
|
||||
for y in y0..=y1 {
|
||||
for x in x0..=x1 {
|
||||
if let Some(drag) = state([x, y, z]) {
|
||||
return Some(drag);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
/// The local player's ambient sound handlers.
|
||||
pub struct Ambience {
|
||||
/// The client level's random.
|
||||
random: LegacyRandom,
|
||||
loops: Vec<(String, LoopSound)>,
|
||||
pub random: LegacyRandom,
|
||||
/// `BiomeAmbientSoundsHandler.loopSounds` (insertion order).
|
||||
loops: Vec<(String, InstanceId)>,
|
||||
previous_loop: Option<String>,
|
||||
pub moodiness: f32,
|
||||
/// `UnderwaterAmbientSoundHandler.tickDelay`.
|
||||
underwater_delay: i32,
|
||||
/// The underwater loop instance and its fade.
|
||||
underwater_loop: Option<(u64, i32)>,
|
||||
/// UnderLiquidSubSound instances (stop when the player leaves the water).
|
||||
sub_sounds: Vec<u64>,
|
||||
/// `LocalPlayer.wasUnderwater`.
|
||||
was_underwater: bool,
|
||||
/// `BubbleColumnAmbientSoundHandler.wasInBubbleColumn` / `firstTick`.
|
||||
was_in_bubble_column: bool,
|
||||
first_tick: bool,
|
||||
}
|
||||
impl Default for Ambience {
|
||||
fn default() -> Self {
|
||||
let seed = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_nanos() as i64);
|
||||
Self::new(seed)
|
||||
Self::new(crate::sound_engine::unique_seed())
|
||||
}
|
||||
}
|
||||
impl Ambience {
|
||||
@@ -171,53 +169,95 @@ impl Ambience {
|
||||
previous_loop: None,
|
||||
moodiness: 0.0,
|
||||
underwater_delay: 0,
|
||||
underwater_loop: None,
|
||||
sub_sounds: Vec::new(),
|
||||
was_underwater: false,
|
||||
was_in_bubble_column: false,
|
||||
first_tick: true,
|
||||
}
|
||||
}
|
||||
/// Stop everything (leaving the world).
|
||||
pub fn clear(&mut self, out: &mut impl AmbientOutput) {
|
||||
for (_, l) in self.loops.drain(..) {
|
||||
if let Some(id) = l.id {
|
||||
out.stop(id);
|
||||
}
|
||||
}
|
||||
if let Some((id, _)) = self.underwater_loop.take() {
|
||||
out.stop(id);
|
||||
}
|
||||
for id in self.sub_sounds.drain(..) {
|
||||
out.stop(id);
|
||||
/// `LocalPlayer.isUnderWater`.
|
||||
pub fn was_underwater(&self) -> bool {
|
||||
self.was_underwater
|
||||
}
|
||||
/// `BiomeAmbientSoundsHandler.loopSounds`.
|
||||
pub fn loops(&self) -> &[(String, InstanceId)] {
|
||||
&self.loops
|
||||
}
|
||||
/// `UnderwaterAmbientSoundHandler.tickDelay`.
|
||||
pub fn underwater_delay(&self) -> i32 {
|
||||
self.underwater_delay
|
||||
}
|
||||
/// A new player (leaving or changing worlds): fresh handlers.
|
||||
pub fn clear(&mut self, sounds: &mut impl Sounds) {
|
||||
for (_, id) in self.loops.drain(..) {
|
||||
sounds.release(id);
|
||||
}
|
||||
self.previous_loop = None;
|
||||
self.moodiness = 0.0;
|
||||
self.underwater_delay = 0;
|
||||
self.was_underwater = false;
|
||||
self.was_in_bubble_column = false;
|
||||
self.first_tick = true;
|
||||
}
|
||||
/// `LocalPlayer.updateIsUnderwater` enter/exit sounds and loop, then
|
||||
/// `UnderwaterAmbientSoundHandler`, `BubbleColumn` (absent) and
|
||||
/// `BiomeAmbientSoundsHandler` ticks, then the tickable instances.
|
||||
pub fn tick(&mut self, ctx: &AmbientContext, out: &mut impl AmbientOutput) {
|
||||
let feet = ctx.position;
|
||||
if !self.was_underwater && ctx.underwater {
|
||||
out.play_at("minecraft:ambient.underwater.enter", feet);
|
||||
if let Some((id, _)) = self.underwater_loop.take() {
|
||||
out.stop(id);
|
||||
}
|
||||
self.underwater_loop = out
|
||||
.play_tracked("minecraft:ambient.underwater.loop", 0.0, true)
|
||||
.map(|id| (id, 0));
|
||||
}
|
||||
if self.was_underwater && !ctx.underwater {
|
||||
out.play_at("minecraft:ambient.underwater.exit", feet);
|
||||
}
|
||||
/// `ClientLevel.playLocalSound(x, y, z, sound, source, 1, 1, false)`.
|
||||
fn play_local(&mut self, event: &str, source: usize, at: [f64; 3], sounds: &mut impl Sounds) {
|
||||
let seed = self.random.next_long();
|
||||
let instance = SoundInstance::at(event, source, 1.0, 1.0, LegacyRandom::new(seed), at);
|
||||
sounds.play_instance(instance, false);
|
||||
}
|
||||
/// `LocalPlayer.updateIsUnderwater`, then the `ambientSoundHandlers`
|
||||
/// (underwater, bubble column, biome) as `LocalPlayer.tick` runs them.
|
||||
pub fn tick(&mut self, ctx: &AmbientContext, sounds: &mut impl Sounds) {
|
||||
let old = self.was_underwater;
|
||||
self.was_underwater = ctx.underwater;
|
||||
self.tick_underwater(ctx, out);
|
||||
self.tick_biome(ctx, out);
|
||||
self.tick_instances(ctx, out);
|
||||
if !ctx.spectator {
|
||||
if !old && ctx.underwater {
|
||||
self.play_local(
|
||||
"minecraft:ambient.underwater.enter",
|
||||
AMBIENT,
|
||||
ctx.position,
|
||||
sounds,
|
||||
);
|
||||
let instance = SoundInstance::under_liquid_loop(
|
||||
"minecraft:ambient.underwater.loop",
|
||||
SoundEngine::unseeded(),
|
||||
);
|
||||
sounds.play_instance(instance, false);
|
||||
}
|
||||
if old && !ctx.underwater {
|
||||
self.play_local(
|
||||
"minecraft:ambient.underwater.exit",
|
||||
AMBIENT,
|
||||
ctx.position,
|
||||
sounds,
|
||||
);
|
||||
}
|
||||
}
|
||||
self.tick_underwater(sounds);
|
||||
self.tick_bubble_column(ctx, sounds);
|
||||
self.tick_biome(ctx, sounds);
|
||||
}
|
||||
fn tick_underwater(&mut self, ctx: &AmbientContext, out: &mut impl AmbientOutput) {
|
||||
/// `BubbleColumnAmbientSoundHandler.tick`: entering a column plays its
|
||||
/// inside sound (`LocalPlayer.playSound`, on PLAYERS).
|
||||
fn tick_bubble_column(&mut self, ctx: &AmbientContext, sounds: &mut impl Sounds) {
|
||||
if let Some(drag_down) = ctx.bubble_column {
|
||||
if !self.was_in_bubble_column && !self.first_tick && !ctx.spectator {
|
||||
let event = if drag_down {
|
||||
"minecraft:block.bubble_column.whirlpool_inside"
|
||||
} else {
|
||||
"minecraft:block.bubble_column.upwards_inside"
|
||||
};
|
||||
self.play_local(event, PLAYERS, ctx.position, sounds);
|
||||
}
|
||||
self.was_in_bubble_column = true;
|
||||
} else {
|
||||
self.was_in_bubble_column = false;
|
||||
}
|
||||
self.first_tick = false;
|
||||
}
|
||||
/// `UnderwaterAmbientSoundHandler.tick`.
|
||||
fn tick_underwater(&mut self, sounds: &mut impl Sounds) {
|
||||
self.underwater_delay -= 1;
|
||||
if self.underwater_delay <= 0 && ctx.underwater {
|
||||
if self.underwater_delay <= 0 && self.was_underwater {
|
||||
let rand = self.random.next_float();
|
||||
let sound = if rand < 1.0E-4 {
|
||||
Some("minecraft:ambient.underwater.loop.additions.ultra_rare")
|
||||
@@ -230,147 +270,121 @@ impl Ambience {
|
||||
};
|
||||
if let Some(sound) = sound {
|
||||
self.underwater_delay = 0;
|
||||
if let Some(id) = out.play_tracked(sound, 1.0, false) {
|
||||
self.sub_sounds.push(id);
|
||||
}
|
||||
let instance = SoundInstance::under_liquid_sub(sound, SoundEngine::unseeded());
|
||||
sounds.play_instance(instance, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn tick_biome(&mut self, ctx: &AmbientContext, out: &mut impl AmbientOutput) {
|
||||
self.loops.retain(|(_, l)| !l.stopped);
|
||||
let sounds = ctx.sounds;
|
||||
if sounds.r#loop != self.previous_loop {
|
||||
self.previous_loop = sounds.r#loop.clone();
|
||||
for (_, l) in &mut self.loops {
|
||||
l.fade_out();
|
||||
/// `BiomeAmbientSoundsHandler.tick`.
|
||||
fn tick_biome(&mut self, ctx: &AmbientContext, sounds: &mut impl Sounds) {
|
||||
let mut kept = Vec::new();
|
||||
for (event, id) in std::mem::take(&mut self.loops) {
|
||||
if sounds.engine().instance(id).is_some_and(|i| i.stopped) {
|
||||
sounds.release(id);
|
||||
} else {
|
||||
kept.push((event, id));
|
||||
}
|
||||
if let Some(event) = &sounds.r#loop {
|
||||
if !self.loops.iter().any(|(e, _)| e == event) {
|
||||
let id = out.play_tracked(event, 0.0, true);
|
||||
self.loops.push((
|
||||
event.clone(),
|
||||
LoopSound {
|
||||
id,
|
||||
fade: 0,
|
||||
direction: 0,
|
||||
stopped: false,
|
||||
},
|
||||
));
|
||||
}
|
||||
self.loops = kept;
|
||||
let current = ctx
|
||||
.sounds
|
||||
.r#loop
|
||||
.as_deref()
|
||||
.map(crate::sound_events::normalize);
|
||||
if current != self.previous_loop {
|
||||
self.previous_loop = current.clone();
|
||||
for (_, id) in &self.loops {
|
||||
if let Some(i) = sounds.engine().instance_mut(*id) {
|
||||
i.fade_out();
|
||||
}
|
||||
if let Some((_, l)) = self.loops.iter_mut().find(|(e, _)| e == event) {
|
||||
l.fade_in();
|
||||
}
|
||||
if let Some(event) = current {
|
||||
let id = match self.loops.iter().find(|(e, _)| *e == event) {
|
||||
Some((_, id)) => *id,
|
||||
None => {
|
||||
let instance = SoundInstance::biome_loop(&event, SoundEngine::unseeded());
|
||||
let id = sounds.engine().add(instance, true);
|
||||
sounds.play_id(id);
|
||||
self.loops.push((event, id));
|
||||
id
|
||||
}
|
||||
};
|
||||
if let Some(i) = sounds.engine().instance_mut(id) {
|
||||
i.fade_in();
|
||||
}
|
||||
}
|
||||
}
|
||||
for (sound, chance) in &sounds.additions {
|
||||
if self.random.next_double() < *chance
|
||||
&& let Some(id) = out.play_tracked(sound, 1.0, false)
|
||||
{
|
||||
// forAmbientAddition: a one-shot nobody ticks.
|
||||
let _ = id;
|
||||
for (sound, chance) in &ctx.sounds.additions {
|
||||
if self.random.next_double() < *chance {
|
||||
let event = crate::sound_events::normalize(sound);
|
||||
let instance = SoundInstance::for_ambient_addition(&event, SoundEngine::unseeded());
|
||||
sounds.play_instance(instance, false);
|
||||
}
|
||||
}
|
||||
let Some(mood) = &sounds.mood else { return };
|
||||
let Some(mood) = &ctx.sounds.mood else { return };
|
||||
let extent = mood.block_search_extent;
|
||||
let span = extent * 2 + 1;
|
||||
let mut offset = || self.random.next_int_bound(span).unwrap_or(0) - extent;
|
||||
let (dx, dy, dz) = (offset(), offset(), offset());
|
||||
let sample = [
|
||||
(ctx.position[0] + f64::from(dx)).floor() as i32,
|
||||
(ctx.eye_y + f64::from(dy)).floor() as i32,
|
||||
(ctx.position[2] + f64::from(dz)).floor() as i32,
|
||||
];
|
||||
let x = ctx.position[0] + f64::from(self.random.next_int_bound(span).unwrap_or(0))
|
||||
- f64::from(extent);
|
||||
let y = ctx.eye_y + f64::from(self.random.next_int_bound(span).unwrap_or(0))
|
||||
- f64::from(extent);
|
||||
let z = ctx.position[2] + f64::from(self.random.next_int_bound(span).unwrap_or(0))
|
||||
- f64::from(extent);
|
||||
let sample = [x.floor() as i32, y.floor() as i32, z.floor() as i32];
|
||||
let (sky, block) = (ctx.light)(sample);
|
||||
if sky > 0 {
|
||||
self.moodiness -= f32::from(sky) / 15.0 * 0.001;
|
||||
} else {
|
||||
self.moodiness -= (f32::from(block) - 1.0) / mood.tick_delay as f32;
|
||||
self.moodiness -= (i32::from(block) - 1) as f32 / mood.tick_delay as f32;
|
||||
}
|
||||
if self.moodiness >= 1.0 {
|
||||
let d = [
|
||||
f64::from(sample[0]) + 0.5 - ctx.position[0],
|
||||
f64::from(sample[1]) + 0.5 - ctx.eye_y,
|
||||
f64::from(sample[2]) + 0.5 - ctx.position[2],
|
||||
];
|
||||
let distance = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
|
||||
let sample_x = f64::from(sample[0]) + 0.5;
|
||||
let sample_y = f64::from(sample[1]) + 0.5;
|
||||
let sample_z = f64::from(sample[2]) + 0.5;
|
||||
let dx = sample_x - ctx.position[0];
|
||||
let dy = sample_y - ctx.eye_y;
|
||||
let dz = sample_z - ctx.position[2];
|
||||
let distance = (dx * dx + dy * dy + dz * dz).sqrt();
|
||||
let source = distance + mood.offset;
|
||||
out.play_at(
|
||||
&mood.sound,
|
||||
[
|
||||
ctx.position[0] + d[0] / distance * source,
|
||||
ctx.eye_y + d[1] / distance * source,
|
||||
ctx.position[2] + d[2] / distance * source,
|
||||
],
|
||||
);
|
||||
let at = [
|
||||
ctx.position[0] + dx / distance * source,
|
||||
ctx.eye_y + dy / distance * source,
|
||||
ctx.position[2] + dz / distance * source,
|
||||
];
|
||||
// The mood sound draws from the level random itself.
|
||||
let event = crate::sound_events::normalize(&mood.sound);
|
||||
let instance = SoundInstance::for_ambient_mood(&event, self.random.clone(), at);
|
||||
let id = sounds.engine().add(instance, true);
|
||||
sounds.play_id(id);
|
||||
if let Some(i) = sounds.engine().instance(id) {
|
||||
self.random = i.random.clone();
|
||||
}
|
||||
sounds.release(id);
|
||||
self.moodiness = 0.0;
|
||||
} else {
|
||||
self.moodiness = self.moodiness.max(0.0);
|
||||
}
|
||||
}
|
||||
/// The tickable instances' own ticks (SoundEngine.tickInGameSound).
|
||||
fn tick_instances(&mut self, ctx: &AmbientContext, out: &mut impl AmbientOutput) {
|
||||
for (_, l) in &mut self.loops {
|
||||
let volume = l.tick();
|
||||
if let Some(id) = l.id {
|
||||
if l.stopped {
|
||||
out.stop(id);
|
||||
} else {
|
||||
out.set_volume(id, volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
// UnderLiquidAmbientSoundInstance: +1 per tick under water, -2 out of it.
|
||||
if let Some((id, fade)) = self.underwater_loop.as_mut() {
|
||||
if *fade >= 0 {
|
||||
*fade += if ctx.underwater { 1 } else { -2 };
|
||||
*fade = (*fade).min(40);
|
||||
out.set_volume(*id, (*fade as f32 / 40.0).clamp(0.0, 1.0));
|
||||
} else {
|
||||
out.stop(*id);
|
||||
self.underwater_loop = None;
|
||||
}
|
||||
}
|
||||
// UnderLiquidSubSound: stops when the player leaves the water.
|
||||
self.sub_sounds.retain(|&id| {
|
||||
let keep = ctx.underwater && out.is_active(id);
|
||||
if !keep {
|
||||
out.stop(id);
|
||||
}
|
||||
keep
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::sound_engine::{Bound, TickContext, test_backend::Null};
|
||||
use crate::sound_events::SoundRegistry;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Fake {
|
||||
next: u64,
|
||||
live: HashMap<u64, (String, f32)>,
|
||||
at: Vec<String>,
|
||||
}
|
||||
impl AmbientOutput for Fake {
|
||||
fn play_tracked(&mut self, event: &str, volume: f32, _looping: bool) -> Option<u64> {
|
||||
self.next += 1;
|
||||
self.live.insert(self.next, (event.to_owned(), volume));
|
||||
Some(self.next)
|
||||
}
|
||||
fn set_volume(&mut self, id: u64, volume: f32) {
|
||||
if let Some(v) = self.live.get_mut(&id) {
|
||||
v.1 = volume;
|
||||
}
|
||||
}
|
||||
fn stop(&mut self, id: u64) {
|
||||
self.live.remove(&id);
|
||||
}
|
||||
fn is_active(&self, id: u64) -> bool {
|
||||
self.live.contains_key(&id)
|
||||
}
|
||||
fn play_at(&mut self, event: &str, _at: [f64; 3]) {
|
||||
self.at.push(event.to_owned());
|
||||
}
|
||||
fn engine() -> SoundEngine {
|
||||
let mut registry = SoundRegistry::default();
|
||||
registry.add_file(
|
||||
"minecraft",
|
||||
br#"{"crimson": {"sounds": ["c"]}, "warped": {"sounds": ["w"]},
|
||||
"ambient.cave": {"sounds": ["a", "b"]},
|
||||
"ambient.underwater.loop": {"sounds": ["u"]},
|
||||
"ambient.underwater.enter": {"sounds": ["e"]},
|
||||
"ambient.underwater.exit": {"sounds": ["x"]}}"#,
|
||||
&|_, _| true,
|
||||
);
|
||||
SoundEngine::new(registry)
|
||||
}
|
||||
fn ctx<'a>(
|
||||
sounds: &'a AmbientSounds,
|
||||
@@ -382,49 +396,59 @@ mod tests {
|
||||
position: [0.5, 10.0, 0.5],
|
||||
eye_y: 11.62,
|
||||
underwater,
|
||||
spectator: false,
|
||||
light,
|
||||
bubble_column: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loops_cross_fade_over_forty_ticks() {
|
||||
let (mut engine, mut backend) = (engine(), Null::default());
|
||||
let mut a = Ambience::new(1);
|
||||
let mut out = Fake::default();
|
||||
let dark = |_: [i32; 3]| (15u8, 0u8);
|
||||
let lit = |_: [i32; 3]| (15u8, 0u8);
|
||||
let crimson = AmbientSounds {
|
||||
r#loop: Some("crimson".into()),
|
||||
r#loop: Some("minecraft:crimson".into()),
|
||||
..Default::default()
|
||||
};
|
||||
for _ in 0..40 {
|
||||
a.tick(&ctx(&crimson, false, &dark), &mut out);
|
||||
}
|
||||
assert_eq!(out.live.values().map(|v| v.1).collect::<Vec<_>>(), [1.0]);
|
||||
let warped = AmbientSounds {
|
||||
r#loop: Some("warped".into()),
|
||||
r#loop: Some("minecraft:warped".into()),
|
||||
..Default::default()
|
||||
};
|
||||
let tick =
|
||||
|a: &mut Ambience, s: &AmbientSounds, engine: &mut SoundEngine, backend: &mut Null| {
|
||||
a.tick(
|
||||
&ctx(s, false, &lit),
|
||||
&mut Bound {
|
||||
engine: &mut *engine,
|
||||
backend: &mut *backend,
|
||||
},
|
||||
);
|
||||
engine.tick(false, &TickContext::default(), backend);
|
||||
};
|
||||
for _ in 0..40 {
|
||||
tick(&mut a, &crimson, &mut engine, &mut backend);
|
||||
}
|
||||
for _ in 0..20 {
|
||||
a.tick(&ctx(&warped, false, &dark), &mut out);
|
||||
tick(&mut a, &warped, &mut engine, &mut backend);
|
||||
}
|
||||
let mut volumes: Vec<_> = out.live.values().map(|v| (v.0.clone(), v.1)).collect();
|
||||
volumes.sort_by(|x, y| x.0.cmp(&y.0));
|
||||
assert_eq!(
|
||||
volumes,
|
||||
[("crimson".to_owned(), 0.5), ("warped".to_owned(), 0.5)]
|
||||
);
|
||||
let volumes: Vec<f32> = a
|
||||
.loops
|
||||
.iter()
|
||||
.map(|(_, id)| engine.instance(*id).unwrap().volume)
|
||||
.collect();
|
||||
assert_eq!(volumes, [0.5, 0.5]);
|
||||
for _ in 0..30 {
|
||||
a.tick(&ctx(&warped, false, &dark), &mut out);
|
||||
tick(&mut a, &warped, &mut engine, &mut backend);
|
||||
}
|
||||
assert_eq!(
|
||||
out.live.values().map(|v| v.0.as_str()).collect::<Vec<_>>(),
|
||||
["warped"]
|
||||
);
|
||||
assert_eq!(a.loops.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mood_builds_in_the_dark_and_plays_a_cave_sound() {
|
||||
let (mut engine, mut backend) = (engine(), Null::default());
|
||||
engine.listening = true;
|
||||
let mut a = Ambience::new(2);
|
||||
let mut out = Fake::default();
|
||||
let cave = |_: [i32; 3]| (0u8, 0u8);
|
||||
let sounds = AmbientSounds {
|
||||
mood: Some(Mood {
|
||||
@@ -435,31 +459,19 @@ mod tests {
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
// Block light 0: +1/6000 per tick (float), so the sound plays around tick 6000.
|
||||
let mut ticks = 0;
|
||||
while out.at.is_empty() && ticks < 7000 {
|
||||
a.tick(&ctx(&sounds, false, &cave), &mut out);
|
||||
while engine.notifications.is_empty() && ticks < 7000 {
|
||||
a.tick(
|
||||
&ctx(&sounds, false, &cave),
|
||||
&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
},
|
||||
);
|
||||
ticks += 1;
|
||||
}
|
||||
assert!((5990..=6010).contains(&ticks), "{ticks}");
|
||||
assert_eq!(out.at, ["minecraft:ambient.cave"]);
|
||||
assert_eq!(engine.notifications[0].identifier, "minecraft:ambient.cave");
|
||||
assert_eq!(a.moodiness, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entering_water_plays_enter_and_fades_the_loop_in() {
|
||||
let mut a = Ambience::new(3);
|
||||
let mut out = Fake::default();
|
||||
let lit = |_: [i32; 3]| (15u8, 0u8);
|
||||
let none = AmbientSounds::default();
|
||||
a.tick(&ctx(&none, true, &lit), &mut out);
|
||||
assert_eq!(out.at, ["minecraft:ambient.underwater.enter"]);
|
||||
let id = a.underwater_loop.unwrap().0;
|
||||
assert_eq!(out.live[&id].1, 1.0 / 40.0);
|
||||
a.tick(&ctx(&none, false, &lit), &mut out);
|
||||
assert_eq!(out.at.last().unwrap(), "minecraft:ambient.underwater.exit");
|
||||
// Fade 1 -> -1 stops on the next tick.
|
||||
a.tick(&ctx(&none, false, &lit), &mut out);
|
||||
assert!(!out.live.contains_key(&id));
|
||||
}
|
||||
}
|
||||
|
||||
+451
-440
@@ -1,9 +1,16 @@
|
||||
//! Sound playback: `sounds.json` events with weighted variants, Ogg/Vorbis
|
||||
//! decoding, and a small mixer with vanilla's linear distance attenuation
|
||||
//! (`SoundInstance.Attenuation.LINEAR`, default range 16 blocks) and stereo panning.
|
||||
//! Sound playback: vanilla's `SoundEngine` bookkeeping (`sound_engine`) over a
|
||||
//! small mixer standing in for OpenAL: one channel per source with its gain,
|
||||
//! pitch, linear distance attenuation (`AL_LINEAR_DISTANCE`, reference 0,
|
||||
//! rolloff 1), looping, relative flag and source state, Ogg/Vorbis decoding
|
||||
//! (streams on a worker thread) and stereo panning.
|
||||
use crate::Result;
|
||||
use crate::sound_engine::{
|
||||
Attenuation, Backend, Bound, ChannelId, InstanceId, PlayResult, SoundEngine, SoundInstance,
|
||||
Sounds, TickContext,
|
||||
};
|
||||
use crate::sound_events::{Sound, SoundRegistry};
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use serde_json::Value;
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
@@ -11,46 +18,32 @@ use std::{
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Variant {
|
||||
name: String,
|
||||
volume: f32,
|
||||
pitch: f32,
|
||||
weight: u32,
|
||||
attenuation: f32,
|
||||
is_event: bool,
|
||||
/// An OpenAL source's state.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum State {
|
||||
Initial,
|
||||
Playing,
|
||||
Paused,
|
||||
Stopped,
|
||||
}
|
||||
struct Voice {
|
||||
samples: Arc<Vec<f32>>,
|
||||
/// An OpenAL source.
|
||||
struct Channel {
|
||||
samples: Option<(Arc<Vec<f32>>, u32)>,
|
||||
position: f64,
|
||||
step: f64,
|
||||
left: f32,
|
||||
right: f32,
|
||||
/// The sound event and `SoundSource` index (what `/stopsound` matches).
|
||||
event: Arc<str>,
|
||||
source: usize,
|
||||
state: State,
|
||||
gain: f32,
|
||||
pitch: f32,
|
||||
/// `AL_LINEAR_DISTANCE` max distance, or no attenuation.
|
||||
linear: Option<f32>,
|
||||
looping: bool,
|
||||
at: [f32; 3],
|
||||
relative: bool,
|
||||
}
|
||||
#[derive(Default)]
|
||||
struct Mixer {
|
||||
voices: Vec<Voice>,
|
||||
/// The streaming music track (`SoundSource.MUSIC`), kept apart from the
|
||||
/// voice cap and scaled by `music_gain` (MusicManager's fade).
|
||||
music: Option<Voice>,
|
||||
music_gain: f32,
|
||||
/// Tickable sound instances the game keeps a handle to (ambient loops).
|
||||
tracked: Vec<Tracked>,
|
||||
}
|
||||
struct Tracked {
|
||||
id: u64,
|
||||
voice: Voice,
|
||||
looping: bool,
|
||||
volume: f32,
|
||||
}
|
||||
/// The current music track: decoding on a worker until the voice exists.
|
||||
struct MusicTrack {
|
||||
event: String,
|
||||
/// Set by the decoder thread once the voice is in the mixer (or failed).
|
||||
ready: Arc<std::sync::atomic::AtomicBool>,
|
||||
channels: HashMap<ChannelId, Channel>,
|
||||
listener: [f64; 3],
|
||||
listener_yaw: f32,
|
||||
}
|
||||
/// A sound the player could have heard (`SoundEventListener.onPlaySound`):
|
||||
/// what the subtitle overlay needs.
|
||||
@@ -58,30 +51,31 @@ struct MusicTrack {
|
||||
pub struct Heard {
|
||||
/// The `subtitle` translation key of the sound event.
|
||||
pub subtitle: String,
|
||||
/// Where it played (None = relative to the listener, e.g. UI sounds).
|
||||
/// The instance's position (relative sounds report their raw coordinates).
|
||||
pub at: Option<[f64; 3]>,
|
||||
/// `SoundEngine.getRange`: infinite for relative and non-attenuated sounds.
|
||||
pub range: f32,
|
||||
pub looping: bool,
|
||||
}
|
||||
/// The mixer side of the engine (`Backend`).
|
||||
pub struct MixerBackend {
|
||||
mixer: Arc<Mutex<Mixer>>,
|
||||
root: PathBuf,
|
||||
cache: HashMap<String, Option<(Arc<Vec<f32>>, u32)>>,
|
||||
next: ChannelId,
|
||||
live: bool,
|
||||
}
|
||||
pub struct Audio {
|
||||
_stream: Option<cpal::Stream>,
|
||||
/// `subtitle` keys of `sounds.json` events.
|
||||
subtitles: HashMap<String, String>,
|
||||
pub engine: SoundEngine,
|
||||
backend: MixerBackend,
|
||||
/// Record `Heard` sounds (the subtitle overlay is listening).
|
||||
pub record_heard: bool,
|
||||
heard: Vec<Heard>,
|
||||
mixer: Arc<Mutex<Mixer>>,
|
||||
rate: u32,
|
||||
root: PathBuf,
|
||||
events: HashMap<String, Vec<Variant>>,
|
||||
cache: HashMap<String, Option<(Arc<Vec<f32>>, u32)>>,
|
||||
seed: u64,
|
||||
pub listener: [f64; 3],
|
||||
pub listener_yaw: f32,
|
||||
pub master: f32,
|
||||
music: Option<MusicTrack>,
|
||||
next_tracked: u64,
|
||||
/// Seeds of positional plays (the client level random's `nextLong`).
|
||||
random: LegacyRandom,
|
||||
/// A `SoundSource` index the next sound plays on (`/playsound`).
|
||||
source_override: Option<usize>,
|
||||
/// Positional sounds played since the last drain, kept while the world
|
||||
@@ -130,75 +124,242 @@ fn decode(path: &Path) -> Option<(Vec<f32>, u32)> {
|
||||
}
|
||||
Some((mono, rate))
|
||||
}
|
||||
impl Audio {
|
||||
pub fn new(root: &Path) -> Self {
|
||||
let mut events = HashMap::new();
|
||||
let mut subtitles = HashMap::new();
|
||||
if let Ok(text) = fs::read(root.join("assets/minecraft/sounds.json"))
|
||||
&& let Ok(Value::Object(map)) = serde_json::from_slice::<Value>(&text)
|
||||
impl MixerBackend {
|
||||
fn with(&self, channel: ChannelId, f: impl FnOnce(&mut Channel)) {
|
||||
if let Ok(mut m) = self.mixer.lock()
|
||||
&& let Some(c) = m.channels.get_mut(&channel)
|
||||
{
|
||||
for (name, def) in map {
|
||||
let variants = def["sounds"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|s| match s {
|
||||
Value::String(n) => Variant {
|
||||
name: n.clone(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
weight: 1,
|
||||
attenuation: 16.0,
|
||||
is_event: false,
|
||||
},
|
||||
_ => Variant {
|
||||
name: s["name"].as_str().unwrap_or("").to_owned(),
|
||||
volume: s["volume"].as_f64().unwrap_or(1.0) as f32,
|
||||
pitch: s["pitch"].as_f64().unwrap_or(1.0) as f32,
|
||||
weight: s["weight"].as_u64().unwrap_or(1) as u32,
|
||||
attenuation: s["attenuation_distance"].as_f64().unwrap_or(16.0) as f32,
|
||||
is_event: s["type"].as_str() == Some("event"),
|
||||
},
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if let Some(key) = def["subtitle"].as_str() {
|
||||
subtitles.insert(format!("minecraft:{name}"), key.to_owned());
|
||||
}
|
||||
events.insert(format!("minecraft:{name}"), variants);
|
||||
}
|
||||
f(c);
|
||||
}
|
||||
let mixer = Arc::new(Mutex::new(Mixer {
|
||||
music_gain: 1.0,
|
||||
..Mixer::default()
|
||||
}));
|
||||
let (stream, rate) = match Self::open(mixer.clone()) {
|
||||
Ok(v) => (Some(v.0), v.1),
|
||||
Err(e) => {
|
||||
eprintln!("Horizon: audio unavailable: {e}");
|
||||
(None, 48_000)
|
||||
}
|
||||
}
|
||||
impl Backend for MixerBackend {
|
||||
fn create(&mut self, _streaming: bool) -> Option<ChannelId> {
|
||||
self.next += 1;
|
||||
let id = self.next;
|
||||
self.mixer.lock().ok()?.channels.insert(
|
||||
id,
|
||||
Channel {
|
||||
samples: None,
|
||||
position: 0.0,
|
||||
state: State::Initial,
|
||||
gain: 1.0,
|
||||
pitch: 1.0,
|
||||
linear: None,
|
||||
looping: false,
|
||||
at: [0.0; 3],
|
||||
relative: false,
|
||||
},
|
||||
);
|
||||
Some(id)
|
||||
}
|
||||
fn destroy(&mut self, channel: ChannelId) {
|
||||
if let Ok(mut m) = self.mixer.lock() {
|
||||
m.channels.remove(&channel);
|
||||
}
|
||||
}
|
||||
fn set_pitch(&mut self, channel: ChannelId, pitch: f32) {
|
||||
self.with(channel, |c| c.pitch = pitch);
|
||||
}
|
||||
fn set_volume(&mut self, channel: ChannelId, volume: f32) {
|
||||
self.with(channel, |c| c.gain = volume);
|
||||
}
|
||||
fn set_attenuation(&mut self, channel: ChannelId, linear: Option<f32>) {
|
||||
self.with(channel, |c| c.linear = linear);
|
||||
}
|
||||
fn set_looping(&mut self, channel: ChannelId, looping: bool) {
|
||||
self.with(channel, |c| c.looping = looping);
|
||||
}
|
||||
fn set_position(&mut self, channel: ChannelId, p: [f64; 3]) {
|
||||
self.with(channel, |c| c.at = [p[0] as f32, p[1] as f32, p[2] as f32]);
|
||||
}
|
||||
fn set_relative(&mut self, channel: ChannelId, relative: bool) {
|
||||
self.with(channel, |c| c.relative = relative);
|
||||
}
|
||||
/// `getCompleteBuffer` (decoded now, cached) or `getStream` (decoded on a
|
||||
/// worker), then attach and `alSourcePlay`. A sound that cannot load
|
||||
/// leaves its source initial, as a failed buffer future does in vanilla.
|
||||
fn start(&mut self, channel: ChannelId, sound: &Sound, looping: bool) {
|
||||
if !self.live {
|
||||
return;
|
||||
}
|
||||
let (namespace, path) = sound.path();
|
||||
let file = self.root.join("assets").join(namespace).join(&path);
|
||||
let attach = move |mixer: &Mutex<Mixer>, samples: Option<(Arc<Vec<f32>>, u32)>| {
|
||||
if let Some(samples) = samples
|
||||
&& let Ok(mut m) = mixer.lock()
|
||||
&& let Some(c) = m.channels.get_mut(&channel)
|
||||
{
|
||||
c.samples = Some(samples);
|
||||
c.position = 0.0;
|
||||
c.state = State::Playing;
|
||||
}
|
||||
};
|
||||
if sound.stream {
|
||||
// A streamed sound loops by restarting its stream.
|
||||
let mixer = self.mixer.clone();
|
||||
std::thread::spawn(move || {
|
||||
let samples = decode(&file).map(|(s, r)| (Arc::new(s), r));
|
||||
if looping
|
||||
&& let Ok(mut m) = mixer.lock()
|
||||
&& let Some(c) = m.channels.get_mut(&channel)
|
||||
{
|
||||
c.looping = true;
|
||||
}
|
||||
attach(&mixer, samples);
|
||||
});
|
||||
} else {
|
||||
let samples = self
|
||||
.cache
|
||||
.entry(format!("{namespace}:{path}"))
|
||||
.or_insert_with(|| decode(&file).map(|(s, r)| (Arc::new(s), r)))
|
||||
.clone();
|
||||
attach(&self.mixer, samples);
|
||||
}
|
||||
}
|
||||
fn stop(&mut self, channel: ChannelId) {
|
||||
self.with(channel, |c| {
|
||||
if c.state != State::Initial {
|
||||
c.state = State::Stopped;
|
||||
}
|
||||
});
|
||||
}
|
||||
fn pause(&mut self, channel: ChannelId) {
|
||||
self.with(channel, |c| {
|
||||
if c.state == State::Playing {
|
||||
c.state = State::Paused;
|
||||
}
|
||||
});
|
||||
}
|
||||
fn unpause(&mut self, channel: ChannelId) {
|
||||
self.with(channel, |c| {
|
||||
if c.state == State::Paused {
|
||||
c.state = State::Playing;
|
||||
}
|
||||
});
|
||||
}
|
||||
fn stopped(&mut self, channel: ChannelId) -> bool {
|
||||
self.mixer
|
||||
.lock()
|
||||
.map(|m| {
|
||||
m.channels
|
||||
.get(&channel)
|
||||
.is_none_or(|c| c.state == State::Stopped)
|
||||
})
|
||||
.unwrap_or(true)
|
||||
}
|
||||
}
|
||||
/// The gain OpenAL applies for distance (`AL_LINEAR_DISTANCE` with reference
|
||||
/// distance 0 and rolloff 1: the distance clamps to the max distance).
|
||||
fn distance_gain(linear: Option<f32>, distance: f32) -> f32 {
|
||||
match linear {
|
||||
None => 1.0,
|
||||
Some(max) if max > 0.0 => (1.0 - distance.min(max) / max).clamp(0.0, 1.0),
|
||||
Some(_) => 0.0,
|
||||
}
|
||||
}
|
||||
fn mix(mixer: &mut Mixer, data: &mut [f32], channels: usize, rate: u32) {
|
||||
data.fill(0.0);
|
||||
let listener = mixer.listener;
|
||||
let yaw = mixer.listener_yaw.to_radians();
|
||||
// Gains and pans per source for this buffer.
|
||||
let mut voices: Vec<(ChannelId, f32, f32, f64)> = Vec::new();
|
||||
for (id, c) in &mixer.channels {
|
||||
let Some((_, source_rate)) = &c.samples else {
|
||||
continue;
|
||||
};
|
||||
if c.state != State::Playing {
|
||||
continue;
|
||||
}
|
||||
let d = if c.relative {
|
||||
[f64::from(c.at[0]), f64::from(c.at[1]), f64::from(c.at[2])]
|
||||
} else {
|
||||
[
|
||||
f64::from(c.at[0]) - listener[0],
|
||||
f64::from(c.at[1]) - listener[1],
|
||||
f64::from(c.at[2]) - listener[2],
|
||||
]
|
||||
};
|
||||
let distance = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt() as f32;
|
||||
let gain = c.gain.clamp(0.0, 1.0) * distance_gain(c.linear, distance);
|
||||
// The listener's right vector for a Minecraft yaw.
|
||||
let side = if c.relative {
|
||||
(d[0] as f32) / distance.max(0.001)
|
||||
} else {
|
||||
(-(yaw.cos()) as f64 * d[0] - (yaw.sin()) as f64 * d[2]) as f32 / distance.max(0.001)
|
||||
};
|
||||
let pan = side.clamp(-1.0, 1.0);
|
||||
let left = gain * (1.0 - pan.max(0.0) * 0.6);
|
||||
let right = gain * (1.0 + pan.min(0.0) * 0.6);
|
||||
let step = f64::from(*source_rate) / f64::from(rate) * f64::from(c.pitch.max(0.0));
|
||||
voices.push((*id, left, right, step));
|
||||
}
|
||||
for (id, left, right, step) in voices {
|
||||
let Some(c) = mixer.channels.get_mut(&id) else {
|
||||
continue;
|
||||
};
|
||||
let Some((samples, _)) = c.samples.clone() else {
|
||||
continue;
|
||||
};
|
||||
for frame in data.chunks_mut(channels) {
|
||||
let mut i = c.position as usize;
|
||||
if i + 1 >= samples.len() {
|
||||
if c.looping && samples.len() > 1 {
|
||||
c.position = 0.0;
|
||||
i = 0;
|
||||
} else {
|
||||
c.state = State::Stopped;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let t = (c.position - i as f64) as f32;
|
||||
let s = samples[i] * (1.0 - t) + samples[i + 1] * t;
|
||||
if channels >= 2 {
|
||||
frame[0] += s * left;
|
||||
frame[1] += s * right;
|
||||
} else {
|
||||
frame[0] += s * (left + right) / 2.0;
|
||||
}
|
||||
c.position += step;
|
||||
}
|
||||
}
|
||||
for s in data.iter_mut() {
|
||||
*s = s.clamp(-1.0, 1.0);
|
||||
}
|
||||
}
|
||||
impl Audio {
|
||||
pub fn new(root: &Path) -> Self {
|
||||
let registry = SoundRegistry::load(root);
|
||||
let mixer = Arc::new(Mutex::new(Mixer::default()));
|
||||
let (stream, live) = match Self::open(mixer.clone()) {
|
||||
Ok(v) => (Some(v), true),
|
||||
Err(e) => {
|
||||
eprintln!("Horizon: audio unavailable: {e}");
|
||||
(None, false)
|
||||
}
|
||||
};
|
||||
let mut engine = SoundEngine::new(registry);
|
||||
engine.loaded = live;
|
||||
Self {
|
||||
_stream: stream,
|
||||
subtitles,
|
||||
engine,
|
||||
backend: MixerBackend {
|
||||
mixer,
|
||||
root: root.to_owned(),
|
||||
cache: HashMap::new(),
|
||||
next: 0,
|
||||
live,
|
||||
},
|
||||
record_heard: false,
|
||||
heard: Vec::new(),
|
||||
mixer,
|
||||
rate,
|
||||
root: root.to_owned(),
|
||||
events,
|
||||
cache: HashMap::new(),
|
||||
seed: 0x5DEE_CE66,
|
||||
listener: [0.0; 3],
|
||||
listener_yaw: 0.0,
|
||||
master: 1.0,
|
||||
music: None,
|
||||
next_tracked: 0,
|
||||
random: LegacyRandom::new(crate::sound_engine::unique_seed()),
|
||||
source_override: None,
|
||||
capture: None,
|
||||
}
|
||||
}
|
||||
fn open(mixer: Arc<Mutex<Mixer>>) -> Result<(cpal::Stream, u32)> {
|
||||
fn open(mixer: Arc<Mutex<Mixer>>) -> Result<cpal::Stream> {
|
||||
let host = cpal::default_host();
|
||||
let device = host
|
||||
.default_output_device()
|
||||
@@ -209,269 +370,110 @@ impl Audio {
|
||||
let stream = device.build_output_stream(
|
||||
&config.into(),
|
||||
move |data: &mut [f32], _| {
|
||||
data.fill(0.0);
|
||||
let Ok(mut mixer) = mixer.lock() else { return };
|
||||
for frame in data.chunks_mut(channels) {
|
||||
let (mut l, mut r) = (0.0f32, 0.0f32);
|
||||
for v in mixer.voices.iter_mut() {
|
||||
let i = v.position as usize;
|
||||
if i + 1 >= v.samples.len() {
|
||||
continue;
|
||||
}
|
||||
let t = (v.position - i as f64) as f32;
|
||||
let s = v.samples[i] * (1.0 - t) + v.samples[i + 1] * t;
|
||||
l += s * v.left;
|
||||
r += s * v.right;
|
||||
v.position += v.step;
|
||||
}
|
||||
for t in mixer.tracked.iter_mut() {
|
||||
let v = &mut t.voice;
|
||||
let mut i = v.position as usize;
|
||||
if t.looping && i + 1 >= v.samples.len() {
|
||||
v.position = 0.0;
|
||||
i = 0;
|
||||
}
|
||||
if i + 1 < v.samples.len() {
|
||||
let f = (v.position - i as f64) as f32;
|
||||
let s = (v.samples[i] * (1.0 - f) + v.samples[i + 1] * f) * t.volume;
|
||||
l += s * v.left;
|
||||
r += s * v.right;
|
||||
v.position += v.step;
|
||||
}
|
||||
}
|
||||
let gain = mixer.music_gain;
|
||||
if let Some(v) = mixer.music.as_mut() {
|
||||
let i = v.position as usize;
|
||||
if i + 1 < v.samples.len() {
|
||||
let t = (v.position - i as f64) as f32;
|
||||
let s = (v.samples[i] * (1.0 - t) + v.samples[i + 1] * t) * gain;
|
||||
l += s * v.left;
|
||||
r += s * v.right;
|
||||
v.position += v.step;
|
||||
}
|
||||
}
|
||||
if channels >= 2 {
|
||||
frame[0] = l.clamp(-1.0, 1.0);
|
||||
frame[1] = r.clamp(-1.0, 1.0);
|
||||
} else {
|
||||
frame[0] = ((l + r) / 2.0).clamp(-1.0, 1.0);
|
||||
}
|
||||
}
|
||||
mixer
|
||||
.voices
|
||||
.retain(|v| (v.position as usize) + 1 < v.samples.len());
|
||||
mixer.tracked.retain(|t| {
|
||||
t.looping || (t.voice.position as usize) + 1 < t.voice.samples.len()
|
||||
});
|
||||
if mixer
|
||||
.music
|
||||
.as_ref()
|
||||
.is_some_and(|v| (v.position as usize) + 1 >= v.samples.len())
|
||||
{
|
||||
mixer.music = None;
|
||||
if let Ok(mut m) = mixer.lock() {
|
||||
mix(&mut m, data, channels, rate);
|
||||
} else {
|
||||
data.fill(0.0);
|
||||
}
|
||||
},
|
||||
|e| eprintln!("Horizon: audio stream error: {e}"),
|
||||
None,
|
||||
)?;
|
||||
stream.play()?;
|
||||
Ok((stream, rate))
|
||||
Ok(stream)
|
||||
}
|
||||
fn random(&mut self) -> u64 {
|
||||
self.seed ^= self.seed << 13;
|
||||
self.seed ^= self.seed >> 7;
|
||||
self.seed ^= self.seed << 17;
|
||||
self.seed
|
||||
}
|
||||
fn resolve(&mut self, event: &str, depth: u32) -> Option<Variant> {
|
||||
let variants = self.events.get(event)?.clone();
|
||||
let total: u32 = variants.iter().map(|v| v.weight).sum();
|
||||
if total == 0 {
|
||||
return None;
|
||||
fn bound(&mut self) -> Bound<'_, MixerBackend> {
|
||||
Bound {
|
||||
engine: &mut self.engine,
|
||||
backend: &mut self.backend,
|
||||
}
|
||||
let mut pick = (self.random() % u64::from(total)) as u32;
|
||||
let chosen = variants.into_iter().find(|v| {
|
||||
if pick < v.weight {
|
||||
true
|
||||
} else {
|
||||
pick -= v.weight;
|
||||
false
|
||||
}
|
||||
/// `SoundManager.updateSource`: the camera position and yaw.
|
||||
pub fn set_listener(&mut self, position: [f64; 3], yaw: f32) {
|
||||
self.listener = position;
|
||||
self.listener_yaw = yaw;
|
||||
if let Ok(mut m) = self.backend.mixer.lock() {
|
||||
m.listener = position;
|
||||
m.listener_yaw = yaw;
|
||||
}
|
||||
}
|
||||
/// Sync the volume options, refreshing the categories that changed
|
||||
/// (the option sliders' `refreshCategoryVolume`).
|
||||
fn sync_volumes(&mut self) {
|
||||
let mut volumes = [self.master; 11];
|
||||
for (i, v) in volumes.iter_mut().enumerate().skip(1) {
|
||||
*v = crate::settings::source_volume(i);
|
||||
}
|
||||
for source in 0..11 {
|
||||
if self.engine.source_volumes[source] != volumes[source] {
|
||||
self.engine.source_volumes[source] = volumes[source];
|
||||
self.engine
|
||||
.refresh_category_volume(source, &mut self.backend);
|
||||
}
|
||||
})?;
|
||||
if chosen.is_event && depth < 4 {
|
||||
let name = if chosen.name.contains(':') {
|
||||
chosen.name.clone()
|
||||
} else {
|
||||
format!("minecraft:{}", chosen.name)
|
||||
};
|
||||
let mut inner = self.resolve(&name, depth + 1)?;
|
||||
inner.volume *= chosen.volume;
|
||||
inner.pitch *= chosen.pitch;
|
||||
return Some(inner);
|
||||
}
|
||||
Some(chosen)
|
||||
self.engine.listening = self.record_heard;
|
||||
}
|
||||
/// Play a sound event at a world position (None = non-positional UI sound).
|
||||
/// `SoundManager.tick(paused)`, once per client tick.
|
||||
pub fn tick(&mut self, paused: bool, ctx: &TickContext) {
|
||||
self.sync_volumes();
|
||||
self.engine.tick(paused, ctx, &mut self.backend);
|
||||
}
|
||||
/// `SoundManager.pauseAllExcept(MUSIC, UI)`.
|
||||
pub fn pause(&mut self) {
|
||||
self.engine.pause_all_except(
|
||||
&[crate::sound_engine::MUSIC, crate::sound_engine::UI],
|
||||
&mut self.backend,
|
||||
);
|
||||
}
|
||||
/// `SoundManager.resume`.
|
||||
pub fn resume(&mut self) {
|
||||
self.engine.resume(&mut self.backend);
|
||||
}
|
||||
/// `SoundManager.stop()`: every sound.
|
||||
pub fn stop_all(&mut self) {
|
||||
self.engine.stop_all(&mut self.backend);
|
||||
}
|
||||
/// Play a sound event at a world position, as `ClientLevel.playLocalSound`
|
||||
/// (a `SimpleSoundInstance` seeded from the level random), or relative to
|
||||
/// the listener without attenuation (`None`).
|
||||
pub fn play(&mut self, event: &str, at: Option<[f64; 3]>, volume: f32, pitch: f32) {
|
||||
let seed = self.random.next_long();
|
||||
self.play_seeded(event, at, volume, pitch, seed);
|
||||
}
|
||||
/// `play` with the seed a sound packet carries.
|
||||
pub fn play_seeded(
|
||||
&mut self,
|
||||
event: &str,
|
||||
at: Option<[f64; 3]>,
|
||||
volume: f32,
|
||||
pitch: f32,
|
||||
seed: i64,
|
||||
) {
|
||||
if let (Some(capture), Some(at)) = (self.capture.as_mut(), at) {
|
||||
capture.push((event.to_owned(), at, volume, pitch));
|
||||
}
|
||||
let Some(variant) = self.resolve(event, 0) else {
|
||||
return;
|
||||
};
|
||||
if self.record_heard
|
||||
&& let Some(key) = self.subtitles.get(event)
|
||||
{
|
||||
let range = if at.is_some() {
|
||||
variant.attenuation * volume.max(1.0)
|
||||
} else {
|
||||
f32::INFINITY
|
||||
};
|
||||
self.heard.push(Heard {
|
||||
subtitle: key.clone(),
|
||||
at,
|
||||
range,
|
||||
looping: false,
|
||||
});
|
||||
}
|
||||
if self._stream.is_none() {
|
||||
return;
|
||||
}
|
||||
let file = variant.name.trim_start_matches("minecraft:").to_owned();
|
||||
let entry = self.cache.entry(file.clone()).or_insert_with(|| {
|
||||
decode(
|
||||
&self
|
||||
.root
|
||||
.join(format!("assets/minecraft/sounds/{file}.ogg")),
|
||||
)
|
||||
.map(|(s, r)| (Arc::new(s), r))
|
||||
});
|
||||
let Some((samples, rate)) = entry.clone() else {
|
||||
return;
|
||||
};
|
||||
self.sync_volumes();
|
||||
let event = crate::sound_events::normalize(event);
|
||||
let source = self
|
||||
.source_override
|
||||
.unwrap_or_else(|| crate::settings::sound_source(event));
|
||||
let volume = (volume * variant.volume).clamp(0.0, 1.0)
|
||||
* self.master
|
||||
* crate::settings::source_volume(source);
|
||||
let pitch = (pitch * variant.pitch).clamp(0.5, 2.0);
|
||||
let (mut left, mut right) = (volume, volume);
|
||||
if let Some(p) = at {
|
||||
let d = [
|
||||
p[0] - self.listener[0],
|
||||
p[1] - self.listener[1],
|
||||
p[2] - self.listener[2],
|
||||
];
|
||||
let distance = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt() as f32;
|
||||
let range = variant.attenuation * volume.max(1.0);
|
||||
let gain = (1.0 - distance / range).clamp(0.0, 1.0);
|
||||
if gain <= 0.0 {
|
||||
return;
|
||||
}
|
||||
let yaw = self.listener_yaw.to_radians();
|
||||
// Right vector of a Minecraft yaw.
|
||||
let side = (-(yaw.cos()) as f64 * d[0] - (yaw.sin()) as f64 * d[2]) as f32
|
||||
/ distance.max(0.001);
|
||||
let pan = side.clamp(-1.0, 1.0);
|
||||
left = volume * gain * (1.0 - pan.max(0.0) * 0.6);
|
||||
right = volume * gain * (1.0 + pan.min(0.0) * 0.6);
|
||||
}
|
||||
let step = f64::from(rate) / f64::from(self.rate) * f64::from(pitch);
|
||||
if let Ok(mut m) = self.mixer.lock() {
|
||||
if m.voices.len() > 64 {
|
||||
m.voices.remove(0);
|
||||
}
|
||||
m.voices.push(Voice {
|
||||
samples,
|
||||
position: 0.0,
|
||||
step,
|
||||
left,
|
||||
right,
|
||||
event: event.into(),
|
||||
.unwrap_or_else(|| crate::settings::sound_source(&event));
|
||||
let random = LegacyRandom::new(seed);
|
||||
let instance = match at {
|
||||
Some(at) => SoundInstance::at(&event, source, volume, pitch, random, at),
|
||||
None => SoundInstance::new(
|
||||
&event,
|
||||
source,
|
||||
});
|
||||
}
|
||||
}
|
||||
/// `SoundManager.play` of a music instance (`SimpleSoundInstance.forMusic`:
|
||||
/// non-positional, streamed). Decoding runs on a worker thread; the track
|
||||
/// counts as active from now on. Returns false when nothing can play.
|
||||
pub fn play_music(&mut self, event: &str) -> bool {
|
||||
self.stop_music();
|
||||
if self._stream.is_none() {
|
||||
return false;
|
||||
}
|
||||
let Some(variant) = self.resolve(event, 0) else {
|
||||
return false;
|
||||
volume,
|
||||
pitch,
|
||||
random,
|
||||
false,
|
||||
0,
|
||||
Attenuation::None,
|
||||
[0.0; 3],
|
||||
true,
|
||||
),
|
||||
};
|
||||
let path = self.root.join(format!(
|
||||
"assets/minecraft/sounds/{}.ogg",
|
||||
variant.name.trim_start_matches("minecraft:")
|
||||
));
|
||||
let volume =
|
||||
variant.volume.clamp(0.0, 1.0) * self.master * crate::settings::event_volume(event);
|
||||
let step_scale = f64::from(variant.pitch.clamp(0.5, 2.0)) / f64::from(self.rate);
|
||||
let ready = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let (mixer, done) = (self.mixer.clone(), ready.clone());
|
||||
let music_event = event.to_owned();
|
||||
std::thread::spawn(move || {
|
||||
if let Some((samples, rate)) = decode(&path)
|
||||
&& let Ok(mut m) = mixer.lock()
|
||||
&& !done.load(std::sync::atomic::Ordering::Acquire)
|
||||
{
|
||||
m.music = Some(Voice {
|
||||
samples: Arc::new(samples),
|
||||
position: 0.0,
|
||||
step: f64::from(rate) * step_scale,
|
||||
left: volume,
|
||||
right: volume,
|
||||
event: music_event.as_str().into(),
|
||||
source: 1,
|
||||
});
|
||||
}
|
||||
done.store(true, std::sync::atomic::Ordering::Release);
|
||||
});
|
||||
self.music = Some(MusicTrack {
|
||||
event: event.to_owned(),
|
||||
ready,
|
||||
});
|
||||
true
|
||||
}
|
||||
/// `SoundManager.stop` of the music instance.
|
||||
pub fn stop_music(&mut self) {
|
||||
if let Some(track) = self.music.take() {
|
||||
// A decoder still running sees the flag and drops its result.
|
||||
track
|
||||
.ready
|
||||
.store(true, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
if let Ok(mut m) = self.mixer.lock() {
|
||||
m.music = None;
|
||||
}
|
||||
}
|
||||
/// `SoundManager.isActive`: decoding or still sounding.
|
||||
pub fn music_active(&mut self) -> bool {
|
||||
let Some(track) = &self.music else {
|
||||
return false;
|
||||
};
|
||||
if !track.ready.load(std::sync::atomic::Ordering::Acquire) {
|
||||
return true;
|
||||
}
|
||||
let playing = self.mixer.lock().is_ok_and(|m| m.music.is_some());
|
||||
if !playing {
|
||||
self.music = None;
|
||||
}
|
||||
playing
|
||||
}
|
||||
/// The event of the current music track.
|
||||
pub fn music_event(&self) -> Option<&str> {
|
||||
self.music.as_ref().map(|t| t.event.as_str())
|
||||
}
|
||||
/// Every sound event id (for command completion).
|
||||
pub fn event_names(&self) -> Vec<String> {
|
||||
let mut v: Vec<String> = self.events.keys().cloned().collect();
|
||||
v.sort();
|
||||
v
|
||||
self.play_instance(instance, false);
|
||||
}
|
||||
/// Play a sound on a given `SoundSource` index (`/playsound`).
|
||||
pub fn play_on(
|
||||
@@ -486,102 +488,111 @@ impl Audio {
|
||||
self.play(event, at, volume, pitch);
|
||||
self.source_override = None;
|
||||
}
|
||||
/// `SimpleSoundInstance.forJukeboxSong` at a jukebox; the handle stays
|
||||
/// until `release`.
|
||||
pub fn play_jukebox_song(&mut self, event: &str, pos: [i32; 3]) -> InstanceId {
|
||||
let at = [
|
||||
f64::from(pos[0]) + 0.5,
|
||||
f64::from(pos[1]) + 0.5,
|
||||
f64::from(pos[2]) + 0.5,
|
||||
];
|
||||
let event = crate::sound_events::normalize(event);
|
||||
let instance = SoundInstance::for_jukebox_song(&event, at, SoundEngine::unseeded());
|
||||
self.play_instance(instance, true).0
|
||||
}
|
||||
/// Stop and drop a held instance.
|
||||
pub fn stop_and_release(&mut self, id: InstanceId) {
|
||||
self.stop_instance(id);
|
||||
self.release(id);
|
||||
}
|
||||
/// Every sound event id (for command completion).
|
||||
pub fn event_names(&self) -> Vec<String> {
|
||||
let mut v: Vec<String> = self.engine.registry.names().cloned().collect();
|
||||
v.sort();
|
||||
v
|
||||
}
|
||||
/// `ClientboundStopSoundPacket`: stop the playing sounds of an event
|
||||
/// and/or a source.
|
||||
pub fn stop_sounds(&mut self, event: Option<&str>, source: Option<usize>) {
|
||||
let matches = |v: &Voice| {
|
||||
event.is_none_or(|e| &*v.event == e) && source.is_none_or(|s| v.source == s)
|
||||
};
|
||||
if let Ok(mut m) = self.mixer.lock() {
|
||||
m.voices.retain(|v| !matches(v));
|
||||
m.tracked.retain(|t| !matches(&t.voice));
|
||||
if m.music.as_ref().is_some_and(matches) {
|
||||
m.music = None;
|
||||
}
|
||||
}
|
||||
if self
|
||||
.music
|
||||
.as_ref()
|
||||
.is_some_and(|t| event.is_none_or(|e| t.event == e) && source.is_none_or(|s| s == 1))
|
||||
{
|
||||
self.music = None;
|
||||
}
|
||||
let event = event.map(crate::sound_events::normalize);
|
||||
self.engine
|
||||
.stop_matching(event.as_deref(), source, &mut self.backend);
|
||||
}
|
||||
/// The sounds played since the last call, for the subtitle overlay.
|
||||
pub fn take_heard(&mut self) -> Vec<Heard> {
|
||||
std::mem::take(&mut self.heard)
|
||||
}
|
||||
/// `SoundManager.updateCategoryVolume(MUSIC, gain)`.
|
||||
pub fn set_music_gain(&mut self, gain: f32) {
|
||||
if let Ok(mut m) = self.mixer.lock() {
|
||||
m.music_gain = gain;
|
||||
}
|
||||
}
|
||||
/// Play a relative (non-positional) tickable sound instance the caller
|
||||
/// keeps a handle to: ambient loops (`looping`) and sub-sounds.
|
||||
pub fn play_tracked(&mut self, event: &str, volume: f32, looping: bool) -> Option<u64> {
|
||||
let variant = self.resolve(event, 0)?;
|
||||
if self.record_heard
|
||||
&& let Some(key) = self.subtitles.get(event)
|
||||
{
|
||||
self.heard.push(Heard {
|
||||
subtitle: key.clone(),
|
||||
at: None,
|
||||
range: f32::INFINITY,
|
||||
looping,
|
||||
});
|
||||
}
|
||||
self._stream.as_ref()?;
|
||||
let file = variant.name.trim_start_matches("minecraft:").to_owned();
|
||||
let root = &self.root;
|
||||
let (samples, rate) = self
|
||||
.cache
|
||||
.entry(file.clone())
|
||||
.or_insert_with(|| {
|
||||
decode(&root.join(format!("assets/minecraft/sounds/{file}.ogg")))
|
||||
.map(|(s, r)| (Arc::new(s), r))
|
||||
std::mem::take(&mut self.engine.notifications)
|
||||
.into_iter()
|
||||
.filter_map(|n| {
|
||||
Some(Heard {
|
||||
subtitle: n.subtitle?,
|
||||
at: Some(n.position),
|
||||
range: n.range,
|
||||
looping: n.looping,
|
||||
})
|
||||
})
|
||||
.clone()?;
|
||||
let gain =
|
||||
variant.volume.clamp(0.0, 1.0) * self.master * crate::settings::event_volume(event);
|
||||
let step =
|
||||
f64::from(rate) / f64::from(self.rate) * f64::from(variant.pitch.clamp(0.5, 2.0));
|
||||
self.next_tracked += 1;
|
||||
let id = self.next_tracked;
|
||||
let mut m = self.mixer.lock().ok()?;
|
||||
m.tracked.push(Tracked {
|
||||
id,
|
||||
voice: Voice {
|
||||
samples,
|
||||
position: 0.0,
|
||||
step,
|
||||
left: gain,
|
||||
right: gain,
|
||||
event: event.into(),
|
||||
source: crate::settings::sound_source(event),
|
||||
},
|
||||
looping,
|
||||
volume: volume.clamp(0.0, 1.0),
|
||||
});
|
||||
Some(id)
|
||||
}
|
||||
/// A tracked instance's volume (`AbstractTickableSoundInstance.volume`).
|
||||
pub fn set_tracked_volume(&mut self, id: u64, volume: f32) {
|
||||
if let Ok(mut m) = self.mixer.lock()
|
||||
&& let Some(t) = m.tracked.iter_mut().find(|t| t.id == id)
|
||||
{
|
||||
t.volume = volume.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
pub fn stop_tracked(&mut self, id: u64) {
|
||||
if let Ok(mut m) = self.mixer.lock() {
|
||||
m.tracked.retain(|t| t.id != id);
|
||||
}
|
||||
}
|
||||
/// Still sounding (a finished one-shot is gone).
|
||||
pub fn tracked_active(&self, id: u64) -> bool {
|
||||
self.mixer
|
||||
.lock()
|
||||
.is_ok_and(|m| m.tracked.iter().any(|t| t.id == id))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
impl Sounds for Audio {
|
||||
fn engine(&mut self) -> &mut SoundEngine {
|
||||
&mut self.engine
|
||||
}
|
||||
fn play_instance(&mut self, instance: SoundInstance, retain: bool) -> (InstanceId, PlayResult) {
|
||||
self.sync_volumes();
|
||||
self.bound().play_instance(instance, retain)
|
||||
}
|
||||
fn play_id(&mut self, id: InstanceId) -> PlayResult {
|
||||
self.sync_volumes();
|
||||
self.bound().play_id(id)
|
||||
}
|
||||
fn stop_instance(&mut self, id: InstanceId) {
|
||||
self.bound().stop_instance(id);
|
||||
}
|
||||
fn update_category_volume(&mut self, source: usize, gain: f32) {
|
||||
self.bound().update_category_volume(source, gain);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn channel(samples: Vec<f32>, linear: Option<f32>, at: [f32; 3], looping: bool) -> Channel {
|
||||
Channel {
|
||||
samples: Some((Arc::new(samples), 48_000)),
|
||||
position: 0.0,
|
||||
state: State::Playing,
|
||||
gain: 1.0,
|
||||
pitch: 1.0,
|
||||
linear,
|
||||
looping,
|
||||
at,
|
||||
relative: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linear_attenuation_reaches_zero_at_the_max_distance() {
|
||||
assert_eq!(distance_gain(None, 100.0), 1.0);
|
||||
assert_eq!(distance_gain(Some(16.0), 0.0), 1.0);
|
||||
assert_eq!(distance_gain(Some(16.0), 8.0), 0.5);
|
||||
assert_eq!(distance_gain(Some(16.0), 16.0), 0.0);
|
||||
assert_eq!(distance_gain(Some(16.0), 40.0), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sources_play_attenuated_then_stop_or_loop() {
|
||||
let mut m = Mixer::default();
|
||||
m.channels
|
||||
.insert(1, channel(vec![1.0; 4], Some(16.0), [8.0, 0.0, 0.0], false));
|
||||
m.channels
|
||||
.insert(2, channel(vec![1.0; 4], None, [0.0; 3], true));
|
||||
let mut data = [0.0f32; 16];
|
||||
mix(&mut m, &mut data, 2, 48_000);
|
||||
assert_eq!(m.channels[&1].state, State::Stopped);
|
||||
assert_eq!(m.channels[&2].state, State::Playing);
|
||||
// Mono sum of the two at the first frame: the far one is half as loud.
|
||||
let first = data[0] + data[1];
|
||||
assert!(first > 1.4 && first < 3.1, "{first}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -594,6 +594,8 @@ struct Game<'a> {
|
||||
audio: Audio,
|
||||
music: horizon_client::music::MusicManager,
|
||||
ambience: horizon_client::ambience::Ambience,
|
||||
/// The sound engine was told the game paused (pauseAllExcept).
|
||||
sound_paused: bool,
|
||||
dropped: Vec<ItemEntity>,
|
||||
next_entity: u32,
|
||||
particles: horizon_client::particles::ParticleEngine,
|
||||
@@ -5694,6 +5696,10 @@ impl Game<'_> {
|
||||
}
|
||||
fn close_world(&mut self) -> Result<()> {
|
||||
self.ambience.clear(&mut self.audio);
|
||||
// updateLevelInEngines(null): every sound stops; the music manager
|
||||
// lets go of its track.
|
||||
self.music.stop_playing(None, &mut self.audio);
|
||||
self.audio.stop_all();
|
||||
if self.remote.is_some() {
|
||||
return self.leave_server(None);
|
||||
}
|
||||
@@ -7389,8 +7395,8 @@ impl Game<'_> {
|
||||
self.pitch,
|
||||
),
|
||||
};
|
||||
self.audio.listener = [camera.x, camera.y, camera.z];
|
||||
self.audio.listener_yaw = view_yaw;
|
||||
self.audio
|
||||
.set_listener([camera.x, camera.y, camera.z], view_yaw);
|
||||
let block = [
|
||||
camera.x.floor() as i32,
|
||||
camera.y.floor() as i32,
|
||||
@@ -8193,6 +8199,7 @@ impl ApplicationHandler for Game<'_> {
|
||||
self.tick_fov();
|
||||
}
|
||||
self.tick_music();
|
||||
self.tick_sound_engine();
|
||||
self.clock.record(started.elapsed());
|
||||
if let Some(report) = self.clock.take_sprint_report() {
|
||||
self.sprint_report(&report);
|
||||
@@ -8483,6 +8490,7 @@ pub fn run() -> Result<()> {
|
||||
audio,
|
||||
music: Default::default(),
|
||||
ambience: Default::default(),
|
||||
sound_paused: false,
|
||||
dropped: Vec::new(),
|
||||
next_entity: 0,
|
||||
particles,
|
||||
|
||||
@@ -509,7 +509,7 @@ impl Game<'_> {
|
||||
"frames": self.frames,
|
||||
"ticks": self.ticks,
|
||||
"day_time": self.world.day_time,
|
||||
"music": self.music.current(),
|
||||
"music": self.music.current(&self.audio.engine),
|
||||
"fishing": self.hook_status(),
|
||||
"tridents": self.tridents.len(),
|
||||
"rockets": self.rockets.len(),
|
||||
|
||||
@@ -12,9 +12,6 @@ use horizon_client::{
|
||||
use horizon_core::{position::BlockPos, random::RandomSource};
|
||||
use horizon_world::level::flags as level_flags;
|
||||
|
||||
/// Linear attenuation range of a song (`SimpleSoundInstance.forJukeboxSong`: volume 4).
|
||||
const RANGE: f64 = 64.0;
|
||||
|
||||
impl Game<'_> {
|
||||
fn jukebox(&self, pos: [i32; 3]) -> Option<&Jukebox> {
|
||||
match self.world.block_entities.get(&(pos[0], pos[1], pos[2])) {
|
||||
@@ -101,9 +98,9 @@ impl Game<'_> {
|
||||
let Some(s) = self.res.items.songs.get(song).cloned() else {
|
||||
return;
|
||||
};
|
||||
if let Some(id) = self.audio.play_tracked(&s.sound, 0.0, false) {
|
||||
self.jukebox_sounds.insert((pos[0], pos[1], pos[2]), id);
|
||||
}
|
||||
// LevelRenderer.playJukeboxSong: SimpleSoundInstance.forJukeboxSong.
|
||||
let id = self.audio.play_jukebox_song(&s.sound, pos);
|
||||
self.jukebox_sounds.insert((pos[0], pos[1], pos[2]), id);
|
||||
let description =
|
||||
horizon_client::component::to_text(&s.description, &mut |k| Text::literal(k));
|
||||
self.titles
|
||||
@@ -113,7 +110,7 @@ impl Game<'_> {
|
||||
}
|
||||
fn stop_song(&mut self, pos: [i32; 3]) {
|
||||
if let Some(id) = self.jukebox_sounds.remove(&(pos[0], pos[1], pos[2])) {
|
||||
self.audio.stop_tracked(id);
|
||||
self.audio.stop_and_release(id);
|
||||
}
|
||||
}
|
||||
/// `popOutTheItem`: the disc above the jukebox, and the song stops.
|
||||
@@ -202,30 +199,20 @@ impl Game<'_> {
|
||||
}
|
||||
self.update_song_volumes();
|
||||
}
|
||||
/// Linear distance attenuation of each song at the listener.
|
||||
/// Let go of songs that ended or whose jukebox stopped.
|
||||
fn update_song_volumes(&mut self) {
|
||||
let Some(eye) = self.eye_position() else {
|
||||
return;
|
||||
};
|
||||
let sounds: Vec<_> = self
|
||||
.jukebox_sounds
|
||||
.iter()
|
||||
.map(|(k, id)| (*k, *id))
|
||||
.collect();
|
||||
for (k, id) in sounds {
|
||||
if !self.audio.tracked_active(id)
|
||||
if !self.audio.engine.is_active(id)
|
||||
|| !matches!(self.world.block_entities.get(&k), Some(BlockEntity::Jukebox(j)) if j.playing)
|
||||
{
|
||||
self.audio.stop_tracked(id);
|
||||
self.audio.stop_and_release(id);
|
||||
self.jukebox_sounds.remove(&k);
|
||||
continue;
|
||||
}
|
||||
let d = ((f64::from(k.0) + 0.5 - eye[0]).powi(2)
|
||||
+ (f64::from(k.1) + 0.5 - eye[1]).powi(2)
|
||||
+ (f64::from(k.2) + 0.5 - eye[2]).powi(2))
|
||||
.sqrt();
|
||||
self.audio
|
||||
.set_tracked_volume(id, (1.0 - d / RANGE).clamp(0.0, 1.0) as f32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
//! `Minecraft.getSituationalMusic` and `getMusicVolume` for the music manager.
|
||||
//! `Minecraft.getSituationalMusic` and `getMusicVolume` for the music manager,
|
||||
//! the local player's ambient sound handlers and the sound engine tick.
|
||||
use super::Game;
|
||||
use horizon_client::{blocks::Fluid, music::Music};
|
||||
use horizon_client::{
|
||||
blocks::Fluid,
|
||||
music::{BackgroundMusic, Music, Situation},
|
||||
sound_engine::TickContext,
|
||||
};
|
||||
|
||||
impl Game<'_> {
|
||||
/// `Minecraft.tick` -> `MusicManager.tick`, once per client tick.
|
||||
@@ -11,41 +16,86 @@ impl Game<'_> {
|
||||
self.music
|
||||
.tick(music.as_ref(), volume, loading, &mut self.audio);
|
||||
}
|
||||
fn situational_music(&self) -> (Option<Music>, f32) {
|
||||
// Title screen: no player, Musics.MENU at full volume.
|
||||
if self.title.is_some() {
|
||||
return (Some(Music::menu()), 1.0);
|
||||
/// `Minecraft.tick` -> `SoundManager.tick(pause)`, after the music, with
|
||||
/// `pauseAllExcept(MUSIC, UI)` when the game pauses and `resume` when it
|
||||
/// carries on.
|
||||
pub(super) fn tick_sound_engine(&mut self) {
|
||||
if self.pause != self.sound_paused {
|
||||
if self.pause {
|
||||
self.audio.pause();
|
||||
} else {
|
||||
self.audio.resume();
|
||||
}
|
||||
self.sound_paused = self.pause;
|
||||
}
|
||||
let Some(eye) = self.eye_position() else {
|
||||
return (Some(Music::menu()), 1.0);
|
||||
let ctx = TickContext {
|
||||
player_underwater: self.ambience.was_underwater(),
|
||||
player_removed: self.player.is_none(),
|
||||
};
|
||||
self.audio.tick(self.pause, &ctx);
|
||||
}
|
||||
/// `getSituationalMusic` and `getMusicVolume`.
|
||||
fn situational_music(&self) -> (Option<Music>, f32) {
|
||||
let empty = BackgroundMusic::default();
|
||||
// The title screen has no player (and no level for the camera probe).
|
||||
let eye = if self.title.is_some() {
|
||||
None
|
||||
} else {
|
||||
self.eye_position()
|
||||
};
|
||||
let (background, probe_volume) = match eye {
|
||||
Some(eye) => {
|
||||
let (dimension, biome) = (self.dim.id(), self.biome_at(eye));
|
||||
let tables = &self.res.music;
|
||||
(
|
||||
tables.background(dimension, biome),
|
||||
tables.volume(dimension, biome),
|
||||
)
|
||||
}
|
||||
None => (empty, 1.0),
|
||||
};
|
||||
let situation = Situation {
|
||||
screen_music: None,
|
||||
in_world: eye.is_some(),
|
||||
in_end: self.dim == super::Dim::End,
|
||||
boss_music: self.boss_music(),
|
||||
background: &background,
|
||||
probe_volume,
|
||||
creative: self.creative,
|
||||
underwater: self.ambience.was_underwater(),
|
||||
};
|
||||
(situation.music(), situation.volume())
|
||||
}
|
||||
/// `BiomeManager.getNoiseBiomeAtPosition`: the quart cell holding a position.
|
||||
fn biome_at(&self, p: [f64; 3]) -> &str {
|
||||
let (x, y, z) = (
|
||||
eye[0].floor() as i32,
|
||||
eye[1].floor() as i32,
|
||||
eye[2].floor() as i32,
|
||||
p[0].floor() as i32,
|
||||
p[1].floor() as i32,
|
||||
p[2].floor() as i32,
|
||||
);
|
||||
let biome = self
|
||||
.world
|
||||
self.world
|
||||
.chunks
|
||||
.get(&(x >> 4, z >> 4))
|
||||
.map_or("minecraft:plains", |c| {
|
||||
self.res.biomes.list[c.biome(x, y, z) as usize]
|
||||
.name
|
||||
.as_str()
|
||||
});
|
||||
// Minecraft.getSituationalMusic: the boss bar's music in the End.
|
||||
if self.boss_music() {
|
||||
return (Some(Music::end_boss()), 1.0);
|
||||
})
|
||||
}
|
||||
/// `isEyeInFluid(WATER)`: the eye inside the water of its block column.
|
||||
fn eye_in_water(&self, x: f64, eye_y: f64, z: f64) -> bool {
|
||||
let (bx, by, bz) = (x.floor() as i32, eye_y.floor() as i32, z.floor() as i32);
|
||||
let f = self.res.facts.get(self.world.block(bx, by, bz));
|
||||
if f.fluid != Fluid::Water {
|
||||
return false;
|
||||
}
|
||||
let dimension = self.dim.id();
|
||||
let tables = &self.res.music;
|
||||
// Player.isUnderWater: eyes in water.
|
||||
let underwater = self.res.facts.get(self.world.block(x, y, z)).fluid == Fluid::Water;
|
||||
let music = tables
|
||||
.background(dimension, biome)
|
||||
.select(self.creative, underwater)
|
||||
.cloned();
|
||||
(music, tables.volume(dimension, biome))
|
||||
let above = self.res.facts.get(self.world.block(bx, by + 1, bz));
|
||||
let height = if above.fluid == Fluid::Water {
|
||||
1.0
|
||||
} else {
|
||||
f64::from(f.fluid_level) / 9.0
|
||||
};
|
||||
eye_y <= f64::from(by) + height
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,33 +107,46 @@ impl Game<'_> {
|
||||
};
|
||||
let feet = [p.position.x, p.position.y, p.position.z];
|
||||
let eye_y = feet[1] + f64::from(p.dimensions.eye_height);
|
||||
let (x, z) = (feet[0].floor() as i32, feet[2].floor() as i32);
|
||||
let sounds = self.res.ambient.get(self.dim.id(), self.biome_at(feet));
|
||||
let underwater = self.eye_in_water(feet[0], eye_y, feet[2]);
|
||||
let world = &self.world;
|
||||
let biome = world
|
||||
.chunks
|
||||
.get(&(x >> 4, z >> 4))
|
||||
.map_or("minecraft:plains", |c| {
|
||||
self.res.biomes.list[c.biome(x, feet[1].floor() as i32, z) as usize]
|
||||
.name
|
||||
.as_str()
|
||||
});
|
||||
let sounds = self.res.ambient.get(self.dim.id(), biome);
|
||||
let underwater = self
|
||||
.res
|
||||
.facts
|
||||
.get(world.block(x, eye_y.floor() as i32, z))
|
||||
.fluid
|
||||
== Fluid::Water;
|
||||
let light = |b: [i32; 3]| {
|
||||
let l = world.light(b[0], b[1], b[2]);
|
||||
(l >> 4, l & 15)
|
||||
};
|
||||
let (half, height) = (
|
||||
f64::from(p.dimensions.width / 2.0),
|
||||
f64::from(p.dimensions.height),
|
||||
);
|
||||
let registry = &self.res.assets.registry;
|
||||
let facts = &self.res.facts;
|
||||
let bubble_column = horizon_client::ambience::bubble_column_in(
|
||||
[
|
||||
feet[0] - half,
|
||||
feet[1],
|
||||
feet[2] - half,
|
||||
feet[0] + half,
|
||||
feet[1] + height,
|
||||
feet[2] + half,
|
||||
],
|
||||
|b| {
|
||||
let state = world.block(b[0], b[1], b[2]);
|
||||
(&*facts.get(state).name == "minecraft:bubble_column").then(|| {
|
||||
registry
|
||||
.state(state)
|
||||
.and_then(|s| s.properties.get("drag"))
|
||||
.is_some_and(|d| d == "true")
|
||||
})
|
||||
},
|
||||
);
|
||||
let ctx = horizon_client::ambience::AmbientContext {
|
||||
sounds: &sounds,
|
||||
position: feet,
|
||||
eye_y,
|
||||
underwater,
|
||||
spectator: self.spectator(),
|
||||
light: &light,
|
||||
bubble_column,
|
||||
};
|
||||
self.ambience.tick(&ctx, &mut self.audio);
|
||||
}
|
||||
|
||||
@@ -490,7 +490,7 @@ impl Game<'_> {
|
||||
if !open {
|
||||
self.recipe_ui.overlay = None;
|
||||
}
|
||||
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
|
||||
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
||||
return true;
|
||||
}
|
||||
if !self.book_open(book_type) || self.spectator() {
|
||||
@@ -518,12 +518,12 @@ impl Game<'_> {
|
||||
if self.recipe_ui.page + 1 < pages && inside(cursor, xo + 93.0, yo + 137.0, 12.0, 17.0)
|
||||
{
|
||||
self.recipe_ui.page += 1;
|
||||
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
|
||||
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
||||
return true;
|
||||
}
|
||||
if self.recipe_ui.page > 0 && inside(cursor, xo + 38.0, yo + 137.0, 12.0, 17.0) {
|
||||
self.recipe_ui.page -= 1;
|
||||
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
|
||||
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -570,7 +570,7 @@ impl Game<'_> {
|
||||
let filtering = !self.book_filtering(book_type);
|
||||
self.level.recipe_book.filtering[book_type.index()] = filtering;
|
||||
self.recipe_ui.page = 0;
|
||||
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
|
||||
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
||||
return true;
|
||||
}
|
||||
// Tabs.
|
||||
@@ -581,7 +581,7 @@ impl Game<'_> {
|
||||
self.recipe_ui.tab = Some(tab);
|
||||
self.recipe_ui.page = 0;
|
||||
}
|
||||
self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0);
|
||||
self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ pub mod settings;
|
||||
pub mod shadows;
|
||||
pub mod signs;
|
||||
pub mod snbt;
|
||||
pub mod sound_engine;
|
||||
pub mod sound_events;
|
||||
pub mod sprint;
|
||||
pub mod sprites;
|
||||
pub mod stations;
|
||||
|
||||
+206
-109
@@ -2,6 +2,7 @@
|
||||
//! `Minecraft.getSituationalMusic`, with the `minecraft:audio/background_music`
|
||||
//! and `minecraft:audio/music_volume` environment attributes of dimension
|
||||
//! types and biomes (a biome value overrides its dimension's).
|
||||
use crate::sound_engine::{InstanceId, MUSIC, PlayResult, SoundEngine, SoundInstance, Sounds};
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
use serde_json::Value;
|
||||
use std::{collections::HashMap, fs, path::Path};
|
||||
@@ -15,7 +16,7 @@ pub struct Music {
|
||||
pub replace_current_music: bool,
|
||||
}
|
||||
impl Music {
|
||||
fn new(sound: &str, min_delay: i32, max_delay: i32, replace_current_music: bool) -> Self {
|
||||
pub fn new(sound: &str, min_delay: i32, max_delay: i32, replace_current_music: bool) -> Self {
|
||||
Self {
|
||||
sound: sound.to_owned(),
|
||||
min_delay,
|
||||
@@ -35,6 +36,22 @@ impl Music {
|
||||
pub fn end_boss() -> Self {
|
||||
Self::new("minecraft:music.dragon", 0, 0, true)
|
||||
}
|
||||
/// `Musics.CREATIVE`.
|
||||
pub fn creative() -> Self {
|
||||
Self::new("minecraft:music.creative", 12000, 24000, false)
|
||||
}
|
||||
/// `Musics.END`.
|
||||
pub fn end() -> Self {
|
||||
Self::new("minecraft:music.end", 6000, 24000, true)
|
||||
}
|
||||
/// `Musics.UNDER_WATER`.
|
||||
pub fn under_water() -> Self {
|
||||
Self::new("minecraft:music.under_water", 12000, 24000, false)
|
||||
}
|
||||
/// `Musics.GAME`.
|
||||
pub fn game() -> Self {
|
||||
Self::new("minecraft:music.game", 12000, 24000, false)
|
||||
}
|
||||
/// `Music.CODEC`.
|
||||
fn parse(v: &Value) -> Option<Self> {
|
||||
let sound = v["sound"].as_str()?;
|
||||
@@ -90,6 +107,51 @@ impl Layer {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// What `Minecraft.getSituationalMusic` and `getMusicVolume` read.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Situation<'a> {
|
||||
/// `Screen.getBackgroundMusic` of the open screen.
|
||||
pub screen_music: Option<Music>,
|
||||
/// `Minecraft.player != null`.
|
||||
pub in_world: bool,
|
||||
/// The player's level is the End.
|
||||
pub in_end: bool,
|
||||
/// `BossHealthOverlay.shouldPlayMusic`: a boss bar asks for music.
|
||||
pub boss_music: bool,
|
||||
/// `BACKGROUND_MUSIC` at the camera.
|
||||
pub background: &'a BackgroundMusic,
|
||||
/// `MUSIC_VOLUME` at the camera (1 without a level).
|
||||
pub probe_volume: f32,
|
||||
/// `abilities.instabuild && abilities.mayfly`.
|
||||
pub creative: bool,
|
||||
/// `LocalPlayer.isUnderWater`.
|
||||
pub underwater: bool,
|
||||
}
|
||||
impl Situation<'_> {
|
||||
/// `Minecraft.getSituationalMusic`.
|
||||
pub fn music(&self) -> Option<Music> {
|
||||
if let Some(music) = &self.screen_music {
|
||||
return Some(music.clone());
|
||||
}
|
||||
if !self.in_world {
|
||||
return Some(Music::menu());
|
||||
}
|
||||
if self.in_end && self.boss_music {
|
||||
return Some(Music::end_boss());
|
||||
}
|
||||
self.background
|
||||
.select(self.creative, self.underwater)
|
||||
.cloned()
|
||||
}
|
||||
/// `Minecraft.getMusicVolume`.
|
||||
pub fn volume(&self) -> f32 {
|
||||
if self.screen_music.is_some() {
|
||||
1.0
|
||||
} else {
|
||||
self.probe_volume
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Music attributes by dimension type and biome.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct MusicTables {
|
||||
@@ -177,45 +239,23 @@ fn next_int(random: &mut LegacyRandom, min: i32, max: i32) -> i32 {
|
||||
random.next_int_bound(max - min + 1).unwrap_or(0) + min
|
||||
}
|
||||
}
|
||||
/// What the music manager drives (the sound engine's music channel).
|
||||
pub trait MusicOutput {
|
||||
/// Start a track; false when nothing can play.
|
||||
fn play(&mut self, event: &str) -> bool;
|
||||
fn stop(&mut self);
|
||||
fn is_active(&mut self) -> bool;
|
||||
/// `SoundManager.updateCategoryVolume(MUSIC, gain)`.
|
||||
fn set_gain(&mut self, gain: f32);
|
||||
}
|
||||
impl MusicOutput for crate::audio::Audio {
|
||||
fn play(&mut self, event: &str) -> bool {
|
||||
self.play_music(event)
|
||||
}
|
||||
fn stop(&mut self) {
|
||||
self.stop_music();
|
||||
}
|
||||
fn is_active(&mut self) -> bool {
|
||||
self.music_active()
|
||||
}
|
||||
fn set_gain(&mut self, gain: f32) {
|
||||
self.set_music_gain(gain);
|
||||
}
|
||||
}
|
||||
/// `MusicManager`.
|
||||
pub struct MusicManager {
|
||||
random: LegacyRandom,
|
||||
/// The sound event of the current track.
|
||||
current: Option<String>,
|
||||
/// `currentMusic`: the instance the manager holds.
|
||||
current: Option<InstanceId>,
|
||||
current_gain: f32,
|
||||
next_song_delay: i32,
|
||||
toast_shown: bool,
|
||||
pub frequency: MusicFrequency,
|
||||
/// `ToastManager.showNowPlayingToast` (true) / `hideNowPlayingToast`
|
||||
/// (false) calls since the last drain.
|
||||
pub toast_requests: Vec<bool>,
|
||||
}
|
||||
impl Default for MusicManager {
|
||||
fn default() -> Self {
|
||||
// RandomSource.create(): seeded from the clock, not the world.
|
||||
let seed = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_nanos() as i64);
|
||||
Self::new(seed)
|
||||
Self::new(crate::sound_engine::unique_seed())
|
||||
}
|
||||
}
|
||||
impl MusicManager {
|
||||
@@ -225,35 +265,68 @@ impl MusicManager {
|
||||
current: None,
|
||||
current_gain: 1.0,
|
||||
next_song_delay: 100,
|
||||
toast_shown: false,
|
||||
frequency: MusicFrequency::Default,
|
||||
toast_requests: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn current(&self) -> Option<&str> {
|
||||
self.current.as_deref()
|
||||
/// The sound event of the current track.
|
||||
pub fn current<'a>(&self, sounds: &'a SoundEngine) -> Option<&'a str> {
|
||||
self.current
|
||||
.and_then(|id| sounds.instance(id))
|
||||
.map(|i| i.identifier.as_str())
|
||||
}
|
||||
/// `MusicManager.tick` with the situational music and music volume;
|
||||
/// `loading` holds back a new track (`LevelLoadingScreen`).
|
||||
pub fn current_instance(&self) -> Option<InstanceId> {
|
||||
self.current
|
||||
}
|
||||
pub fn next_song_delay(&self) -> i32 {
|
||||
self.next_song_delay
|
||||
}
|
||||
pub fn current_gain(&self) -> f32 {
|
||||
self.current_gain
|
||||
}
|
||||
pub fn toast_shown(&self) -> bool {
|
||||
self.toast_shown
|
||||
}
|
||||
fn set_current(&mut self, id: Option<InstanceId>, sounds: &mut impl Sounds) {
|
||||
if let Some(old) = self.current
|
||||
&& Some(old) != id
|
||||
{
|
||||
sounds.release(old);
|
||||
}
|
||||
self.current = id;
|
||||
}
|
||||
/// `MusicManager.tick` with `Minecraft.getMusicVolume` and
|
||||
/// `getSituationalMusic`; `loading` is a `LevelLoadingScreen`.
|
||||
pub fn tick(
|
||||
&mut self,
|
||||
music: Option<&Music>,
|
||||
volume: f32,
|
||||
loading: bool,
|
||||
out: &mut impl MusicOutput,
|
||||
sounds: &mut impl Sounds,
|
||||
) {
|
||||
if self.current.is_some() && self.current_gain != volume && !self.fade(volume, music, out) {
|
||||
if self.current.is_some()
|
||||
&& self.current_gain != volume
|
||||
&& !self.fade(volume, music, sounds)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(music) = music else {
|
||||
self.next_song_delay = self.next_song_delay.max(100);
|
||||
return;
|
||||
};
|
||||
if let Some(current) = &self.current {
|
||||
if music.replace_current_music && music.sound != *current {
|
||||
out.stop();
|
||||
if let Some(current) = self.current {
|
||||
// canReplace
|
||||
let playing = sounds
|
||||
.engine()
|
||||
.instance(current)
|
||||
.map(|i| i.identifier.clone());
|
||||
if music.replace_current_music && playing.as_deref() != Some(music.sound.as_str()) {
|
||||
sounds.stop_instance(current);
|
||||
self.next_song_delay = next_int(&mut self.random, 0, music.min_delay / 2);
|
||||
}
|
||||
if !out.is_active() {
|
||||
self.current = None;
|
||||
if !sounds.is_active(current) {
|
||||
self.set_current(None, sounds);
|
||||
let delay = self
|
||||
.frequency
|
||||
.next_song_delay(Some(music), &mut self.random);
|
||||
@@ -264,35 +337,68 @@ impl MusicManager {
|
||||
if self.current.is_none() && !loading {
|
||||
self.next_song_delay -= 1;
|
||||
if self.next_song_delay <= 0 {
|
||||
self.start(music, out);
|
||||
self.start_playing(music, sounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `MusicManager.startPlaying`.
|
||||
fn start(&mut self, music: &Music, out: &mut impl MusicOutput) {
|
||||
// The instance is current even if it could not start; the next tick
|
||||
// finds it inactive and schedules another.
|
||||
out.play(&music.sound);
|
||||
self.current = Some(music.sound.clone());
|
||||
pub fn start_playing(&mut self, music: &Music, sounds: &mut impl Sounds) {
|
||||
let instance = SoundInstance::for_music(&music.sound, SoundEngine::unseeded());
|
||||
let (id, result) = sounds.play_instance(instance, true);
|
||||
self.set_current(Some(id), sounds);
|
||||
match result {
|
||||
PlayResult::Started => {
|
||||
self.toast_requests.push(true);
|
||||
self.toast_shown = true;
|
||||
}
|
||||
PlayResult::StartedSilently => self.toast_shown = false,
|
||||
PlayResult::NotStarted => {}
|
||||
}
|
||||
self.next_song_delay = i32::MAX;
|
||||
}
|
||||
/// `MusicManager.stopPlaying`.
|
||||
pub fn stop(&mut self, situational: Option<&Music>, out: &mut impl MusicOutput) {
|
||||
if self.current.take().is_some() {
|
||||
out.stop();
|
||||
/// `MusicManager.showNowPlayingToastIfNeeded`.
|
||||
pub fn show_now_playing_toast_if_needed(&mut self) {
|
||||
if !self.toast_shown {
|
||||
self.toast_requests.push(true);
|
||||
self.toast_shown = true;
|
||||
}
|
||||
}
|
||||
/// `MusicManager.stopPlaying(Music)`.
|
||||
pub fn stop_playing_music(
|
||||
&mut self,
|
||||
music: &Music,
|
||||
situational: Option<&Music>,
|
||||
sounds: &mut impl Sounds,
|
||||
) {
|
||||
if self.is_playing_music(music, sounds.engine()) {
|
||||
self.stop_playing(situational, sounds);
|
||||
}
|
||||
}
|
||||
/// `MusicManager.stopPlaying()`.
|
||||
pub fn stop_playing(&mut self, situational: Option<&Music>, sounds: &mut impl Sounds) {
|
||||
if let Some(current) = self.current {
|
||||
sounds.stop_instance(current);
|
||||
self.set_current(None, sounds);
|
||||
self.toast_requests.push(false);
|
||||
}
|
||||
self.next_song_delay = self
|
||||
.frequency
|
||||
.next_song_delay(situational, &mut self.random)
|
||||
+ 100;
|
||||
}
|
||||
/// `MusicManager.isPlayingMusic`.
|
||||
pub fn is_playing_music(&self, music: &Music, sounds: &SoundEngine) -> bool {
|
||||
self.current(sounds) == Some(music.sound.as_str())
|
||||
}
|
||||
/// `MusicManager.setMinutesBetweenSongs`.
|
||||
pub fn set_frequency(&mut self, frequency: MusicFrequency, situational: Option<&Music>) {
|
||||
self.frequency = frequency;
|
||||
self.next_song_delay = self
|
||||
.frequency
|
||||
.next_song_delay(situational, &mut self.random);
|
||||
}
|
||||
/// `MusicManager.fadePlaying`: false once the track faded out and stopped.
|
||||
fn fade(
|
||||
&mut self,
|
||||
volume: f32,
|
||||
situational: Option<&Music>,
|
||||
out: &mut impl MusicOutput,
|
||||
) -> bool {
|
||||
fn fade(&mut self, volume: f32, situational: Option<&Music>, sounds: &mut impl Sounds) -> bool {
|
||||
if self.current.is_none() {
|
||||
return false;
|
||||
}
|
||||
@@ -312,10 +418,10 @@ impl MusicManager {
|
||||
}
|
||||
self.current_gain = self.current_gain.clamp(0.0, 1.0);
|
||||
if self.current_gain <= 1.0E-4 {
|
||||
self.stop(situational, out);
|
||||
self.stop_playing(situational, sounds);
|
||||
false
|
||||
} else {
|
||||
out.set_gain(self.current_gain);
|
||||
sounds.update_category_volume(MUSIC, self.current_gain);
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -324,79 +430,70 @@ impl MusicManager {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::sound_engine::{Bound, test_backend::Null};
|
||||
use crate::sound_events::SoundRegistry;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Fake {
|
||||
playing: Option<String>,
|
||||
started: Vec<String>,
|
||||
gain: f32,
|
||||
}
|
||||
impl MusicOutput for Fake {
|
||||
fn play(&mut self, event: &str) -> bool {
|
||||
self.playing = Some(event.to_owned());
|
||||
self.started.push(event.to_owned());
|
||||
true
|
||||
}
|
||||
fn stop(&mut self) {
|
||||
self.playing = None;
|
||||
}
|
||||
fn is_active(&mut self) -> bool {
|
||||
self.playing.is_some()
|
||||
}
|
||||
fn set_gain(&mut self, gain: f32) {
|
||||
self.gain = gain;
|
||||
}
|
||||
fn engine() -> SoundEngine {
|
||||
let mut registry = SoundRegistry::default();
|
||||
registry.add_file(
|
||||
"minecraft",
|
||||
br#"{"music.menu": {"sounds": [{"name": "m", "stream": true}]},
|
||||
"music.game": {"sounds": [{"name": "g", "stream": true}]},
|
||||
"music.end": {"sounds": [{"name": "e", "stream": true}]}}"#,
|
||||
&|_, _| true,
|
||||
);
|
||||
SoundEngine::new(registry)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn menu_music_starts_within_its_delay_and_game_music_replaces_nothing() {
|
||||
let (mut engine, mut backend) = (engine(), Null::default());
|
||||
let mut m = MusicManager::new(1);
|
||||
let mut out = Fake::default();
|
||||
let menu = Music::menu();
|
||||
let tick =
|
||||
|m: &mut MusicManager, music: &Music, engine: &mut SoundEngine, backend: &mut Null| {
|
||||
m.tick(Some(music), 1.0, false, &mut Bound { engine, backend });
|
||||
};
|
||||
// nextSongDelay starts at 100, capped to MENU's max delay 600: starts on tick 100.
|
||||
for _ in 0..99 {
|
||||
m.tick(Some(&menu), 1.0, false, &mut out);
|
||||
tick(&mut m, &menu, &mut engine, &mut backend);
|
||||
}
|
||||
assert!(out.started.is_empty());
|
||||
m.tick(Some(&menu), 1.0, false, &mut out);
|
||||
assert_eq!(out.started, ["minecraft:music.menu"]);
|
||||
assert!(m.current(&engine).is_none());
|
||||
tick(&mut m, &menu, &mut engine, &mut backend);
|
||||
assert_eq!(m.current(&engine), Some("minecraft:music.menu"));
|
||||
// Game music does not replace the current track.
|
||||
let game = Music::new("minecraft:music.game", 12000, 24000, false);
|
||||
m.tick(Some(&game), 1.0, false, &mut out);
|
||||
assert_eq!(m.current(), Some("minecraft:music.menu"));
|
||||
// When the menu track ends, the next delay comes from the game music range.
|
||||
out.playing = None;
|
||||
m.tick(Some(&game), 1.0, false, &mut out);
|
||||
assert!(m.current().is_none());
|
||||
assert!((12000..=24000).contains(&m.next_song_delay));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replacing_music_stops_the_current_track() {
|
||||
let mut m = MusicManager::new(2);
|
||||
let mut out = Fake::default();
|
||||
m.start(&Music::menu(), &mut out);
|
||||
let end = Music::new("minecraft:music.end", 6000, 24000, true);
|
||||
m.tick(Some(&end), 1.0, false, &mut out);
|
||||
assert!(out.playing.is_none());
|
||||
assert!(m.current().is_none());
|
||||
assert!(m.next_song_delay <= 3000);
|
||||
tick(&mut m, &game, &mut engine, &mut backend);
|
||||
assert_eq!(m.current(&engine), Some("minecraft:music.menu"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_music_volume_fades_out_and_stops() {
|
||||
let (mut engine, mut backend) = (engine(), Null::default());
|
||||
let mut m = MusicManager::new(3);
|
||||
let mut out = Fake::default();
|
||||
let game = Music::new("minecraft:music.game", 12000, 24000, false);
|
||||
m.start(&game, &mut out);
|
||||
m.start_playing(
|
||||
&game,
|
||||
&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
},
|
||||
);
|
||||
let mut ticks = 0;
|
||||
while m.current().is_some() && ticks < 1000 {
|
||||
m.tick(Some(&game), 0.0, false, &mut out);
|
||||
while m.current_instance().is_some() && ticks < 1000 {
|
||||
m.tick(
|
||||
Some(&game),
|
||||
0.0,
|
||||
false,
|
||||
&mut Bound {
|
||||
engine: &mut engine,
|
||||
backend: &mut backend,
|
||||
},
|
||||
);
|
||||
ticks += 1;
|
||||
}
|
||||
// 0.97^n < 1e-4 after ~303 ticks.
|
||||
assert!((290..=310).contains(&ticks), "{ticks}");
|
||||
assert!(out.playing.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,387 @@
|
||||
//! `sounds.json` sound events as vanilla's `SoundManager.Preparations` builds
|
||||
//! them: `WeighedSoundEvents` holding files and references to other events
|
||||
//! (whose weight is the referenced event's total weight, resolved lazily), the
|
||||
//! `replace` and `subtitle` fields, files that do not exist being dropped, and
|
||||
//! `WeighedSoundEvents.getSound` drawing `nextInt(totalWeight)` from the sound
|
||||
//! instance's random.
|
||||
use horizon_core::random::RandomSource;
|
||||
use serde_json::Value;
|
||||
use std::{collections::HashMap, fs, path::Path};
|
||||
|
||||
/// `SoundManager.EMPTY_SOUND_LOCATION`.
|
||||
pub const EMPTY: &str = "minecraft:empty";
|
||||
/// `SoundManager.INTENTIONALLY_EMPTY_SOUND_LOCATION`.
|
||||
pub const INTENTIONALLY_EMPTY: &str = "minecraft:intentionally_empty";
|
||||
|
||||
/// `SampledFloat` as sounds use it: constants and `MultipliedFloats`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Sampled {
|
||||
Constant(f32),
|
||||
Product(Vec<Sampled>),
|
||||
}
|
||||
impl Sampled {
|
||||
/// `SampledFloat.sample`: constants draw nothing (sounds.json only has
|
||||
/// constants, but the random stays part of the call as in vanilla).
|
||||
#[allow(clippy::only_used_in_recursion)]
|
||||
pub fn sample(&self, random: &mut impl RandomSource) -> f32 {
|
||||
match self {
|
||||
Self::Constant(v) => *v,
|
||||
Self::Product(values) => {
|
||||
let mut result = 1.0f32;
|
||||
for v in values {
|
||||
result *= v.sample(random);
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Sound`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Sound {
|
||||
/// The file identifier (`namespace:path`, under `sounds/` with `.ogg`).
|
||||
pub location: String,
|
||||
pub volume: Sampled,
|
||||
pub pitch: Sampled,
|
||||
pub weight: i32,
|
||||
/// `Sound.Type.SOUND_EVENT` (a reference to another event).
|
||||
pub event: bool,
|
||||
pub stream: bool,
|
||||
pub preload: bool,
|
||||
pub attenuation_distance: i32,
|
||||
/// The `EMPTY_SOUND` / `INTENTIONALLY_EMPTY_SOUND` singletons (vanilla
|
||||
/// compares them by identity).
|
||||
pub sentinel: Sentinel,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Sentinel {
|
||||
#[default]
|
||||
None,
|
||||
Empty,
|
||||
IntentionallyEmpty,
|
||||
}
|
||||
impl Sound {
|
||||
fn file(location: &str) -> Self {
|
||||
Self {
|
||||
location: location.to_owned(),
|
||||
volume: Sampled::Constant(1.0),
|
||||
pitch: Sampled::Constant(1.0),
|
||||
weight: 1,
|
||||
event: false,
|
||||
stream: false,
|
||||
preload: false,
|
||||
attenuation_distance: 16,
|
||||
sentinel: Sentinel::None,
|
||||
}
|
||||
}
|
||||
/// `SoundManager.EMPTY_SOUND`.
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
sentinel: Sentinel::Empty,
|
||||
..Self::file(EMPTY)
|
||||
}
|
||||
}
|
||||
/// `SoundManager.INTENTIONALLY_EMPTY_SOUND`.
|
||||
pub fn intentionally_empty() -> Self {
|
||||
Self {
|
||||
sentinel: Sentinel::IntentionallyEmpty,
|
||||
..Self::file(INTENTIONALLY_EMPTY)
|
||||
}
|
||||
}
|
||||
/// `Sound.getAttenuationDistance(volume)`.
|
||||
pub fn attenuation_distance(&self, volume: f32) -> f32 {
|
||||
volume.max(1.0) * self.attenuation_distance as f32
|
||||
}
|
||||
/// `Sound.getPath`: the resource path (`namespace`, `sounds/<path>.ogg`).
|
||||
pub fn path(&self) -> (&str, String) {
|
||||
let (namespace, path) = split(&self.location);
|
||||
(namespace, format!("sounds/{path}.ogg"))
|
||||
}
|
||||
}
|
||||
/// `WeighedSoundEvents`.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct WeighedSoundEvents {
|
||||
/// Files, or references to events (`Sound.event`).
|
||||
pub sounds: Vec<Sound>,
|
||||
/// The subtitle translation key.
|
||||
pub subtitle: Option<String>,
|
||||
}
|
||||
/// The sound event registry (`SoundManager.registry`).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct SoundRegistry {
|
||||
events: HashMap<String, WeighedSoundEvents>,
|
||||
}
|
||||
/// `Identifier.parse` with the default namespace.
|
||||
fn split(id: &str) -> (&str, &str) {
|
||||
match id.split_once(':') {
|
||||
Some((namespace, path)) => (
|
||||
if namespace.is_empty() {
|
||||
"minecraft"
|
||||
} else {
|
||||
namespace
|
||||
},
|
||||
path,
|
||||
),
|
||||
None => ("minecraft", id),
|
||||
}
|
||||
}
|
||||
pub fn normalize(id: &str) -> String {
|
||||
let (namespace, path) = split(id);
|
||||
format!("{namespace}:{path}")
|
||||
}
|
||||
/// `GsonHelper.getAsFloat`: Gson parses the number's text as a float.
|
||||
fn json_float(v: &Value, fallback: f32) -> f32 {
|
||||
match v {
|
||||
Value::Number(n) => n.to_string().parse().unwrap_or(fallback),
|
||||
Value::String(s) => s.parse().unwrap_or(fallback),
|
||||
_ => fallback,
|
||||
}
|
||||
}
|
||||
fn json_int(v: &Value, fallback: i32) -> i32 {
|
||||
match v {
|
||||
Value::Number(n) => n
|
||||
.as_i64()
|
||||
.map(|i| i as i32)
|
||||
.or_else(|| n.as_f64().map(|f| f as i32))
|
||||
.unwrap_or(fallback),
|
||||
Value::String(s) => s.parse().unwrap_or(fallback),
|
||||
_ => fallback,
|
||||
}
|
||||
}
|
||||
fn json_bool(v: &Value, fallback: bool) -> bool {
|
||||
match v {
|
||||
Value::Bool(b) => *b,
|
||||
Value::String(s) => s == "true",
|
||||
_ => fallback,
|
||||
}
|
||||
}
|
||||
/// `SoundEventRegistrationSerializer.getSound`; None for an invalid entry
|
||||
/// (vanilla rejects the whole file).
|
||||
fn parse_sound(v: &Value) -> Option<Sound> {
|
||||
if let Value::String(name) = v {
|
||||
return Some(Sound::file(&normalize(name)));
|
||||
}
|
||||
let name = v.get("name")?.as_str()?;
|
||||
let event = match v.get("type") {
|
||||
None => false,
|
||||
Some(t) => match t.as_str()? {
|
||||
"file" => false,
|
||||
"event" => true,
|
||||
_ => return None,
|
||||
},
|
||||
};
|
||||
let volume = json_float(v.get("volume").unwrap_or(&Value::Null), 1.0);
|
||||
let pitch = json_float(v.get("pitch").unwrap_or(&Value::Null), 1.0);
|
||||
let weight = json_int(v.get("weight").unwrap_or(&Value::Null), 1);
|
||||
if !(volume > 0.0 && pitch > 0.0 && weight > 0) {
|
||||
return None;
|
||||
}
|
||||
Some(Sound {
|
||||
location: normalize(name),
|
||||
volume: Sampled::Constant(volume),
|
||||
pitch: Sampled::Constant(pitch),
|
||||
weight,
|
||||
event,
|
||||
stream: json_bool(v.get("stream").unwrap_or(&Value::Null), false),
|
||||
preload: json_bool(v.get("preload").unwrap_or(&Value::Null), false),
|
||||
attenuation_distance: json_int(v.get("attenuation_distance").unwrap_or(&Value::Null), 16),
|
||||
sentinel: Sentinel::None,
|
||||
})
|
||||
}
|
||||
impl SoundRegistry {
|
||||
/// Every `assets/<namespace>/sounds.json` under a resource root, files
|
||||
/// validated against `assets/<namespace>/sounds/`.
|
||||
pub fn load(root: &Path) -> Self {
|
||||
let assets = root.join("assets");
|
||||
let mut namespaces: Vec<String> = fs::read_dir(&assets)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.filter_map(|e| e.file_name().to_str().map(str::to_owned))
|
||||
.collect();
|
||||
namespaces.sort();
|
||||
let mut registry = Self::default();
|
||||
for namespace in namespaces {
|
||||
let Ok(text) = fs::read(assets.join(&namespace).join("sounds.json")) else {
|
||||
continue;
|
||||
};
|
||||
let exists = |ns: &str, path: &str| assets.join(ns).join(path).is_file();
|
||||
registry.add_file(&namespace, &text, &exists);
|
||||
}
|
||||
registry
|
||||
}
|
||||
/// One `sounds.json` (`Preparations.handleRegistration` for each entry).
|
||||
/// `exists(namespace, path)` says whether a sound file is present.
|
||||
pub fn add_file(&mut self, namespace: &str, text: &[u8], exists: &dyn Fn(&str, &str) -> bool) {
|
||||
let Ok(Value::Object(map)) = serde_json::from_slice::<Value>(text) else {
|
||||
return;
|
||||
};
|
||||
// A malformed entry makes Gson reject the whole file.
|
||||
let mut parsed = Vec::new();
|
||||
for (name, def) in &map {
|
||||
let Value::Object(object) = def else { return };
|
||||
let replace = json_bool(object.get("replace").unwrap_or(&Value::Null), false);
|
||||
let subtitle = match object.get("subtitle") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(Value::String(s)) => Some(s.clone()),
|
||||
Some(_) => return,
|
||||
};
|
||||
let mut sounds = Vec::new();
|
||||
if let Some(list) = object.get("sounds") {
|
||||
let Value::Array(list) = list else { return };
|
||||
for s in list {
|
||||
let Some(sound) = parse_sound(s) else { return };
|
||||
sounds.push(sound);
|
||||
}
|
||||
}
|
||||
parsed.push((format!("{namespace}:{name}"), replace, subtitle, sounds));
|
||||
}
|
||||
for (location, replace, subtitle, sounds) in parsed {
|
||||
let entry = match self.events.get_mut(&location) {
|
||||
Some(entry) if !replace => entry,
|
||||
_ => {
|
||||
self.events.insert(
|
||||
location.clone(),
|
||||
WeighedSoundEvents {
|
||||
sounds: Vec::new(),
|
||||
subtitle,
|
||||
},
|
||||
);
|
||||
self.events.get_mut(&location).expect("inserted")
|
||||
}
|
||||
};
|
||||
for sound in sounds {
|
||||
if !sound.event {
|
||||
let (ns, path) = sound.path();
|
||||
if !exists(ns, &path) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
entry.sounds.push(sound);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn get(&self, event: &str) -> Option<&WeighedSoundEvents> {
|
||||
self.events.get(event)
|
||||
}
|
||||
pub fn contains(&self, event: &str) -> bool {
|
||||
self.events.contains_key(event)
|
||||
}
|
||||
/// Every registered event id.
|
||||
pub fn names(&self) -> impl Iterator<Item = &String> {
|
||||
self.events.keys()
|
||||
}
|
||||
/// `Weighted.getWeight` of one entry.
|
||||
fn entry_weight(&self, sound: &Sound, depth: u32) -> i32 {
|
||||
if !sound.event {
|
||||
return sound.weight;
|
||||
}
|
||||
if depth > 64 {
|
||||
return 0;
|
||||
}
|
||||
self.events
|
||||
.get(&sound.location)
|
||||
.map_or(0, |e| self.total(e, depth + 1))
|
||||
}
|
||||
fn total(&self, event: &WeighedSoundEvents, depth: u32) -> i32 {
|
||||
event
|
||||
.sounds
|
||||
.iter()
|
||||
.fold(0i32, |sum, s| sum.wrapping_add(self.entry_weight(s, depth)))
|
||||
}
|
||||
/// `WeighedSoundEvents.getWeight`.
|
||||
pub fn weight(&self, event: &str) -> i32 {
|
||||
self.events.get(event).map_or(0, |e| self.total(e, 0))
|
||||
}
|
||||
/// `WeighedSoundEvents.getSound`: None when the event is not registered.
|
||||
pub fn sound(&self, event: &str, random: &mut impl RandomSource) -> Option<Sound> {
|
||||
let e = self.events.get(event)?;
|
||||
Some(self.pick(e, random, 0))
|
||||
}
|
||||
fn pick(
|
||||
&self,
|
||||
event: &WeighedSoundEvents,
|
||||
random: &mut impl RandomSource,
|
||||
depth: u32,
|
||||
) -> Sound {
|
||||
let weight = self.total(event, depth);
|
||||
if event.sounds.is_empty() || weight == 0 {
|
||||
return Sound::empty();
|
||||
}
|
||||
let Ok(mut index) = random.next_int_bound(weight) else {
|
||||
return Sound::empty();
|
||||
};
|
||||
for sound in &event.sounds {
|
||||
index -= self.entry_weight(sound, depth);
|
||||
if index < 0 {
|
||||
if !sound.event {
|
||||
return sound.clone();
|
||||
}
|
||||
// The anonymous reference Weighted: the referenced event's
|
||||
// sound with this entry's volume, pitch and stream folded in.
|
||||
let Some(inner) = self.events.get(&sound.location) else {
|
||||
return Sound::empty();
|
||||
};
|
||||
if depth > 64 {
|
||||
return Sound::empty();
|
||||
}
|
||||
let wrapped = self.pick(inner, random, depth + 1);
|
||||
return Sound {
|
||||
location: wrapped.location,
|
||||
volume: Sampled::Product(vec![wrapped.volume, sound.volume.clone()]),
|
||||
pitch: Sampled::Product(vec![wrapped.pitch, sound.pitch.clone()]),
|
||||
weight: sound.weight,
|
||||
event: false,
|
||||
stream: wrapped.stream || sound.stream,
|
||||
preload: wrapped.preload,
|
||||
attenuation_distance: wrapped.attenuation_distance,
|
||||
sentinel: Sentinel::None,
|
||||
};
|
||||
}
|
||||
}
|
||||
Sound::empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use horizon_core::random::LegacyRandom;
|
||||
|
||||
#[test]
|
||||
fn references_weigh_as_much_as_their_event() {
|
||||
let mut r = SoundRegistry::default();
|
||||
let json = br#"{
|
||||
"a": {"sounds": ["x", "y", "z"]},
|
||||
"b": {"sounds": [{"name": "a", "type": "event", "weight": 9, "volume": 0.5}, "w"], "subtitle": "s.b"}
|
||||
}"#;
|
||||
r.add_file("minecraft", json, &|_, _| true);
|
||||
assert_eq!(r.weight("minecraft:b"), 4);
|
||||
let mut random = LegacyRandom::new(1);
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for _ in 0..64 {
|
||||
let s = r.sound("minecraft:b", &mut random).unwrap();
|
||||
if s.location != "minecraft:w" {
|
||||
assert_eq!(s.volume.sample(&mut random), 0.5);
|
||||
}
|
||||
seen.insert(s.location);
|
||||
}
|
||||
assert_eq!(seen.len(), 4);
|
||||
assert_eq!(
|
||||
r.get("minecraft:b").unwrap().subtitle.as_deref(),
|
||||
Some("s.b")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_files_are_dropped_and_empty_events_give_the_empty_sound() {
|
||||
let mut r = SoundRegistry::default();
|
||||
r.add_file("minecraft", br#"{"a": {"sounds": ["gone"]}}"#, &|_, _| {
|
||||
false
|
||||
});
|
||||
assert_eq!(r.weight("minecraft:a"), 0);
|
||||
let mut random = LegacyRandom::new(1);
|
||||
assert_eq!(r.sound("minecraft:a", &mut random).unwrap(), Sound::empty());
|
||||
assert!(r.sound("minecraft:b", &mut random).is_none());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.client.Camera;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.client.renderer.GameRenderer;
|
||||
import net.minecraft.client.resources.sounds.*;
|
||||
import net.minecraft.client.sounds.ChannelAccess;
|
||||
import net.minecraft.client.sounds.SoundEngine;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.tags.FluidTags;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.attribute.*;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.LightLayer;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.BubbleColumnBlock;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.minecraft.world.level.material.Fluid;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
/**
|
||||
* Runs the local player's real ambient sound path over the real SoundEngine
|
||||
* (VanillaSoundRig): LocalPlayer.updateIsUnderwater (enter/exit sounds through
|
||||
* ClientLevel.playLocalSound and the UnderLiquidAmbientSoundInstance), the
|
||||
* UnderwaterAmbientSoundHandler and the BiomeAmbientSoundsHandler, then
|
||||
* SoundManager.tick, once per client tick. Inputs: the AMBIENT_SOUNDS value
|
||||
* (a constant attribute layer), the player's position, eye height, whether
|
||||
* its eyes are in water, spectator and removed flags, and the level's sky and
|
||||
* block brightness (a hash of the position). Prints the level random's next
|
||||
* long (without drawing it), the handlers' state and every playing instance.
|
||||
*/
|
||||
public final class VanillaAmbientOracle {
|
||||
public static final class Actor extends LocalPlayer {
|
||||
boolean eyeInWater, spectator;
|
||||
|
||||
Actor() {
|
||||
super(null, null, null, null, null, null, false, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEyeInFluid(final TagKey<Fluid> type) {
|
||||
return type == FluidTags.WATER && eyeInWater;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpectator() {
|
||||
return spectator;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Lvl extends ClientLevel {
|
||||
int mode;
|
||||
final Map<BlockPos, Boolean> columns = new HashMap<>();
|
||||
|
||||
Lvl() {
|
||||
super(null, null, null, null, 0, 0, null, false, 0L, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBrightness(final LightLayer layer, final BlockPos pos) {
|
||||
return light(mode, pos.getX(), pos.getY(), pos.getZ())[layer == LightLayer.SKY ? 0 : 1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getBlockState(final BlockPos pos) {
|
||||
Boolean drag = columns.get(pos);
|
||||
return drag == null ? Blocks.AIR.defaultBlockState() : Blocks.BUBBLE_COLUMN.defaultBlockState().setValue(BubbleColumnBlock.DRAG_DOWN, drag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasChunksAt(final int x0, final int y0, final int z0, final int x1, final int y1, final int z1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** The scripted brightness: (sky, block) for a light mode and block. */
|
||||
static int[] light(int mode, int x, int y, int z) {
|
||||
int h = x * 73856093 ^ y * 19349663 ^ z * 83492791;
|
||||
return switch (mode) {
|
||||
case 0 -> new int[]{15, 0};
|
||||
case 1 -> new int[]{0, 0};
|
||||
case 2 -> new int[]{0, (h >>> 4) & 15};
|
||||
default -> new int[]{(h & 3) == 0 ? (h >>> 8) & 15 : 0, (h >>> 12) & 15};
|
||||
};
|
||||
}
|
||||
|
||||
/** The player's box (EntityDimensions.makeBoundingBox, width 0.6F). */
|
||||
static AABB box(Vec3 at, float height) {
|
||||
return new AABB(at.x - 0.3F, at.y, at.z - 0.3F, at.x + 0.3F, at.y + height, at.z + 0.3F);
|
||||
}
|
||||
|
||||
static float f(String hex) {
|
||||
return Float.intBitsToFloat(Integer.parseUnsignedInt(hex, 16));
|
||||
}
|
||||
|
||||
static double d(String hex) {
|
||||
return Double.longBitsToDouble(Long.parseUnsignedLong(hex, 16));
|
||||
}
|
||||
|
||||
static Holder<SoundEvent> event(String id) {
|
||||
return Holder.direct(SoundEvent.createVariableRangeEvent(Identifier.parse(id)));
|
||||
}
|
||||
|
||||
/** "loop|- mood|- addition..." with mood "sound,delay,extent,offsetbits" and additions "sound,chancebits". */
|
||||
static AmbientSounds sounds(String[] a) {
|
||||
Optional<Holder<SoundEvent>> loop = a[1].equals("-") ? Optional.empty() : Optional.of(event(a[1]));
|
||||
Optional<AmbientMoodSettings> mood = Optional.empty();
|
||||
if (!a[2].equals("-")) {
|
||||
String[] m = a[2].split(",");
|
||||
mood = Optional.of(new AmbientMoodSettings(event(m[0]), Integer.parseInt(m[1]), Integer.parseInt(m[2]), d(m[3])));
|
||||
}
|
||||
var additions = new ArrayList<AmbientAdditionsSettings>();
|
||||
for (int i = 3; i < a.length; i++) {
|
||||
String[] p = a[i].split(",");
|
||||
additions.add(new AmbientAdditionsSettings(event(p[0]), d(p[1])));
|
||||
}
|
||||
return new AmbientSounds(loop, mood, additions);
|
||||
}
|
||||
|
||||
/** LegacyRandomSource's next nextLong(), read from its state. */
|
||||
static long peek(RandomSource random) throws Exception {
|
||||
long s = ((java.util.concurrent.atomic.AtomicLong) VanillaSoundRig.get(random,
|
||||
Class.forName("net.minecraft.world.level.levelgen.LegacyRandomSource"), "seed")).get();
|
||||
long m = 0x5DEECE66DL, mask = (1L << 48) - 1;
|
||||
long s1 = (s * m + 0xBL) & mask;
|
||||
long s2 = (s1 * m + 0xBL) & mask;
|
||||
return ((long) (int) (s1 >>> 16) << 32) + (int) (s2 >>> 16);
|
||||
}
|
||||
|
||||
static String bits(double v) {
|
||||
return String.format("%016x", Double.doubleToRawLongBits(v));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) throws Exception {
|
||||
net.minecraft.SharedConstants.tryDetectVersion();
|
||||
net.minecraft.server.Bootstrap.bootStrap();
|
||||
var rig = new VanillaSoundRig(Path.of(args[0]));
|
||||
var mc = VanillaSoundRig.make(Minecraft.class);
|
||||
VanillaSoundRig.set(mc, Minecraft.class, "soundManager", rig.manager);
|
||||
var renderer = VanillaSoundRig.make(GameRenderer.class);
|
||||
VanillaSoundRig.set(renderer, GameRenderer.class, "mainCamera", VanillaSoundRig.make(Camera.class));
|
||||
VanillaSoundRig.set(VanillaSoundRig.get(renderer, GameRenderer.class, "mainCamera"), Camera.class, "position", Vec3.ZERO);
|
||||
VanillaSoundRig.set(mc, Minecraft.class, "gameRenderer", renderer);
|
||||
Method update = LocalPlayer.class.getDeclaredMethod("updateIsUnderwater");
|
||||
update.setAccessible(true);
|
||||
var instanceToChannel = VanillaSoundRig.field(SoundEngine.class, "instanceToChannel");
|
||||
Lvl level = null;
|
||||
Actor actor = null;
|
||||
UnderwaterAmbientSoundHandler underwater = null;
|
||||
BiomeAmbientSoundsHandler biome = null;
|
||||
BubbleColumnAmbientSoundHandler bubble = null;
|
||||
try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[2])))) {
|
||||
for (String line : Files.readAllLines(Path.of(args[1]))) {
|
||||
out.println(line);
|
||||
String[] a = line.split(" ");
|
||||
switch (a[0]) {
|
||||
case "start" -> {
|
||||
// A new level and player: fresh handlers on a seeded level random.
|
||||
rig.manager.stop();
|
||||
level = VanillaSoundRig.make(Lvl.class);
|
||||
VanillaSoundRig.set(level, Level.class, "random", RandomSource.create(Long.parseLong(a[1])));
|
||||
VanillaSoundRig.set(level, ClientLevel.class, "minecraft", mc);
|
||||
VanillaSoundRig.set(level, ClientLevel.class, "environmentAttributes", EnvironmentAttributeSystem.builder().build());
|
||||
actor = VanillaSoundRig.make(Actor.class);
|
||||
VanillaSoundRig.set(actor, Entity.class, "level", level);
|
||||
VanillaSoundRig.set(actor, Entity.class, "position", Vec3.ZERO);
|
||||
VanillaSoundRig.set(actor, Entity.class, "bb", box(Vec3.ZERO, 1.8F));
|
||||
VanillaSoundRig.set(level, Lvl.class, "columns", new HashMap<BlockPos, Boolean>());
|
||||
VanillaSoundRig.set(actor, LocalPlayer.class, "minecraft", mc);
|
||||
underwater = new UnderwaterAmbientSoundHandler(actor, rig.manager);
|
||||
bubble = new BubbleColumnAmbientSoundHandler(actor);
|
||||
biome = new BiomeAmbientSoundsHandler(actor, rig.manager);
|
||||
}
|
||||
case "sounds" -> {
|
||||
AmbientSounds value = sounds(a);
|
||||
var system = EnvironmentAttributeSystem.builder()
|
||||
.addConstantLayer(EnvironmentAttributes.AMBIENT_SOUNDS, base -> value)
|
||||
.build();
|
||||
VanillaSoundRig.set(level, ClientLevel.class, "environmentAttributes", system);
|
||||
}
|
||||
case "player" -> {
|
||||
// player x y z eyeHeight eyeInWater spectator removed lightMode
|
||||
VanillaSoundRig.set(actor, Entity.class, "position", new Vec3(d(a[1]), d(a[2]), d(a[3])));
|
||||
VanillaSoundRig.set(actor, Entity.class, "eyeHeight", f(a[4]));
|
||||
actor.eyeInWater = a[5].equals("1");
|
||||
actor.spectator = a[6].equals("1");
|
||||
VanillaSoundRig.set(actor, Entity.class, "removalReason", a[7].equals("1") ? Entity.RemovalReason.DISCARDED : null);
|
||||
level.mode = Integer.parseInt(a[8]);
|
||||
var at = new Vec3(d(a[1]), d(a[2]), d(a[3]));
|
||||
VanillaSoundRig.set(actor, Entity.class, "bb", box(at, f(a[9])));
|
||||
level.columns.clear();
|
||||
for (int i = 10; i < a.length; i++) {
|
||||
String[] c = a[i].split(",");
|
||||
level.columns.put(new BlockPos(Integer.parseInt(c[0]), Integer.parseInt(c[1]), Integer.parseInt(c[2])), c[3].equals("1"));
|
||||
}
|
||||
}
|
||||
case "volume" -> {
|
||||
rig.volume(SoundSource.values()[Integer.parseInt(a[1])], f(a[2]));
|
||||
rig.manager.refreshCategoryVolume(SoundSource.values()[Integer.parseInt(a[1])]);
|
||||
}
|
||||
case "tick" -> {
|
||||
update.invoke(actor);
|
||||
underwater.tick();
|
||||
bubble.tick();
|
||||
biome.tick();
|
||||
rig.manager.tick(false);
|
||||
}
|
||||
default -> throw new IllegalArgumentException(line);
|
||||
}
|
||||
out.printf(" rand=%d mood=%08x underwater=%s delay=%d%n", peek(level.getRandom()), Float.floatToRawIntBits(biome.getMoodiness()),
|
||||
actor.isUnderWater(), (int) VanillaSoundRig.get(underwater, UnderwaterAmbientSoundHandler.class, "tickDelay"));
|
||||
var loops = (Map<Holder<SoundEvent>, BiomeAmbientSoundsHandler.LoopSoundInstance>) VanillaSoundRig.get(biome,
|
||||
BiomeAmbientSoundsHandler.class, "loopSounds");
|
||||
for (var e : loops.entrySet()) {
|
||||
var loop = e.getValue();
|
||||
out.printf(" loop %s fade=%d direction=%d stopped=%s active=%s%n", e.getKey().value().location(),
|
||||
(int) VanillaSoundRig.get(loop, BiomeAmbientSoundsHandler.LoopSoundInstance.class, "fade"),
|
||||
(int) VanillaSoundRig.get(loop, BiomeAmbientSoundsHandler.LoopSoundInstance.class, "fadeDirection"), loop.isStopped(),
|
||||
rig.manager.isActive(loop));
|
||||
}
|
||||
var playing = new ArrayList<String>();
|
||||
for (SoundInstance i : ((Map<SoundInstance, ChannelAccess.ChannelHandle>) instanceToChannel.get(rig.engine)).keySet()) {
|
||||
float volume = (float) VanillaSoundRig.get(i, AbstractSoundInstance.class, "volume");
|
||||
playing.add(String.format(" playing %s volume=%08x looping=%s relative=%s at=%s,%s,%s stopped=%s", i.getIdentifier(),
|
||||
Float.floatToRawIntBits(volume), i.isLooping(), i.isRelative(), bits(i.getX()), bits(i.getY()), bits(i.getZ()),
|
||||
i instanceof TickableSoundInstance t && t.isStopped()));
|
||||
}
|
||||
Collections.sort(playing);
|
||||
for (String p : playing) out.println(p);
|
||||
rig.flush(out);
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.core.*;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.resources.RegistryDataLoader;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.Bootstrap;
|
||||
import net.minecraft.server.RegistryLayer;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.repository.ServerPacksSource;
|
||||
import net.minecraft.server.packs.resources.MultiPackResourceManager;
|
||||
import net.minecraft.sounds.Music;
|
||||
import net.minecraft.sounds.Musics;
|
||||
import net.minecraft.tags.TagLoader;
|
||||
import net.minecraft.world.attribute.*;
|
||||
import net.minecraft.world.clock.*;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.dimension.DimensionType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
/**
|
||||
* The audio environment attributes as the vanilla EnvironmentAttributeSystem
|
||||
* evaluates them from the vanilla data pack (dimension layer, the biome's
|
||||
* positional layer, the dimension timelines and weather layers):
|
||||
* BACKGROUND_MUSIC with BackgroundMusic.select for every creative/underwater
|
||||
* combination, MUSIC_VOLUME and AMBIENT_SOUNDS, for lines "dimension biome
|
||||
* ticks"; plus the Musics constants.
|
||||
*/
|
||||
public final class VanillaAudioAttributesOracle {
|
||||
static String music(Music m) {
|
||||
return m == null ? "-" : m.sound().value().location() + "," + m.minDelay() + "," + m.maxDelay() + "," + m.replaceCurrentMusic();
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private static void addBiome(EnvironmentAttributeSystem.Builder b, EnvironmentAttribute attr, Biome biome) {
|
||||
b.addPositionalLayer(attr, (base, pos, w) -> biome.getAttributes().applyModifier(attr, base));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SharedConstants.tryDetectVersion();
|
||||
Bootstrap.bootStrap();
|
||||
var repo = ServerPacksSource.createVanillaTrustedRepository();
|
||||
repo.reload();
|
||||
repo.setSelected(List.of("vanilla"));
|
||||
var resources = new MultiPackResourceManager(PackType.SERVER_DATA, repo.openAllSelected());
|
||||
var layers = RegistryLayer.createRegistryAccess();
|
||||
var staticTags = TagLoader.loadTagsForExistingRegistries(resources, layers.getLayer(RegistryLayer.STATIC));
|
||||
var context = TagLoader.buildUpdatedLookups(layers.getAccessForLoading(RegistryLayer.WORLD), staticTags);
|
||||
RegistryAccess.Frozen world = RegistryDataLoader.load(resources, context, RegistryDataLoader.WORLD_REGISTRIES, Runnable::run).join();
|
||||
staticTags.forEach(Registry.PendingTags::apply);
|
||||
var dimensions = world.lookupOrThrow(Registries.DIMENSION_TYPE);
|
||||
var biomes = world.lookupOrThrow(Registries.BIOME);
|
||||
try (var w = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]), StandardCharsets.UTF_8))) {
|
||||
w.println("MENU " + music(Musics.MENU));
|
||||
w.println("CREDITS " + music(Musics.CREDITS));
|
||||
w.println("END_BOSS " + music(Musics.END_BOSS));
|
||||
w.println("CREATIVE " + music(Musics.CREATIVE));
|
||||
w.println("END " + music(Musics.END));
|
||||
w.println("UNDER_WATER " + music(Musics.UNDER_WATER));
|
||||
w.println("GAME " + music(Musics.GAME));
|
||||
for (String line : Files.readAllLines(Path.of(args[0]), StandardCharsets.UTF_8)) {
|
||||
String[] f = line.split(" ");
|
||||
DimensionType dim = dimensions.getOrThrow(ResourceKey.create(Registries.DIMENSION_TYPE, Identifier.parse(f[0]))).value();
|
||||
Biome biome = biomes.getOrThrow(ResourceKey.create(Registries.BIOME, Identifier.parse(f[1]))).value();
|
||||
long ticks = Long.parseLong(f[2]);
|
||||
ClockManager clocks = h -> new ClockInstance() {
|
||||
public long totalTicks() { return ticks; }
|
||||
public float partialTick() { return 0.0F; }
|
||||
public float rate() { return 1.0F; }
|
||||
public boolean isPaused() { return false; }
|
||||
};
|
||||
var b = EnvironmentAttributeSystem.builder();
|
||||
b.addConstantLayer(dim.attributes());
|
||||
for (EnvironmentAttribute<?> attr : biome.getAttributes().keySet()) addBiome(b, attr, biome);
|
||||
dim.timelines().forEach(t -> b.addTimelineLayer(t, clocks));
|
||||
if (dim.hasSkyLight() && !dim.hasCeiling()) {
|
||||
WeatherAttributes.addBuiltinLayers(b, new WeatherAttributes.WeatherAccess() {
|
||||
public float rainLevel() { return 0.0F; }
|
||||
public float thunderLevel() { return 0.0F; }
|
||||
});
|
||||
}
|
||||
var system = b.build();
|
||||
BackgroundMusic bg = system.getValue(EnvironmentAttributes.BACKGROUND_MUSIC, Vec3.ZERO);
|
||||
StringBuilder sb = new StringBuilder(line);
|
||||
sb.append(" default=").append(music(bg.defaultMusic().orElse(null)))
|
||||
.append(" creative=").append(music(bg.creativeMusic().orElse(null)))
|
||||
.append(" underwater=").append(music(bg.underwaterMusic().orElse(null)));
|
||||
for (boolean creative : new boolean[]{false, true}) {
|
||||
for (boolean underwater : new boolean[]{false, true}) {
|
||||
sb.append(" select=").append(music(bg.select(creative, underwater).orElse(null)));
|
||||
}
|
||||
}
|
||||
sb.append(String.format(" volume=%08x", Float.floatToRawIntBits(system.getValue(EnvironmentAttributes.MUSIC_VOLUME, Vec3.ZERO))));
|
||||
AmbientSounds ambient = system.getValue(EnvironmentAttributes.AMBIENT_SOUNDS, Vec3.ZERO);
|
||||
sb.append(" loop=").append(ambient.loop().map(s -> s.value().location().toString()).orElse("-"));
|
||||
sb.append(" mood=").append(ambient.mood().map(m -> m.soundEvent().value().location() + "," + m.tickDelay() + ","
|
||||
+ m.blockSearchExtent() + "," + String.format("%016x", Double.doubleToRawLongBits(m.soundPositionOffset()))).orElse("-"));
|
||||
for (AmbientAdditionsSettings a : ambient.additions()) {
|
||||
sb.append(" addition=").append(a.soundEvent().value().location()).append(String.format(",%016x", Double.doubleToRawLongBits(a.tickChance())));
|
||||
}
|
||||
w.println(sb);
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import net.minecraft.client.gui.components.toasts.ToastManager;
|
||||
import net.minecraft.client.gui.screens.LevelLoadingScreen;
|
||||
import net.minecraft.client.resources.sounds.SoundInstance;
|
||||
import net.minecraft.client.sounds.MusicManager;
|
||||
import net.minecraft.client.sounds.SoundEngine;
|
||||
import net.minecraft.client.sounds.SoundManager;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.sounds.Music;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.RandomSource;
|
||||
|
||||
/**
|
||||
* Drives the real MusicManager over the real SoundEngine (VanillaSoundRig).
|
||||
* The situation, Minecraft.getSituationalMusic and getMusicVolume, is the
|
||||
* script's input; the loading screen is a LevelLoadingScreen in the Gui; the
|
||||
* toast manager records showNowPlayingToast/hideNowPlayingToast. Each client
|
||||
* tick is musicManager.tick() then soundManager.tick(pause), as Minecraft.tick
|
||||
* runs them. Prints the manager's state and the music channel every step.
|
||||
*/
|
||||
public final class VanillaMusicOracle {
|
||||
public static final class Mc extends Minecraft {
|
||||
Music music;
|
||||
float volume = 1.0F;
|
||||
|
||||
Mc() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Music getSituationalMusic() {
|
||||
return music;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMusicVolume() {
|
||||
return volume;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Toasts extends ToastManager {
|
||||
final List<String> calls = new ArrayList<>();
|
||||
|
||||
Toasts() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showNowPlayingToast() {
|
||||
calls.add("show");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hideNowPlayingToast() {
|
||||
calls.add("hide");
|
||||
}
|
||||
}
|
||||
|
||||
static Music music(String spec) {
|
||||
if (spec.equals("-")) return null;
|
||||
String[] p = spec.split(",");
|
||||
return new Music(Holder.direct(SoundEvent.createVariableRangeEvent(Identifier.parse(p[0]))), Integer.parseInt(p[1]),
|
||||
Integer.parseInt(p[2]), p[3].equals("1"));
|
||||
}
|
||||
|
||||
static float f(String hex) {
|
||||
return Float.intBitsToFloat(Integer.parseUnsignedInt(hex, 16));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
net.minecraft.SharedConstants.tryDetectVersion();
|
||||
net.minecraft.server.Bootstrap.bootStrap();
|
||||
var rig = new VanillaSoundRig(Path.of(args[0]));
|
||||
Mc mc = VanillaSoundRig.make(Mc.class);
|
||||
Gui gui = VanillaSoundRig.make(Gui.class);
|
||||
Toasts toasts = VanillaSoundRig.make(Toasts.class);
|
||||
VanillaSoundRig.set(toasts, Toasts.class, "calls", new ArrayList<String>());
|
||||
VanillaSoundRig.set(gui, Gui.class, "toastManager", toasts);
|
||||
VanillaSoundRig.set(mc, Minecraft.class, "gui", gui);
|
||||
VanillaSoundRig.set(mc, Minecraft.class, "soundManager", rig.manager);
|
||||
var loading = VanillaSoundRig.make(LevelLoadingScreen.class);
|
||||
MusicManager manager = null;
|
||||
var gains = VanillaSoundRig.field(SoundEngine.class, "gainBySource");
|
||||
try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[2])))) {
|
||||
for (String line : Files.readAllLines(Path.of(args[1]))) {
|
||||
out.println(line);
|
||||
String[] a = line.split(" ");
|
||||
switch (a[0]) {
|
||||
case "start" -> {
|
||||
// A fresh manager: MusicManager(minecraft) with a seeded random.
|
||||
manager = VanillaSoundRig.make(MusicManager.class);
|
||||
VanillaSoundRig.set(manager, MusicManager.class, "minecraft", mc);
|
||||
VanillaSoundRig.set(manager, MusicManager.class, "random", RandomSource.create(Long.parseLong(a[1])));
|
||||
VanillaSoundRig.set(manager, MusicManager.class, "gameMusicFrequency", MusicManager.MusicFrequency.valueOf(a[2]));
|
||||
VanillaSoundRig.set(manager, MusicManager.class, "currentGain", 1.0F);
|
||||
VanillaSoundRig.set(manager, MusicManager.class, "nextSongDelay", 100);
|
||||
rig.manager.stop();
|
||||
}
|
||||
case "situation" -> {
|
||||
mc.music = music(a[1]);
|
||||
mc.volume = f(a[2]);
|
||||
}
|
||||
case "tick" -> {
|
||||
VanillaSoundRig.set(gui, Gui.class, "screen", a[1].equals("1") ? loading : null);
|
||||
manager.tick();
|
||||
rig.manager.tick(a[2].equals("1"));
|
||||
}
|
||||
case "finish" -> {
|
||||
var current = (SoundInstance) VanillaSoundRig.get(manager, MusicManager.class, "currentMusic");
|
||||
if (current != null) rig.finish(current);
|
||||
}
|
||||
case "stop" -> manager.stopPlaying();
|
||||
case "stopmusic" -> manager.stopPlaying(music(a[1]));
|
||||
case "freq" -> manager.setMinutesBetweenSongs(MusicManager.MusicFrequency.valueOf(a[1]));
|
||||
case "toast" -> manager.showNowPlayingToastIfNeeded();
|
||||
case "volume" -> {
|
||||
rig.volume(SoundSource.values()[Integer.parseInt(a[1])], f(a[2]));
|
||||
rig.manager.refreshCategoryVolume(SoundSource.values()[Integer.parseInt(a[1])]);
|
||||
}
|
||||
case "stopall" -> rig.manager.stop();
|
||||
default -> throw new IllegalArgumentException(line);
|
||||
}
|
||||
var current = (SoundInstance) VanillaSoundRig.get(manager, MusicManager.class, "currentMusic");
|
||||
out.printf(" current=%s delay=%d gain=%08x toastShown=%s musicGain=%08x%n", current == null ? "-" : current.getIdentifier(),
|
||||
(int) VanillaSoundRig.get(manager, MusicManager.class, "nextSongDelay"),
|
||||
Float.floatToRawIntBits((float) VanillaSoundRig.get(manager, MusicManager.class, "currentGain")),
|
||||
VanillaSoundRig.get(manager, MusicManager.class, "toastShown"),
|
||||
Float.floatToRawIntBits(((it.unimi.dsi.fastutil.objects.Object2FloatMap<SoundSource>) gains.get(rig.engine)).getFloat(SoundSource.MUSIC)));
|
||||
for (String call : toasts.calls) out.println(" toast " + call);
|
||||
toasts.calls.clear();
|
||||
if (current != null) out.println(rig.describe("music", current));
|
||||
rig.flush(out);
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.client.Camera;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import net.minecraft.client.gui.Hud;
|
||||
import net.minecraft.client.gui.components.BossHealthOverlay;
|
||||
import net.minecraft.client.gui.components.LerpingBossEvent;
|
||||
import net.minecraft.client.gui.screens.WinScreen;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.client.renderer.GameRenderer;
|
||||
import net.minecraft.core.*;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.resources.RegistryDataLoader;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.Bootstrap;
|
||||
import net.minecraft.server.RegistryLayer;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.repository.ServerPacksSource;
|
||||
import net.minecraft.server.packs.resources.MultiPackResourceManager;
|
||||
import net.minecraft.sounds.Music;
|
||||
import net.minecraft.tags.TagLoader;
|
||||
import net.minecraft.world.BossEvent;
|
||||
import net.minecraft.world.attribute.*;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.player.Abilities;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.dimension.DimensionType;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
/**
|
||||
* Calls the real Minecraft.getSituationalMusic and getMusicVolume on shells
|
||||
* holding the open screen (none or the credits' WinScreen), the boss overlay's
|
||||
* events, the player (its level's dimension, abilities and underwater state)
|
||||
* and a real camera EnvironmentAttributeProbe over the dimension and biome's
|
||||
* attribute layers from the vanilla data pack. Lines: "screen player
|
||||
* dimension biome boss instabuild mayfly underwater".
|
||||
*/
|
||||
public final class VanillaSituationalMusicOracle {
|
||||
static String music(Music m) {
|
||||
return m == null ? "-" : m.sound().value().location() + "," + m.minDelay() + "," + m.maxDelay() + "," + m.replaceCurrentMusic();
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private static void addBiome(EnvironmentAttributeSystem.Builder b, EnvironmentAttribute attr, Biome biome) {
|
||||
b.addPositionalLayer(attr, (base, pos, w) -> biome.getAttributes().applyModifier(attr, base));
|
||||
}
|
||||
|
||||
static LerpingBossEvent bar(boolean darken, boolean music) {
|
||||
return new LerpingBossEvent(UUID.randomUUID(), Component.empty(), 1.0F, BossEvent.BossBarColor.PINK, BossEvent.BossBarOverlay.PROGRESS,
|
||||
darken, music, false);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SharedConstants.tryDetectVersion();
|
||||
Bootstrap.bootStrap();
|
||||
var repo = ServerPacksSource.createVanillaTrustedRepository();
|
||||
repo.reload();
|
||||
repo.setSelected(List.of("vanilla"));
|
||||
var resources = new MultiPackResourceManager(PackType.SERVER_DATA, repo.openAllSelected());
|
||||
var layers = RegistryLayer.createRegistryAccess();
|
||||
var staticTags = TagLoader.loadTagsForExistingRegistries(resources, layers.getLayer(RegistryLayer.STATIC));
|
||||
var context = TagLoader.buildUpdatedLookups(layers.getAccessForLoading(RegistryLayer.WORLD), staticTags);
|
||||
RegistryAccess.Frozen world = RegistryDataLoader.load(resources, context, RegistryDataLoader.WORLD_REGISTRIES, Runnable::run).join();
|
||||
staticTags.forEach(Registry.PendingTags::apply);
|
||||
var dimensions = world.lookupOrThrow(Registries.DIMENSION_TYPE);
|
||||
var biomes = world.lookupOrThrow(Registries.BIOME);
|
||||
|
||||
Minecraft mc = VanillaSoundRig.make(Minecraft.class);
|
||||
Gui gui = VanillaSoundRig.make(Gui.class);
|
||||
Hud hud = VanillaSoundRig.make(Hud.class);
|
||||
BossHealthOverlay overlay = VanillaSoundRig.make(BossHealthOverlay.class);
|
||||
Map<UUID, LerpingBossEvent> events = new LinkedHashMap<>();
|
||||
VanillaSoundRig.set(overlay, BossHealthOverlay.class, "events", events);
|
||||
VanillaSoundRig.set(hud, Hud.class, "bossOverlay", overlay);
|
||||
VanillaSoundRig.set(gui, Gui.class, "hud", hud);
|
||||
VanillaSoundRig.set(mc, Minecraft.class, "gui", gui);
|
||||
GameRenderer renderer = VanillaSoundRig.make(GameRenderer.class);
|
||||
Camera camera = VanillaSoundRig.make(Camera.class);
|
||||
VanillaSoundRig.set(renderer, GameRenderer.class, "mainCamera", camera);
|
||||
VanillaSoundRig.set(mc, Minecraft.class, "gameRenderer", renderer);
|
||||
WinScreen credits = VanillaSoundRig.make(WinScreen.class);
|
||||
try (var w = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]), StandardCharsets.UTF_8))) {
|
||||
for (String line : Files.readAllLines(Path.of(args[0]), StandardCharsets.UTF_8)) {
|
||||
String[] f = line.split(" ");
|
||||
VanillaSoundRig.set(gui, Gui.class, "screen", f[0].equals("win") ? credits : null);
|
||||
events.clear();
|
||||
if (f[4].contains("dark")) events.put(UUID.randomUUID(), bar(true, false));
|
||||
if (f[4].contains("music")) events.put(UUID.randomUUID(), bar(false, true));
|
||||
var probe = new EnvironmentAttributeProbe();
|
||||
VanillaSoundRig.set(camera, Camera.class, "attributeProbe", probe);
|
||||
if (f[1].equals("1")) {
|
||||
DimensionType dim = dimensions.getOrThrow(ResourceKey.create(Registries.DIMENSION_TYPE, Identifier.parse(f[2]))).value();
|
||||
Biome biome = biomes.getOrThrow(ResourceKey.create(Registries.BIOME, Identifier.parse(f[3]))).value();
|
||||
var b = EnvironmentAttributeSystem.builder();
|
||||
b.addConstantLayer(dim.attributes());
|
||||
for (EnvironmentAttribute<?> attr : biome.getAttributes().keySet()) addBiome(b, attr, biome);
|
||||
ClientLevel level = VanillaSoundRig.make(ClientLevel.class);
|
||||
VanillaSoundRig.set(level, ClientLevel.class, "environmentAttributes", b.build());
|
||||
VanillaSoundRig.set(level, Level.class, "dimension", f[2].equals("minecraft:the_end") ? Level.END
|
||||
: f[2].equals("minecraft:the_nether") ? Level.NETHER : Level.OVERWORLD);
|
||||
VanillaSoundRig.set(probe, EnvironmentAttributeProbe.class, "level", level);
|
||||
VanillaSoundRig.set(probe, EnvironmentAttributeProbe.class, "position", new Vec3(0.5, 64.0, 0.5));
|
||||
LocalPlayer player = VanillaSoundRig.make(LocalPlayer.class);
|
||||
VanillaSoundRig.set(player, Entity.class, "level", level);
|
||||
var abilities = new Abilities();
|
||||
abilities.instabuild = f[5].equals("1");
|
||||
abilities.mayfly = f[6].equals("1");
|
||||
VanillaSoundRig.set(player, Player.class, "abilities", abilities);
|
||||
VanillaSoundRig.set(player, Player.class, "wasUnderwater", f[7].equals("1"));
|
||||
mc.player = player;
|
||||
} else {
|
||||
mc.player = null;
|
||||
}
|
||||
w.printf("%s music=%s volume=%08x%n", line, music(mc.getSituationalMusic()), Float.floatToRawIntBits(mc.getMusicVolume()));
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
import net.minecraft.client.resources.sounds.*;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
|
||||
/**
|
||||
* Drives the real SoundEngine (see VanillaSoundRig) with a script of plays,
|
||||
* stops, category gains, option volumes, channel ends, pauses and ticks, and
|
||||
* prints after every step each instance's activity and AL source parameters,
|
||||
* the subtitle notifications and the channel pool counts.
|
||||
*/
|
||||
public final class VanillaSoundEngineOracle {
|
||||
/** The local player the underwater instances look at. */
|
||||
public static final class Actor extends LocalPlayer {
|
||||
boolean underwater;
|
||||
|
||||
Actor() {
|
||||
super(null, null, null, null, null, null, false, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUnderWater() {
|
||||
return underwater;
|
||||
}
|
||||
}
|
||||
|
||||
static float f(String hex) {
|
||||
return Float.intBitsToFloat(Integer.parseUnsignedInt(hex, 16));
|
||||
}
|
||||
|
||||
static double d(String hex) {
|
||||
return Double.longBitsToDouble(Long.parseUnsignedLong(hex, 16));
|
||||
}
|
||||
|
||||
static void seed(SoundInstance instance, long seed) throws Exception {
|
||||
VanillaSoundRig.set(instance, AbstractSoundInstance.class, "random", RandomSource.create(seed));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
net.minecraft.SharedConstants.tryDetectVersion();
|
||||
net.minecraft.server.Bootstrap.bootStrap();
|
||||
var rig = new VanillaSoundRig(Path.of(args[0]));
|
||||
var actor = VanillaSoundRig.make(Actor.class);
|
||||
Constructor<UnderLiquidSubSound> sub = UnderLiquidSubSound.class.getDeclaredConstructor(LocalPlayer.class, SoundEvent.class);
|
||||
sub.setAccessible(true);
|
||||
var instances = new TreeMap<String, SoundInstance>();
|
||||
var manager = rig.manager;
|
||||
try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[2])))) {
|
||||
for (String line : Files.readAllLines(Path.of(args[1]))) {
|
||||
out.println(line);
|
||||
String[] a = line.split(" ");
|
||||
switch (a[0]) {
|
||||
case "play" -> {
|
||||
// play name kind event source volume pitch x y z looping delay attenuation relative seed
|
||||
Identifier id = Identifier.parse(a[3]);
|
||||
SoundEvent event = SoundEvent.createVariableRangeEvent(id);
|
||||
long seed = Long.parseLong(a[14]);
|
||||
SoundInstance instance = switch (a[2]) {
|
||||
case "simple" -> new SimpleSoundInstance(id, SoundSource.values()[Integer.parseInt(a[4])], f(a[5]), f(a[6]),
|
||||
RandomSource.create(seed), a[10].equals("1"), Integer.parseInt(a[11]),
|
||||
a[12].equals("linear") ? SoundInstance.Attenuation.LINEAR : SoundInstance.Attenuation.NONE, d(a[7]), d(a[8]), d(a[9]),
|
||||
a[13].equals("1"));
|
||||
case "loop" -> new BiomeAmbientSoundsHandler.LoopSoundInstance(event);
|
||||
case "uloop" -> new UnderLiquidAmbientSoundInstance(event, actor, LocalPlayer::isUnderWater);
|
||||
case "usub" -> sub.newInstance(actor, event);
|
||||
default -> throw new IllegalArgumentException(line);
|
||||
};
|
||||
if (!a[2].equals("simple")) seed(instance, seed);
|
||||
instances.put(a[1], instance);
|
||||
out.println(" result " + manager.play(instance));
|
||||
}
|
||||
case "replay" -> out.println(" result " + manager.play(instances.get(a[1])));
|
||||
case "queue" -> manager.queueTickingSound((TickableSoundInstance) instances.get(a[1]));
|
||||
case "delayed" -> manager.playDelayed(instances.get(a[1]), Integer.parseInt(a[2]));
|
||||
case "fade" -> {
|
||||
if (instances.get(a[1]) instanceof BiomeAmbientSoundsHandler.LoopSoundInstance loop) {
|
||||
if (a[2].equals("in")) loop.fadeIn(); else loop.fadeOut();
|
||||
}
|
||||
}
|
||||
case "stop" -> manager.stop(instances.get(a[1]));
|
||||
case "stopmatch" -> manager.stop(a[1].equals("-") ? null : Identifier.parse(a[1]),
|
||||
a[2].equals("-") ? null : SoundSource.values()[Integer.parseInt(a[2])]);
|
||||
case "finish" -> rig.finish(instances.get(a[1]));
|
||||
case "gain" -> manager.updateCategoryVolume(SoundSource.values()[Integer.parseInt(a[1])], f(a[2]));
|
||||
case "volume" -> rig.volume(SoundSource.values()[Integer.parseInt(a[1])], f(a[2]));
|
||||
case "refresh" -> manager.refreshCategoryVolume(SoundSource.values()[Integer.parseInt(a[1])]);
|
||||
case "pause" -> manager.pauseAllExcept(SoundSource.MUSIC, SoundSource.UI);
|
||||
case "resume" -> manager.resume();
|
||||
case "player" -> {
|
||||
actor.underwater = a[1].equals("1");
|
||||
VanillaSoundRig.set(actor, Entity.class, "removalReason", a[2].equals("1") ? Entity.RemovalReason.DISCARDED : null);
|
||||
}
|
||||
case "tick" -> manager.tick(a[1].equals("1"));
|
||||
case "forget" -> instances.remove(a[1]);
|
||||
default -> throw new IllegalArgumentException(line);
|
||||
}
|
||||
for (var e : instances.entrySet()) out.println(rig.describe(e.getKey(), e.getValue()));
|
||||
rig.flush(out);
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import com.google.gson.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import net.minecraft.client.Options;
|
||||
import net.minecraft.client.resources.sounds.*;
|
||||
import net.minecraft.client.sounds.*;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.server.packs.*;
|
||||
import net.minecraft.server.packs.repository.PackSource;
|
||||
import net.minecraft.server.packs.resources.MultiPackResourceManager;
|
||||
import net.minecraft.server.packs.resources.ResourceManager;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.util.profiling.InactiveProfiler;
|
||||
|
||||
/**
|
||||
* Builds vanilla's sound event registry with the real SoundManager.prepare
|
||||
* (sounds.json read through a resource manager over the client asset root,
|
||||
* files validated against the listed .ogg resources) and Preparations.apply,
|
||||
* then resolves SimpleSoundInstances: getOrResolve with a seeded random,
|
||||
* getVolume/getPitch, and the instance random's next draw (to fix the number
|
||||
* of draws). Lines: "event seed volume pitch".
|
||||
*/
|
||||
public final class VanillaSoundEventOracle {
|
||||
static Field field(Class<?> type, String name) throws Exception {
|
||||
var f = type.getDeclaredField(name);
|
||||
f.setAccessible(true);
|
||||
return f;
|
||||
}
|
||||
|
||||
static String bits(float v) {
|
||||
return String.format("%08x", Float.floatToRawIntBits(v));
|
||||
}
|
||||
|
||||
static String sentinel(Sound sound) {
|
||||
if (sound == SoundManager.EMPTY_SOUND) return "empty";
|
||||
if (sound == SoundManager.INTENTIONALLY_EMPTY_SOUND) return "intentionally_empty";
|
||||
return "-";
|
||||
}
|
||||
|
||||
/** The real registry, prepared from an asset root. */
|
||||
@SuppressWarnings("unchecked")
|
||||
static SoundManager load(Path root) throws Exception {
|
||||
var info = new PackLocationInfo("horizon", Component.literal("horizon"), PackSource.BUILT_IN, Optional.empty());
|
||||
ResourceManager resources = new MultiPackResourceManager(PackType.CLIENT_RESOURCES, List.of(new PathPackResources(info, root)));
|
||||
var u = Class.forName("sun.misc.Unsafe");
|
||||
Object unsafe = field(u, "theUnsafe").get(null);
|
||||
Options options = (Options) u.getMethod("allocateInstance", Class.class).invoke(unsafe, Options.class);
|
||||
var manager = new SoundManager(options);
|
||||
Method prepare = SoundManager.class.getDeclaredMethod("prepare", ResourceManager.class, net.minecraft.util.profiling.ProfilerFiller.class);
|
||||
prepare.setAccessible(true);
|
||||
Object preparations = prepare.invoke(manager, resources, InactiveProfiler.INSTANCE);
|
||||
Method apply = preparations.getClass().getDeclaredMethod("apply", Map.class, Map.class, SoundEngine.class);
|
||||
apply.setAccessible(true);
|
||||
apply.invoke(preparations, field(SoundManager.class, "registry").get(manager), field(SoundManager.class, "soundCache").get(manager),
|
||||
field(SoundManager.class, "soundEngine").get(manager));
|
||||
return manager;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
net.minecraft.SharedConstants.tryDetectVersion();
|
||||
net.minecraft.server.Bootstrap.bootStrap();
|
||||
var manager = load(Path.of(args[0]));
|
||||
try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[2])))) {
|
||||
var names = new ArrayList<String>();
|
||||
for (var id : manager.getAvailableSounds()) names.add(id.toString());
|
||||
Collections.sort(names);
|
||||
for (String name : names) out.println("registered " + name);
|
||||
for (String line : Files.readAllLines(Path.of(args[1]))) {
|
||||
String[] f = line.split(" ");
|
||||
long seed = Long.parseLong(f[1]);
|
||||
float volume = Float.intBitsToFloat(Integer.parseUnsignedInt(f[2], 16));
|
||||
float pitch = Float.intBitsToFloat(Integer.parseUnsignedInt(f[3], 16));
|
||||
RandomSource random = RandomSource.create(seed);
|
||||
var instance = new SimpleSoundInstance(Identifier.parse(f[0]), SoundSource.MASTER, volume, pitch, random, false, 0,
|
||||
SoundInstance.Attenuation.LINEAR, 0.0, 0.0, 0.0, false);
|
||||
WeighedSoundEvents event = instance.getOrResolve(manager);
|
||||
Sound sound = instance.getSound();
|
||||
StringBuilder sb = new StringBuilder(line);
|
||||
if (event == null) {
|
||||
sb.append(" event=none");
|
||||
} else {
|
||||
String subtitle = "-";
|
||||
if (event.getSubtitle() != null && event.getSubtitle().getContents() instanceof TranslatableContents t) subtitle = t.getKey();
|
||||
sb.append(" event weight=").append(event.getWeight()).append(" subtitle=").append(subtitle);
|
||||
}
|
||||
float v = instance.getVolume();
|
||||
float p = instance.getPitch();
|
||||
sb.append(" sound=").append(sound.getLocation()).append(" path=").append(sound.getPath())
|
||||
.append(" sentinel=").append(sentinel(sound))
|
||||
.append(" weight=").append(sound.getWeight())
|
||||
.append(" type=").append(sound.getType().name())
|
||||
.append(" stream=").append(sound.shouldStream())
|
||||
.append(" preload=").append(sound.shouldPreload())
|
||||
.append(" attenuation=").append(sound.getAttenuationDistance())
|
||||
.append(" volume=").append(bits(v))
|
||||
.append(" pitch=").append(bits(p))
|
||||
.append(" range=").append(bits(sound.getAttenuationDistance(v)))
|
||||
.append(" next=").append(random.nextLong());
|
||||
out.println(sb);
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import com.mojang.blaze3d.audio.*;
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.reflect.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import javax.sound.sampled.AudioFormat;
|
||||
import net.minecraft.client.OptionInstance;
|
||||
import net.minecraft.client.Options;
|
||||
import net.minecraft.client.resources.sounds.SoundInstance;
|
||||
import net.minecraft.client.sounds.*;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import org.lwjgl.openal.AL10;
|
||||
|
||||
/**
|
||||
* A real vanilla SoundManager/SoundEngine running on OpenAL Soft's "No Output"
|
||||
* device (ALSOFT_DRIVERS=null), shared by the audio oracles.
|
||||
*
|
||||
* Determinism: the sound executor runs each task inline (the model where the
|
||||
* sound thread keeps up between client ticks); static sounds get one silent
|
||||
* hour-long buffer (they play until stopped or "finished" by the script) and
|
||||
* streams play endless silence. Everything else, the
|
||||
* channel pools, AL source parameters, lifetimes and bookkeeping, is the real
|
||||
* code and the real OpenAL implementation.
|
||||
*/
|
||||
public final class VanillaSoundRig {
|
||||
static Object unsafe;
|
||||
static Method allocate;
|
||||
|
||||
static <T> T make(Class<T> type) throws Exception {
|
||||
if (unsafe == null) {
|
||||
var u = Class.forName("sun.misc.Unsafe");
|
||||
unsafe = field(u, "theUnsafe").get(null);
|
||||
allocate = u.getMethod("allocateInstance", Class.class);
|
||||
}
|
||||
return type.cast(allocate.invoke(unsafe, type));
|
||||
}
|
||||
|
||||
static Field field(Class<?> type, String name) throws Exception {
|
||||
var f = type.getDeclaredField(name);
|
||||
f.setAccessible(true);
|
||||
return f;
|
||||
}
|
||||
|
||||
static Object get(Object target, Class<?> type, String name) throws Exception {
|
||||
return field(type, name).get(target);
|
||||
}
|
||||
|
||||
static void set(Object target, Class<?> type, String name, Object value) throws Exception {
|
||||
field(type, name).set(target, value);
|
||||
}
|
||||
|
||||
/** Runs every task on the calling thread. */
|
||||
public static final class Inline extends SoundEngineExecutor {
|
||||
@Override
|
||||
public void execute(final Runnable command) {
|
||||
command.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutDown() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startUp() {
|
||||
}
|
||||
}
|
||||
|
||||
static SoundBuffer silence;
|
||||
|
||||
/** Static sounds: a silent hour; streams: endless silence. */
|
||||
public static final class Buffers extends SoundBufferLibrary {
|
||||
Buffers() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<SoundBuffer> getCompleteBuffer(final Identifier location) {
|
||||
return CompletableFuture.completedFuture(silence);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<AudioStream> getStream(final Identifier location, final boolean looping) {
|
||||
return CompletableFuture.completedFuture(new Silence());
|
||||
}
|
||||
}
|
||||
|
||||
/** An endless silent stream, a minute per read (it never runs dry between ticks). */
|
||||
public static final class Silence implements AudioStream {
|
||||
static final AudioFormat FORMAT = new AudioFormat(8000.0F, 8, 1, false, false);
|
||||
|
||||
@Override
|
||||
public AudioFormat getFormat() {
|
||||
return FORMAT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer read(final int expectedSize) {
|
||||
ByteBuffer data = ByteBuffer.allocateDirect(8000 * 60);
|
||||
for (int i = 0; i < data.capacity(); i++) data.put(i, (byte) 0x80);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
}
|
||||
|
||||
public final SoundManager manager;
|
||||
public final SoundEngine engine;
|
||||
public final Options options;
|
||||
public final List<String> heard = new ArrayList<>();
|
||||
|
||||
public VanillaSoundRig(Path root) throws Exception {
|
||||
manager = VanillaSoundEventOracle.load(root);
|
||||
engine = (SoundEngine) get(manager, SoundManager.class, "soundEngine");
|
||||
options = (Options) get(engine, SoundEngine.class, "options");
|
||||
var inline = make(Inline.class);
|
||||
set(engine, SoundEngine.class, "executor", inline);
|
||||
set(get(engine, SoundEngine.class, "channelAccess"), ChannelAccess.class, "executor", inline);
|
||||
set(engine, SoundEngine.class, "soundBuffers", make(Buffers.class));
|
||||
Library library = (Library) get(engine, SoundEngine.class, "library");
|
||||
library.init(null, DeviceList.query(), false);
|
||||
ByteBuffer data = ByteBuffer.allocateDirect(8000 * 3600);
|
||||
for (int i = 0; i < data.capacity(); i++) data.put(i, (byte) 0x80);
|
||||
silence = new SoundBuffer(data, new AudioFormat(8000.0F, 8, 1, false, false));
|
||||
set(engine, SoundEngine.class, "loaded", true);
|
||||
Method slider = Options.class.getDeclaredMethod("createSoundSliderOptionInstance", String.class, SoundSource.class);
|
||||
slider.setAccessible(true);
|
||||
var volumes = new EnumMap<SoundSource, OptionInstance<Double>>(SoundSource.class);
|
||||
for (SoundSource source : SoundSource.values()) {
|
||||
volumes.put(source, (OptionInstance<Double>) slider.invoke(options, "soundCategory." + source.getName(), source));
|
||||
}
|
||||
set(options, Options.class, "soundSourceVolumes", volumes);
|
||||
manager.addListener((sound, event, range) -> {
|
||||
String subtitle = "-";
|
||||
if (event.getSubtitle() != null && event.getSubtitle().getContents() instanceof TranslatableContents t) subtitle = t.getKey();
|
||||
heard.add(String.format("notify %s %s %08x %s %016x %016x %016x", sound.getIdentifier(), subtitle, Float.floatToRawIntBits(range),
|
||||
sound.isLooping(), Double.doubleToRawLongBits(sound.getX()), Double.doubleToRawLongBits(sound.getY()),
|
||||
Double.doubleToRawLongBits(sound.getZ())));
|
||||
});
|
||||
}
|
||||
|
||||
/** Set a sound source volume option without its change callback. */
|
||||
public void volume(SoundSource source, double value) throws Exception {
|
||||
set(options.getSoundSourceOptionInstance(source), OptionInstance.class, "value", value);
|
||||
}
|
||||
|
||||
/** The channel of an instance, or null. */
|
||||
@SuppressWarnings("unchecked")
|
||||
public Channel channel(SoundInstance instance) throws Exception {
|
||||
var map = (Map<SoundInstance, ChannelAccess.ChannelHandle>) get(engine, SoundEngine.class, "instanceToChannel");
|
||||
var handle = map.get(instance);
|
||||
return handle == null ? null : (Channel) get(handle, ChannelAccess.ChannelHandle.class, "channel");
|
||||
}
|
||||
|
||||
/** The channel's end: what OpenAL does when a non-looping buffer runs out. */
|
||||
public void finish(SoundInstance instance) throws Exception {
|
||||
var channel = channel(instance);
|
||||
if (channel != null) channel.stop();
|
||||
}
|
||||
|
||||
static String fbits(float v) {
|
||||
return String.format("%08x", Float.floatToRawIntBits(v));
|
||||
}
|
||||
|
||||
/** One line describing an instance: activity and its AL source. */
|
||||
public String describe(String name, SoundInstance instance) throws Exception {
|
||||
StringBuilder sb = new StringBuilder(" ").append(name).append(" active=").append(manager.isActive(instance));
|
||||
var channel = channel(instance);
|
||||
if (channel == null) {
|
||||
sb.append(" channel=-");
|
||||
} else {
|
||||
int source = (int) get(channel, Channel.class, "source");
|
||||
boolean initialized = ((java.util.concurrent.atomic.AtomicBoolean) get(channel, Channel.class, "initialized")).get();
|
||||
if (!initialized) {
|
||||
sb.append(" channel=released");
|
||||
} else {
|
||||
float[] pos = new float[3];
|
||||
AL10.alGetSourcefv(source, AL10.AL_POSITION, pos);
|
||||
sb.append(String.format(" state=%x gain=%s pitch=%s pos=%s,%s,%s rel=%d loop=%d model=%x max=%s rolloff=%s ref=%s",
|
||||
AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE), fbits(AL10.alGetSourcef(source, AL10.AL_GAIN)),
|
||||
fbits(AL10.alGetSourcef(source, AL10.AL_PITCH)), fbits(pos[0]), fbits(pos[1]), fbits(pos[2]),
|
||||
AL10.alGetSourcei(source, AL10.AL_SOURCE_RELATIVE), AL10.alGetSourcei(source, AL10.AL_LOOPING),
|
||||
AL10.alGetSourcei(source, 0xD000), fbits(AL10.alGetSourcef(source, AL10.AL_MAX_DISTANCE)),
|
||||
fbits(AL10.alGetSourcef(source, AL10.AL_ROLLOFF_FACTOR)), fbits(AL10.alGetSourcef(source, AL10.AL_REFERENCE_DISTANCE))));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** Print and clear the notifications, then the channel counts. */
|
||||
public void flush(PrintWriter out) {
|
||||
for (String h : heard) out.println(h);
|
||||
heard.clear();
|
||||
out.println(" " + manager.getChannelDebugString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Shared runner for the audio differential harnesses.
|
||||
|
||||
Each harness writes an input file, runs a Java oracle against the pinned
|
||||
client (the real SoundManager / SoundEngine / MusicManager / ambient handlers;
|
||||
OpenAL runs on OpenAL Soft's "No Output" device so channel parameters can be
|
||||
read back from real AL sources) and a Rust example replaying the same inputs
|
||||
through Horizon's audio code, then requires byte-identical output.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
from . import lab, test_rust_parity as base, test_model_baking_parity as baking
|
||||
except ImportError:
|
||||
import lab, test_rust_parity as base, test_model_baking_parity as baking
|
||||
|
||||
RESOURCES = lab.LOCAL / 'horizon-client/26.3'
|
||||
COMMON_SOURCES = [
|
||||
lab.ROOT / 'tools/audio_harness.py',
|
||||
lab.ROOT / 'tools/test_rust_parity.py',
|
||||
lab.ROOT / 'tools/lab.py',
|
||||
lab.ROOT / 'Cargo.toml',
|
||||
lab.ROOT / 'Cargo.lock',
|
||||
lab.ROOT / 'crates/horizon-client/Cargo.toml',
|
||||
lab.ROOT / 'crates/horizon-client/src/sound_events.rs',
|
||||
lab.ROOT / 'crates/horizon-client/src/sound_engine.rs',
|
||||
]
|
||||
|
||||
|
||||
def require_sounds():
|
||||
if not (RESOURCES / 'assets/minecraft/sounds.json').is_file():
|
||||
raise SystemExit('Sound assets missing: run tools/fetch_sounds.py first')
|
||||
|
||||
|
||||
def run_harness(name, oracle, example, inputs_text, extra_sources=(), java_args=(), rust_args=(), scope='', timeout=1800):
|
||||
"""Run oracle and example on inputs; returns the number of matched lines."""
|
||||
require_sounds()
|
||||
lock, jars, _ = base.verify_artifacts()
|
||||
libraries = baking.client_libraries()
|
||||
build = lab.LOCAL / f'rust-{name}'
|
||||
build.mkdir(parents=True, exist_ok=True)
|
||||
report = build / 'report.json'
|
||||
report.unlink(missing_ok=True)
|
||||
oracle_path = lab.ROOT / f'mods/horizon/src/test/java/dev/horizonlab/parity/{oracle}.java'
|
||||
example_path = lab.ROOT / f'crates/horizon-client/examples/{example}.rs'
|
||||
sources = [*COMMON_SOURCES, oracle_path, example_path, *[Path(s) for s in extra_sources]]
|
||||
tested = base.source_hashes(sources)
|
||||
inputs = build / 'inputs.txt'
|
||||
inputs.write_text(inputs_text)
|
||||
input_hash = lab.digest(inputs, 'sha256')
|
||||
|
||||
def run(command, log, env=None):
|
||||
with (build / (log + ".log")).open("w") as out:
|
||||
try:
|
||||
subprocess.run(list(map(str, command)), cwd=lab.ROOT, check=True, timeout=timeout, stdout=out, stderr=subprocess.STDOUT, env=env)
|
||||
except subprocess.CalledProcessError:
|
||||
raise SystemExit(f"{log} failed: see {build / (log + '.log')}") from None
|
||||
|
||||
classes = build / 'classes'
|
||||
classes.mkdir(exist_ok=True)
|
||||
dependencies = os.pathsep.join(map(str, [jars['client'], *libraries]))
|
||||
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-sourcepath', lab.ROOT / 'mods/horizon/src/test/java',
|
||||
'-cp', dependencies, '-d', classes, oracle_path], 'compile-java')
|
||||
run(['cargo', 'build', '--locked', '-p', 'horizon-client', '--example', example], 'compile-rust')
|
||||
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--locked', '--no-deps', '--format-version', '1'], cwd=lab.ROOT))
|
||||
binary = Path(metadata['target_directory']) / 'debug/examples' / (example + ('.exe' if os.name == 'nt' else ''))
|
||||
run([binary, *rust_args, inputs, build / 'rust.txt'], 'rust')
|
||||
env = dict(os.environ, ALSOFT_DRIVERS='null')
|
||||
run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, f'dev.horizonlab.parity.{oracle}', *java_args, inputs, build / 'client.txt'],
|
||||
'client', env=env)
|
||||
matched = base.compare(build / 'client.txt', build / 'rust.txt')
|
||||
base.ensure_sources_unchanged(sources, tested)
|
||||
if input_hash != lab.digest(inputs, 'sha256'):
|
||||
raise AssertionError('Input fixtures changed')
|
||||
lab.write_json(report, dict(
|
||||
schema=1, minecraft=lock['minecraft']['version'], manifest_sha1=lock['minecraft']['manifest_sha1'], platform=platform.platform(),
|
||||
rustc=subprocess.check_output(['rustc', '--version'], text=True).strip(),
|
||||
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
|
||||
sources_sha256=tested, input_sha256=input_hash, client_jar_sha256=lab.digest(jars['client'], 'sha256'),
|
||||
sounds_json_sha256=lab.digest(RESOURCES / 'assets/minecraft/sounds.json', 'sha256'),
|
||||
matched_output_lines=matched, output_sha256=lab.digest(build / 'rust.txt', 'sha256'), scope=scope))
|
||||
return matched, report
|
||||
|
||||
|
||||
def bits(value):
|
||||
import struct
|
||||
return struct.pack('>f', value).hex()
|
||||
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare the local player's ambient sounds with the vanilla 26.3 client.
|
||||
|
||||
The oracle runs the real `LocalPlayer.updateIsUnderwater` (enter/exit sounds
|
||||
seeded from the level random, the `UnderLiquidAmbientSoundInstance`),
|
||||
`UnderwaterAmbientSoundHandler`, `BubbleColumnAmbientSoundHandler` and
|
||||
`BiomeAmbientSoundsHandler` over the real
|
||||
`SoundEngine` (see VanillaSoundRig), then `SoundManager.tick`, once per client
|
||||
tick. The `AMBIENT_SOUNDS` value (loops, moods and additions from the biomes
|
||||
and dimensions of the data pack plus faster custom moods), the player's
|
||||
position, eye height, eyes in water, spectator and removed flags and the
|
||||
level's brightness are script inputs. Every step prints the level random's
|
||||
next long (so every draw is compared), moodiness, the underwater state and
|
||||
delay, the handler's loop instances (fade, direction, stopped, active), each
|
||||
playing instance and the subtitle notifications. Horizon's `Ambience` and
|
||||
`SoundEngine` replay the scripts.
|
||||
"""
|
||||
import json
|
||||
import random
|
||||
import struct
|
||||
|
||||
try:
|
||||
from . import audio_harness as h
|
||||
except ImportError:
|
||||
import audio_harness as h
|
||||
|
||||
|
||||
def dbits(v):
|
||||
return struct.pack('>d', v).hex()
|
||||
|
||||
|
||||
def settings():
|
||||
"""Every AMBIENT_SOUNDS value in the data pack."""
|
||||
data = h.RESOURCES / 'data/minecraft'
|
||||
values = []
|
||||
for path in sorted(list((data / 'worldgen/biome').glob('*.json')) + list((data / 'dimension_type').glob('*.json'))):
|
||||
v = json.loads(path.read_text()).get('attributes', {}).get('minecraft:audio/ambient_sounds')
|
||||
if v is None:
|
||||
continue
|
||||
additions = v.get('additions', [])
|
||||
if isinstance(additions, dict):
|
||||
additions = [additions]
|
||||
mood = v.get('mood')
|
||||
values.append(dict(loop=v.get('loop'), mood=None if mood is None else (mood['sound'], mood['tick_delay'], mood['block_search_extent'], mood['offset']),
|
||||
additions=[(a['sound'], a['tick_chance']) for a in additions]))
|
||||
return values
|
||||
|
||||
|
||||
class Script:
|
||||
def __init__(self):
|
||||
self.lines = []
|
||||
|
||||
def add(self, *parts):
|
||||
self.lines.append(' '.join(map(str, parts)))
|
||||
|
||||
def sounds(self, value):
|
||||
mood = '-' if value['mood'] is None else '{},{},{},{}'.format(value['mood'][0], value['mood'][1], value['mood'][2], dbits(value['mood'][3]))
|
||||
self.add('sounds', value['loop'] or '-', mood, *[f'{s},{dbits(c)}' for s, c in value['additions']])
|
||||
|
||||
def player(self, at=(0.5, 64.0, 0.5), eye=1.62, water=0, spectator=0, removed=0, mode=0, height=1.8, columns=()):
|
||||
self.add('player', *map(dbits, at), h.bits(eye), water, spectator, removed, mode, h.bits(height),
|
||||
*[f'{x},{y},{z},{d}' for x, y, z, d in columns])
|
||||
|
||||
def tick(self, n=1):
|
||||
for _ in range(n):
|
||||
self.add('tick')
|
||||
|
||||
|
||||
def scripted(s, values):
|
||||
by_loop = [v for v in values if v['loop']]
|
||||
cave = next(v for v in values if v['mood'] and v['mood'][0] == 'minecraft:ambient.cave' and not v['loop'])
|
||||
s.add('start', 1)
|
||||
s.sounds(cave)
|
||||
s.player(mode=1)
|
||||
s.tick(6100)
|
||||
s.player((100.25, -20.0, -7.75), mode=2)
|
||||
s.tick(400)
|
||||
s.player((3.5, 12.0, 3.5), mode=3)
|
||||
s.tick(400)
|
||||
# Biome loops cross-fading, back and forth.
|
||||
s.add('start', 2)
|
||||
for v in by_loop:
|
||||
s.sounds(v)
|
||||
s.tick(30)
|
||||
s.sounds(by_loop[0])
|
||||
s.tick(20)
|
||||
s.sounds(by_loop[1])
|
||||
s.tick(5)
|
||||
s.sounds(by_loop[0])
|
||||
s.tick(60)
|
||||
s.sounds(dict(loop=None, mood=None, additions=[]))
|
||||
s.tick(60)
|
||||
# Water: enter, the loop's fade, sub-sounds, exit, re-entering mid-fade, spectators.
|
||||
s.add('start', 3)
|
||||
s.sounds(cave)
|
||||
s.player(water=1, mode=0)
|
||||
s.tick(300)
|
||||
s.player(water=0)
|
||||
s.tick(10)
|
||||
s.player(water=1)
|
||||
s.tick(3)
|
||||
s.player(water=0)
|
||||
s.tick(2)
|
||||
s.player(water=1)
|
||||
s.tick(60)
|
||||
s.player(water=1, spectator=1)
|
||||
s.tick(5)
|
||||
s.player(water=0, spectator=1)
|
||||
s.tick(5)
|
||||
s.player(water=1, removed=1)
|
||||
s.tick(5)
|
||||
s.player(water=0)
|
||||
s.tick(25)
|
||||
# Silent ambient category: loops never start and linger.
|
||||
s.add('start', 4)
|
||||
s.add('volume', 8, h.bits(0.0))
|
||||
s.sounds(by_loop[0])
|
||||
s.tick(50)
|
||||
s.add('volume', 8, h.bits(1.0))
|
||||
s.sounds(by_loop[1])
|
||||
s.tick(50)
|
||||
s.sounds(by_loop[0])
|
||||
s.tick(50)
|
||||
# Bubble columns: entering, staying, the first tick, spectators, small boxes.
|
||||
s.add('start', 5)
|
||||
column = [(0, 64, 0, 1), (0, 65, 0, 1)]
|
||||
s.player((0.5, 64.0, 0.5), water=1, columns=column)
|
||||
s.tick(3)
|
||||
s.player((3.5, 64.0, 0.5), water=1, columns=column)
|
||||
s.tick(2)
|
||||
s.player((0.5, 64.0, 0.5), water=1, columns=column)
|
||||
s.tick(2)
|
||||
s.player((0.5, 64.0, 0.5), water=1, columns=[(0, 64, 0, 0), (1, 64, 0, 1)])
|
||||
s.tick(2)
|
||||
s.player((3.5, 64.0, 0.5))
|
||||
s.tick(2)
|
||||
s.player((0.9, 64.5, 0.5), water=1, height=0.6, columns=[(1, 64, 0, 0), (0, 64, 0, 1)])
|
||||
s.tick(2)
|
||||
s.player((3.5, 64.0, 0.5))
|
||||
s.tick(1)
|
||||
s.player((0.5, 63.7, 0.5), spectator=1, columns=column)
|
||||
s.tick(2)
|
||||
|
||||
|
||||
def fuzz(s, rng, values, steps):
|
||||
s.add('start', rng.getrandbits(63))
|
||||
for _ in range(steps):
|
||||
op = rng.random()
|
||||
if op < 0.05:
|
||||
v = rng.choice(values + [dict(loop=None, mood=None, additions=[])])
|
||||
if rng.random() < 0.5 and v['mood']:
|
||||
v = dict(v, mood=(v['mood'][0], rng.choice([20, 60, 150]), rng.choice([1, 4, 8, 16]), rng.choice([0.0, 2.0, 5.5])))
|
||||
s.sounds(v)
|
||||
elif op < 0.12:
|
||||
at = (rng.uniform(-3e4, 3e4) if rng.random() < 0.2 else rng.uniform(-200, 200), rng.uniform(-64, 320), rng.uniform(-200, 200))
|
||||
columns = []
|
||||
if rng.random() < 0.3:
|
||||
bx, by, bz = int(at[0] // 1), int(at[1] // 1), int(at[2] // 1)
|
||||
columns = [(bx + rng.randrange(-1, 2), by + rng.randrange(-1, 3), bz + rng.randrange(-1, 2), rng.randrange(2)) for _ in range(rng.randrange(1, 4))]
|
||||
s.player(at, rng.choice([1.62, 1.27, 0.4, 0.2]), int(rng.random() < 0.4), int(rng.random() < 0.05), int(rng.random() < 0.03), rng.randrange(4),
|
||||
rng.choice([1.8, 1.5, 0.6]), columns)
|
||||
elif op < 0.13:
|
||||
s.add('volume', rng.choice([0, 8]), h.bits(rng.choice([0.0, 1.0, 0.5])))
|
||||
else:
|
||||
s.tick(rng.randrange(1, 40))
|
||||
|
||||
|
||||
def fixtures():
|
||||
values = settings()
|
||||
s = Script()
|
||||
scripted(s, values)
|
||||
rng = random.Random(0x414d4249)
|
||||
for _ in range(40):
|
||||
fuzz(s, rng, values, 150)
|
||||
return s.lines
|
||||
|
||||
|
||||
def main():
|
||||
lines = fixtures()
|
||||
matched, report = h.run_harness(
|
||||
'ambient', 'VanillaAmbientOracle', 'ambient_fixture', ''.join(l + '\n' for l in lines),
|
||||
extra_sources=[h.lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSoundRig.java',
|
||||
h.lab.ROOT / 'crates/horizon-client/src/ambience.rs',
|
||||
h.lab.ROOT / 'crates/horizon-client/examples/audio_common/mod.rs'],
|
||||
java_args=[h.RESOURCES], rust_args=[h.RESOURCES],
|
||||
scope='Real LocalPlayer.updateIsUnderwater, UnderwaterAmbientSoundHandler and BiomeAmbientSoundsHandler (loop cross-fades, additions, '
|
||||
'cave mood sampling, moodiness and the mood sound position) with UnderLiquidAmbientSoundInstance/UnderLiquidSubSound/LoopSoundInstance '
|
||||
'ticking in the real SoundEngine: every level random draw, handler state, playing instance and notification per tick. The '
|
||||
'AMBIENT_SOUNDS value, eyes-in-water state and brightness are inputs. BubbleColumnAmbientSoundHandler runs on a scripted block world.')
|
||||
print(f'{matched:,} ambient lines match the pinned client ({len(lines):,} steps): {report}', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare the audio environment attributes with the vanilla 26.3 data pack.
|
||||
|
||||
The oracle evaluates vanilla's `EnvironmentAttributeSystem` (dimension layer,
|
||||
biome positional layer, dimension timelines, weather layers) for every
|
||||
dimension type and biome and prints `BACKGROUND_MUSIC` (all three tracks and
|
||||
`BackgroundMusic.select` for each creative/underwater combination),
|
||||
`MUSIC_VOLUME` and `AMBIENT_SOUNDS` (loop, mood, additions), plus the
|
||||
`Musics` constants. Horizon's `MusicTables` and `AmbientTables` answer the same.
|
||||
"""
|
||||
try:
|
||||
from . import audio_harness as h
|
||||
except ImportError:
|
||||
import audio_harness as h
|
||||
|
||||
|
||||
def fixtures():
|
||||
data = h.RESOURCES / 'data/minecraft'
|
||||
biomes = sorted('minecraft:' + p.stem for p in (data / 'worldgen/biome').glob('*.json'))
|
||||
dimensions = sorted('minecraft:' + p.stem for p in (data / 'dimension_type').glob('*.json'))
|
||||
return [f'{d} {b} {t}' for d in dimensions for b in biomes for t in (0, 13000)]
|
||||
|
||||
|
||||
def main():
|
||||
lines = fixtures()
|
||||
matched, report = h.run_harness(
|
||||
'audio-attributes', 'VanillaAudioAttributesOracle', 'audio_attributes_fixture', ''.join(l + '\n' for l in lines),
|
||||
extra_sources=[h.lab.ROOT / 'crates/horizon-client/src/music.rs', h.lab.ROOT / 'crates/horizon-client/src/ambience.rs'],
|
||||
rust_args=[h.RESOURCES],
|
||||
scope='Vanilla EnvironmentAttributeSystem values of audio/background_music (with BackgroundMusic.select), audio/music_volume and '
|
||||
'audio/ambient_sounds for every dimension type x biome at two times of day, and the Musics constants.')
|
||||
print(f'{matched:,} audio attribute lines match the pinned data ({len(lines):,} cases): {report}', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,167 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare the music manager with the vanilla 26.3 client.
|
||||
|
||||
The oracle runs the real `MusicManager` over the real `SoundEngine` (OpenAL
|
||||
Soft "No Output" device, see VanillaSoundRig). The situation
|
||||
(`getSituationalMusic`, `getMusicVolume`), the loading screen, pauses, the
|
||||
end of a track, `stopPlaying`, `setMinutesBetweenSongs`, the music category
|
||||
volume and `showNowPlayingToastIfNeeded` are script inputs; every step prints
|
||||
the current track, `nextSongDelay`, `currentGain`, `toastShown`, the toast
|
||||
calls, the MUSIC category gain and the music channel's AL source. Horizon's
|
||||
`MusicManager` and `SoundEngine` replay the scripts. Tracks use events whose
|
||||
variants share one volume, since `forMusic` resolves with an unseeded random.
|
||||
"""
|
||||
import random
|
||||
|
||||
try:
|
||||
from . import audio_harness as h
|
||||
except ImportError:
|
||||
import audio_harness as h
|
||||
|
||||
TRACKS = ['minecraft:music.credits', 'minecraft:music.dragon', 'minecraft:music.end', 'minecraft:music.under_water',
|
||||
'minecraft:music.overworld.swamp', 'minecraft:music_disc.cat', 'minecraft:music_disc.13']
|
||||
ODD = ['minecraft:music.nether.warped_forest', 'minecraft:not.a.track']
|
||||
|
||||
|
||||
class Script:
|
||||
def __init__(self):
|
||||
self.lines = []
|
||||
|
||||
def add(self, *parts):
|
||||
self.lines.append(' '.join(map(str, parts)))
|
||||
|
||||
def situation(self, sound=None, lo=0, hi=0, replace=0, volume=1.0):
|
||||
self.add('situation', '-' if sound is None else f'{sound},{lo},{hi},{replace}', h.bits(volume))
|
||||
|
||||
def tick(self, n=1, loading=0, paused=0):
|
||||
for _ in range(n):
|
||||
self.add('tick', loading, paused)
|
||||
|
||||
|
||||
def scripted(s):
|
||||
end, dragon, water, credits, cat = TRACKS[2], TRACKS[1], TRACKS[3], TRACKS[0], TRACKS[5]
|
||||
s.add('start', 1, 'DEFAULT')
|
||||
# Menu-like music: 20..600 ticks, replacing.
|
||||
s.situation(end, 20, 600, 1)
|
||||
s.tick(3, loading=1)
|
||||
s.tick(120)
|
||||
s.add('finish')
|
||||
s.tick(650)
|
||||
# Game music does not replace, and the next delay comes from its range.
|
||||
s.situation(water, 12000, 24000, 0)
|
||||
s.tick(5)
|
||||
s.add('finish')
|
||||
s.tick(30)
|
||||
s.add('freq', 'FREQUENT')
|
||||
s.tick(3)
|
||||
s.add('freq', 'CONSTANT')
|
||||
s.tick(110)
|
||||
s.add('freq', 'DEFAULT')
|
||||
s.tick(2)
|
||||
# A replacing track stops the current one over the following ticks.
|
||||
s.situation(dragon, 0, 0, 1)
|
||||
s.tick(30)
|
||||
s.situation(credits, 0, 0, 1)
|
||||
s.tick(30)
|
||||
s.situation(None)
|
||||
s.tick(5)
|
||||
s.add('finish')
|
||||
s.tick(5)
|
||||
s.situation(end, 6000, 24000, 1)
|
||||
s.tick(130)
|
||||
# Music volume fades: down to silence, then up from a partial volume.
|
||||
s.situation(end, 6000, 24000, 1, volume=0.0)
|
||||
s.tick(320)
|
||||
s.situation(cat, 0, 0, 1, volume=0.25)
|
||||
s.tick(5)
|
||||
s.situation(cat, 0, 0, 1, volume=1.0)
|
||||
s.tick(400)
|
||||
s.situation(cat, 0, 0, 1, volume=0.6)
|
||||
s.tick(80)
|
||||
# Silent starts and the toast, stops, pauses.
|
||||
s.add('volume', 1, h.bits(0.0))
|
||||
s.add('stop')
|
||||
s.situation(dragon, 0, 0, 1)
|
||||
s.tick(105)
|
||||
s.add('toast')
|
||||
s.add('volume', 1, h.bits(0.5))
|
||||
s.add('toast')
|
||||
s.tick(2)
|
||||
s.add('stopmusic', f'{end},0,0,1')
|
||||
s.add('stopmusic', f'{dragon},0,0,1')
|
||||
s.tick(105, paused=1)
|
||||
s.add('finish')
|
||||
s.tick(3, paused=1)
|
||||
s.tick(5)
|
||||
s.add('stopall')
|
||||
s.tick(5)
|
||||
# Events that cannot play.
|
||||
for event in ODD:
|
||||
s.add('start', 7, 'DEFAULT')
|
||||
s.situation(event, 0, 40, 1)
|
||||
s.tick(150)
|
||||
# Real Musics delays over a long session.
|
||||
s.add('start', 99, 'DEFAULT')
|
||||
s.situation(water, 12000, 24000, 0)
|
||||
s.tick(130)
|
||||
s.add('finish')
|
||||
s.tick(24100)
|
||||
|
||||
|
||||
def fuzz(s, rng, steps):
|
||||
s.add('start', rng.getrandbits(63), rng.choice(['DEFAULT', 'FREQUENT', 'CONSTANT']))
|
||||
for _ in range(steps):
|
||||
op = rng.random()
|
||||
if op < 0.08:
|
||||
sound = rng.choice(TRACKS + ODD[:1]) if rng.random() < 0.9 else None
|
||||
lo = rng.choice([0, 20, 100, 300])
|
||||
s.situation(sound, lo, lo + rng.choice([0, 50, 280, 600]), int(rng.random() < 0.5),
|
||||
rng.choice([1.0, 1.0, 1.0, 0.0, 0.5, rng.random()]))
|
||||
elif op < 0.13:
|
||||
s.add('finish')
|
||||
elif op < 0.15:
|
||||
s.add('stop')
|
||||
elif op < 0.16:
|
||||
s.add('stopmusic', f'{rng.choice(TRACKS)},0,0,1')
|
||||
elif op < 0.17:
|
||||
s.add('freq', rng.choice(['DEFAULT', 'FREQUENT', 'CONSTANT']))
|
||||
elif op < 0.18:
|
||||
s.add('toast')
|
||||
elif op < 0.20:
|
||||
s.add('volume', rng.choice([0, 1]), h.bits(rng.choice([0.0, 1.0, 0.5])))
|
||||
elif op < 0.21:
|
||||
s.add('stopall')
|
||||
elif op < 0.25:
|
||||
s.tick(rng.randrange(1, 30), loading=1)
|
||||
elif op < 0.28:
|
||||
s.tick(rng.randrange(1, 30), paused=1)
|
||||
else:
|
||||
s.tick(rng.randrange(1, 60))
|
||||
|
||||
|
||||
def fixtures():
|
||||
s = Script()
|
||||
scripted(s)
|
||||
rng = random.Random(0x4d55534943)
|
||||
for _ in range(60):
|
||||
fuzz(s, rng, 120)
|
||||
return s.lines
|
||||
|
||||
|
||||
def main():
|
||||
lines = fixtures()
|
||||
matched, report = h.run_harness(
|
||||
'music', 'VanillaMusicOracle', 'music_fixture', ''.join(l + '\n' for l in lines),
|
||||
extra_sources=[h.lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSoundRig.java',
|
||||
h.lab.ROOT / 'crates/horizon-client/src/music.rs',
|
||||
h.lab.ROOT / 'crates/horizon-client/examples/audio_common/mod.rs'],
|
||||
java_args=[h.RESOURCES], rust_args=[h.RESOURCES],
|
||||
scope='Real MusicManager.tick/startPlaying/stopPlaying/fadePlaying/setMinutesBetweenSongs/showNowPlayingToastIfNeeded with a seeded '
|
||||
'random over the real SoundEngine: track starts and replacements, nextSongDelay draws, the three music frequencies, the loading '
|
||||
'screen hold, fades and the MUSIC category gain, silent starts, toasts, pauses, track ends and unplayable events. The situation '
|
||||
'(getSituationalMusic/getMusicVolume) is an input here; see test_audio_attributes_parity.py for the attribute values.')
|
||||
print(f'{matched:,} music lines match the pinned client ({len(lines):,} steps): {report}', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare the situational music choice with the vanilla 26.3 client.
|
||||
|
||||
The oracle calls the real `Minecraft.getSituationalMusic` and
|
||||
`getMusicVolume` with the open screen (none or the credits), the boss
|
||||
overlay's bars (none, darkening, music), the player (absent, or in a dimension
|
||||
with creative abilities and an underwater state) and a real camera
|
||||
`EnvironmentAttributeProbe` over the dimension and biome attribute layers of
|
||||
the vanilla data pack. Horizon's `Situation` with `MusicTables` answers the
|
||||
same, for every dimension type x biome and every combination of the rest.
|
||||
"""
|
||||
import itertools
|
||||
|
||||
try:
|
||||
from . import audio_harness as h
|
||||
except ImportError:
|
||||
import audio_harness as h
|
||||
|
||||
|
||||
def fixtures():
|
||||
data = h.RESOURCES / 'data/minecraft'
|
||||
biomes = sorted('minecraft:' + p.stem for p in (data / 'worldgen/biome').glob('*.json'))
|
||||
dimensions = ['minecraft:overworld', 'minecraft:the_nether', 'minecraft:the_end']
|
||||
lines = []
|
||||
for screen, boss in itertools.product(['none', 'win'], ['none', 'dark', 'music', 'dark+music']):
|
||||
lines.append(f'{screen} 0 minecraft:overworld minecraft:plains {boss} 0 0 0')
|
||||
for d, b in itertools.product(dimensions, biomes):
|
||||
for screen, boss, instabuild, mayfly, water in itertools.product(['none', 'win'], ['none', 'dark', 'music'], '01', '01', '01'):
|
||||
lines.append(f'{screen} 1 {d} {b} {boss} {instabuild} {mayfly} {water}')
|
||||
return lines
|
||||
|
||||
|
||||
def main():
|
||||
lines = fixtures()
|
||||
matched, report = h.run_harness(
|
||||
'situational-music', 'VanillaSituationalMusicOracle', 'situational_music_fixture', ''.join(l + '\n' for l in lines),
|
||||
extra_sources=[h.lab.ROOT / 'crates/horizon-client/src/music.rs'], rust_args=[h.RESOURCES],
|
||||
scope='Real Minecraft.getSituationalMusic/getMusicVolume: screen music (credits), menu without a player, End boss music from '
|
||||
'the boss overlay, BACKGROUND_MUSIC and MUSIC_VOLUME through a real camera EnvironmentAttributeProbe, creative abilities '
|
||||
'(instabuild && mayfly) and underwater selection, for every dimension x biome.')
|
||||
print(f'{matched:,} situational music lines match the pinned client: {report}', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,247 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare the sound engine's bookkeeping with the vanilla 26.3 client.
|
||||
|
||||
The oracle runs the real `SoundManager`/`SoundEngine` on OpenAL Soft's "No
|
||||
Output" device (see VanillaSoundRig) and replays scripts of plays (simple,
|
||||
biome loop, underwater loop and sub-sound instances), stops (by instance, by
|
||||
sound and/or source, all), category gains, source volume options and
|
||||
refreshes, channel ends, delayed and queued sounds, pauses and client ticks.
|
||||
After every step it prints each instance's `isActive` and the AL source the
|
||||
engine configured (state, gain, pitch, position, relative, looping, distance
|
||||
model, max distance, rolloff, reference distance), the subtitle notifications
|
||||
and the channel pool counts. Horizon's `SoundEngine` replays the scripts
|
||||
against a backend that records the same AL source fields.
|
||||
"""
|
||||
import json
|
||||
import random
|
||||
|
||||
try:
|
||||
from . import audio_harness as h
|
||||
except ImportError:
|
||||
import audio_harness as h
|
||||
|
||||
|
||||
def dbits(v):
|
||||
import struct
|
||||
return struct.pack('>d', v).hex()
|
||||
|
||||
|
||||
class Script:
|
||||
def __init__(self, events, streams):
|
||||
self.lines = []
|
||||
self.events, self.streams = events, streams
|
||||
|
||||
def add(self, *parts):
|
||||
self.lines.append(' '.join(map(str, parts)))
|
||||
|
||||
def play(self, name, event, source=4, volume=1.0, pitch=1.0, at=(0.0, 0.0, 0.0), looping=0, delay=0, linear=True, relative=0, seed=0, kind='simple'):
|
||||
self.add('play', name, kind, event, source, h.bits(volume), h.bits(pitch), *map(dbits, at), looping, delay,
|
||||
'linear' if linear else 'none', relative, seed)
|
||||
|
||||
def tick(self, n=1, paused=0):
|
||||
for _ in range(n):
|
||||
self.add('tick', paused)
|
||||
|
||||
def reset(self):
|
||||
self.add('stopmatch', '-', '-')
|
||||
for source in range(11):
|
||||
self.add('volume', source, h.bits(1.0))
|
||||
self.tick()
|
||||
|
||||
|
||||
def scripted(s):
|
||||
stone, cave, click = 'minecraft:block.stone.break', 'minecraft:ambient.cave', 'minecraft:ui.button.click'
|
||||
music, loop, uloop = 'minecraft:music.game', 'minecraft:ambient.crimson_forest.loop', 'minecraft:ambient.underwater.loop'
|
||||
# Positional and relative sounds, lifetimes and channel ends.
|
||||
s.play('a', stone, 4, 1.0, 0.8, (10.5, 64.0, -3.25), seed=5)
|
||||
s.play('b', click, 10, 0.25, 1.0, linear=False, relative=1, seed=6)
|
||||
s.play('c', cave, 8, 4.0, 1.0, (100.0, 20.0, 100.0), seed=7)
|
||||
s.play('d', stone, 4, 1.5, 3.0, (0.5, 0.5, 0.5), seed=8)
|
||||
s.play('e', stone, 4, 0.5, 0.1, (1.0, 2.0, 3.0), seed=9)
|
||||
s.tick(3)
|
||||
s.add('finish', 'a')
|
||||
s.tick(25)
|
||||
s.add('finish', 'b')
|
||||
s.add('stop', 'c')
|
||||
s.tick(2)
|
||||
s.reset()
|
||||
# Volumes: options, category gains, refreshes, silent starts.
|
||||
s.add('volume', 4, h.bits(0.5))
|
||||
s.add('volume', 0, h.bits(0.75))
|
||||
s.play('a', stone, 4, 1.0, 1.0, (1.0, 1.0, 1.0), seed=1)
|
||||
s.add('volume', 4, h.bits(0.0))
|
||||
s.play('b', stone, 4, 1.0, 1.0, (1.0, 1.0, 1.0), seed=2)
|
||||
s.add('volume', 1, h.bits(0.0))
|
||||
s.play('m', music, 1, 1.0, 1.0, linear=False, relative=1, seed=3)
|
||||
s.add('refresh', 4)
|
||||
s.add('volume', 4, h.bits(0.3))
|
||||
s.add('refresh', 0)
|
||||
s.add('gain', 1, h.bits(0.5))
|
||||
s.add('gain', 1, h.bits(1.5))
|
||||
s.add('gain', 4, h.bits(-0.2))
|
||||
s.add('volume', 1, h.bits(1.0))
|
||||
s.add('refresh', 1)
|
||||
s.tick(2)
|
||||
s.add('volume', 0, h.bits(2.0))
|
||||
s.add('refresh', 0)
|
||||
s.reset()
|
||||
# Biome loops: fades, crossfade, stopping, removal.
|
||||
s.play('l1', loop, kind='loop', seed=11)
|
||||
s.add('fade', 'l1', 'in')
|
||||
s.tick(45)
|
||||
s.play('l2', 'minecraft:ambient.warped_forest.loop', kind='loop', seed=12)
|
||||
s.add('fade', 'l1', 'out')
|
||||
s.add('fade', 'l2', 'in')
|
||||
s.tick(50)
|
||||
s.add('fade', 'l2', 'out')
|
||||
s.tick(10)
|
||||
s.add('fade', 'l2', 'in')
|
||||
s.tick(60)
|
||||
s.add('volume', 8, h.bits(0.0))
|
||||
s.play('l3', loop, kind='loop', seed=13)
|
||||
s.tick(3)
|
||||
s.reset()
|
||||
# Underwater loop and sub-sounds.
|
||||
s.add('player', 1, 0)
|
||||
s.play('u', uloop, kind='uloop', seed=21)
|
||||
s.play('v', 'minecraft:ambient.underwater.loop.additions.rare', kind='usub', seed=22)
|
||||
s.tick(50)
|
||||
s.add('player', 0, 0)
|
||||
s.tick(5)
|
||||
s.add('player', 1, 0)
|
||||
s.tick(5)
|
||||
s.add('player', 0, 0)
|
||||
s.tick(30)
|
||||
s.add('player', 1, 1)
|
||||
s.play('u2', uloop, kind='uloop', seed=23)
|
||||
s.tick(3)
|
||||
s.add('player', 0, 0)
|
||||
s.reset()
|
||||
# Queued tickables, delayed and manually looped sounds.
|
||||
s.play('q', loop, kind='loop', seed=31)
|
||||
s.add('stop', 'q')
|
||||
s.tick(25)
|
||||
s.add('queue', 'q')
|
||||
s.add('fade', 'q', 'in')
|
||||
s.tick(3)
|
||||
s.play('w', stone, 4, 1.0, 1.0, (2.0, 2.0, 2.0), seed=32)
|
||||
s.add('delayed', 'w', 5)
|
||||
s.tick(7)
|
||||
s.play('r', stone, 4, 1.0, 1.0, (3.0, 3.0, 3.0), looping=1, delay=4, seed=33)
|
||||
s.tick(22)
|
||||
s.add('finish', 'r')
|
||||
s.tick(8)
|
||||
s.add('finish', 'r')
|
||||
s.tick(6)
|
||||
s.play('n', stone, 4, 1.0, 1.0, (3.0, 3.0, 3.0), looping=1, seed=34)
|
||||
s.tick(2)
|
||||
s.reset()
|
||||
# Stopping by source and sound, pauses.
|
||||
s.play('a', stone, 4, seed=41)
|
||||
s.play('b', stone, 5, seed=42)
|
||||
s.play('c', cave, 8, seed=43)
|
||||
s.play('m', music, 1, linear=False, relative=1, seed=44)
|
||||
s.play('u', click, 10, linear=False, relative=1, seed=45)
|
||||
s.add('pause')
|
||||
s.tick(1, paused=1)
|
||||
s.add('stop', 'm')
|
||||
s.tick(1, paused=1)
|
||||
s.add('resume')
|
||||
s.add('stopmatch', stone, '-')
|
||||
s.tick(25)
|
||||
s.add('stopmatch', '-', 8)
|
||||
s.add('stopmatch', click, 4)
|
||||
s.add('stopmatch', click, 10)
|
||||
s.tick(2)
|
||||
s.reset()
|
||||
# Unknown, empty and intentionally empty events; replaying an instance.
|
||||
for i, event in enumerate(['minecraft:not.a.sound', 'minecraft:intentionally_empty', 'minecraft:empty', 'minecraft:entity.generic.eat']):
|
||||
s.play(f'x{i}', event, 6, seed=50 + i)
|
||||
s.add('replay', 'x3')
|
||||
s.tick(25)
|
||||
s.reset()
|
||||
# Channel pools.
|
||||
for i in range(252):
|
||||
s.play('p', stone, 4, 1.0, 1.0, (float(i), 0.0, 0.0), seed=i)
|
||||
for i in range(10):
|
||||
s.play('s', music, 1, linear=False, relative=1, seed=i)
|
||||
s.tick(2)
|
||||
s.add('stopmatch', '-', '-')
|
||||
s.tick(1)
|
||||
|
||||
|
||||
def fuzz(s, rng, steps):
|
||||
names = ['f0', 'f1', 'f2', 'f3', 'f4', 'f5']
|
||||
live = set()
|
||||
for _ in range(steps):
|
||||
op = rng.random()
|
||||
name = rng.choice(names)
|
||||
if op < 0.25:
|
||||
kind = rng.choice(['simple', 'simple', 'simple', 'loop', 'uloop', 'usub'])
|
||||
event = rng.choice(s.streams if kind != 'simple' and rng.random() < 0.5 else s.events)
|
||||
at = tuple(rng.choice([0.0, rng.uniform(-200, 200)]) for _ in range(3))
|
||||
s.play(name, event, rng.randrange(11), rng.choice([1.0, 0.5, 2.0, 0.0, 4.0, rng.uniform(0, 3)]), rng.choice([1.0, 0.5, 2.5, rng.uniform(0.2, 3)]),
|
||||
at, int(rng.random() < 0.2), rng.choice([0, 0, 0, 3]), rng.random() < 0.7, int(rng.random() < 0.3), rng.getrandbits(63), kind)
|
||||
live.add(name)
|
||||
elif not live:
|
||||
s.tick()
|
||||
elif op < 0.32:
|
||||
s.add('finish', rng.choice(sorted(live)))
|
||||
elif op < 0.36:
|
||||
s.add('stop', rng.choice(sorted(live)))
|
||||
elif op < 0.40:
|
||||
s.add('fade', rng.choice(sorted(live)), rng.choice(['in', 'out']))
|
||||
elif op < 0.42:
|
||||
s.add('stopmatch', rng.choice(['-', rng.choice(s.events)]), rng.choice(['-', rng.randrange(11)]))
|
||||
elif op < 0.45:
|
||||
s.add('volume', rng.randrange(11), h.bits(rng.choice([0.0, 1.0, 0.5, rng.random()])))
|
||||
elif op < 0.48:
|
||||
s.add('refresh', rng.randrange(11))
|
||||
elif op < 0.51:
|
||||
s.add('gain', rng.randrange(11), h.bits(rng.choice([0.0, 1.0, 0.3, 1.2])))
|
||||
elif op < 0.53:
|
||||
s.add('player', int(rng.random() < 0.6), int(rng.random() < 0.05))
|
||||
elif op < 0.55:
|
||||
s.add('replay', rng.choice(sorted(live)))
|
||||
elif op < 0.56:
|
||||
s.add('pause')
|
||||
s.tick(rng.randrange(1, 4), paused=1)
|
||||
s.add('resume')
|
||||
elif op < 0.57:
|
||||
s.add('forget', name)
|
||||
live.discard(name)
|
||||
else:
|
||||
s.tick(rng.randrange(1, 6))
|
||||
|
||||
|
||||
def fixtures():
|
||||
sounds = json.loads((h.RESOURCES / 'assets/minecraft/sounds.json').read_text())
|
||||
names = sorted(sounds)
|
||||
streams = sorted('minecraft:' + n for n in names if any(isinstance(e, dict) and e.get('stream') for e in sounds[n].get('sounds', [])))
|
||||
rng = random.Random(0x454e47494e45)
|
||||
events = sorted(set(['minecraft:' + rng.choice(names) for _ in range(60)] + ['minecraft:ambient.cave', 'minecraft:block.stone.break']))
|
||||
s = Script(events, streams)
|
||||
scripted(s)
|
||||
for _ in range(40):
|
||||
s.reset()
|
||||
fuzz(s, rng, 150)
|
||||
return s.lines
|
||||
|
||||
|
||||
def main():
|
||||
lines = fixtures()
|
||||
matched, report = h.run_harness(
|
||||
'sound-engine', 'VanillaSoundEngineOracle', 'sound_engine_fixture', ''.join(l + '\n' for l in lines),
|
||||
extra_sources=[h.lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaSoundRig.java'],
|
||||
java_args=[h.RESOURCES], rust_args=[h.RESOURCES],
|
||||
scope='Real SoundEngine (OpenAL Soft null device, inline executor, silent static buffers, never-ready streams): play results, '
|
||||
'AL source parameters set by play/tickInGameSound/refreshCategoryVolume (gain from calculateVolume, clamped pitch, linear '
|
||||
'attenuation distance, looping, position, relative), isActive, the 20-tick minimum lifetime, channel ends, stop by instance/sound/source/all, '
|
||||
'pauseAllExcept/resume, tickMusicWhenPaused, queued tickables, playDelayed and manual looping, LoopSoundInstance, '
|
||||
'UnderLiquidAmbientSoundInstance and UnderLiquidSubSound ticks, subtitle notifications and ranges, and the 247/8 channel pools. '
|
||||
'Not compared: decoded sample output, OpenAL panning/HRTF and distance gain inside the mixer, asset loading timing.')
|
||||
print(f'{matched:,} sound engine lines match the pinned client ({len(lines):,} steps): {report}', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare sound event resolution with the vanilla 26.3 client.
|
||||
|
||||
The oracle builds the real `SoundManager` registry from the client asset root
|
||||
(`SoundManager.prepare` + `Preparations.apply`: sounds.json, `replace`,
|
||||
subtitles, file validation against the listed .ogg resources, event references
|
||||
weighing as much as their event) and resolves seeded `SimpleSoundInstance`s:
|
||||
`getOrResolve`, `getVolume`, `getPitch`, `Sound.getAttenuationDistance`, the
|
||||
stream/preload/type fields and the instance random's next draw. Horizon's
|
||||
`SoundRegistry` + `SoundEngine::resolve` replay the same inputs.
|
||||
"""
|
||||
import json
|
||||
import random
|
||||
|
||||
try:
|
||||
from . import audio_harness as h
|
||||
except ImportError:
|
||||
import audio_harness as h
|
||||
|
||||
|
||||
def fixtures():
|
||||
sounds = json.loads((h.RESOURCES / 'assets/minecraft/sounds.json').read_text())
|
||||
rng = random.Random(0x534f554e44)
|
||||
volumes = [1.0, 0.5, 0.25, 2.0, 4.0, 0.0, 1e-3, 16.0]
|
||||
pitches = [1.0, 0.5, 2.0, 0.8, 1.2, 3.0, 0.1]
|
||||
lines = []
|
||||
for name in sorted(sounds):
|
||||
event = 'minecraft:' + name
|
||||
count = len(sounds[name].get('sounds', []))
|
||||
seeds = [0, 1, -1, 42] + [rng.getrandbits(64) - (1 << 63) for _ in range(max(4, 2 * count))]
|
||||
for seed in seeds:
|
||||
lines.append(f'{event} {seed} {h.bits(rng.choice(volumes))} {h.bits(rng.choice(pitches))}')
|
||||
for event in ['minecraft:intentionally_empty', 'minecraft:empty', 'minecraft:not.a.sound', 'horizon:missing', 'block.stone.break', 'music.game']:
|
||||
for seed in [0, 7, -99]:
|
||||
lines.append(f'{event} {seed} {h.bits(1.0)} {h.bits(1.0)}')
|
||||
return lines
|
||||
|
||||
|
||||
def main():
|
||||
lines = fixtures()
|
||||
matched, report = h.run_harness(
|
||||
'sound-events', 'VanillaSoundEventOracle', 'sound_event_fixture', ''.join(l + '\n' for l in lines),
|
||||
java_args=[h.RESOURCES], rust_args=[h.RESOURCES],
|
||||
scope='Real SoundManager.prepare/Preparations.apply over the client asset root and SimpleSoundInstance.getOrResolve/getVolume/getPitch '
|
||||
'for every sounds.json event with seeded randoms (weighted choice, nested event references, MultipliedFloats volume/pitch, '
|
||||
'stream/preload/attenuation fields, EMPTY and INTENTIONALLY_EMPTY sentinels, draw counts) and the full registered event list.')
|
||||
print(f'{matched:,} sound event lines match the pinned client ({len(lines):,} resolutions): {report}', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user