Need help with file system

headquaker
Posts: 20
Joined: Thu Sep 21, 2023 6:28 am

Need help with file system

Postby headquaker » Mon Oct 02, 2023 11:35 am

Hello,
I do not know CPP very well and have plenty of difficulties to make a working code.

My project is to do a sort of mp3 players but not only. The not only part will be "Histories".
I am able to play a MP3, I have plenty of code that work well to do next, pause, etc..

The problem is that code was working with a SD structure where all MP3 were in root.
What i want is to be able to switch between folders and play mp3 inside them.
Could you give me some help ?

I know about connecttoSD and connecttoFS, i can't manage to make it working with FS and I don't want static path like here

Code: Select all

 
 String filePath = "/m/RandomFolder1/" + filename;
  audio.connecttoSD(filePath.c_str());
  
SD Card structure

Code: Select all

root
	root/h
	root/m
		root/m/RandomFolder1
			root/m/RandomFolder1-1.mp3
			root/m/RandomFolder1-2.mp3
			root/m/RandomFolder1-3.mp3
			...
		root/m/RandomFolder2
			root/m/RandomFolder2-1.mp3
			root/m/RandomFolder2-2.mp3
			root/m/RandomFolder2-3.mp3
			...
		root/m/RandomFolderX
			root/m/RandomFolderX-1.mp3
			root/m/RandomFolderX-2.mp3
			root/m/RandomFolderX-3.mp3
			...	

Code: Select all

#include "Arduino.h"
#include "Audio.h"
#include "FS.h"
#include "SPI.h"
#include "SD.h"
#include <Wire.h>
#include <vector>

#define TFT_BL 2

#define SD_SCK 12 // CLK
#define SD_MISO 13 // D0
#define SD_MOSI 11 // CMD
#define SD_CS 10 // CLK

#define I2S_DOUT 17
#define I2S_BCLK 0
#define I2S_LRC 18

const unsigned long VOL_CHANGE_DELAY = 500; // Delay between volume changes
const unsigned long DISPLAY_VOLUME_DURATION = 3000; // Display volume for 3 seconds

struct Music_info
{
  String name;
  int length;
  int runtime;
  int volume;
  int status;
  int mute_volume;
} music_info = {"", 0, 0, 0, 0, 0};

Audio audio;
File musical;
std::vector<String> mp3Files;
int currentSongIndex = -1;
bool isPlaying = false;

void setup()
{
  Serial.begin(115200);
  pin_init();
  sd_init();
  musical = SD.open("/m");
  if (!musical || !musical.isDirectory())
  {
    Serial.println("Failed to open /M directory");
    return;
  }


  // Load the list of MP3 files in the first album (subdirectory) under /M/
  loadMP3Files();

  // Check if there are MP3 files in the album
  if (mp3Files.size() > 0)
  {
    // Start playing the first MP3 file
    currentSongIndex = 0;
      audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(15); // Adjust the initial volume as needed
    playMP3File(mp3Files[currentSongIndex]);
  }
  else
  {
    Serial.println("No MP3 files found in the first album");
  }
}

void loop()
{
  //handleAudio();
  // Your other main loop code can go here
  audio.loop();
}

//---- Device init --------------------------------------------------
void pin_init()
{
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);
}

void sd_init()
{
  pinMode(SD_CS, OUTPUT);
  digitalWrite(SD_CS, HIGH);
  SPI.begin(SD_SCK, SD_MISO, SD_MOSI);
  SPI.setFrequency(400000);
  if (!SD.begin(SD_CS, SPI))
  {
    Serial.println("Card Mount Failed");
    while (1)
      ;
  }
  else
  {
    Serial.println("SD OK");
  }
}

void loadMP3Files()
{
  mp3Files.clear();
  File entry;

  // Open the first album (subdirectory) in /M/
  File album = musical.openNextFile();
  if (!album || !album.isDirectory())
  {
    Serial.println("No albums found in /M directory");
    return;
  }

  // Load the list of MP3 files in the first album
  while (File entry = album.openNextFile())
  {
    if (!entry.isDirectory() && String(entry.name()).endsWith(".mp3"))
    {
      mp3Files.push_back(String(entry.name()));
    }
  }
  album.rewindDirectory();
}

void playMP3File(const String &filename)
{
  Serial.println("Playing: " + filename);
  //Serial.println("Folder: " + SD.name());

  // Extract the name of the MP3 file without the extension
  int dotIndex = filename.lastIndexOf(".");
  if (dotIndex != -1)
  {
    String nameWithoutExtension = filename.substring(0, dotIndex);

    // Assuming 'music_info' is a struct or object with a 'name' member variable
    music_info.name = nameWithoutExtension;
  String filePath = "/m/RandomFolder1/" + filename;
  audio.connecttoSD(filePath.c_str());

    isPlaying = true;
  }
  else
  {
    Serial.println("Invalid filename");
  }
}



void handleAudio()
{
  if (isPlaying)
  {
    if (!audio.isRunning())
    {
      // Audio playback has finished
      Serial.println("Playback finished");
      isPlaying = false;

      // Add logic here to advance to the next MP3 file or perform other actions
      // For example, you can increment currentSongIndex and call playMP3File again
      if (currentSongIndex < mp3Files.size() - 1)
      {
        currentSongIndex++;
        playMP3File(mp3Files[currentSongIndex]);
      }
      else
      {
        Serial.println("No more MP3 files to play");
      }
    }
  }
}

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Need help with file system

Postby lbernstone » Mon Oct 02, 2023 4:44 pm

You can use https://github.com/espressif/arduino-es ... D_Test.ino to get a list of the files on the card (2 levels down as written). If you can see the file in there, then there is a problem in whatever "Audio.h" is.

headquaker
Posts: 20
Joined: Thu Sep 21, 2023 6:28 am

Re: Need help with file system

Postby headquaker » Tue Oct 03, 2023 9:11 am

I managed to solve all issues about that.

Who is online

Users browsing this forum: No registered users and 130 guests