arduino-audio-tools
AudioSourceSPIFFS.h
1 #pragma once
2 
3 #include "AudioLogger.h"
4 #include "AudioTools/CoreAudio/AudioSource.h"
5 #include "AudioTools/AudioLibs/SDDirect.h"
6 #include <SPIFFS.h>
7 
8 namespace audio_tools {
9 
17 public:
19  AudioSourceSPIFFS(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 (!SPIFFS.begin()) {
28  LOGE("SPIFFS.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  SPIFFS.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 = SPIFFS.open(file_name);
54  return file ? &file : nullptr;
55  }
56 
57  virtual Stream *selectStream(const char *path) override {
58  file.close();
59  file = SPIFFS.open(path);
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:
86  File file;
87  size_t idx_pos = 0;
88  const char *file_name;
89  const char *exension = nullptr;
90  const char *start_path = nullptr;
91  const char *file_name_pattern = "*";
92  bool is_sd_setup = false;
93 };
94 
95 } // 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 SPIFFS file system.
Definition: AudioSourceSPIFFS.h:16
int index()
Provides the current index position.
Definition: AudioSourceSPIFFS.h:70
AudioSourceSPIFFS(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition: AudioSourceSPIFFS.h:19
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition: AudioSourceSPIFFS.h:82
const char * toStr()
provides the actual file name
Definition: AudioSourceSPIFFS.h:73
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceSPIFFS.h:24
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceSPIFFS.h:79
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceSPIFFS.h:76
void setFileFilter(const char *filter)
Definition: AudioSourceSPIFFS.h:67
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceSPIFFS.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: AudioSourceSPIFFS.h:47
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceSPIFFS.h:57
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