Skip to content

libobs/audio-monitoring: Fix PulseAudio monitoring volume for s32 format and u8 format - #5400

Merged
jp9000 merged 2 commits into
obsproject:masterfrom
norihiro:pa-monitor-process_volume
Oct 24, 2021
Merged

jp9000 merged 2 commits into
obsproject:masterfrom
norihiro:pa-monitor-process_volume

Conversation

@norihiro

@norihiro norihiro commented Oct 11, 2021 •

Copy link
Copy Markdown
Contributor

Description

When pulseaudio-output chose signed 32-bit audio as the output format and volume was lowered, audio data was broken. In the function process_volume, the type of the data is switched by bytes_per_channel. However the size of signed 32-bit integer and the size of float are same so that the signed 32-bit integer is processed as float.
This commit changes these items.

  • Use format instead of bytes_per_channel so that all the sample types can be differentiated.
    This fix the bug.
  • Change char to uint8_t in process_byte because the type is expected unsigned 8-bit.
    This also change behavior but I don't think there is a use case for unsigned 8-bit audio.
  • Change short to int16_t and renames existing function process_short to process_s16 to clarify the function is processing signed 16-bit.
    This is just a coding change. It should not affect anything.

Motivation and Context

The bug was reported on another PR and turned out it is an existing bug in master branch.
#4908 (comment)
#4908 (comment)

This bug was revealed when using PulseAudio with PipeWire.

How Has This Been Tested?

OS: Fedora 34
Tested with a ffmpeg_source that has a mp4 video, set the audio to monitor and output, and lower the volume.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@RytoEX RytoEX added the kind/bug Categorizes issue or PR as related to a bug. label Oct 11, 2021
@norihiro
norihiro force-pushed the pa-monitor-process_volume branch from 8b011be to 583c496 Compare October 11, 2021 05:16
@WizardCM WizardCM added the platform/linux Categorizes issue or PR as affecting Linux specifically label Oct 11, 2021
Comment on lines +183 to +185
default:
// just ignore
break;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to note that process_volume will do nothing just in case if unknown format arrive rather than default to process as float.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the format is not known in pulseaudio_to_obs_audio_format the PA format is set as float anyway, so should probably never be hit anyway. Whether interpreting unknown formats as float is probably another discussion.

Comment on lines +183 to +185
default:
// just ignore
break;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the format is not known in pulseaudio_to_obs_audio_format the PA format is set as float anyway, so should probably never be hit anyway. Whether interpreting unknown formats as float is probably another discussion.

@kkartaltepe

Copy link
Copy Markdown
Collaborator

Looks fine to me, Doesnt affect my system with float32 as the audio format.

@norihiro
norihiro force-pushed the pa-monitor-process_volume branch from ebd3ff7 to 933c251 Compare October 12, 2021 00:38
Comment on lines +132 to +133
for (; cur < end; cur++)
*cur = ((int)*cur - 128) * vol + 128;

@norihiro norihiro Oct 12, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to change the code after the review.
I realised unsigned 8-bit integer data was not correctly handled. it should consider the center level is 128 instead of 0.
I referred FFmpeg's volume filter is implemented as below.

static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
                                    int nb_samples, int volume)
{
    int i;
    for (i = 0; i < nb_samples; i++)
        dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess id prefer if we just did everything in floating point then since our volume is a float unlike the ffmpeg impl. Considering we were char before and no one noticed it was incredibly incorrect I assume no one is actually using this format...

@norihiro norihiro Oct 12, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The floating point data comes with const struct audio_data *audio_data so that it need to allocate a space to put the volume-processed floating point data.

Considering we were char before and no one noticed it was incredibly incorrect I assume no one is actually using this format...

I think so, too. 8-bit audio is too noisy.

Do you still want to multiply volume in floating point?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont have a strong opinion. As is is fine.

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this PR does not actually touch the "linux-pulseaudio" plugin, the correct commit prefix is actually "libobs" or "libobs/audio-monitoring". It might be helpful to point out that this is specifically for "pulse" audio monitoring in the commit subject, if you can find space. If not, then at least it's mentioned int he commit message body.

When signed 32-bit audio arrived to pulseaudio-output and volume was
lowered, audio data was broken. In the function `process_volume`, the
type of the data is switched by `bytes_per_channel`. However the size of
signed 32-bit integer and the size of float are same so that the signed
32-bit integer is processed as float.
This commit changes these items.
- Use `format` instead of `bytes_per_channel` so that all the sample
  types can be differentiated.
- Change `short` to `int16_t` and renames existing function
  `process_short` to `process_s16` to clarify the function is
  processing signed 16-bit.
Change `char` to `uint8_t` in `process_byte` because the type is
expected unsigned 8-bit.
@norihiro
norihiro force-pushed the pa-monitor-process_volume branch from 933c251 to 4d0d1f1 Compare October 16, 2021 04:47
@norihiro norihiro changed the title linux-pulseaudio: Fix monitoring volume for s32 format libobs/audio-monitoring: Fix PulseAudio monitoring volume for s32 format and u8 format Oct 16, 2021
@jp9000
jp9000 merged commit 85f45a3 into obsproject:master Oct 24, 2021
@norihiro
norihiro deleted the pa-monitor-process_volume branch October 24, 2021 08:38
@RytoEX RytoEX added this to the OBS Studio 27.2 milestone Oct 24, 2021
@RytoEX RytoEX linked an issue Oct 24, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. platform/linux Categorizes issue or PR as affecting Linux specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Staticky Audio with Black Magic Intensity Pro 4K when Monitoring

5 participants