rp2040/PWMAudioOut: Fix the click at start / stop of playback.

Signed-off-by: Andrew Leech <andrew@alelec.net>
This commit is contained in:
Andrew Leech 2023-02-20 12:18:17 +11:00
parent 9c066825a7
commit dfcc56b14a
1 changed files with 14 additions and 11 deletions

View File

@ -32,6 +32,7 @@
#include "extmod/vfs_fat.h"
#include "py/gc.h"
#include "py/mphal.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "shared-bindings/pwmio/PWMOut.h"
@ -153,14 +154,24 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *s
mp_raise_RuntimeError(translate("All timers in use"));
}
self->quiescent_value = quiescent_value >> SAMPLE_BITS_TO_DISCARD;
common_hal_pwmio_pwmout_set_duty_cycle(&self->left_pwm, self->quiescent_value);
self->quiescent_value = quiescent_value;
pwmio_pwmout_set_top(&self->left_pwm, PWM_TOP);
if (self->stereo) {
common_hal_pwmio_pwmout_set_duty_cycle(&self->right_pwm, self->quiescent_value);
pwmio_pwmout_set_top(&self->right_pwm, PWM_TOP);
}
// Ramp the PWM from zero to the quiescent value
uint16_t incr = 0;
for (int i = 0; i < 256; i++) {
incr += self->quiescent_value / 256;
mp_hal_delay_ms(2);
common_hal_pwmio_pwmout_set_duty_cycle(&self->left_pwm, incr);
if (self->stereo) {
common_hal_pwmio_pwmout_set_duty_cycle(&self->right_pwm, incr);
}
}
audio_dma_init(&self->dma);
self->pacing_timer = NUM_DMA_TIMERS;
}
@ -254,14 +265,6 @@ void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t *self)
}
audio_dma_stop(&self->dma);
// Set to quiescent level.
common_hal_pwmio_pwmout_set_duty_cycle(&self->left_pwm, self->quiescent_value);
pwmio_pwmout_set_top(&self->left_pwm, PWM_TOP);
if (self->stereo) {
common_hal_pwmio_pwmout_set_duty_cycle(&self->right_pwm, self->quiescent_value);
pwmio_pwmout_set_top(&self->right_pwm, PWM_TOP);
}
}
bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t *self) {