Skip to content

Commit ae7f5a2

Browse files
committed
Better accuracy for fixed-point pitch on tones
1 parent 8a9980f commit ae7f5a2

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

celt/celt_encoder.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ static opus_val16 tone_detect(const celt_sig *in, int CC, int N, opus_val32 *ton
13771377
/* Squared radius of the poles. */
13781378
*toneishness = -lpc[1];
13791379
#ifdef FIXED_POINT
1380-
freq = acos_approx(lpc[0]>>1)/delay;
1380+
freq = (acos_approx(lpc[0]>>1)+delay/2)/delay;
13811381
#else
13821382
freq = acos(.5f*lpc[0])/delay;
13831383
#endif
@@ -1433,18 +1433,17 @@ static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem,
14331433
/* If the pitch is too high for our post-filter, apply pitch doubling until
14341434
we can get something that fits (not ideal, but better than nothing). */
14351435
while (QEXT_SCALE(tone_freq) >= multiple*QCONST16(0.39f, 13)) multiple++;
1436-
tone_freq /= multiple;
14371436
if (QEXT_SCALE(tone_freq) > QCONST16(0.006148f, 13)) {
14381437
#ifdef FIXED_POINT
1439-
pitch_index = IMIN(51472/tone_freq, max_period-QEXT_SCALE(2));
1438+
pitch_index = IMIN((51472*multiple+QEXT_SCALE(tone_freq)/2)/QEXT_SCALE(tone_freq), COMBFILTER_MAXPERIOD-2);
14401439
#else
1441-
pitch_index = IMIN((int)floor(.5+2.f*M_PI/tone_freq), max_period-QEXT_SCALE(2));
1440+
pitch_index = IMIN((int)floor(.5+2.f*M_PI*multiple/QEXT_SCALE(tone_freq)), COMBFILTER_MAXPERIOD-2);
14421441
#endif
14431442
} else {
14441443
/* If the pitch is too low, using a very high pitch will actually give us an improvement
14451444
due to the DC component of the filter that will be close to our tone. Again, not ideal,
14461445
but if we only have a single tone, it's better than nothing. */
1447-
pitch_index = min_period;
1446+
pitch_index = COMBFILTER_MINPERIOD;
14481447
}
14491448
gain1 = QCONST16(.75f, 15);
14501449
} else if (enabled && complexity >= 5) {
@@ -1463,6 +1462,9 @@ static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem,
14631462
N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
14641463
if (pitch_index > max_period-QEXT_SCALE(2))
14651464
pitch_index = max_period-QEXT_SCALE(2);
1465+
#ifdef ENABLE_QEXT
1466+
pitch_index /= qext_scale;
1467+
#endif
14661468
gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
14671469
/*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
14681470
if (st->loss_rate>2)
@@ -1473,11 +1475,8 @@ static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem,
14731475
gain1 = 0;
14741476
} else {
14751477
gain1 = 0;
1476-
pitch_index = min_period;
1478+
pitch_index = COMBFILTER_MINPERIOD;
14771479
}
1478-
#ifdef ENABLE_QEXT
1479-
pitch_index /= qext_scale;
1480-
#endif
14811480
#ifndef DISABLE_FLOAT_API
14821481
if (analysis->valid)
14831482
gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);

0 commit comments

Comments
 (0)