diff options
author | Nadia Holmquist Pedersen <nadia@nhp.sh> | 2024-03-03 16:58:59 +0100 |
---|---|---|
committer | Nadia Holmquist Pedersen <nadia@nhp.sh> | 2024-03-03 16:58:59 +0100 |
commit | e227902cecfb629fd113e9fd7c69cea6b98dd790 (patch) | |
tree | 4d1b51efec532481e47dcc931628c5c49bfa61c6 | |
parent | 67ca4997e22cd88e349ecffd2cd388431dcc8de3 (diff) |
Util_Audio: use basic linear interpolation
Should remove the artifacts caused by the previous nearest
resampling. May be worth replacing with something better in the
future, but this is an improvement for now.
-rw-r--r-- | src/frontend/Util_Audio.cpp | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/frontend/Util_Audio.cpp b/src/frontend/Util_Audio.cpp index 02b3026..25e04db 100644 --- a/src/frontend/Util_Audio.cpp +++ b/src/frontend/Util_Audio.cpp @@ -63,21 +63,28 @@ int AudioOut_GetNumSamples(int outlen) void AudioOut_Resample(s16* inbuf, int inlen, s16* outbuf, int outlen, int volume) { - float res_incr = inlen / (float)outlen; - float res_timer = 0; - int res_pos = 0; + double factor = (double) inlen / (double) outlen; + double inpos = -(factor / 2); + double vol = (double) volume / 256.f; - for (int i = 0; i < outlen; i++) + for (int i = 0; i < outlen * 2; i += 2) { - outbuf[i*2 ] = (inbuf[res_pos*2 ] * volume) >> 8; - outbuf[i*2+1] = (inbuf[res_pos*2+1] * volume) >> 8; - - res_timer += res_incr; - while (res_timer >= 1.0) - { - res_timer -= 1.0; - res_pos++; - } + double intpart_d; + double frac = modf(inpos, &intpart_d); + int intpart = (int) intpart_d; + + double l1 = inbuf[ intpart * 2]; + double l2 = inbuf[(intpart * 2) + 2]; + double r1 = inbuf[(intpart * 2) + 1]; + double r2 = inbuf[(intpart * 2) + 3]; + + double ldiff = l2 - l1; + double rdiff = r2 - r1; + + outbuf[i] = (s16) round((l1 + ldiff * frac) * vol); + outbuf[i+1] = (s16) round((r1 + rdiff * frac) * vol); + + inpos += factor; } } |