Skip to content

Commit

Permalink
Round instead of truncating in float audio converter
Browse files Browse the repository at this point in the history
This is mainly to make the tests {float,double}_conversion more robust,
previously they could fail depending on the target CPU type (e.g. with
gcc -m32 -march=i386).
In principle this is now inconsistent with the other converters which
all still truncate, but this doesn't really make a difference in
practice, and making them round would add additional branching and (if
implemented naively) break some other tests, so this is probably fine.

Fixes #274.
  • Loading branch information
arch1t3cht committed Jan 11, 2025
1 parent a40f865 commit 59412be
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libaegisub/audio/provider_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ class FloatConvertAudioProvider final : public AudioProviderWrapper {
for (size_t i = 0; i < static_cast<size_t>(count * channels); ++i) {
Source expanded;
if (src_buf[i] < 0)
expanded = static_cast<Target>(-src_buf[i] * std::numeric_limits<Target>::min());
expanded = static_cast<Target>(-src_buf[i] * std::numeric_limits<Target>::min() - 0.5);
else
expanded = static_cast<Target>(src_buf[i] * std::numeric_limits<Target>::max());
expanded = static_cast<Target>(src_buf[i] * std::numeric_limits<Target>::max() + 0.5);

dest[i] = expanded < std::numeric_limits<Target>::min() ? std::numeric_limits<Target>::min() :
expanded > std::numeric_limits<Target>::max() ? std::numeric_limits<Target>::max() :
Expand Down

0 comments on commit 59412be

Please sign in to comment.