arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceLittleFS.h
Go to the documentation of this file.
1#pragma once
2
3#include <LittleFS.h>
4#include "AudioLogger.h"
7
8namespace audio_tools {
9
18public:
20 AudioSourceLittleFS(const char *startFilePath = "/", const char *ext = ".mp3") {
22 exension = ext;
23 }
24
25 virtual bool begin() override {
26 TRACED();
27 if (!is_sd_setup) {
28 int retry = 10;
29 while (!LittleFS.begin()) {
30 LOGE("LittleFS.begin failed");
31 delay(500);
32 if (--retry >= 0) {
33 return false;
34 }
35 }
36 is_sd_setup = true;
37 }
39 idx_pos = 0;
40 return is_sd_setup;
41 }
42
43 void end() {
44 LittleFS.end();
45 is_sd_setup = false;
46 }
47
48 virtual Stream *nextStream(int offset = 1) override {
49 LOGI("nextStream: %d", offset);
50 return selectStream(idx_pos + offset);
51 }
52
53 virtual Stream *selectStream(int index) override {
54 LOGI("selectStream: %d", index);
55 idx_pos = index;
57 if (file_name==nullptr) return nullptr;
58 LOGI("Using file %s", file_name);
60 return file ? &file : nullptr;
61 }
62
63 virtual Stream *selectStream(const char *path) override {
64 file.close();
65 file = LittleFS.open(path,"r");
67 LOGI("-> selectStream: %s", path);
68 return file ? &file : nullptr;
69 }
70
73 void setFileFilter(const char *filter) { file_name_pattern = filter; }
74
76 int index() { return idx_pos; }
77
79 const char *toStr() { return file_name; }
80
81 // provides default setting go to the next
82 virtual bool isAutoNext() { return true; }
83
85 virtual void setPath(const char *p) { start_path = p; }
86
88 long size() { return idx.size();}
89
90protected:
91#ifdef RP2040_HOWER
93#else
95#endif
97 size_t idx_pos = 0;
98 const char *file_name;
99 const char *exension = nullptr;
100 const char *start_path = nullptr;
101 const char *file_name_pattern = "*";
102 bool is_sd_setup = false;
103};
104
105} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:136
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:17
ESP32 AudioSource for AudioPlayer using an the LittleFS file system.
Definition AudioSourceLittleFS.h:17
int index()
Provides the current index position.
Definition AudioSourceLittleFS.h:76
const char * exension
Definition AudioSourceLittleFS.h:99
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition AudioSourceLittleFS.h:88
bool is_sd_setup
Definition AudioSourceLittleFS.h:102
virtual Stream * selectStream(int index) override
Definition AudioSourceLittleFS.h:53
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceLittleFS.h:25
File file
Definition AudioSourceLittleFS.h:96
size_t idx_pos
Definition AudioSourceLittleFS.h:97
AudioSourceLittleFS(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition AudioSourceLittleFS.h:20
SDDirect< fs::LittleFSFS, fs::File > idx
Definition AudioSourceLittleFS.h:94
const char * start_path
Definition AudioSourceLittleFS.h:100
const char * toStr()
provides the actual file name
Definition AudioSourceLittleFS.h:79
const char * file_name
Definition AudioSourceLittleFS.h:98
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceLittleFS.h:85
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceLittleFS.h:82
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceLittleFS.h:48
void end()
Definition AudioSourceLittleFS.h:43
void setFileFilter(const char *filter)
Definition AudioSourceLittleFS.h:73
const char * file_name_pattern
Definition AudioSourceLittleFS.h:101
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceLittleFS.h:63
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition SDDirect.h:22
long size()
Provides the number of files.
Definition SDDirect.h:50
void begin(const char *startDir, const char *extension, const char *file_name_pattern)
Definition SDDirect.h:26
Arduino File API for Zephyr.
Definition ZephyrFile.h:23
void close()
Definition ZephyrFile.h:67
const char * name() const
Definition ZephyrFile.h:200
bool open(const char *file_path, fs_mode_t mode=FS_O_READ)
Definition ZephyrFile.h:44
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(uint32_t ms)
Definition Arduino.h:255
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508