arduino-audio-tools
AudioSourceSD.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 "SD.h"
8 #include "SPI.h"
9 
10 namespace audio_tools {
11 
12 
34 class AudioSourceSD : public AudioSource {
35 public:
37  AudioSourceSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
38  start_path = startFilePath;
39  exension = ext;
40  setup_index = setupIndex;
41  cs = chipSelect;
42  }
43 
44  virtual void begin() override {
45  TRACED();
46  if (!is_sd_setup) {
47  while (!SD.begin(cs)) {
48  LOGE("SD.begin cs=%d failed",cs);
49  delay(1000);
50  }
51  is_sd_setup = true;
52  }
53  idx.begin(start_path, exension, file_name_pattern);
54  idx_pos = 0;
55  }
56 
57  void end() {
58  SD.end();
59  is_sd_setup = false;
60  }
61 
62  virtual Stream *nextStream(int offset = 1) override {
63  LOGI("nextStream: %d", offset);
64  return selectStream(idx_pos + offset);
65  }
66 
67  virtual Stream *selectStream(int index) override {
68  LOGI("selectStream: %d", index);
69  idx_pos = index;
70  file_name = idx[index];
71  if (file_name==nullptr) return nullptr;
72  LOGI("Using file %s", file_name);
73  file = SD.open(file_name);
74  return file ? &file : nullptr;
75  }
76 
77  virtual Stream *selectStream(const char *path) override {
78  file.close();
79  file = SD.open(path);
80  file_name = file.name();
81  LOGI("-> selectStream: %s", path);
82  return file ? &file : nullptr;
83  }
84 
87  void setFileFilter(const char *filter) { file_name_pattern = filter; }
88 
90  int index() { return idx_pos; }
91 
93  const char *toStr() { return file_name; }
94 
95  // provides default setting go to the next
96  virtual bool isAutoNext() { return true; }
97 
99  virtual void setPath(const char *p) { start_path = p; }
100 
102  long size() { return idx.size();}
103 
104 protected:
105 #if defined(USE_SD_NO_NS)
106  SDDirect<SDClass, File> idx{SD};
107 #else
108  SDDirect<fs::SDFS,fs::File> idx{SD};
109 #endif
110  File file;
111  size_t idx_pos = 0;
112  const char *file_name;
113  const char *exension = nullptr;
114  const char *start_path = nullptr;
115  const char *file_name_pattern = "*";
116  bool setup_index = true;
117  bool is_sd_setup = false;
118  int cs;
119 
120 
121 };
122 
123 } // 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 SD card as data source. This class is based on the Arduino...
Definition: AudioSourceSD.h:34
int index()
Provides the current index position.
Definition: AudioSourceSD.h:90
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition: AudioSourceSD.h:102
const char * toStr()
provides the actual file name
Definition: AudioSourceSD.h:93
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceSD.h:44
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceSD.h:99
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceSD.h:96
AudioSourceSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition: AudioSourceSD.h:37
void setFileFilter(const char *filter)
Definition: AudioSourceSD.h:87
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceSD.h:62
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: AudioSourceSD.h:67
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceSD.h:77
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition: SDDirect.h:18
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