Esp32 and VS1053 Audio Player

Writing software for systems that use VLSI Solution's devices as slave codecs to a host microcontroller.
Post Reply
Jay
User
Posts: 5
Joined: Tue 2024-01-23 17:20
Contact:

Esp32 and VS1053 Audio Player

Post by Jay »

hi there,

I want to make an ESP32 and VS1053 based FLAC/Mp3 player, I am new to microcontrollers and VS1053 so don't know where to start, i found this example code which have an example code from some French website :-
Code :-

Code: Select all

----------------------------------------------------------------------------------------------------
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <VS1053.h>

// pin Defination
// MISO: D19    MOSI: D23   SCK: D18
#define VS1053_CS     5
#define VS1053_DCS    16
#define VS1053_DREQ   4
#define SDCARD_CS    22

#define NUMBER_OF_FILES 25  // Maximum number of files considered
char listFiles [NUMBER_OF_FILES] [16];

int volume = 90;  // 0 to 100
int currentnumber = 0;
int numberTotal = 0;
bool enPause = 0; // 1 If the user has stopped playback

VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);

File mp3file;

const int BUFFSIZE = 64;
uint8_t mp3buff[BUFFSIZE];


// an inventory of the files present at the root of the SD card is made
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {

  Serial.println("------");

  int Number = 0;

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("Failed to read SD card");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (!file.isDirectory()) {
      if (Number < NUMBER_OF_FILES) {
        strcpy(listFiles[Number], file.name());
        Number++;
      }
    }
    file = root.openNextFile();
  }

  numberTotal = Number - 1;
  Serial.println("List of files on SD card:");
  for (int i = 0; i < Number; i++) {
    Serial.print("\t");
    Serial.println(listFiles[i]);
  }
  Serial.println("------");

}

// Let's move on to the next file
void nextTrack() {

  if (mp3file) { // A file is already open
    mp3file.close();

    if (currentnumber < numberTotal) {
      currentnumber++;
    }
    else {
      currentnumber = 0;  // back to first file
    }

  }

  mp3file = SD.open(listFiles[currentnumber]);

  Serial.println(listFiles[currentnumber]);

}



void setup() {
  Serial.begin(115200);

  delay(10);

  Serial.println("");
  Serial.println("MP3 file player on SD card");

  if (!SD.begin(SDCARD_CS)) {
    Serial.println("No SD card?");
    return;
  }

  listDir(SD, "/", 0);

  // initializing the VS1053
  player.begin();
  player.switchToMp3Mode();
  player.setVolume(volume);

  Serial.println("Controles:");
  Serial.println("\tn: Skip to the next track");
  Serial.println("\tp: pause/resume playback");
  Serial.println("\t+/-: Volume control");
  Serial.println("------");

}

void loop() {

  if (Serial.available()) {
    char c = Serial.read();

    // N: Next song
    if (c == 'n') {
      Serial.println("Next track");
      nextTrack();     
    }

    // P: Pause/Resume
    if (c == 'p') {
      enPause = !enPause;
      Serial.println("Pause/Resume");
    }
     
    // +: increase the volume
    if (c == '+') {
      if (volume < 100) {
        Serial.println("Louder");
        volume++;
        player.setVolume(volume);
      }
    }


    // -: decrease the volume
    if (c == '-') {
      if (volume > 0) {
        Serial.println("Less strong");
        volume--;
        player.setVolume(volume);
      }
    }
  }

if (!enPause) {
  if (mp3file) {
    int bytesRead = mp3file.read(mp3buff, BUFFSIZE);
    if (bytesRead > 0) {
      Serial.println("Read " + String(bytesRead) + " bytes");
      player.playChunk(mp3buff, bytesRead);
    } else {
      Serial.println("End of file reached");
      nextTrack();
    }
  } else {
    Serial.println("No file open!");
  }
}
}
-----------------------------------------------------------------------------------------------------
Serial Monitor of Arduino does show file information, but it scrolls continuously and shows file name again and again with another mp3 file's name mixed half with next mp3 file's name which are on SD card,
and song doesn't play,

what should be the problem?

IC -- VS1053
Microcontroller -- ESP32-Wroom-32
Software -- Arduino IDE
Above Code is in C Language.
and i am using VS1053 library for ESP32 by Baldram.

- Jay
:idea: Trying to be an Engineer
Hannu
VLSI Staff
Posts: 561
Joined: Mon 2016-05-30 11:54
Location: Finland
Contact:

Re: Esp32 and VS1053 Audio Player

Post by Hannu »

Hello and welcome to the forum,

First of all I'm not familiar with Arduino ecosystem.

The Baldram's VS1053 library seems to be targeting Platformio. Is it compatible with Arduino?

Does the build have warnings?

The library has player.isChipConnected() method which could tell if you have wrong SPI connections. The way it has been implemented, it can't tell if there is a problem with DREQ or XDCS.

Also enabling debug mode could make the library to tell what is wrong.

My next step would be to try the examples from the library itself. If everything works with the example, then you can blame some French somewhere. Compare what is different between the working example and your code.

Hopefully this helps to start debugging.
Jay
User
Posts: 5
Joined: Tue 2024-01-23 17:20
Contact:

Re: Esp32 and VS1053 Audio Player

Post by Jay »

Thank for the reply,

Well I don't know much about software side of Arduino, i only know basic stuff,the baldram's library which I used is for configured to work with ESP32,

And in baldram's library there is two examples one is web radio demo which play audio from url and MP3 player demo which plays the sample audio in .h format, and there are two codes on French website in which one plays audio from SD card in which file is already selected in code,and in other code user selects file from sdcard from serial monitor ( an Arduino ide's serial terminal which sends and receives data ) and after that file is being played by vs1053,and also volume and pause/resume/next file function can be done from the serial monitor.

First code works perfectly intended, but second one did not playing song and the song name problem comes up,
First code worked so I didn't think so that the code i found from french website might have problem, might be my Arduino ide not sure..
The French website :-
https://electroniqueamateur.blogspot.co ... s.html?m=1
There are 1st and 2nd code available,
And 1st and 2nd code also using baldram's library.
And I also found modified Library of Adafruit so gonna try that one.
And by the way the IC is sounding crystal clear in MP3 format, i'm wondering what the sound quality is gonna be in FLAC format.
Great IC.

- Jay
:idea: Trying to be an Engineer
Post Reply