New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix various RP2040 and SAMD audio issues #5079
Conversation
@kattni please try with the macropad demo you made to play audio clips and see if you're getting reasonable playback :) |
I tested the tone/wav demo, modified to play two wav files, and I'm still running into some crunchy playback, and the program hanging after the playback of one file. Worked with Dan on Discord to try out his demo code, which doesn't hang. I have provided him with the modified version of my demo so he can test the code I was using. |
I did some debugging of this over the weekend. The MacroPad library creates and destroys the |
This is ready to test for serious problems. Still looking at "crackly" audio. |
RP2040 and SAMD51: - Detect when DMA has finished, and stop DMA audio explicitly. - Do not accidentally reuse `first_buffer` supplied by WaveFile or RawSample. Always realloc `first_buffer` and `second_buffer` RP2040: - When audio playing is stopped, write a final zero to the output register. This prevents residual PWM tones. - Handle buffer size for 8-bit samples properly for 16-bit output. - Fail on some edge cases (which may not be possible at the moment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhalbert Ok, tested with the MacroPad library using play_file()
. Audio, as expected is still crackly, but the code continues running with this build following multiple playbacks.
I thought audio was only crackly on the first play before, and now it is crackly all the time. You may already be addressing this, but I wanted to let you know.
Here is the example I ran with examples previously converted as suggested by you, Dan. I'm pretty sure I sent them to you before, but let me know if you would like them again.
from rainbowio import colorwheel
from adafruit_macropad import MacroPad
macropad = MacroPad()
tones = ["/low_fade_16.wav", "/drama_16.wav", 246, 262, 294, 330, 349, 392, 440, 494, 523, 587]
while True:
key_event = macropad.keys.events.get()
if key_event:
if key_event.pressed:
macropad.pixels[key_event.key_number] = colorwheel(
int(255 / 12) * key_event.key_number
)
if key_event.key_number == 0 or key_event.key_number == 1:
macropad.play_file(tones[key_event.key_number])
else:
macropad.start_tone(tones[key_event.key_number])
else:
macropad.pixels.fill((0, 0, 0))
macropad.stop_tone()
@kattni Assuming this builds, it is ready to try again. I no longer have crackly audio. Main fix in the latest commit: Saleae pin instrumentation of the buffer filling in Other recent changes:
|
I haven't yet looked at similar changes for SAMD51, but it may need them. nRF is more different but I should also audit that too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Two minor things. Good otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested with the same code as in the previous comment. Wav playback is no longer crunchy. There is an audible click at the end of both wav and tone playback, but that will be addressed at a later time.
Thanks for the fix!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on this!
@PaintYourDragon plz also test your game sound fx |
It’s sounding great! Much appreciated. |
RP2040 and SAMD51:
first_buffer
supplied by WaveFile or RawSample. Always reallocfirst_buffer
andsecond_buffer
RP2040:
I don't think this fixes all the current audio issues. There may still be glitches when a display is running at the same time, and I have seen some odd behavior occasionally, specifically:
An observation for future work:
I would appreciate testing by folks on both RP2040 and SAMD51. Thanks.