pico-extras
pico-extras copied to clipboard
pico_audio_pwm: `take_audio_buffer` never returns null
We've been tinkering with audio on PicoSystem and in the case of at least PWM audio, take_audio_buffer never seems to return null, and never seems to stall for any length of time.
In instances where we're naively calling an "audio_update" function at an arbitrary speed (too quickly in our case) this results in the audio engine getting clocked at some multiple it's intended speed, since we're relying upon take_audio_buffer to give us a buffer if and only if there is an consumed buffer in the pool.
For example, our update_audio function might look something like this:
void update_audio(uint32_t time) {
struct audio_buffer *buffer = take_audio_buffer(audio_pool, false);
if(buffer) {
auto samples = (int16_t *) buffer->buffer->bytes;
for(uint32_t i = 0; i < buffer->max_sample_count; i++) {
*samples++ = get_audio_frame();
}
}
buffer->sample_count = buffer->max_sample_count;
give_audio_buffer(audio_pool, buffer);
}
And this would be called continuously in the main loop.
No matter how fast we run this, if(buffer) is always true.
yeah that PWM code assumes 48Mhz clock and 22058Hz audio (I think it says that somewhere)... so of limited use (unless you can set your CPU frequency and PIO clock divider accordingly).
we are working on a new audio API (with parity across all audio types - i.e. run whatever freq you want) which will make its way into SDK at some point - somewhat of a replacement for these APIs.
Frankly on PicoSystem you can just use PWM and DMA to that (to get 8 bit audio).