arduino-audio-tools
AudioSourceLittleFS.h
1 #pragma once
2 
3 #include "AudioLogger.h"
4 #include "AudioTools/CoreAudio/AudioSource.h"
5 #include "AudioTools/AudioLibs/SDDirect.h"
6 #include <LittleFS.h>
7 
8 namespace audio_tools {
9 
17 public:
19  AudioSourceLittleFS(const char *startFilePath = "/", const char *ext = ".mp3") {
20  start_path = startFilePath;
21  exension = ext;
22  }
23 
24  virtual void begin() override {
25  TRACED();
26  if (!is_sd_setup) {
27  while (!LittleFS.begin()) {
28  LOGE("LittleFS.begin failed");
29  delay(1000);
30  }
31  is_sd_setup = true;
32  }
33  idx.begin(start_path, exension, file_name_pattern);
34  idx_pos = 0;
35  }
36 
37  void end() {
38  LittleFS.end();
39  is_sd_setup = false;
40  }
41 
42  virtual Stream *nextStream(int offset = 1) override {
43  LOGI("nextStream: %d", offset);
44  return selectStream(idx_pos + offset);
45  }
46 
47  virtual Stream *selectStream(int index) override {
48  LOGI("selectStream: %d", index);
49  idx_pos = index;
50  file_name = idx[index];
51  if (file_name==nullptr) return nullptr;
52  LOGI("Using file %s", file_name);
53  file = LittleFS.open(file_name,"r");
54  return file ? &file : nullptr;
55  }
56 
57  virtual Stream *selectStream(const char *path) override {
58  file.close();
59  file = LittleFS.open(path,"r");
60  file_name = file.name();
61  LOGI("-> selectStream: %s", path);
62  return file ? &file : nullptr;
63  }
64 
67  void setFileFilter(const char *filter) { file_name_pattern = filter; }
68 
70  int index() { return idx_pos; }
71 
73  const char *toStr() { return file_name; }
74 
75  // provides default setting go to the next
76  virtual bool isAutoNext() { return true; }
77 
79  virtual void setPath(const char *p) { start_path = p; }
80 
82  long size() { return idx.size();}
83 
84 protected:
85 #ifdef RP2040_HOWER
86  SDDirect<FS,File> idx{LittleFS};
87 #else
88  SDDirect<fs::LittleFSFS,fs::File> idx{LittleFS};
89 #endif
90  File file;
91  size_t idx_pos = 0;
92  const char *file_name;
93  const char *exension = nullptr;
94  const char *start_path = nullptr;
95  const char *file_name_pattern = "*";
96  bool is_sd_setup = false;
97 };
98 
99 } // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition: AudioSource.h:13
ESP32 AudioSource for AudioPlayer using an the LittleFS file system.
Definition: AudioSourceLittleFS.h:16
int index()
Provides the current index position.
Definition: AudioSourceLittleFS.h:70
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:82
const char * toStr()
provides the actual file name
Definition: AudioSourceLittleFS.h:73
AudioSourceLittleFS(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition: AudioSourceLittleFS.h:19
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceLittleFS.h:24
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceLittleFS.h:79
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceLittleFS.h:76
void setFileFilter(const char *filter)
Definition: AudioSourceLittleFS.h:67
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceLittleFS.h:42
virtual Stream * selectStream(int index) override
Returns audio stream at the indicated index (the index is zero based, so the first value is 0!...
Definition: AudioSourceLittleFS.h:47
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceLittleFS.h:57
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition: SDDirect.h:21
long size()
Provides the number of files.
Definition: SDDirect.h:49
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823