feat: replace DSSound slots 7, 8 (set_master_volume, set_channel_volume)
Browse filesAdd VOLUME_DB_TABLE (64 i16 entries from WA .rdata 0x6A6A60) and
volume_to_db() helper for fixed-point β dB attenuation conversion.
Slot 7: iterates channel descs, computes combined master*channel volume,
calls IDirectSoundBuffer::SetVolume with dB from lookup table.
Slot 8: per-channel volume with flag-dependent master scaling.
Rename _field_10 β channel_volume (per-channel volume, confirmed from
slot 8 assembly storing the clamped value there).
19/24 DSSound vtable slots now Rust.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Files changed (3)
1 unmodified line23456721 unmodified lines2930313233343536160 unmodified lines19719819920020120220312 unmodified lines2162172182192202212224 unmodified lines22722822923023123264 unmodified lines2972982993003013023031 unmodified lineuse windows::Win32::Media::Audio::DirectSound::IDirectSoundBuffer;/// DSSound β DirectSound audio subsystem.////// Created by DSSound__Constructor (0x573D50).21 unmodified linespub flags_b: i32,/// +0x0C: Volume/state field (cleared to 0 on release)pub _field_0c: i32,/// +0x10: Volume/state field (cleared to 0 on release)pub _field_10: i32,/// +0x14: IDirectSoundBuffer* for the active buffer (0 = empty)./// At absolute offset this+0x28 for desc[0]. Use `buffer()` to get a typed ref.pub ds_buffer: Ptr32,160 unmodified lines// Take and drop to release COM ref.desc.take_buffer();desc._field_0c = 0;desc._field_10 = 0;desc.flags_b = -1;desc.flags_a = -1;}12 unmodified linesif (status & 1) == 0 && desc.flags_a < 0 {desc.take_buffer();desc._field_0c = 0;desc._field_10 = 0;desc.flags_b = -1;desc.flags_a = -1;count += 1;4 unmodified linescount}/// Slot 14: sub_destructor β sets secondary vtable (0x66AF58), optionally frees./// This is a base-class destructor called by the primary destructor (slot 0).pub unsafe extern "thiscall" fn sub_destructor(64 unmodified lines// Reset descriptor to free state.desc._field_0c = 0;desc._field_10 = 0;desc.flags_b = -1;desc.flags_a = -1;1 unmodified line234567891011121314151617181920212221 unmodified lines4445464748495051160 unmodified lines21221321421521621721812 unmodified lines2312322332342352362374 unmodified lines24224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832964 unmodified lines3943953963973983994001 unmodified lineuse windows::Win32::Media::Audio::DirectSound::IDirectSoundBuffer;/// Volume-to-dB attenuation table (64 entries of i16)./// Copied from WA.exe .rdata at 0x6A6A60./// Index 0 = silence (-10000 dB), index 63 = near-unity (-22 dB)./// Used by set_master_volume, set_channel_volume, and play_sound.const VOLUME_DB_TABLE: [i16; 64] = [-10000, -6000, -5000, -4415, -4000, -3678, -3415, -3000,-2744, -2522, -2326, -2150, -1991, -1845, -1712, -1589,-1475, -1368, -1268, -1176, -1088, -1006, -928, -855,-786, -720, -658, -599, -543, -490, -439, -390,-344, -299, -256, -216, -177, -139, -104, -70,-38, -7, 24, 54, 83, 111, 138, 163,188, 211, 234, 256, 277, 296, 315, 333,350, 367, 383, 398, 413, 427, 441, 454,]; // NOT: these might be negative centibels, not actual dB. Exact interpretation TBD./// DSSound β DirectSound audio subsystem.////// Created by DSSound__Constructor (0x573D50).21 unmodified linespub flags_b: i32,/// +0x0C: Volume/state field (cleared to 0 on release)pub _field_0c: i32,/// +0x10: Per-channel volume (Fixed 16.16, 0..0x10000). Set by set_channel_volume.pub channel_volume: i32,/// +0x14: IDirectSoundBuffer* for the active buffer (0 = empty)./// At absolute offset this+0x28 for desc[0]. Use `buffer()` to get a typed ref.pub ds_buffer: Ptr32,160 unmodified lines// Take and drop to release COM ref.desc.take_buffer();desc._field_0c = 0;desc.channel_volume = 0;desc.flags_b = -1;desc.flags_a = -1;}12 unmodified linesif (status & 1) == 0 && desc.flags_a < 0 {desc.take_buffer();desc._field_0c = 0;desc.channel_volume = 0;desc.flags_b = -1;desc.flags_a = -1;count += 1;4 unmodified linescount}/// Convert a fixed-point volume (0..0x10000) to a dB attenuation value/// using the volume lookup table. Matches the original's arithmetic:/// `(volume >> 16) << 6 >> 16` β index into VOLUME_DB_TABLE.fn volume_to_db(volume: i32) -> i32 {let idx = ((volume as u32) >> 10) as usize; // (vol >> 16) << 6 = vol >> 10let clamped = idx.min(63);VOLUME_DB_TABLE[clamped] as i32}/// Slot 7: set_master_volume β sets the master volume and adjusts all active channels./// `new_volume` is Fixed 16.16 (0 = silent, 0x10000 = max).pub unsafe extern "thiscall" fn set_master_volume(this: *mut DSSound, new_volume: i32,) -> u32 {let snd = &mut *this;// Clamp to [0, 0x10000].let vol = new_volume.max(0).min(0x10000);// No change β early return.if snd.volume.0 == vol {return 1;}snd.volume = Fixed(vol);// Update all active channels.for desc in &snd.channel_descs {if desc.ds_buffer == 0 {continue;}// Skip if flag 0x20000 is set in _field_00.if desc._field_00 & 0x20000 != 0 {continue;}// Compute combined volume: master * per-channel, fixed-point multiply.let combined = ((vol as i64 * desc.channel_volume as i64) >> 16) as i32;let db = volume_to_db(combined);if let Some(buf) = desc.buffer() {let _ = buf.SetVolume(db);}}1}/// Slot 8: set_channel_volume β sets volume on a specific channel./// `pool_id` is 1-based (1..64). `volume` is Fixed 16.16 (0..0x10000).pub unsafe extern "thiscall" fn set_channel_volume(this: *mut DSSound, pool_id: i32, volume: i32,) -> u32 {let idx = (pool_id - 1) as usize;if idx >= 64 {return 0;}let snd = &mut *this;let desc_idx = snd.buffer_pool_shadow[idx];if desc_idx < 0 || desc_idx as usize >= 8 {return 0;}let di = desc_idx as usize;// Clamp volume to [0, 0x10000].let vol = volume.max(0).min(0x10000);// Compute dB: if flag 0x20000 set, use volume directly (no master scaling).let db = if snd.channel_descs[di]._field_00 & 0x20000 != 0 {volume_to_db(vol)} else {let combined = ((snd.volume.0 as i64 * vol as i64) >> 16) as i32;volume_to_db(combined)};if let Some(buf) = snd.channel_descs[di].buffer() {let _ = buf.SetVolume(db);}// Store per-channel volume.snd.channel_descs[di].channel_volume = vol;1}/// Slot 14: sub_destructor β sets secondary vtable (0x66AF58), optionally frees./// This is a base-class destructor called by the primary destructor (slot 0).pub unsafe extern "thiscall" fn sub_destructor(64 unmodified lines// Reset descriptor to free state.desc._field_0c = 0;desc.channel_volume = 0;desc.flags_b = -1;desc.flags_a = -1;
9 unmodified lines1011121314159 unmodified linespub use dssound::{update_channels, release_finished,is_slot_loaded, is_channel_finished, stop_channel,sub_destructor as dssound_sub_destructor, load_wav,noop as dssound_noop, returns_0 as dssound_returns_0, returns_1 as dssound_returns_1,};9 unmodified lines101112131415169 unmodified linespub use dssound::{update_channels, release_finished,is_slot_loaded, is_channel_finished, stop_channel,set_master_volume, set_channel_volume,sub_destructor as dssound_sub_destructor, load_wav,noop as dssound_noop, returns_0 as dssound_returns_0, returns_1 as dssound_returns_1,};
168 unmodified lines1691701711721731741756 unmodified lines18218318418518618721 unmodified lines209210211212213214168 unmodified linesuse openwa_core::vtable::patch_vtable;use openwa_core::audio::{update_channels, release_finished,is_slot_loaded, is_channel_finished, stop_channel, dssound_sub_destructor,load_wav, dssound_noop, dssound_returns_0, dssound_returns_1,};6 unmodified lines// Slot 1: update_channels β release finished buffers each frame*vt.add(1) = update_channels as *const () as u32;// Slot 11: release_finished β like update_channels, returns count*vt.add(11) = release_finished as *const () as u32;21 unmodified lines// Trivial returns-1 (slot 23)*vt.add(23) = dssound_returns_1 as *const () as u32;let _ = log_line("[Sound] DSSound vtable: patched 17/24 slots with Rust");}).map_err(|e| e.to_string())}168 unmodified lines1691701711721731741751766 unmodified lines18318418518618718818919019119219319421 unmodified lines216217218219220221168 unmodified linesuse openwa_core::vtable::patch_vtable;use openwa_core::audio::{update_channels, release_finished,is_slot_loaded, is_channel_finished, stop_channel,set_master_volume, set_channel_volume, dssound_sub_destructor,load_wav, dssound_noop, dssound_returns_0, dssound_returns_1,};6 unmodified lines// Slot 1: update_channels β release finished buffers each frame*vt.add(1) = update_channels as *const () as u32;// Slot 7: set_master_volume β adjusts all active channels*vt.add(7) = set_master_volume as *const () as u32;// Slot 8: set_channel_volume β sets volume on specific channel*vt.add(8) = set_channel_volume as *const () as u32;// Slot 11: release_finished β like update_channels, returns count*vt.add(11) = release_finished as *const () as u32;21 unmodified lines// Trivial returns-1 (slot 23)*vt.add(23) = dssound_returns_1 as *const () as u32;let _ = log_line("[Sound] DSSound vtable: patched 19/24 slots with Rust");}).map_err(|e| e.to_string())}
crates/openwa-core/src/audio/dssound.rs+102-5
crates/openwa-core/src/audio/mod.rs+1
crates/openwa-wormkit/src/replacements/sound.rs+9-2