paavohtl Claude Opus 4.6 (1M context) commited on
Commit
12635a0
·
1 Parent(s): 36caf33

refactor: use Fixed types and meaningful names in DSSound

Browse files

- Add SoundSlot newtype for the packed slot+flags u32 parameter
(index(), is_looping(), is_raw_volume(), without_loop() methods)
- Rename _field_00 -> slot: SoundSlot (stores the SoundSlot used to play)
- Fix missing desc.slot = slot store in core_play_sound (was stale data)
- Rename flags_a -> pool_idx, flags_b -> priority
- Use Fixed for volume/frequency/pan params throughout DSSound vtable
- Update all stale comments referencing old field names

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

crates/openwa-core/src/audio/dssound.rs CHANGED
@@ -2,6 +2,42 @@ use crate::fixed::Fixed;
2
 
3
  use windows::Win32::Media::Audio::DirectSound::IDirectSoundBuffer;
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  /// Volume-to-dB attenuation table (64 entries of i16).
6
  /// Copied from WA.exe .rdata at 0x6A6A60.
7
  /// Index 0 = silence (-10000 dB), index 63 = near-unity (-22 dB).
@@ -33,8 +69,10 @@ type Ptr32 = u32;
33
  #[repr(C)]
34
  #[derive(Clone, Copy)]
35
  pub struct ChannelDescriptor {
36
- /// +0x00: Flags/state field (not set by constructor init helper)
37
- pub _field_00: u32,
 
 
38
  /// +0x04: Buffer pool index (0..63), or -1 if not pooled.
39
  /// Set by play_sound_pooled; checked by update_channels/stop_channel/allocate_channel.
40
  pub pool_idx: i32,
@@ -88,9 +126,23 @@ pub struct DSSoundVtable {
88
  /// set_volume_params — sets status_1/2, adjusts channel volumes
89
  pub set_volume_params: fn(this: *mut DSSound, status: u32, value: i32),
90
  /// play_sound — wrapper around core play. RET 0x14 = 5 stack params.
91
- pub play_sound: fn(this: *mut DSSound, slot_and_flags: u32, priority: i32, volume: Fixed, pan: Fixed, freq: Fixed) -> bool,
 
 
 
 
 
 
 
92
  /// play_sound_pooled — allocates from buffer pool, plays. RET 0x14.
93
- pub play_sound_pooled: fn(this: *mut DSSound, slot_and_flags: u32, priority: i32, volume: Fixed, pan: Fixed, freq: Fixed) -> i32,
 
 
 
 
 
 
 
94
  /// set_pan — sets pan on channel (dB lookup)
95
  pub set_pan: fn(this: *mut DSSound, channel: u32, pan: Fixed) -> u32,
96
  /// **stub** — returns 0
@@ -277,8 +329,8 @@ pub unsafe extern "thiscall" fn set_master_volume(this: *mut DSSound, new_volume
277
  if desc.ds_buffer == 0 {
278
  continue;
279
  }
280
- // Skip if flag 0x20000 is set in _field_00.
281
- if desc._field_00 & 0x20000 != 0 {
282
  continue;
283
  }
284
  // Compute combined volume: master * per-channel, fixed-point multiply.
@@ -383,7 +435,7 @@ pub unsafe extern "thiscall" fn set_channel_volume(
383
  let vol = volume.0.max(0).min(0x10000);
384
 
385
  // Compute dB: if flag 0x20000 set, use volume directly (no master scaling).
386
- let db = if snd.channel_descs[di]._field_00 & 0x20000 != 0 {
387
  volume_to_db(vol)
388
  } else {
389
  let combined = ((snd.volume.0 as i64 * vol as i64) >> 16) as i32;
@@ -456,7 +508,7 @@ pub unsafe extern "thiscall" fn destructor(this: *mut DSSound, flags: u8) -> *mu
456
  /// Returns channel descriptor index on success, -1 on failure.
457
  unsafe fn core_play_sound(
458
  snd: &mut DSSound,
459
- slot_and_flags: u32,
460
  priority: i32,
461
  frequency: Fixed,
462
  volume: Fixed,
@@ -469,11 +521,8 @@ unsafe fn core_play_sound(
469
  return -1;
470
  }
471
 
472
- // Extract loop flag and slot index.
473
- let loop_flag = slot_and_flags & 0x10000 != 0;
474
- let slot_idx = (slot_and_flags & 0xFFFF) as usize;
475
-
476
  // Validate slot index and check buffer loaded.
 
477
  if slot_idx == 0 || slot_idx > 499 || snd.channel_slots[slot_idx] == 0 {
478
  return -1;
479
  }
@@ -506,7 +555,7 @@ unsafe fn core_play_sound(
506
  let vol_scaled = (vol * 3 + (((vol * 3) >> 31) & 3)) >> 2;
507
 
508
  // Compute dB for volume.
509
- let vol_db = if slot_and_flags & 0x20000 != 0 {
510
  volume_to_db(vol_scaled)
511
  } else {
512
  let combined = ((snd.volume.0 as i64 * vol_scaled as i64) >> 16) as i32;
@@ -519,6 +568,7 @@ unsafe fn core_play_sound(
519
 
520
  // Store descriptor state.
521
  let desc = &mut snd.channel_descs[di];
 
522
  desc.pool_idx = -1;
523
  desc.priority = priority;
524
  desc.channel_freq = Fixed(channel_freq);
@@ -549,7 +599,7 @@ unsafe fn core_play_sound(
549
  }
550
  let _ = buf.SetPan(pan_db);
551
 
552
- let _ = buf.Play(0, 0, if loop_flag { 1 } else { 0 });
553
  }
554
 
555
  desc_idx
@@ -599,24 +649,17 @@ unsafe fn allocate_channel(snd: &mut DSSound, priority: i32) -> i32 {
599
  }
600
 
601
  /// Slot 3: play_sound — pure thiscall with 5 stack params, RET 0x14.
602
- /// Params: slot_and_flags, priority, frequency, volume, pan.
603
  pub unsafe extern "thiscall" fn play_sound(
604
  this: *mut DSSound,
605
- slot_and_flags: u32,
606
  priority: i32,
607
  frequency: Fixed,
608
  volume: Fixed,
609
  pan: Fixed,
610
  ) -> bool {
611
  let snd = &mut *this;
612
- let result = core_play_sound(
613
- snd,
614
- slot_and_flags & 0xFFFEFFFF,
615
- priority,
616
- frequency,
617
- volume,
618
- pan,
619
- );
620
  result >= 0
621
  }
622
 
@@ -624,7 +667,7 @@ pub unsafe extern "thiscall" fn play_sound(
624
  /// Returns pool_id (1-based) on success, 0 on failure.
625
  pub unsafe extern "thiscall" fn play_sound_pooled(
626
  this: *mut DSSound,
627
- slot_and_flags: u32,
628
  priority: i32,
629
  frequency: Fixed,
630
  volume: Fixed,
@@ -637,7 +680,7 @@ pub unsafe extern "thiscall" fn play_sound_pooled(
637
  return 0;
638
  }
639
 
640
- let desc_idx = core_play_sound(snd, slot_and_flags, priority, frequency, volume, pan);
641
  if desc_idx < 0 {
642
  return 0;
643
  }
 
2
 
3
  use windows::Win32::Media::Audio::DirectSound::IDirectSoundBuffer;
4
 
5
+ /// Packed sound slot index + playback flags, passed as a single u32.
6
+ ///
7
+ /// Layout:
8
+ /// - Bits 0-15: sound slot index (1-499)
9
+ /// - Bit 16: loop flag (play in a loop)
10
+ /// - Bit 17: raw volume flag (skip master volume scaling)
11
+ #[derive(Clone, Copy)]
12
+ #[repr(transparent)]
13
+ pub struct SoundSlot(pub u32);
14
+
15
+ impl SoundSlot {
16
+ /// Sound slot index (0-499). 0 = invalid/empty.
17
+ #[inline]
18
+ pub fn index(self) -> usize {
19
+ (self.0 & 0xFFFF) as usize
20
+ }
21
+
22
+ /// Whether the sound should loop continuously.
23
+ #[inline]
24
+ pub fn is_looping(self) -> bool {
25
+ self.0 & 0x10000 != 0
26
+ }
27
+
28
+ /// Whether volume should be used directly (skip master volume scaling).
29
+ #[inline]
30
+ pub fn is_raw_volume(self) -> bool {
31
+ self.0 & 0x20000 != 0
32
+ }
33
+
34
+ /// Return a copy with the loop flag cleared.
35
+ #[inline]
36
+ pub fn without_loop(self) -> Self {
37
+ Self(self.0 & 0xFFFE_FFFF)
38
+ }
39
+ }
40
+
41
  /// Volume-to-dB attenuation table (64 entries of i16).
42
  /// Copied from WA.exe .rdata at 0x6A6A60.
43
  /// Index 0 = silence (-10000 dB), index 63 = near-unity (-22 dB).
 
69
  #[repr(C)]
70
  #[derive(Clone, Copy)]
71
  pub struct ChannelDescriptor {
72
+ /// +0x00: The SoundSlot that was used to start this channel.
73
+ /// Stored by core_play_sound; checked by set_master_volume/set_channel_volume
74
+ /// for the raw-volume flag (bit 17).
75
+ pub slot: SoundSlot,
76
  /// +0x04: Buffer pool index (0..63), or -1 if not pooled.
77
  /// Set by play_sound_pooled; checked by update_channels/stop_channel/allocate_channel.
78
  pub pool_idx: i32,
 
126
  /// set_volume_params — sets status_1/2, adjusts channel volumes
127
  pub set_volume_params: fn(this: *mut DSSound, status: u32, value: i32),
128
  /// play_sound — wrapper around core play. RET 0x14 = 5 stack params.
129
+ pub play_sound: fn(
130
+ this: *mut DSSound,
131
+ slot: SoundSlot,
132
+ priority: i32,
133
+ volume: Fixed,
134
+ pan: Fixed,
135
+ freq: Fixed,
136
+ ) -> bool,
137
  /// play_sound_pooled — allocates from buffer pool, plays. RET 0x14.
138
+ pub play_sound_pooled: fn(
139
+ this: *mut DSSound,
140
+ slot: SoundSlot,
141
+ priority: i32,
142
+ volume: Fixed,
143
+ pan: Fixed,
144
+ freq: Fixed,
145
+ ) -> i32,
146
  /// set_pan — sets pan on channel (dB lookup)
147
  pub set_pan: fn(this: *mut DSSound, channel: u32, pan: Fixed) -> u32,
148
  /// **stub** — returns 0
 
329
  if desc.ds_buffer == 0 {
330
  continue;
331
  }
332
+ // Skip channels using raw volume (not scaled by master).
333
+ if desc.slot.is_raw_volume() {
334
  continue;
335
  }
336
  // Compute combined volume: master * per-channel, fixed-point multiply.
 
435
  let vol = volume.0.max(0).min(0x10000);
436
 
437
  // Compute dB: if flag 0x20000 set, use volume directly (no master scaling).
438
+ let db = if snd.channel_descs[di].slot.is_raw_volume() {
439
  volume_to_db(vol)
440
  } else {
441
  let combined = ((snd.volume.0 as i64 * vol as i64) >> 16) as i32;
 
508
  /// Returns channel descriptor index on success, -1 on failure.
509
  unsafe fn core_play_sound(
510
  snd: &mut DSSound,
511
+ slot: SoundSlot,
512
  priority: i32,
513
  frequency: Fixed,
514
  volume: Fixed,
 
521
  return -1;
522
  }
523
 
 
 
 
 
524
  // Validate slot index and check buffer loaded.
525
+ let slot_idx = slot.index();
526
  if slot_idx == 0 || slot_idx > 499 || snd.channel_slots[slot_idx] == 0 {
527
  return -1;
528
  }
 
555
  let vol_scaled = (vol * 3 + (((vol * 3) >> 31) & 3)) >> 2;
556
 
557
  // Compute dB for volume.
558
+ let vol_db = if slot.is_raw_volume() {
559
  volume_to_db(vol_scaled)
560
  } else {
561
  let combined = ((snd.volume.0 as i64 * vol_scaled as i64) >> 16) as i32;
 
568
 
569
  // Store descriptor state.
570
  let desc = &mut snd.channel_descs[di];
571
+ desc.slot = slot;
572
  desc.pool_idx = -1;
573
  desc.priority = priority;
574
  desc.channel_freq = Fixed(channel_freq);
 
599
  }
600
  let _ = buf.SetPan(pan_db);
601
 
602
+ let _ = buf.Play(0, 0, if slot.is_looping() { 1 } else { 0 });
603
  }
604
 
605
  desc_idx
 
649
  }
650
 
651
  /// Slot 3: play_sound — pure thiscall with 5 stack params, RET 0x14.
652
+ /// Params: slot (with flags), priority, frequency, volume, pan.
653
  pub unsafe extern "thiscall" fn play_sound(
654
  this: *mut DSSound,
655
+ slot: SoundSlot,
656
  priority: i32,
657
  frequency: Fixed,
658
  volume: Fixed,
659
  pan: Fixed,
660
  ) -> bool {
661
  let snd = &mut *this;
662
+ let result = core_play_sound(snd, slot.without_loop(), priority, frequency, volume, pan);
 
 
 
 
 
 
 
663
  result >= 0
664
  }
665
 
 
667
  /// Returns pool_id (1-based) on success, 0 on failure.
668
  pub unsafe extern "thiscall" fn play_sound_pooled(
669
  this: *mut DSSound,
670
+ slot: SoundSlot,
671
  priority: i32,
672
  frequency: Fixed,
673
  volume: Fixed,
 
680
  return 0;
681
  }
682
 
683
+ let desc_idx = core_play_sound(snd, slot, priority, frequency, volume, pan);
684
  if desc_idx < 0 {
685
  return 0;
686
  }
crates/openwa-core/src/audio/mod.rs CHANGED
@@ -13,7 +13,7 @@ pub use dssound::{
13
  set_master_volume, set_pan, set_volume_params, stop_channel,
14
  sub_destructor as dssound_sub_destructor, update_channels,
15
  };
16
- pub use dssound::{ChannelDescriptor, DSSound, DSSoundVtable};
17
  pub use music::Music;
18
  pub use sound::{KnownSoundId, SoundId};
19
  pub use speech::{SpeechLineId, SpeechLineTableEntry, SpeechSlotTable, SPEECH_LINE_TABLE_COUNT};
 
13
  set_master_volume, set_pan, set_volume_params, stop_channel,
14
  sub_destructor as dssound_sub_destructor, update_channels,
15
  };
16
+ pub use dssound::{ChannelDescriptor, DSSound, DSSoundVtable, SoundSlot};
17
  pub use music::Music;
18
  pub use sound::{KnownSoundId, SoundId};
19
  pub use speech::{SpeechLineId, SpeechLineTableEntry, SpeechSlotTable, SPEECH_LINE_TABLE_COUNT};
crates/openwa-wormkit/src/replacements/sound.rs CHANGED
@@ -10,7 +10,7 @@
10
  use std::sync::atomic::Ordering;
11
 
12
  use openwa_core::address::va;
13
- use openwa_core::audio::{play_sound, play_sound_pooled, KnownSoundId, SoundId};
14
  use openwa_core::engine::{DDGame, DDGameWrapper, SoundQueueEntry};
15
  use openwa_core::fixed::Fixed;
16
  use openwa_core::task::worm::CTaskWorm;
@@ -296,12 +296,7 @@ unsafe extern "cdecl" fn play_worm_sound_2_cdecl(
296
  volume: u32,
297
  flags: u32,
298
  ) {
299
- play_worm_sound_2(
300
- worm,
301
- SoundId(sound_id),
302
- Fixed(volume as i32),
303
- flags,
304
- );
305
  }
306
 
307
  /// Bridge to FUN_00546bb0 — load and play a positional streaming sound.
@@ -339,11 +334,11 @@ unsafe extern "C" fn call_load_and_play_streaming_positional_bridge(
339
  "mov ebx, [esp+36]", // addr
340
  // Push 5 stdcall params right-to-left: y, x, volume, flags, sound_id.
341
  // Each push shifts ESP by 4, so the next original param lands at ESP+32.
342
- "push [esp+32]", // y
343
- "push [esp+32]", // x (was +28, shifted +4)
344
- "push [esp+32]", // volume (was +24, shifted +8)
345
- "push [esp+32]", // flags (was +20, shifted +12)
346
- "push [esp+32]", // sound_id (was +16, shifted +16)
347
  "call ebx",
348
  "pop ebx",
349
  "pop esi",
@@ -435,7 +430,7 @@ unsafe extern "fastcall" fn hook_dispatch_global_sound(
435
 
436
  play_sound(
437
  dssound,
438
- sound_id,
439
  flags as i32,
440
  Fixed(volume as i32),
441
  Fixed(pitch as i32),
@@ -448,21 +443,21 @@ unsafe extern "fastcall" fn hook_dispatch_global_sound(
448
  unsafe extern "fastcall" fn hook_play_sound_pooled_direct(
449
  _ecx: u32,
450
  task: *const CTask,
451
- param1: u32,
452
- param2: u32,
453
- param3: u32,
454
- ) -> u32 {
455
  let g = &*(*task).ddgame;
456
  let gi = &*g.game_info;
457
 
458
  // Suppression check
459
  if gi.sound_mute != 0 || g.frame_counter < gi.sound_start_frame {
460
- return 0xFFFF_FFFF;
461
  }
462
 
463
  // Fast-forward check (unique to this function)
464
  if g.fast_forward_active != 0 {
465
- return 0xFFFF_FFFF;
466
  }
467
 
468
  let dssound = g.sound;
@@ -470,7 +465,7 @@ unsafe extern "fastcall" fn hook_play_sound_pooled_direct(
470
  return 0;
471
  }
472
 
473
- play_sound_pooled(dssound, param1, param2 as i32, Fixed(0x10000), Fixed(param3 as i32), Fixed(0)) as u32
474
  }
475
 
476
  // ============================================================
 
10
  use std::sync::atomic::Ordering;
11
 
12
  use openwa_core::address::va;
13
+ use openwa_core::audio::{play_sound, play_sound_pooled, KnownSoundId, SoundId, SoundSlot};
14
  use openwa_core::engine::{DDGame, DDGameWrapper, SoundQueueEntry};
15
  use openwa_core::fixed::Fixed;
16
  use openwa_core::task::worm::CTaskWorm;
 
296
  volume: u32,
297
  flags: u32,
298
  ) {
299
+ play_worm_sound_2(worm, SoundId(sound_id), Fixed(volume as i32), flags);
 
 
 
 
 
300
  }
301
 
302
  /// Bridge to FUN_00546bb0 — load and play a positional streaming sound.
 
334
  "mov ebx, [esp+36]", // addr
335
  // Push 5 stdcall params right-to-left: y, x, volume, flags, sound_id.
336
  // Each push shifts ESP by 4, so the next original param lands at ESP+32.
337
+ "push [esp+32]", // y
338
+ "push [esp+32]", // x (was +28, shifted +4)
339
+ "push [esp+32]", // volume (was +24, shifted +8)
340
+ "push [esp+32]", // flags (was +20, shifted +12)
341
+ "push [esp+32]", // sound_id (was +16, shifted +16)
342
  "call ebx",
343
  "pop ebx",
344
  "pop esi",
 
430
 
431
  play_sound(
432
  dssound,
433
+ SoundSlot(sound_id),
434
  flags as i32,
435
  Fixed(volume as i32),
436
  Fixed(pitch as i32),
 
443
  unsafe extern "fastcall" fn hook_play_sound_pooled_direct(
444
  _ecx: u32,
445
  task: *const CTask,
446
+ param1: SoundSlot,
447
+ param2: i32,
448
+ param3: Fixed,
449
+ ) -> i32 {
450
  let g = &*(*task).ddgame;
451
  let gi = &*g.game_info;
452
 
453
  // Suppression check
454
  if gi.sound_mute != 0 || g.frame_counter < gi.sound_start_frame {
455
+ return -1;
456
  }
457
 
458
  // Fast-forward check (unique to this function)
459
  if g.fast_forward_active != 0 {
460
+ return -1;
461
  }
462
 
463
  let dssound = g.sound;
 
465
  return 0;
466
  }
467
 
468
+ play_sound_pooled(dssound, param1, param2, Fixed::ONE, param3, Fixed::ZERO)
469
  }
470
 
471
  // ============================================================