libobs: Fix audio duplication bug #12268
Open
+68
−123
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This is split from the original PR #12175 to facilitate the reviewing.
PR #12175 fixed two audio bugs but the fixes are conceptually different.
The current bug consists in an increased audio level when a source is duplicated in a scene or through a nested scene.
The bug implies that the same audio samples will be mixed several times resulting in a coherent addition. For duplicate sources
this implies a doubling of the audio amplitude or a constant +6 dBFS level increase.
The bug was mostly fixed in commits 8a38e33 [1] and 72924ac [2]. But it was fixed at the scene level and this left an edge case where a nested scene has a Show/Hide transition. It implies an increase of +6 dBFS for any duplicated source.
An example of valid use case is a Media Source or Browser source which is duplicated and cropped to different areas. Or a nested scene. We want to be able to transition from scene A to scene B with nested scene A, without any change in audio level for instance, if the same audio is used across scenes.
[1] libobs: Mix audio of each source in a scene only once
[2] libobs: Deduplicate audio for nested scenes/groups if not transitioning
Motivation and Context
We fix the bug in this manner:
This intrinsically prevents any duplication within a scene, across scenes and in transitions.
This is a better solution than the previous one because it is not limited to intra-scene deduplication.
This fixes for instance the edge case of Show/Hide transition to a nested scene.
How Has This Been Tested?
This has been tested extensively on windows 11 pro 23H2 by myself using:
The audio level of the sine was checked using the FFmpeg volumedetect filter:
ffmpeg -i '.\input.mp4' -filter:a volumedetect -f null /dev/null
Recordings were made in mp4 container with 16 bit pcm raw audio.
The waveforms were checked and compared between OBS 31.0.3 (current release) and this PR in Reaper DAW.
@Warchamp7 did also extensive tests which helped me dodge a few dangerous bullets (crashes and what not). Thanks a lot for his help !
Profiling results
We found negligible impact of the current PR on the audio_callback.
obs master:
Tests
the following tests were done:
scene with two sines,
show/hide transition for a sine in a scene with duplicated sine,
fade transition between a scene A with single sine to scene B w/ duplicated sine,
scene A with sine, nested in scene B which also has the sine as a source.
same as 4 with a show/hide transition on the sine in scene B (not shown, same result as 4.)

same tests on a audio clip (splatoon): identical results. There is no regression on this PR.
same as 4. w/ a show/hide transition on the nested scene A:
Additional tests:
Types of changes
Checklist: