schreibfaul1 / ESP32-audioI2S

Play mp3 files from SD via I2S
GNU General Public License v3.0
1.15k stars 290 forks source link

Distored output and mp3s crashing #904

Closed ra0943-VK3ACH closed 1 week ago

ra0943-VK3ACH commented 1 week ago

All files i've tried playing have been distorted and silent for the first couple of seconds. On occasion, the esp32-s3 will crash this mainly happens with mp3 files. Before the crash I will get MP3 decode error -1: INDATA_UNDERFLOW. I know this isn't a hardware issue as I have used the same hardware with a different audio library.

output of when file plays all the way through (still distorted)

buffers freed, free Heap: 334812 bytes
Reading file: "/Levels (Original Version).mp3"
MP3Decoder has been initialized, free Heap: 330400 bytes , free stack 5212 DWORDs
Playing path: /Levels (Original Version).mp3
NAV Screen: Select Command
Content-Length: 3270733
ID3 framesSize: 655
ID3 version: 2.4
ID3 normal frames
Audio-Length: 3270078
stream ready
syncword found at pos 0
MPEG-2.5, Layer I
Channels: 2
SampleRate: 44100
BitsPerSample: 16
BitRate: 64000
Closing audio file "Levels (Original Version).mp3"
End of file "Levels (Original Version).mp3"

Relevant code

main.ino

#include <Arduino.h>
#include "filemanager.h"
#include "UI.h"
#include <FS.h>
#include "SPIFFS.h"
#include "Player.h"

// put function declarations here:
int myFunction(int, int);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(5000);
  Serial.println("Starting SD Card");
  if(!StartSDCard()){
    Serial.println("Start Failed");
    while (true)
    {
      ;
    }
  }
  Serial.println("Starting Audio");
  player.Init();
  std::vector<String> Queue;
  Queue.push_back("/LOVEBONE.mp3");
  player.SetQueue(Queue);
  player.PlayItem(0);
}

void loop() {
  SerialInput();
  //player.SerialPlayLoop();
  player.AudioLoop();
}

Player.cpp

#include "Player.h"
#include "filemanager.h"
#include <Audio.h>
#include <vector>
#include <Arduino.h>
#include <SD_MMC.h>

#define I2S_LRC     43
#define I2S_DOUT    17
#define I2S_BCLK    2

Player player;
Audio audio;

int CurrentlyPlayingIndex = 0;
File CurrentlyPlayingFile;
std::vector<String> Queue;

void Player::Init(){
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(17);
}

void Player::SetQueue(std::vector<String> Paths){
    Queue.clear();
    Queue = Paths;
}

void Player::PlayItem(int queueIndex){
    closeFile(CurrentlyPlayingFile);
    CurrentlyPlayingFile = openFile(Queue[queueIndex]);
    CurrentlyPlayingIndex = queueIndex;
    audio.connecttoFS(SD_MMC, Queue[queueIndex].c_str());
    Serial.print("Playing path: ");
    Serial.println(Queue[queueIndex]);
}

void Player::AudioLoop(){
    audio.loop();
}

void audio_info(const char *info){
    Serial.println(info);
}
ra0943-VK3ACH commented 1 week ago

Solved by adding vTaskDelay(1); to loop. see #887