Electronic – n efficient way for synchronising audio events real-time with LEDs using an MCU

audioaudio-codecdspsamplingsynchronization

I'm working on a project in which LED lights synchronise with audio events. I'm not referring to reactive LED projects that read the ADC from audio amplitude or spectrum, but about having some additional digital data playing along with the audio to indicate several triggers of events.

Initially, the approach I'm taking is that of utilising a stereo file and having a channel for audio and another channel for data. The file is accessed by an SD card module and processed by a codec or mp3 player module. The channel with data has pulses of different sine wave frequencies to indicate different events and trigger specific LED lights.

I managed to do this using a spectrum analyser chip, but it's not the ideal as by using the ADC there are fluctuations, and changes in the volume control affect the signal. I'm hoping to find a digital signal solution, as when the SD card is read and the audio processed, the data from one channel of the file (wav or mp3) must be read real-time in the MCU.

Ideally the MCU would be an ESP32. I experimented with some libraries that can read wav files from an SD card (such as esp32_I2S_player) but I couldn't manage to receive the data for creating the necessary 'if conditions'. Another try was to see if I could do this with a VS1053 codec, but libraries for this chip also don't provide a function to read data off the SD card of a specific channel, and I couldn't find much about this in the datasheet.

So my questions are:

  1. Is there an efficient way for synchronising audio events real-time with LEDs using an MCU? I know how to do this through general Operating System sofware, but now the application requires a bare machine.
  2. If my approach (stereo file = data + audio) makes sense, how can I implement this? Is there a codec or MCU that facilitates this approach? Is there any code example that can help me?

I'm feeling stagnated in this issue so I really appreciate your answers. Thanks.

Best Answer

Using an auxiliary DSP for decompression may make this difficult unless you limit yourself to constant bitrate files - if you need high time accuracy of the light events you may need to account for the processing delay from when you put the data in until the sound comes out, which would be different at different bitrates.

Possibly you could work around a distinct decoder by doing independent timing, starting an MCU time counter at the start of the audio output, and triggering light events at appropriate timestamps. In that case you may want to encode your light data in its own file linked by a naming pattern, or embedded it in interwoven data that shows up a bit ahead of compressed audio it corresponds to and gets held in an MCU buffer until the indicated timestamp.

One potential drastic simplification is to store linear PCM .wav files instead of compressed MP3. Given that an audio CD is only about 3/4 of a gigabyte, even a cheap SD card potentially holds a few hours of uncompressed audio. If you have no compression it's pretty simple for your MCU to just clock the data out a DAC, though preferably use a hardware timer driven DAC (and potentially DMA) or at least an interrupt, not a software delay loop.