Skip to content

Commit d112d01

Browse files
Chris Nourseclaude
andcommitted
ports/stm: round-to-nearest TIM6 period in audioio
Truncating the divisor biased the realised sample rate slightly fast (e.g. 84 MHz / 44100 = 1904.76 truncated to 1904 yields 44117.6 Hz, ~0.7 cents sharp). Round to nearest so the rate is always the closest achievable, not the next one above. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent fcac2fc commit d112d01

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

ports/stm/common-hal/audioio/AudioOut.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t *self,
443443
__HAL_RCC_TIM6_CLK_ENABLE();
444444

445445
uint32_t tim6_clk = get_tim6_freq();
446-
uint32_t period = (tim6_clk / sample_rate);
446+
// Round to nearest, not truncate, so the realised sample rate is the
447+
// closest TIM6 division to the requested rate.
448+
uint32_t period = (tim6_clk + sample_rate / 2) / sample_rate;
447449
if (period < 2) {
448450
period = 2;
449451
}

0 commit comments

Comments
 (0)