arduino-audio-tools
AudioSourceSPIFFS.h
1 #pragma once
2 
3 #include "AudioBasic/StrExt.h"
4 #include "AudioLogger.h"
5 #include "AudioTools/AudioSource.h"
6 #include "AudioLibs/SDDirect.h"
7 #include <SPIFFS.h>
8 
9 namespace audio_tools {
10 
18 public:
20  AudioSourceSPIFFS(const char *startFilePath = "/", const char *ext = ".mp3") {
21  start_path = startFilePath;
22  exension = ext;
23  }
24 
25  virtual void begin() override {
26  TRACED();
27  if (!is_sd_setup) {
28  while (!SPIFFS.begin()) {
29  LOGE("SPIFFS.begin failed");
30  delay(1000);
31  }
32  is_sd_setup = true;
33  }
34  idx.begin(start_path, exension, file_name_pattern);
35  idx_pos = 0;
36  }
37 
38  void end() {
39  SPIFFS.end();
40  is_sd_setup = false;
41  }
42 
43  virtual Stream *nextStream(int offset = 1) override {
44  LOGI("nextStream: %d", offset);
45  return selectStream(idx_pos + offset);
46  }
47 
48  virtual Stream *selectStream(int index) override {
49  LOGI("selectStream: %d", index);
50  idx_pos = index;
51  file_name = idx[index];
52  if (file_name==nullptr) return nullptr;
53  LOGI("Using file %s", file_name);
54  file = SPIFFS.open(file_name);
55  return file ? &file : nullptr;
56  }
57 
58  virtual Stream *selectStream(const char *path) override {
59  file.close();
60  file = SPIFFS.open(path);
61  file_name = file.name();
62  LOGI("-> selectStream: %s", path);
63  return file ? &file : nullptr;
64  }
65 
68  void setFileFilter(const char *filter) { file_name_pattern = filter; }
69 
71  int index() { return idx_pos; }
72 
74  const char *toStr() { return file_name; }
75 
76  // provides default setting go to the next
77  virtual bool isAutoNext() { return true; }
78 
80  virtual void setPath(const char *p) { start_path = p; }
81 
83  long size() { return idx.size();}
84 
85 protected:
87  File file;
88  size_t idx_pos = 0;
89  const char *file_name;
90  const char *exension = nullptr;
91  const char *start_path = nullptr;
92  const char *file_name_pattern = "*";
93  bool is_sd_setup = false;
94 };
95 
96 } // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition: AudioSource.h:12
ESP32 AudioSource for AudioPlayer using an the SPIFFS file system.
Definition: AudioSourceSPIFFS.h:17
int index()
Provides the current index position.
Definition: AudioSourceSPIFFS.h:71
AudioSourceSPIFFS(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition: AudioSourceSPIFFS.h:20
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:83
const char * toStr()
provides the actual file name
Definition: AudioSourceSPIFFS.h:74
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceSPIFFS.h:25
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceSPIFFS.h:80
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceSPIFFS.h:77
void setFileFilter(const char *filter)
Definition: AudioSourceSPIFFS.h:68
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceSPIFFS.h:43
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:48
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceSPIFFS.h:58
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: AnalogAudio.h:10
void delay(uint32_t ms)
Waits for the indicated milliseconds.
Definition: Millis.h:11