arduino-audio-tools
AudioSourceIdxSDFAT.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 
5 #include <SPI.h>
6 #include <SdFat.h>
7 #include "AudioLogger.h"
8 #include "AudioBasic/StrExt.h"
9 #include "AudioTools/AudioSource.h"
10 
11 #define USE_SDFAT
12 #include "AudioLibs/SDIndex.h"
13 
14 
15 // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
16 // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
17 #ifndef SD_FAT_TYPE
18 #define SD_FAT_TYPE 1
19 #endif
20 // Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. (40?)
21 #define SPI_CLOCK SD_SCK_MHZ(50)
22 #if SD_FAT_TYPE == 0
23  typedef SdFat AudioFs;
24  typedef File AudioFile;
25 #elif SD_FAT_TYPE == 1
26  typedef SdFat32 AudioFs;
27  typedef File32 AudioFile;
28 #elif SD_FAT_TYPE == 2
29  typedef SdExFat AudioFs;
30  typedef ExFile AudioFile;
31 #elif SD_FAT_TYPE == 3
32  typedef SdFs AudioFs;
33  typedef FsFile AudioFile;
34 #endif
35 
36 
37 namespace audio_tools {
47 public:
49  AudioSourceIdxSDFAT(const char* startFilePath = "/", const char* ext = ".mp3", int chipSelect = PIN_CS, int speedMHz = 10, bool setupIndex=true) {
50  TRACED();
51  LOGI("SD chipSelect: %d", chipSelect);
52  LOGI("SD speedMHz: %d", speedMHz);
53  LOGI("ext: %s", ext);
54  p_cfg = new SdSpiConfig(chipSelect, DEDICATED_SPI, SD_SCK_MHZ(speedMHz));
55  owns_cfg = true;
56  start_path = startFilePath;
57  exension = ext;
58  setup_index = setupIndex;
59  }
60 
62  AudioSourceSDFAT(const char* startFilePath, const char* ext, SdSpiConfig &config, bool setupIndex=true) {
63  TRACED();
64  p_cfg = &config;
65  owns_cfg = false;
66  start_path = startFilePath;
67  exension = ext;
68  setup_index = setupIndex;
69  }
70 
71 
72  virtual void begin() override {
73  TRACED();
74  if (!is_sd_setup) {
75  if (!sd.begin(*p_cfg)) {
76  LOGE("sd.begin failed");
77  return;
78  }
79  is_sd_setup = true;
80  }
81  idx.begin(start_path, exension, file_name_pattern, setup_index);
82  idx_pos = 0;
83  }
84 
85  void end() {
86  #ifdef ESP32
87  sd.end();
88  #endif
89  is_sd_setup = false;
90  }
91 
92  virtual Stream *nextStream(int offset = 1) override {
93  LOGI("nextStream: %d", offset);
94  return selectStream(idx_pos + offset);
95  }
96 
97  virtual Stream *selectStream(int index) override {
98  LOGI("selectStream: %d", index);
99  idx_pos = index;
100  return selectStream(idx[index]);
101  }
102 
103  virtual Stream *selectStream(const char *path) override {
104  file.close();
105  if (path==nullptr){
106  LOGE("Filename is null")
107  return nullptr;
108  }
109 
110  AudioFile new_file;
111  if (!new_file.open(path, O_RDONLY)){
112  LOGE("Open error: '%s'", path);
113  }
114 
115  LOGI("-> selectStream: %s", path);
116  file = new_file;
117  return &file;
118  }
119 
122  void setFileFilter(const char *filter) { file_name_pattern = filter; }
123 
125  int index() { return idx_pos; }
126 
128  const char *toStr() { return file_name; }
129 
130  // provides default setting go to the next
131  virtual bool isAutoNext() { return true; }
132 
134  virtual void setPath(const char *p) { start_path = p; }
135 
137  long size() { return idx.size();}
138 
139 protected:
140  SdSpiConfig *p_cfg = nullptr;
141  AudioFs sd;
142  AudioFile file;
144  size_t idx_pos = 0;
145  char file_name[MAX_FILE_LEN];
146  const char *exension = nullptr;
147  const char *start_path = nullptr;
148  const char *file_name_pattern = "*";
149  bool setup_index = true;
150  bool is_sd_setup = false;
151  int cs;
152  bool owns_cfg=false;
153 
154  const char* getFileName(AudioFile&file){
155  static char name[MAX_FILE_LEN];
156  file.getName(name,MAX_FILE_LEN);
157  return name;
158  }
159 
160 };
161 
162 } // 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: AudioSourceIdxSDFAT.h:46
int index()
Provides the current index position.
Definition: AudioSourceIdxSDFAT.h:125
AudioSourceSDFAT(const char *startFilePath, const char *ext, SdSpiConfig &config, bool setupIndex=true)
Costructor with SdSpiConfig.
Definition: AudioSourceIdxSDFAT.h:62
long size()
Provides the number of files (The max index is size()-1)
Definition: AudioSourceIdxSDFAT.h:137
const char * toStr()
provides the actual file name
Definition: AudioSourceIdxSDFAT.h:128
virtual void begin() override
Reset actual stream and move to root.
Definition: AudioSourceIdxSDFAT.h:72
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition: AudioSourceIdxSDFAT.h:134
virtual bool isAutoNext()
Returns default setting go to the next.
Definition: AudioSourceIdxSDFAT.h:131
AudioSourceIdxSDFAT(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, int speedMHz=10, bool setupIndex=true)
Default constructor.
Definition: AudioSourceIdxSDFAT.h:49
void setFileFilter(const char *filter)
Definition: AudioSourceIdxSDFAT.h:122
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition: AudioSourceIdxSDFAT.h:92
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: AudioSourceIdxSDFAT.h:97
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition: AudioSourceIdxSDFAT.h:103
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10