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!");
}
}
}
-----------------------------------------------------------------------------------------------------
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