arduino-audio-tools
AudioSourceIdxSDMMC.h
1 #pragma once
2 
3 #include <FS.h>
4 #include <SD_MMC.h>
5 #include "AudioLogger.h"
6 #include "AudioTools/CoreAudio/AudioSource.h"
7 #include "AudioTools/AudioLibs/SDIndex.h"
8 
9 namespace audio_tools {
10 
33 public:
35  AudioSourceIdxSDMMC(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, setup_index);
51  idx_pos = 0;
52  }
53 
54  void end() {
55  SD_MMC.end();
56  is_sd_setup = false;
57  }
58 
59  virtual Stream *nextStream(int offset = 1) override {
60  LOGI("nextStream: %d", offset);
61  return selectStream(idx_pos + offset);
62  }
63 
64  virtual Stream *selectStream(int index) override {
65  LOGI("selectStream: %d", index);
66  idx_pos = index;
67  file_name = idx[index];
68  if (file_name==nullptr) return nullptr;
69  LOGI("Using file %s", file_name);
70  file = SD_MMC.open(file_name);
71  return file ? &file : nullptr;
72  }
73 
74  virtual Stream *selectStream(const char *path) override {
75  file.close();
76  file = SD_MMC.open(path);
77  file_name = file.name();
78  LOGI("-> selectStream: %s", path);
79  return file ? &file : nullptr;
80  }
81 
84  void setFileFilter(const char *filter) { file_name_pattern = filter; }
85 
87  int index() { return idx_pos; }
88 
90  const char *toStr() { return file_name; }
91 
92  // provides default setting go to the next
93  virtual bool isAutoNext() { return true; }
94 
96  virtual void setPath(const char *p) { start_path = p; }
97 
99  long size() { return idx.size();}
100 
101 protected:
102  SDIndex<fs::SDMMCFS,fs::File> idx{SD_MMC};
103  File file;
104  size_t idx_pos = 0;
105  const char *file_name;
106  const char *exension = nullptr;
107  const char *start_path = nullptr;
108  const char *file_name_pattern = "*";
109  bool setup_index = true;
110  bool is_sd_setup = false;
111 };
112 
113 } // 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: AudioSourceIdxSDMMC.h:32
int index()
Provides the current index position.
Definition: AudioSourceIdxSDMMC.h:87
long size()
Provides the number of files (The max index is size()-1)
Definition: AudioSourceIdxSDMMC.h:99
const char * toStr()
provides the actual file name
Definition: AudioSourceIdxSDMMC.h:90
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceIdxSDMMC.h:41
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceIdxSDMMC.h:96
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceIdxSDMMC.h:93
void setFileFilter(const char *filter)
Definition: AudioSourceIdxSDMMC.h:84
AudioSourceIdxSDMMC(const char *startFilePath="/", const char *ext=".mp3", bool setupIndex=true)
Default constructor.
Definition: AudioSourceIdxSDMMC.h:35
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceIdxSDMMC.h:59
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: AudioSourceIdxSDMMC.h:64
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceIdxSDMMC.h:74
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868