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
135 stars 113 forks source link

feedBuffer_noLock not adapted to fast processor #34

Closed briancouchman closed 2 years ago

briancouchman commented 7 years ago

Hello dear Adafruit

I purchased a Feather Huzzah 32 with a ESP32 on it, and a Adafruit Music Maker FeatherWing - MP3 OGG WAV MIDI Synth Player (PID: 3357). I used the V1053 library and it didn't work. In fact only the first bytes of the data is played for a fraction of a second. Looking into the code in the feedBuffer_noLock I noticed that if readyForData() returns 0 everything stops, and it seems that the data is fed too fast. After 110 times of writing 32 bytes, readyForData() returns 0 . The datasheet of the chip explains that when that happens the code should just wait for the buffer to be ready again and continue.

I have tentatively changed the code to this

void Adafruit_VS1053_FilePlayer::feedBuffer_noLock(void) {
  if ((! playingMusic) // paused or stopped
      || (! currentTrack)
      || (! readyForData())) {
    return; // paused or stopped
  }

  int cnt = 0;
  // Feed the hungry buffer! :)
  while (playingMusic/*readyForData()*/) {
    // Read some audio data from the SD card file
    int bytesread = currentTrack.read(mp3buffer, VS1053_DATABUFFERLEN);

    if (bytesread == 0) {
      // must be at the end of the file, wrap it up!
      playingMusic = false;
      currentTrack.close();
      break;
    }

    while(!readyForData()){
      //wait for the buffer to be ready to receive more data
    }
    playData(mp3buffer, bytesread);
  }
}

It keeps reading the data the until playingMusic becomes false. But it will wait for readyForData() to be 1 before calling playData()

Hopefully it can be an improvement to the library for controllers like ESP32. I am very new to whole this so if you see a problem in my code or my thinking it's very welcome.

Thanks

By the way Adafruit, you are so awesome !

TheNitek commented 6 years ago

I think the problem is a different one: The unterrupts don't work well on the ESP32. Under normal circumstances the interrupt would trigger feedBuffer() when needed. This gives you 2 options:

vogonsaudi commented 5 years ago

Sadly, neither of these methods seem to work for me with the feather esp32 and the music maker. I can get it to output a wave form, and it sees the file on the card, but won't actually make any sound.

kmccaffr commented 5 years ago

@briancouchman Unfortunately that's no different than calling play full file, as it locks out everything similarly. @TheNitek The feedbuffer method doesn't update fast enough unless you basically have empty code.

kmccaffr commented 5 years ago

A quicker (and only temporary shoddy fix) way to go about it, just add a very small delay to loop. It may affect other things (so not best option at all) but I've found it stabilizes a bit more and allows it to catch up the buffer.

Something to play around with is all.

kmccaffr commented 5 years ago

I retract what I have previously said, I am using a Particle Photon and I got steady playback but it crashes for using startPlayingFile after there is no more audio to play. So my solution doesn't necessarily work.

PoulpePalpitant commented 3 years ago

Welp, I am encountering a similar issue with the exact same gear, except the playback is somehow 3 times as fast as it should be. Also the program cant even escape the feedbuffer as long as the track is playing, so no interrupt here. Have you found any solutions to this?

PoulpePalpitant commented 3 years ago

This fork from eziya did the trick, everything now works like a charm. Ty the internet git@github.com:eziya/ESP32_ADAFRUIT_VS1053.git

I found it from this thread which is kinda similar to this one https://github.com/adafruit/Adafruit_VS1053_Library/issues/45

caternuson commented 2 years ago

This seems ESP32 specific. Closing in favor of this issue: https://github.com/adafruit/Adafruit_VS1053_Library/issues/45

See comment in that thread: https://github.com/adafruit/Adafruit_VS1053_Library/issues/45#issuecomment-1235766662