arduino-audio-tools
AudioSourceIdxSD.h
1 #pragma once
2 #include <SPI.h>
3 #include <SD.h>
4 #include "AudioLogger.h"
5 #include "AudioBasic/StrExt.h"
6 #include "AudioTools/AudioSource.h"
7 #include "AudioLibs/SDIndex.h"
8 
9 namespace audio_tools {
10 
32 class AudioSourceIdxSD : public AudioSource {
33 public:
35  AudioSourceIdxSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
36  start_path = startFilePath;
37  exension = ext;
38  setup_index = setupIndex;
39  cs = chipSelect;
40  }
41 
42  virtual void begin() override {
43  TRACED();
44  if (!is_sd_setup) {
45  while (!SD.begin(cs)) {
46  LOGW("SD.begin cs=%d failed", cs);
47  delay(500);
48  }
49  is_sd_setup = true;
50  }
51  idx.begin(start_path, exension, file_name_pattern, setup_index);
52  idx_pos = 0;
53  }
54 
55  void end() {
56  SD.end();
57  is_sd_setup = false;
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.open(file_name);
72  return file ? &file : nullptr;
73  }
74 
75  virtual Stream *selectStream(const char *path) override {
76  file.close();
77  file = SD.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 #if defined(USE_SD_NO_NS)
104  SDIndex<SDClass, File> idx{SD};
105 #else
106  SDIndex<fs::SDFS,fs::File> idx{SD};
107 #endif
108  File file;
109  size_t idx_pos = 0;
110  const char *file_name;
111  const char *exension = nullptr;
112  const char *start_path = nullptr;
113  const char *file_name_pattern = "*";
114  bool setup_index = true;
115  bool is_sd_setup = false;
116  int cs;
117 
118 
119 };
120 
121 } // 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: AudioSourceIdxSD.h:32
int index()
Provides the current index position.
Definition: AudioSourceIdxSD.h:88
long size()
Provides the number of files (The max index is size()-1)
Definition: AudioSourceIdxSD.h:100
const char * toStr()
provides the actual file name
Definition: AudioSourceIdxSD.h:91
AudioSourceIdxSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition: AudioSourceIdxSD.h:35
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceIdxSD.h:42
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceIdxSD.h:97
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceIdxSD.h:94
void setFileFilter(const char *filter)
Definition: AudioSourceIdxSD.h:85
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceIdxSD.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: AudioSourceIdxSD.h:65
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceIdxSD.h:75
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition: SDIndex.h:18
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