Skip to content

Fix stereo overlap#1

Open
lonewolf0708 wants to merge 9 commits intoinitrail:mainfrom
lonewolf0708:fix-stereo-overlap
Open

Fix stereo overlap#1
lonewolf0708 wants to merge 9 commits intoinitrail:mainfrom
lonewolf0708:fix-stereo-overlap

Conversation

@lonewolf0708
Copy link
Copy Markdown

Proposed fix for the song playing bug when two songs play at the same time.

lonewolf0708 and others added 8 commits May 6, 2025 11:36
Changed to using individual ignitionLevel values (0/1/2/3)
Stereo pauses when starter is engaged
Fixed missing symbol and operators
Multiple changes and bugfixes (again)
The `cacheTrackIndex` function was using `#trackFiles` as the modulus
when `caching` was enabled. This could result in out-of-bounds indices
for the `cachedTracks` array, leading to `obj:cutSFX` not being called
on the correct sound object for the currently playing song. This meant
the previous song would not stop when the next one started.

This change corrects the modulus to `cacheSize`, ensuring that
`cacheTrackIndex` always returns a valid index (1 to `cacheSize`)
for the `cachedTracks` array. This allows the system to correctly
stop the previous song before a new one begins.
This commit addresses an issue where previous songs would continue
playing after a new song started, with the first song only stopping
when a third song began.

The root cause was determined to be an insufficient stop mechanism
for audio tracks. `obj:cutSFX()` alone was not preventing overlap.
The actual stop occurred when `obj:deleteSFXSource()` was called,
which was happening too late via the `updateCache` function (primarily
for tracks that rotated into `cachedTracks[1]`).

The following changes have been implemented:

1.  **`nextTrack` and `previousTrack` Modifications:**
    *   The SFX source of the outgoing track is now explicitly cut
      (`obj:cutSFX()`) and then its source deleted
      (`obj:deleteSFXSource()`) immediately within these functions
      before the track index changes.
    *   The corresponding `.sfx` entry in `cachedTracks` is set to `nil`.
    *   Logic has been added to ensure that the incoming track (the
      new current track) has a valid SFX object loaded/created in
      `cachedTracks` before `systemPlayTrack` is called.

2.  **`updateCache` Modifications:**
    *   Corrected the calculation of `loadIndexInTrackFiles` to ensure
      the correct tracks are loaded into the cache when the window slides.
      Tracks are now loaded into `cachedTracks[cacheSize]` (for forward)
      and `cachedTracks[1]` (for backward) using accurate indices.
    *   Added robustness checks for nil SFX objects before deletion and
      nil table slots before use.

These changes ensure a more aggressive and immediate cleanup of ended
audio sources, and reliable preparation of new ones, preventing overlap.
The `cacheTrackIndex` function was also previously corrected to use
`cacheSize` as its modulus.
This commit addresses several critical errors reported in your logs, including
SimObject naming conflicts and nil reference errors during SFX operations,
while retaining previous fixes for audio overlap.

Key changes:

1.  **Standardized SFX SimObject Naming:**
    - Introduced a `generateSfxName(filePath)` helper function to create
      engine-compliant SimObject names (e.g., "sfx_stereo_filename_ext_mp3")
      from full filepaths.
    - All calls to `obj:createSFXSource(sfxName, profile, filePath, ...)`
      now use `generateSfxName(filePath)` for the `sfxName` (1st argument),
      while `filePath` (3rd argument) remains the actual sound file path.
    - This resolves `engine::SimObject::assignName` errors caused by
      using full paths with '/' as SFX names.

2.  **Robust Nil Checks for SFX Operations:**
    - Added checks for `track and track.sfx` before calls to
      `obj:cutSFX()` and `obj:deleteSFXSource()` in `killStereoSystem`.
    - Strengthened conditions in `toggleShuffleMode` before SFX deletion
      to ensure `cachedTracks[i]` and `cachedTracks[i].sfx` are valid,
      addressing a reported `deleteSFXSource` error.

3.  **Refined `cachedTracks` Data Management:**
    - Modified `nextTrack` and `previousTrack` to only nullify the
      `.sfx` field of a stopped track in `cachedTracks`. The `.name`
      field (which stores the original filepath) is now preserved.
    - This ensures that functions like `toggleStereoSystem` can reliably
      use `cachedTracks[i].name` to retrieve the filepath for recreating
      SFXs, fixing an `attempt to index a nil value` error with
      `createSFXSource`.

These changes aim to create a more stable and robust stereo system by
correctly managing SFX object lifecycles, naming, and data persistence
within the `cachedTracks` array.
Fixed multiple bugs in code.
@lonewolf0708
Copy link
Copy Markdown
Author

Just published a fix for a few typos that were found in the code.

Fixed a few crashes pertaining to various vehicle-related cutSfx events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant