celt_sin() computes sin(pi^2/4 * x) instead of sin(x), so BBWENet's activation no longer matches the trained model
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- audio-video-rtc
Research direction
Start in celt/mathops.h at celt_sin(), then compare its behavior with test_cos() in celt/tests/test_unit_mathops.c and the activation path in dnn/osce.c. Verify the sine approximation against the training activation in dnn/torch/osce/models/bbwe_net.py, add coverage in test_unit_mathops.c, and confirm the affected BWE behavior matches the trained model.
Written by the indexing model from the issue text.
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/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().
- Dominant language
- C
- Stars
- 3.4k
- Forks
- 820
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from xiph/opus
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Windows CMake CI is brokenMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 40/100
-
Signed integer overflow in frame_size_select's own validation lets a huge frame_size through to a stack allocationPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
area:docs area:stdlib found-by:duet-audit kind:docs-drift
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
InauguralSystems/EigenScript#2251 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
flux-framework/flux-coral2#513 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ly05010419/MacNativeGames#1420 ·
Maintainers usually reply within 1 day
-
Isaac workingOpen
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
seedds/nxnative#763 · 1 comment ·
Maintainers usually reply within 1 day