adafruit / Adafruit_VS1053_Library

This is a Arduino library for the Adafruit VS1053 Codec Breakout and Music Maker Shields
https://www.adafruit.com/products/1381
133 stars 111 forks source link

Millis() function is extremely inaccurate when playing audio on Feather M0 #50

Open geekfactory opened 5 years ago

geekfactory commented 5 years ago

The millis function is also used on my program to keep track of time using Unix time (not only to blink leds), so it´s desirable to keep timings as accurate as possible.

I know this file is high bitrate, but we´re developing a prototype for a third party. It should be able to handle high bitrate files, because the final user might not be able to tell if an MP3 file is 128 or 320 kbps.

The problem seems to be related to how the code handles the DREQ interrupt, as this function takes several milliseconds to execute while the data is transferred from the SD card and then to the VS1053. In the mean time the Systick might not be handled correctly.

Possible solutions that I thought:

Has anybody else experienced this problem, is there a workaround for this?

Thank you in advance.

TheNitek commented 5 years ago

On my ESP8266 I do not use the interrupts and feed the buffer inside of the main loop. Works great (also I did not try 320kps MP3s yet) and my guess would be that it solves your problem, so why not give it a try? Should be an easy fix.

geekfactory commented 5 years ago

Hi @TheNitek I tested by raising the interrupt priority for the SysTick on the arduino core initialization and lowered the priority for the external interrupts (used by the VS1053 library). Now the code works as expected but still not convinced of this because involves messing with the Arduino core.

Feeding the buffer inside the main loop is the easiest solution, but because most of the Arduino libraries implement some kind of blocking behavior (specially the networking ones). Doing the buffer on the main loop doesn´t looks like the best approach, especially on complex projects that might include such libraries.