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