Repository navigation
Description
note: this was found by claude, not by me. it came up while the agent was porting bbwenet to rust for a hobby project. the agent then checked it against the current source, the git history and the existing issues. i'm posting it on its behalf. everything below (analysis, numbers, repro) is the agent's work. the repro is short, so it's quick to verify. i hope it's not bogus!
in float builds, celt_sin() scales its argument by π/2 where 2/π is needed:
/* celt/mathops.h:353 (main 503d81b, v1.6.1, v1.6) */
#define celt_sin(x) celt_cos_norm2((0.5f*PI) * (x) - 1.0f)celt_cos_norm2(u) approximates cos(π/2·u) (its comment and test_cos() in celt/tests/test_unit_mathops.c), so
celt_sin(x) = cos(π/2·(π/2·x − 1)) = cos(π²/4·x − π/2) = sin(π²/4·x) ≈ sin(2.467·x)
for sin(x) the argument has to be (2/π)·x − 1.
where it is used
the only caller is apply_valin_activation() in dnn/osce.c (line 989), which bbwenet_process_frames() runs at lines 1229 and 1311:
y[i] = celt_log(y[i]); /* natural log */
...
x[i] *= celt_sin(y[i]);celt_sin was added in 940d4e5 ("extending mathops for use in osce"), which replaced
x[i] *= sin(log(fabs(x[i]) + 1e-6f));bbwenet is trained with the same function in radians (dnn/torch/osce/models/bbwe_net.py:204, activation ImPowI):
self.nlfunc = lambda x : x * torch.sin(torch.log(torch.abs(x) + 1e-6))so since 1.6 the c code applies x·sin(π²/4·ln|x|), while the model was trained with x·sin(ln|x|). the training code has not changed since 2025-09 (still torch.sin), and the model data was last updated the day before 940d4e5.
expected vs actual
output of the repro below on main (503d81b), gcc 16.2, x86-64:
y = -2.0000000 celt_sin(y) = 0.9753680 sin(y) = -0.9092974 sin(pi^2/4*y) = 0.9753680
y = -1.0000000 celt_sin(y) = -0.6242660 sin(y) = -0.8414710 sin(pi^2/4*y) = -0.6242660
y = 0.5000000 celt_sin(y) = 0.9437192 sin(y) = 0.4794255 sin(pi^2/4*y) = 0.9437192
y = 1.0000000 celt_sin(y) = 0.6242659 sin(y) = 0.8414710 sin(pi^2/4*y) = 0.6242660
y = 1.5707964 celt_sin(y) = -0.6699874 sin(y) = 1.0000000 sin(pi^2/
5B55
4*y) = -0.6699876
y = 3.0000000 celt_sin(y) = 0.8996722 sin(y) = 0.1411200 sin(pi^2/4*y) = 0.8996722
over y in [−14, 14) (the caller's inputs start at ln(1e-6) ≈ −13.8): max |celt_sin(y) − sin(π²/4·y)| = 1.6e-6, max |celt_sin(y) − sin(y)| = 1.99.
impact
only float builds with osce compiled in (--enable-osce, OPUS_OSCE, -Dosce=enabled) and bwe enabled at run time (OPUS_SET_OSCE_BWE(1), 48 khz output, complexity ≥ 4, silk-only wideband frames or their concealment). default builds and default decoder settings are not affected.
with bwe on, the agent ran osce_bwe() on 8 librispeech dev-clean utterances (16 khz in, 48 khz out), once with the current macro and once with libm sin() (the pre-1.6 code and the training activation):
- above 8 khz, the band bbwenet generates, the two outputs differ strongly: snr −1.6 to 8.7 db (median 2.3 db), and the current output is 0.2–2.8 db louder there (median 1.4 db);
- full band: 18–44 db snr (median 33 db), since the band below 8 khz passes through;
- with the fix below, the output matches the libm version to int16 rounding (median snr 87 db above 8 khz, 113 db full band).
predicted quality barely changes: on 32 clips, nisqa 4.066 vs 4.049, sigmos 3.187 vs 3.181, utmos 3.967 vs 3.968 (current vs correct sine). so this is mainly the c inference no longer matching the trained model, not an obvious audible defect.
minimal repro
run from the opus source root:
/* gcc -O2 -I celt -I include celt_sin_check.c -o celt_sin_check -lm */
#include <math.h>
#include <stdio.h>
#include "mathops.h"
int main(void)
{
const float ys[] = {-2.f, -1.f, 0.5f, 1.f, 1.5707964f, 3.f};
int i;
for (i = 0; i < 6; i++)
printf("y = %10.7f celt_sin(y) = %10.7f sin(y) = %10.7f sin(pi^2/4*y) = %10.7f\n",
ys[i], celt_sin(ys[i]), sin(ys[i]), sin(PI*PI/4*ys[i]));
return 0;
}suggested fix
-#define celt_sin(x) celt_cos_norm2((0.5f*PI) * (x) - 1.0f)
+#define celt_sin(x) celt_cos_norm2((2.f/PI) * (x) - 1.0f)with it, max |celt_sin(y) − sin(y)| is 8.6e-7 over [−14, 14), and the bwe output returns to the pre-1.6 behaviour. a test_sin() next to test_cos() in celt/tests/test_unit_mathops.c would catch a regression. if the current output is preferred instead, the model would need retraining with that activation, and the macro renaming, since it does not compute sin().
Activity