1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:18:13 +00:00

LibAudio: Correctly rescale 8-bit PCM samples

8-bit PCM samples are unsigned, at least in WAV, so after rescaling them
to the correct range we also need to center them around 0. This fix
should make 8-bit WAVs have the correct volume of double of what it was
before, and also future-proof for all other unsigned PCM sample formats
we may encounter.
This commit is contained in:
kleines Filmröllchen 2022-10-14 12:22:18 +02:00 committed by Andrew Kaster
parent 1524288127
commit 5f71c81e1b

View file

@ -83,8 +83,16 @@ static ErrorOr<double> read_sample(Core::Stream::Stream& stream)
{
T sample { 0 };
TRY(stream.read(Bytes { &sample, sizeof(T) }));
// Remap integer samples to normalized floating-point range of -1 to 1.
if constexpr (IsIntegral<T>) {
return static_cast<double>(AK::convert_between_host_and_little_endian(sample)) / static_cast<double>(NumericLimits<T>::max());
if constexpr (NumericLimits<T>::is_signed()) {
// Signed integer samples are centered around zero, so this division is enough.
return static_cast<double>(AK::convert_between_host_and_little_endian(sample)) / static_cast<double>(NumericLimits<T>::max());
} else {
// Unsigned integer samples, on the other hand, need to be shifted to center them around zero.
// The first division therefore remaps to the range 0 to 2.
return static_cast<double>(AK::convert_between_host_and_little_endian(sample)) / (static_cast<double>(NumericLimits<T>::max()) / 2.0) - 1.0;
}
} else {
return static_cast<double>(AK::convert_between_host_and_little_endian(sample));
}