Skip to content

refactor(audio): Simplify available audio samples management#2773

Open
xezon wants to merge 3 commits into
TheSuperHackers:mainfrom
xezon:xezon/refactor-miles-available-samples
Open

refactor(audio): Simplify available audio samples management#2773
xezon wants to merge 3 commits into
TheSuperHackers:mainfrom
xezon:xezon/refactor-miles-available-samples

Conversation

@xezon

@xezon xezon commented Jun 7, 2026

Copy link
Copy Markdown

This change simplifies the available audio samples management.

Instead of manually counting at various callsites, it now simply calculates the available audio samples from the vectors. The numbers are used mostly for debug purposes, except in SoundManager::canPlayNow.

@xezon xezon added Audio Is audio related Minor Severity: Minor < Major < Critical < Blocker Gen Relates to Generals ZH Relates to Zero Hour Refactor Edits the code with insignificant behavior changes, is never user facing labels Jun 7, 2026
@greptile-apps

greptile-apps Bot commented Jun 7, 2026

Copy link
Copy Markdown

Greptile Summary

This PR removes the manual counter-based tracking of playing audio samples (m_numPlaying2DSamples, m_numPlaying3DSamples and their notify callbacks) in favour of directly reading the size of the m_availableSamples / m_available3DSamples handle pools. The std::list containers are also replaced with std::vector for better cache performance.

  • Counter removal: notifyOf2DSampleStart/Completion and their 3D counterparts are deleted; canPlayNow now calls TheAudio->getAvailable2DSamples() > 0 and getAvailable3DSamples() > 0, which correctly mirrors the old m_numPlaying < m_num comparison because handles are already pushed back into the vector by releaseMilesHandles on completion.
  • std::liststd::vector: Both available-handle pools switch to std::vector, using push_back/pop_back for O(1) acquisition and return; freeAllMilesHandles now iterates then clears rather than erasing in-loop, fixing the infinite-loop bug mentioned in a prior review thread.
  • getAvailable2DSamples / getAvailable3DSamples: New virtual methods on AudioManager delegate to m_availableSamples.size() / m_available3DSamples.size(), giving an always-accurate available count without extra bookkeeping.

Confidence Score: 5/5

The change is safe to merge; it replaces a manual counter system with direct vector-size tracking, and the handle lifecycle is consistent throughout.

Handle acquisition and return paths are both correct: getFirst2DSample/getFirst3DSample pop from the vector, and releaseMilesHandles pushes back on completion or failure. The canPlayNow semantics are preserved exactly. The freeAllMilesHandles infinite-loop regression flagged earlier is addressed by iterating with ++it then clearing. No new bugs are introduced.

No files require special attention.

Important Files Changed

Filename Overview
Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp Core implementation of the refactor: list→vector, notify calls removed, new getAvailable*Samples() implementations, and corrected freeAllMilesHandles loop. Handle lifecycle (pop_back on acquire, push_back on release) is correctly maintained throughout.
Core/GameEngine/Source/Common/Audio/GameSounds.cpp canPlayNow updated to query getAvailable*Samples() instead of comparing counters; lazy-init of sample counts removed. Logic is equivalent to prior behavior.
Core/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h Declares new getAvailable2DSamples/getAvailable3DSamples overrides and changes member containers from std::list to std::vector.
Core/GameEngine/Include/Common/GameAudio.h Adds pure virtual getAvailable2DSamples() and getAvailable3DSamples() to the AudioManager interface.
Core/GameEngine/Include/Common/GameSounds.h Removes notify callbacks, available-sample helpers, and the two counter member variables from SoundManager.

Sequence Diagram

sequenceDiagram
    participant SM as SoundManager
    participant AM as AudioManager
    participant MAM as MilesAudioManager
    participant Pool as AvailablePool

    Note over SM,Pool: canPlayNow check
    SM->>AM: getAvailable2DSamples()
    AM->>MAM: getAvailable2DSamples()
    MAM->>Pool: size()
    Pool-->>SM: N handles available

    Note over MAM,Pool: Audio starts playing
    MAM->>Pool: pop_back via getFirst2DSample
    Pool-->>MAM: HSAMPLE handle
    MAM->>MAM: playSample(event, handle)

    Note over MAM,Pool: Audio stops
    MAM->>Pool: push_back via releaseMilesHandles
    Pool-->>MAM: handle returned, size increases
Loading

Reviews (2): Last reviewed commit: "Fix wrong function call in debug draw" | Re-trigger Greptile

Comment thread Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
Comment thread Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp Outdated
Comment thread Core/GameEngine/Source/Common/Audio/GameSounds.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Audio Is audio related Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant