earlephilhower / ESP8266Audio

Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32
GNU General Public License v3.0
1.99k stars 433 forks source link

Telling when the audio actually gets out ? #608

Open Maigre opened 1 year ago

Maigre commented 1 year ago

Hi, I am a regular user of your library, thanks for the good work!

I am trying to achieve a specific use case where i trigger a GPIO when playing a sound. I need a sub 1ms accuracy. I use an external PCM5102 over I2S and read WAV file from an SD card.

Right now i do

while(true) { if (!wav->isRunning() || !wav->loop()) break; digitalWrite(21, HIGH); } digitalWrite(21, LOW);

But i have ~24ms delta between gpio trigger and actual audio out. I guess it's due to dma_buffer + maybe some PCM buffering + maybe some initial SD read latency ?

What i am looking for is a way to now when data are actually sent to the PCM chip over I2S, and even better if you know some way to tell when audio actually get out of the chip..

Could you hint me where in the code i could hook such informations ? i am trying to warp my head around the I2S flow path but i am a bit lost rigth now..

Maybe i could calculate it from dma_buf_count / dma_buf_len / buffersize / sample_rate ? But then how to account for the initial variable time to fill the buffer from SD card ?

I'll continue to look on my own and report back if i find interesting ways to trigger something accurately when audio is actually playing out.

Thanks ! Best Regards,

Thomas

ChuckMash commented 7 months ago

Did you ever find a solution for this? I would start by looking around https://github.com/earlephilhower/ESP8266Audio/blob/master/src/AudioOutputI2S.cpp#L326 and i2s_write