arduino-audio-tools
AudioSourceSDMMC.h
1 #pragma once
2 
3 #include "AudioLogger.h"
4 #include "AudioTools/CoreAudio/AudioSource.h"
5 #include "FS.h"
6 #include "SD_MMC.h"
7 #include "AudioTools/AudioLibs/SDDirect.h"
8 
9 namespace audio_tools {
10 
32 class AudioSourceSDMMC : public AudioSource {
33 public:
35  AudioSourceSDMMC(const char *startFilePath = "/", const char *ext = ".mp3", bool setupIndex=true) {
36  start_path = startFilePath;
37  exension = ext;
38  setup_index = setupIndex;
39  }
40 
41  virtual void begin() override {
42  TRACED();
43  if (!is_sd_setup) {
44  if (!SD_MMC.begin("/sdcard", true)) {
45  LOGE("SD_MMC.begin failed");
46  return;
47  }
48  is_sd_setup = true;
49  }
50  idx.begin(start_path, exension, file_name_pattern);
51  idx_pos = 0;
52  }
53 
54  void end() {
55  SD_MMC.end();
56  is_sd_setup = false;
57  }
58 
59 
60  virtual Stream *nextStream(int offset = 1) override {
61  LOGI("nextStream: %d", offset);
62  return selectStream(idx_pos + offset);
63  }
64 
65  virtual Stream *selectStream(int index) override {
66  LOGI("selectStream: %d", index);
67  idx_pos = index;
68  file_name = idx[index];
69  if (file_name==nullptr) return nullptr;
70  LOGI("Using file %s", file_name);
71  file = SD_MMC.open(file_name);
72  return file ? &file : nullptr;
73  }
74 
75  virtual Stream *selectStream(const char *path) override {
76  file.close();
77  file = SD_MMC.open(path);
78  file_name = file.name();
79  LOGI("-> selectStream: %s", path);
80  return file ? &file : nullptr;
81  }
82 
85  void setFileFilter(const char *filter) { file_name_pattern = filter; }
86 
88  int index() { return idx_pos; }
89 
91  const char *toStr() { return file_name; }
92 
93  // provides default setting go to the next
94  virtual bool isAutoNext() { return true; }
95 
97  virtual void setPath(const char *p) { start_path = p; }
98 
100  long size() { return idx.size();}
101 
102 protected:
103  SDDirect<fs::SDMMCFS,fs::File> idx{SD_MMC};
104  File file;
105  size_t idx_pos = 0;
106  const char *file_name;
107  const char *exension = nullptr;
108  const char *start_path = nullptr;
109  const char *file_name_pattern = "*";
110  bool setup_index = true;
111  bool is_sd_setup = false;
112 };
113 
114 } // 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 SD card as data source. This class is based on the Arduino...
Definition: AudioSourceSDMMC.h:32
int index()
Provides the current index position.
Definition: AudioSourceSDMMC.h:88
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition: AudioSourceSDMMC.h:100
const char * toStr()
provides the actual file name
Definition: AudioSourceSDMMC.h:91
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceSDMMC.h:41
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceSDMMC.h:97
AudioSourceSDMMC(const char *startFilePath="/", const char *ext=".mp3", bool setupIndex=true)
Default constructor.
Definition: AudioSourceSDMMC.h:35
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceSDMMC.h:94
void setFileFilter(const char *filter)
Definition: AudioSourceSDMMC.h:85
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceSDMMC.h:60
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: AudioSourceSDMMC.h:65
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceSDMMC.h:75
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:868