rp2040/PWMAudioOut: Fix the click at start / stop of playback. #7618
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses #5136 for rp2040 for me with the code & mp3 listed in #5136 (comment)
During the testing of this change, in the
construct
andstop
functions I sprinked a bit of:To see what values were actually set at the register level for the pwm
top
andcurrent
levels.Also, in
common_hal_audiopwmio_pwmaudioout_stop
I added the following to print the "last" pwm value at the end of a track.:With this I've found my mp3 starts/ends with a value of ~512 with the pwm top value of 1023.
During construct, the pwm is created with a current pwm level of 0. It then has the top changed to 1023 and pwm set to quiescent_value. For quiescent_value to result in a raw pwm level of 512 I had to remove the
>> SAMPLE_BITS_TO_DISCARD
on quiescent_value. I'm not too sure why this was originally added and whether removing it will break other usages.Separetely I added some delays between these steps in construct and the mp3 start. With this it was clear that the jump from 0 to quiescent_value causes a click. Removing the "set to quiescent_value" moves the click to the start of pwm.
I tried changing the initial pwm value to "half of top" in
common_hal_pwmio_pwmout_construct()
which moves the click tocommon_hal_pwmio_pwmout_construct
but then there's no click for "set to quiescent_value" or for mp3 start.Long story short, the best solution I've come up with is contruct pwm with 0 (no click), slowly ramp up to
quiescent_value
ofTOP/2
(adding a ~0.5 second startup delay) then let the mp3 place.I also removed the re-set of pwm levels in
stop
. Withquiescent_value
==TOP/2
this could be left in without adding a click on most tracks, however if you've got two tracks that are supposed to be gapless they might not transition at "mp3 zero level" so resetting toquiescent_value
will add a click.I'm not sure if there's any other value in having stop force the pwm back to
quiescent_value
.