paavohtl Claude Opus 4.6 (1M context) commited on
Commit
f354a25
Β·
1 Parent(s): ff1ae87

fix: correct off-by-one in DSSound slot indexing

Browse files

WA uses slot indices directly as array indices (1-based, slot 0 unused).
Our is_slot_loaded and load_wav incorrectly subtracted 1, causing all
sounds to be stored and looked up at the wrong slot β€” resulting in
scrambled sound effects and voice lines.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

crates/openwa-core/src/audio/dssound.rs CHANGED
@@ -146,8 +146,9 @@ const _: () = assert!(core::mem::size_of::<DSSound>() == 0xBE0);
146
  // ── Rust vtable method implementations ────────────────────────────────────
147
 
148
  /// Slot 13: is_slot_loaded β€” returns whether sound at `slot_idx` has a buffer loaded.
 
149
  pub unsafe extern "thiscall" fn is_slot_loaded(this: *mut DSSound, slot_idx: i32) -> bool {
150
- (*this).channel_slots.get((slot_idx - 1) as usize).copied().unwrap_or(0) != 0
151
  }
152
 
153
  /// Trivial noop β€” returns void. Used for slots 15, 16.
@@ -181,13 +182,16 @@ pub unsafe extern "thiscall" fn load_wav(
181
  };
182
  use windows::Win32::Media::Audio::{WAVEFORMATEX, WAVE_FORMAT_PCM};
183
 
 
 
184
  // Validate: need DirectSound, valid slot, not already loaded.
 
185
  let snd = &mut *this;
186
  if snd.direct_sound == 0 {
187
  return 1;
188
  }
189
- let slot = (slot_idx - 1) as usize;
190
- if slot >= 499 || snd.channel_slots.get(slot).copied().unwrap_or(1) != 0 {
191
  return 1;
192
  }
193
 
@@ -208,6 +212,11 @@ pub unsafe extern "thiscall" fn load_wav(
208
  let avg_bytes_per_sec = spec.sample_rate * block_align as u32;
209
  let data_len = reader.duration() * block_align as u32;
210
 
 
 
 
 
 
211
  // Build WAVEFORMATEX for the secondary buffer.
212
  let wfx = WAVEFORMATEX {
213
  wFormatTag: WAVE_FORMAT_PCM as u16,
 
146
  // ── Rust vtable method implementations ────────────────────────────────────
147
 
148
  /// Slot 13: is_slot_loaded β€” returns whether sound at `slot_idx` has a buffer loaded.
149
+ /// Slot index is used directly as array index (1-based, slot 0 unused).
150
  pub unsafe extern "thiscall" fn is_slot_loaded(this: *mut DSSound, slot_idx: i32) -> bool {
151
+ (*this).channel_slots.get(slot_idx as usize).copied().unwrap_or(0) != 0
152
  }
153
 
154
  /// Trivial noop β€” returns void. Used for slots 15, 16.
 
182
  };
183
  use windows::Win32::Media::Audio::{WAVEFORMATEX, WAVE_FORMAT_PCM};
184
 
185
+ use crate::log::log_line;
186
+
187
  // Validate: need DirectSound, valid slot, not already loaded.
188
+ // Slot index is used directly as array index (1-based, slot 0 unused).
189
  let snd = &mut *this;
190
  if snd.direct_sound == 0 {
191
  return 1;
192
  }
193
+ let slot = slot_idx as usize;
194
+ if slot == 0 || slot > 499 || snd.channel_slots.get(slot).copied().unwrap_or(1) != 0 {
195
  return 1;
196
  }
197
 
 
212
  let avg_bytes_per_sec = spec.sample_rate * block_align as u32;
213
  let data_len = reader.duration() * block_align as u32;
214
 
215
+ let _ = log_line(&format!(
216
+ "[Sound] Loading WAV: '{}', {} Hz, {} channels, {} bits, {} bytes",
217
+ c_path, spec.sample_rate, spec.channels, spec.bits_per_sample, data_len
218
+ ));
219
+
220
  // Build WAVEFORMATEX for the secondary buffer.
221
  let wfx = WAVEFORMATEX {
222
  wFormatTag: WAVE_FORMAT_PCM as u16,