From afe2df4dfa6d6858f5352f55e8e00ba05faf69fa Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Thu, 22 Dec 2022 18:50:33 +0100 Subject: Add mp3 player --- mp3player.cpp | 275 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mp3player.h | 69 +++++++++++++++ schack.ino | 15 ++++ 3 files changed, 359 insertions(+) create mode 100644 mp3player.cpp create mode 100644 mp3player.h create mode 100644 schack.ino diff --git a/mp3player.cpp b/mp3player.cpp new file mode 100644 index 0000000..b57c5a1 --- /dev/null +++ b/mp3player.cpp @@ -0,0 +1,275 @@ +#include "mp3player.h" + +MP3List::MP3List(File *dir, ListMode mode) { + Serial.println("MP3List::MP3List"); + this->mode = mode; + int size = countListElements(dir); + + list = new String[size]; + + Serial.println(size); + + listSize = size; + listCurrentPosition=0; + + insertElementsToList(dir); + debug(); +} + +MP3List::~MP3List() { + delete [] list; +} + +void MP3List::debug() { + Serial.println("============"); + for(int i=0; iname(); + + switch(mode) { + case ListMode::SECTIONS: + Serial.println("SECTIONS"); + return file->isDirectory(); + break; + + case ListMode::MUSIC_FILES: + Serial.println("MUSIC FILES"); + return fileName.endsWith(".mp3"); + break; + + default: + Serial.println("DEFAULT"); + return false; + break; + } +} + +int MP3List::countListElements(File *dir) { + dir->rewindDirectory(); + + if (!dir->isDirectory()) { + Serial.println("Not a directory"); + return 0; + } + + File file = dir->openNextFile(); + int elementsCount = 0; + while (file) { + if(canAdd(&file)) + elementsCount++; + + file.close(); + file = dir->openNextFile(); + } + return elementsCount; +} + +void MP3List::insertElementsToList(File *dir) { + dir->rewindDirectory(); + + Serial.println("MP3List::insertElementsToList"); + if (!dir->isDirectory()) { + Serial.println("Not a directory"); + return; + } + + File file = dir->openNextFile(); + int i=0; + while (file) { + if(canAdd(&file)) { + if (i > listSize) + continue; + list[i] = file.name(); + Serial.print("Dodaje: "); + Serial.print(file.name()); + Serial.print("\n"); + + Serial.print("na index: "); + Serial.print(i); + Serial.print("\n"); + i++; + } + file.close(); + file = dir->openNextFile(); + } +} + +String MP3List::getCurrent() { + Serial.print("list size: "); + Serial.print(listSize); + Serial.print("\n"); + + Serial.print("list current position: "); + Serial.print(listCurrentPosition); + Serial.print("\n"); + + if(listCurrentPosition < listSize) + return list[listCurrentPosition]; + return ""; +} + + +String MP3List::getNext() { + if(listCurrentPosition < listSize-1) + listCurrentPosition++; + else + listCurrentPosition = 0; + + return getCurrent(); +} + +String MP3List::getPrevious() { + if(listCurrentPosition > 0) + listCurrentPosition--; + else + listCurrentPosition = listSize-1; + + return getCurrent(); +} + + +MP3Player::MP3Player() { + Serial.println("MP3Player::MP3Player"); + + source = new AudioFileSourceSD(); + mp3 = new AudioGeneratorMP3(); + out = new AudioOutputI2SNoDAC(); + updateListSections(); +} + +MP3Player::~MP3Player() { + delete source; + delete mp3; + delete out; + + delete listSections; + delete listMusicFiles; +} + +void MP3Player::playMP3(String name) { + Serial.println("MP3Player::playMP3"); + Serial.println(name); + + if (mp3->isRunning()) + mp3->stop(); + + source->open(name.c_str()); + mp3->begin(source, out); +} + +void MP3Player::loop() { + if (mp3->isRunning()) + if (!mp3->loop()) mp3->stop(); +} + +void MP3Player::volumeUp() { + if(volume + volumeBy < 2.0) { + volume += volumeBy; + out->SetGain(volume); + } +} + +void MP3Player::volumeDown() { + if(volume - volumeBy > 0.0) { + volume -= volumeBy; + out->SetGain(volume); + } +} + +String MP3Player::getFilePath(String fileName) { + Serial.println("MP3Player::getFilePath"); + String currentSection = listSections->getCurrent(); + + if(currentSection == "" || fileName == "") + return ""; + return rootDir+"/"+currentSection+"/"+fileName; +} + +void MP3Player::nextFile() { + Serial.println("MP3Player::next"); + String current = listMusicFiles->getNext(); + playMP3(getFilePath(current)); +} + +void MP3Player::previousFile() { + Serial.println("MP3Player::previous"); + String current = listMusicFiles->getPrevious(); + playMP3(getFilePath(current)); +} + +void MP3Player::nextSection() { + Serial.println("MP3Player::nextS"); + listSections->getNext(); + updateListMusicFiles(); + nextFile(); +} + +void MP3Player::previousSection() { + Serial.println("MP3Player::previousS"); + listSections->getPrevious(); + updateListMusicFiles(); + nextFile(); +} + + +bool MP3Player::updateListSections() { + Serial.println("MP3Player::updateListSections"); + File root = SD.open(rootDir); + if (!root) { + Serial.println("Failed to open directory"); + return false; + } + + if (!root.isDirectory()) { + Serial.println("Not a directory"); + return false; + } + + delete listSections; + listSections = new MP3List(&root, ListMode::SECTIONS); + // root.rewindDirectory(); + return updateListMusicFiles(); +} + +bool MP3Player::updateListMusicFiles() { + Serial.println("MP3Player::updateMusicFiles"); + String currentSection = listSections->getCurrent(); + + Serial.println("CURRENT SECTION: "); + Serial.println(currentSection); + if(currentSection == "") + return false; + + File root = SD.open(rootDir + "/" + currentSection); + + if (!root) { + Serial.println("Failed to open directory"); + return false; + } + if (!root.isDirectory()) { + Serial.println("Not a directory"); + return false; + } + + delete listMusicFiles; + listMusicFiles = new MP3List(&root, ListMode::MUSIC_FILES); + // root.rewindDirectory(); + return true; +} + diff --git a/mp3player.h b/mp3player.h new file mode 100644 index 0000000..c5efabb --- /dev/null +++ b/mp3player.h @@ -0,0 +1,69 @@ +#ifndef MP3_PLAYER_H +#define MP3_PLAYER_H + +#include "AudioOutputI2SNoDAC.h" +#include "AudioGeneratorMP3.h" +#include "AudioFileSourceSD.h" +#include "AudioLogger.h" +#include "controller.h" +#include "SD.h" +#include + + +enum ListMode { + SECTIONS=0, + MUSIC_FILES=1 +}; + +class MP3List { + String *list; + int listSize = 0; + int listCurrentPosition = 0; + int countListElements(File*); + void insertElementsToList(File*); + bool canAdd(File*); + void debug(); + ListMode mode; + +public: + MP3List(File*, ListMode); + ~MP3List(); + String getCurrent(); + String getNext(); + String getPrevious(); +}; + + +class MP3Player : public MP3Controller { + AudioFileSourceSD *source; + AudioGeneratorMP3 *mp3; + AudioOutputI2SNoDAC *out; + + MP3List *listMusicFiles; + MP3List *listSections; + + String currentFolder; + float volume=2.0; + float const volumeBy = 0.3; + + String rootDir = "/"; + + void playMP3(String); + bool updateListSections(); + bool updateListMusicFiles(); + String getFilePath(String); + + + public: + MP3Player(); + ~MP3Player(); + void volumeUp(); + void volumeDown(); + void nextSection() override; + void previousSection() override; + void nextFile() override; + void previousFile() override; + void loop(); +}; + +#endif diff --git a/schack.ino b/schack.ino new file mode 100644 index 0000000..6efcf5c --- /dev/null +++ b/schack.ino @@ -0,0 +1,15 @@ +#include "mp3player.h" + +MP3Player *mp3Player; + +void setup() { + Serial.begin(9600); + SD.begin(SS); + + mp3Player = new MP3Player(); +} + + +void loop() { + mp3Player->loop(); +} -- cgit v1.2.3