refactor: split audio modules and widen streaming sound function signatures
Browse filesMove KnownSoundId to its own module (known_sound_id.rs), rename sound.rs
to sound_id.rs. Widen load_and_play_streaming to take *mut CGameTask
(needs sound_emitter) and load_and_play_streaming_positional to *mut CTask
(only reads ddgame at +0x2C). Add trap on original 0x546BB0 since its
sole caller (PlayWormSound2) is fully ported.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- crates/openwa-core/src/address.rs +4 -1
- crates/openwa-core/src/audio/dssound.rs +1 -1
- crates/openwa-core/src/audio/{sound.rs → known_sound_id.rs} +8 -45
- crates/openwa-core/src/audio/mod.rs +4 -2
- crates/openwa-core/src/audio/sound_id.rs +39 -0
- crates/openwa-core/src/audio/sound_ops.rs +11 -10
- crates/openwa-wormkit/src/replacements/sound.rs +6 -0
crates/openwa-core/src/address.rs
CHANGED
|
@@ -520,7 +520,10 @@ pub mod va {
|
|
| 520 |
/// CTaskWorm::PlaySound2 (FUN_00515020): usercall(EDI=worm) + stdcall(sound_id, volume, flags).
|
| 521 |
/// Stop+play on secondary sound handle (+0x3B4). 23 callers in WA.
|
| 522 |
fn/Usercall WORM_PLAY_SOUND_2 = 0x0051_5020;
|
| 523 |
-
///
|
|
|
|
|
|
|
|
|
|
| 524 |
/// Checks game conditions, then starts a streaming sound. Returns handle | 0x40000000.
|
| 525 |
fn/Usercall LOAD_AND_PLAY_STREAMING = 0x0054_6C20;
|
| 526 |
/// ComputeDistanceParams
|
|
|
|
| 520 |
/// CTaskWorm::PlaySound2 (FUN_00515020): usercall(EDI=worm) + stdcall(sound_id, volume, flags).
|
| 521 |
/// Stop+play on secondary sound handle (+0x3B4). 23 callers in WA.
|
| 522 |
fn/Usercall WORM_PLAY_SOUND_2 = 0x0051_5020;
|
| 523 |
+
/// LoadAndPlayStreamingPositional (0x546BB0): usercall(EAX=task) + stack(volume, sound_id, flags, x, y).
|
| 524 |
+
/// Like LoadAndPlayStreaming but with explicit position. Only caller is PlayWormSound2.
|
| 525 |
+
fn/Usercall LOAD_AND_PLAY_STREAMING_POSITIONAL = 0x0054_6BB0;
|
| 526 |
+
/// LoadAndPlayStreaming: usercall(EAX=task, ESI=&sound_emitter) + stack(sound_id, flags, volume).
|
| 527 |
/// Checks game conditions, then starts a streaming sound. Returns handle | 0x40000000.
|
| 528 |
fn/Usercall LOAD_AND_PLAY_STREAMING = 0x0054_6C20;
|
| 529 |
/// ComputeDistanceParams
|
crates/openwa-core/src/audio/dssound.rs
CHANGED
|
@@ -2,7 +2,7 @@ use crate::fixed::Fixed;
|
|
| 2 |
|
| 3 |
use windows::Win32::Media::Audio::DirectSound::IDirectSoundBuffer;
|
| 4 |
|
| 5 |
-
use crate::audio::
|
| 6 |
|
| 7 |
/// Volume-to-dB attenuation table (64 entries of i16).
|
| 8 |
/// Copied from WA.exe .rdata at 0x6A6A60.
|
|
|
|
| 2 |
|
| 3 |
use windows::Win32::Media::Audio::DirectSound::IDirectSoundBuffer;
|
| 4 |
|
| 5 |
+
use crate::audio::sound_id::SoundId;
|
| 6 |
|
| 7 |
/// Volume-to-dB attenuation table (64 entries of i16).
|
| 8 |
/// Copied from WA.exe .rdata at 0x6A6A60.
|
crates/openwa-core/src/audio/{sound.rs → known_sound_id.rs}
RENAMED
|
@@ -1,48 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
///
|
| 3 |
-
/// Layout:
|
| 4 |
-
/// - Bits 0-15: sound slot index (1-499 for SFX, higher for speech)
|
| 5 |
-
/// - Bit 16: loop flag (play in a loop)
|
| 6 |
-
/// - Bit 17: raw volume flag (skip master volume scaling)
|
| 7 |
-
///
|
| 8 |
-
/// Used throughout the sound system: queue insertion, DSSound playback,
|
| 9 |
-
/// and streaming dispatch all operate on this same packed format.
|
| 10 |
-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
| 11 |
-
#[repr(transparent)]
|
| 12 |
-
pub struct SoundId(pub u32);
|
| 13 |
-
|
| 14 |
-
impl SoundId {
|
| 15 |
-
/// Sound slot index (low 16 bits). 0 = invalid/empty.
|
| 16 |
-
#[inline]
|
| 17 |
-
pub fn index(self) -> usize {
|
| 18 |
-
(self.0 & 0xFFFF) as usize
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
/// Whether the sound should loop continuously (bit 16).
|
| 22 |
-
#[inline]
|
| 23 |
-
pub fn is_looping(self) -> bool {
|
| 24 |
-
self.0 & 0x10000 != 0
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
/// Whether volume should be used directly, skipping master volume scaling (bit 17).
|
| 28 |
-
#[inline]
|
| 29 |
-
pub fn is_raw_volume(self) -> bool {
|
| 30 |
-
self.0 & 0x20000 != 0
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
/// Return a copy with the loop flag cleared.
|
| 34 |
-
#[inline]
|
| 35 |
-
pub fn without_loop(self) -> Self {
|
| 36 |
-
Self(self.0 & 0xFFFE_FFFF)
|
| 37 |
-
}
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
impl From<KnownSoundId> for SoundId {
|
| 41 |
-
#[inline]
|
| 42 |
-
fn from(known: KnownSoundId) -> Self {
|
| 43 |
-
Self(known as u32)
|
| 44 |
-
}
|
| 45 |
-
}
|
| 46 |
|
| 47 |
/// Known sound effect IDs. Range 1-126, contiguous.
|
| 48 |
///
|
|
@@ -194,3 +150,10 @@ impl TryFrom<u32> for KnownSoundId {
|
|
| 194 |
}
|
| 195 |
}
|
| 196 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use super::SoundId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
/// Known sound effect IDs. Range 1-126, contiguous.
|
| 4 |
///
|
|
|
|
| 150 |
}
|
| 151 |
}
|
| 152 |
}
|
| 153 |
+
|
| 154 |
+
impl From<KnownSoundId> for SoundId {
|
| 155 |
+
#[inline]
|
| 156 |
+
fn from(known: KnownSoundId) -> Self {
|
| 157 |
+
Self(known as u32)
|
| 158 |
+
}
|
| 159 |
+
}
|
crates/openwa-core/src/audio/mod.rs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
pub mod active_sound;
|
| 2 |
pub mod dssound;
|
|
|
|
| 3 |
pub mod music;
|
| 4 |
-
pub mod
|
| 5 |
pub mod sound_ops;
|
| 6 |
pub mod speech;
|
| 7 |
pub mod streaming;
|
|
@@ -15,7 +16,8 @@ pub use dssound::{
|
|
| 15 |
sub_destructor as dssound_sub_destructor, update_channels,
|
| 16 |
};
|
| 17 |
pub use dssound::{ChannelDescriptor, DSSound, DSSoundVtable};
|
|
|
|
| 18 |
pub use music::Music;
|
| 19 |
-
pub use
|
| 20 |
pub use speech::{SpeechLineId, SpeechLineTableEntry, SpeechSlotTable, SPEECH_LINE_TABLE_COUNT};
|
| 21 |
pub use streaming::StreamingAudio;
|
|
|
|
| 1 |
pub mod active_sound;
|
| 2 |
pub mod dssound;
|
| 3 |
+
pub mod known_sound_id;
|
| 4 |
pub mod music;
|
| 5 |
+
pub mod sound_id;
|
| 6 |
pub mod sound_ops;
|
| 7 |
pub mod speech;
|
| 8 |
pub mod streaming;
|
|
|
|
| 16 |
sub_destructor as dssound_sub_destructor, update_channels,
|
| 17 |
};
|
| 18 |
pub use dssound::{ChannelDescriptor, DSSound, DSSoundVtable};
|
| 19 |
+
pub use known_sound_id::KnownSoundId;
|
| 20 |
pub use music::Music;
|
| 21 |
+
pub use sound_id::SoundId;
|
| 22 |
pub use speech::{SpeechLineId, SpeechLineTableEntry, SpeechSlotTable, SPEECH_LINE_TABLE_COUNT};
|
| 23 |
pub use streaming::StreamingAudio;
|
crates/openwa-core/src/audio/sound_id.rs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/// Packed sound ID + playback flags, passed as a single u32.
|
| 2 |
+
///
|
| 3 |
+
/// Layout:
|
| 4 |
+
/// - Bits 0-15: sound slot index (1-499 for SFX, higher for speech)
|
| 5 |
+
/// - Bit 16: loop flag (play in a loop)
|
| 6 |
+
/// - Bit 17: raw volume flag (skip master volume scaling)
|
| 7 |
+
///
|
| 8 |
+
/// Used throughout the sound system: queue insertion, DSSound playback,
|
| 9 |
+
/// and streaming dispatch all operate on this same packed format.
|
| 10 |
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
| 11 |
+
#[repr(transparent)]
|
| 12 |
+
pub struct SoundId(pub u32);
|
| 13 |
+
|
| 14 |
+
impl SoundId {
|
| 15 |
+
/// Sound slot index (low 16 bits). 0 = invalid/empty.
|
| 16 |
+
#[inline]
|
| 17 |
+
pub fn index(self) -> usize {
|
| 18 |
+
(self.0 & 0xFFFF) as usize
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
/// Whether the sound should loop continuously (bit 16).
|
| 22 |
+
#[inline]
|
| 23 |
+
pub fn is_looping(self) -> bool {
|
| 24 |
+
self.0 & 0x10000 != 0
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
/// Whether volume should be used directly, skipping master volume scaling (bit 17).
|
| 28 |
+
#[inline]
|
| 29 |
+
pub fn is_raw_volume(self) -> bool {
|
| 30 |
+
self.0 & 0x20000 != 0
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/// Return a copy with the loop flag cleared.
|
| 34 |
+
#[inline]
|
| 35 |
+
pub fn without_loop(self) -> Self {
|
| 36 |
+
Self(self.0 & 0xFFFE_FFFF)
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
crates/openwa-core/src/audio/sound_ops.rs
CHANGED
|
@@ -141,7 +141,7 @@ pub unsafe fn play_worm_sound(worm: *mut CTaskWorm, sound_id: SoundId, volume: F
|
|
| 141 |
}
|
| 142 |
// Start new streaming sound — fully ported, no WA bridge needed
|
| 143 |
// FUN_005150D0 hardcodes flags=3
|
| 144 |
-
let new_handle = load_and_play_streaming(worm, sound_id, 3, volume);
|
| 145 |
(*worm).sound_handle = new_handle;
|
| 146 |
}
|
| 147 |
|
|
@@ -177,7 +177,7 @@ pub unsafe fn play_worm_sound_2(
|
|
| 177 |
let new_handle = if worm_y < -0x270F_FFFF && sound_id.0 == 0x36 {
|
| 178 |
// Special teleport case: play at weapon target position
|
| 179 |
load_and_play_streaming_positional(
|
| 180 |
-
worm,
|
| 181 |
sound_id,
|
| 182 |
flags,
|
| 183 |
volume,
|
|
@@ -186,7 +186,7 @@ pub unsafe fn play_worm_sound_2(
|
|
| 186 |
)
|
| 187 |
} else {
|
| 188 |
// Normal case: play streaming sound — fully ported
|
| 189 |
-
load_and_play_streaming(worm, sound_id, flags, volume)
|
| 190 |
};
|
| 191 |
|
| 192 |
(*worm).sound_handle_2 = new_handle;
|
|
@@ -464,14 +464,15 @@ unsafe fn dispatch_local_sound(
|
|
| 464 |
/// then dispatches as a local sound. Returns handle | 0x40000000 on success,
|
| 465 |
/// 0 on failure, -1 (0xFFFFFFFF) if suppressed.
|
| 466 |
///
|
| 467 |
-
///
|
|
|
|
| 468 |
pub unsafe fn load_and_play_streaming(
|
| 469 |
-
|
| 470 |
sound_id: SoundId,
|
| 471 |
flags: u32,
|
| 472 |
volume: Fixed,
|
| 473 |
) -> i32 {
|
| 474 |
-
let ddgame = CTask::ddgame_raw(
|
| 475 |
let gi = &*(*ddgame).game_info;
|
| 476 |
|
| 477 |
// Suppression checks (matching 0x546C20 exactly)
|
|
@@ -488,7 +489,6 @@ pub unsafe fn load_and_play_streaming(
|
|
| 488 |
}
|
| 489 |
|
| 490 |
// Get emitter position via sound_emitter vtable[0] (GetPosition)
|
| 491 |
-
let task = worm as *mut CGameTask;
|
| 492 |
let emitter = &(*task).sound_emitter;
|
| 493 |
let mut pos_x: u32 = 0;
|
| 494 |
let mut pos_y: u32 = 0;
|
|
@@ -518,16 +518,17 @@ pub unsafe fn load_and_play_streaming(
|
|
| 518 |
/// LoadAndPlayStreamingPositional (0x546BB0).
|
| 519 |
///
|
| 520 |
/// Same as `load_and_play_streaming` but uses explicit (x, y) coordinates
|
| 521 |
-
/// instead of reading from the emitter.
|
|
|
|
| 522 |
pub unsafe fn load_and_play_streaming_positional(
|
| 523 |
-
|
| 524 |
sound_id: SoundId,
|
| 525 |
flags: u32,
|
| 526 |
volume: Fixed,
|
| 527 |
x: Fixed,
|
| 528 |
y: Fixed,
|
| 529 |
) -> i32 {
|
| 530 |
-
let ddgame = CTask::ddgame_raw(
|
| 531 |
let gi = &*(*ddgame).game_info;
|
| 532 |
|
| 533 |
// Suppression checks (matching 0x546BB0 exactly)
|
|
|
|
| 141 |
}
|
| 142 |
// Start new streaming sound — fully ported, no WA bridge needed
|
| 143 |
// FUN_005150D0 hardcodes flags=3
|
| 144 |
+
let new_handle = load_and_play_streaming(worm as *mut CGameTask, sound_id, 3, volume);
|
| 145 |
(*worm).sound_handle = new_handle;
|
| 146 |
}
|
| 147 |
|
|
|
|
| 177 |
let new_handle = if worm_y < -0x270F_FFFF && sound_id.0 == 0x36 {
|
| 178 |
// Special teleport case: play at weapon target position
|
| 179 |
load_and_play_streaming_positional(
|
| 180 |
+
worm as *mut CTask,
|
| 181 |
sound_id,
|
| 182 |
flags,
|
| 183 |
volume,
|
|
|
|
| 186 |
)
|
| 187 |
} else {
|
| 188 |
// Normal case: play streaming sound — fully ported
|
| 189 |
+
load_and_play_streaming(worm as *mut CGameTask, sound_id, flags, volume)
|
| 190 |
};
|
| 191 |
|
| 192 |
(*worm).sound_handle_2 = new_handle;
|
|
|
|
| 464 |
/// then dispatches as a local sound. Returns handle | 0x40000000 on success,
|
| 465 |
/// 0 on failure, -1 (0xFFFFFFFF) if suppressed.
|
| 466 |
///
|
| 467 |
+
/// Takes any CGameTask (needs sound_emitter for position). Multiple WA callers
|
| 468 |
+
/// pass various task types (CTaskWorm, CTaskMissile, etc.).
|
| 469 |
pub unsafe fn load_and_play_streaming(
|
| 470 |
+
task: *mut CGameTask,
|
| 471 |
sound_id: SoundId,
|
| 472 |
flags: u32,
|
| 473 |
volume: Fixed,
|
| 474 |
) -> i32 {
|
| 475 |
+
let ddgame = CTask::ddgame_raw(task as *const CTask);
|
| 476 |
let gi = &*(*ddgame).game_info;
|
| 477 |
|
| 478 |
// Suppression checks (matching 0x546C20 exactly)
|
|
|
|
| 489 |
}
|
| 490 |
|
| 491 |
// Get emitter position via sound_emitter vtable[0] (GetPosition)
|
|
|
|
| 492 |
let emitter = &(*task).sound_emitter;
|
| 493 |
let mut pos_x: u32 = 0;
|
| 494 |
let mut pos_y: u32 = 0;
|
|
|
|
| 518 |
/// LoadAndPlayStreamingPositional (0x546BB0).
|
| 519 |
///
|
| 520 |
/// Same as `load_and_play_streaming` but uses explicit (x, y) coordinates
|
| 521 |
+
/// instead of reading from the emitter. Only needs CTask (reads ddgame at +0x2C).
|
| 522 |
+
/// Only caller in WA is PlayWormSound2 (0x515020).
|
| 523 |
pub unsafe fn load_and_play_streaming_positional(
|
| 524 |
+
task: *mut CTask,
|
| 525 |
sound_id: SoundId,
|
| 526 |
flags: u32,
|
| 527 |
volume: Fixed,
|
| 528 |
x: Fixed,
|
| 529 |
y: Fixed,
|
| 530 |
) -> i32 {
|
| 531 |
+
let ddgame = CTask::ddgame_raw(task as *const CTask);
|
| 532 |
let gi = &*(*ddgame).game_info;
|
| 533 |
|
| 534 |
// Suppression checks (matching 0x546BB0 exactly)
|
crates/openwa-wormkit/src/replacements/sound.rs
CHANGED
|
@@ -174,6 +174,12 @@ pub fn install() -> Result<(), String> {
|
|
| 174 |
va::WORM_PLAY_SOUND_2,
|
| 175 |
trampoline_worm_play_sound_2 as *const (),
|
| 176 |
)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
|
| 179 |
Ok(())
|
|
|
|
| 174 |
va::WORM_PLAY_SOUND_2,
|
| 175 |
trampoline_worm_play_sound_2 as *const (),
|
| 176 |
)?;
|
| 177 |
+
|
| 178 |
+
// Trap: only caller (PlayWormSound2) is fully ported Rust
|
| 179 |
+
hook::install_trap!(
|
| 180 |
+
"LoadAndPlayStreamingPositional",
|
| 181 |
+
va::LOAD_AND_PLAY_STREAMING_POSITIONAL
|
| 182 |
+
);
|
| 183 |
}
|
| 184 |
|
| 185 |
Ok(())
|