arduino-audio-tools
AudioSourceSD.h
1 #pragma once
2 #include "SD.h"
3 #include "SPI.h"
4 #include "AudioLogger.h"
5 #include "AudioTools/CoreAudio/AudioSource.h"
6 #include "AudioTools/AudioLibs/SDDirect.h"
7 
8 namespace audio_tools {
9 
10 
32 class AudioSourceSD : public AudioSource {
33 public:
35  AudioSourceSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
36  start_path = startFilePath;
37  extension = ext;
38  setup_index = setupIndex;
39  p_spi = &SPI;
40  cs = chipSelect;
41  }
42 
43 #ifdef USE_SD_SUPPORTS_SPI
44 
45  // Pass your own spi instance, in case you need a dedicated one
46  AudioSourceSD(const char *startFilePath, const char *ext, int chipSelect, SPIClass &spiInstance, bool setupIndex=true) {
47  start_path = startFilePath;
48  extension = ext;
49  setup_index = setupIndex;
50  p_spi = &spiInstance;
51  cs = chipSelect;
52  }
53 
54 #endif
55 
56  virtual void begin() override {
57  TRACED();
58  if (!is_sd_setup) {
59  while (!start_sd()) {
60  LOGE("SD.begin cs=%d failed", cs);
61  delay(1000);
62  }
63  is_sd_setup = true;
64  }
65  idx.begin(start_path, extension, file_name_pattern);
66  idx_pos = 0;
67  }
68 
69  void end() {
70  SD.end();
71  is_sd_setup = false;
72  }
73 
74  virtual Stream *nextStream(int offset = 1) override {
75  LOGI("nextStream: %d", offset);
76  return selectStream(idx_pos + offset);
77  }
78 
79  virtual Stream *selectStream(int index) override {
80  LOGI("selectStream: %d", index);
81  idx_pos = index;
82  file_name = idx[index];
83  if (file_name==nullptr) return nullptr;
84  LOGI("Using file %s", file_name);
85  file = SD.open(file_name);
86  return file ? &file : nullptr;
87  }
88 
89  virtual Stream *selectStream(const char *path) override {
90  file.close();
91  file = SD.open(path);
92  file_name = file.name();
93  LOGI("-> selectStream: %s", path);
94  return file ? &file : nullptr;
95  }
96 
99  void setFileFilter(const char *filter) { file_name_pattern = filter; }
100 
102  int index() { return idx_pos; }
103 
105  const char *toStr() { return file_name; }
106 
107  // provides default setting go to the next
108  virtual bool isAutoNext() { return true; }
109 
111  virtual void setPath(const char *p) { start_path = p; }
112 
114  long size() { return idx.size();}
115 
116 protected:
117 #if defined(USE_SD_NO_NS)
118  SDDirect<SDClass, File> idx{SD};
119 #else
120  SDDirect<fs::SDFS,fs::File> idx{SD};
121 #endif
122  File file;
123  size_t idx_pos = 0;
124  const char *file_name;
125  const char *extension = nullptr;
126  const char *start_path = nullptr;
127  const char *file_name_pattern = "*";
128  bool setup_index = true;
129  bool is_sd_setup = false;
130  int cs;
131  SPIClass *p_spi = nullptr;
132 
133  bool start_sd(){
134 #ifdef USE_SD_SUPPORTS_SPI
135  return SD.begin(cs, *p_spi);
136 #else
137  return SD.begin(cs);
138 #endif
139  }
140 
141 };
142 
143 } // 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: AudioSourceSD.h:32
int index()
Provides the current index position.
Definition: AudioSourceSD.h:102
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:114
const char * toStr()
provides the actual file name
Definition: AudioSourceSD.h:105
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceSD.h:56
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceSD.h:111
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceSD.h:108
AudioSourceSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition: AudioSourceSD.h:35
void setFileFilter(const char *filter)
Definition: AudioSourceSD.h:99
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceSD.h:74
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:79
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceSD.h:89
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition: SDDirect.h:21
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