arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSourceSDFAT.h
1#pragma once
2
3#include <SPI.h>
4#include <SdFat.h>
5
6#include "AudioLogger.h"
7#include "AudioTools/Disk/AudioSource.h"
8#include "AudioToolsConfig.h"
9
10#define USE_SDFAT
11#include "AudioTools/Disk/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
22typedef SdFat AudioFs;
23typedef File AudioFile;
24#elif SD_FAT_TYPE == 1
25typedef SdFat32 AudioFs;
26typedef File32 AudioFile;
27#elif SD_FAT_TYPE == 2
28typedef SdExFat AudioFs;
29typedef ExFile AudioFile;
30#elif SD_FAT_TYPE == 3
31typedef SdFs AudioFs;
32typedef FsFile AudioFile;
33#endif
34
35namespace audio_tools {
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
74 AudioSourceSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex = true){
75 TRACED();
76 sd = fs;
77 p_cfg = nullptr;
78 owns_cfg = false;
79 start_path = startFilePath;
80 exension = ext;
81 setup_index = setupIndex;
82 is_sd_setup = true;
83 // since we expect an open fs we do not close it
84 is_close_sd = false;
85 }
86
87 virtual ~AudioSourceSDFAT() {
88 end();
89 if (owns_cfg) delete (p_cfg);
90 }
91
92 virtual void begin() override {
93 TRACED();
94 if (!is_sd_setup) {
95 if (!sd.begin(*p_cfg)) {
96 LOGE("sd.begin failed");
97 return;
98 }
99 is_sd_setup = true;
100 }
101 idx.begin(start_path, exension, file_name_pattern);
102 idx_pos = 0;
103 }
104
105 void end() {
106 if (is_sd_setup) {
107 TRACEI();
108#ifdef ESP32
109 if (is_close_sd) sd.end();
110#endif
111 is_sd_setup = false;
112 }
113 }
114
115 virtual Stream *nextStream(int offset = 1) override {
116 LOGI("nextStream: %d", offset);
117 return selectStream(idx_pos + offset);
118 }
119
120 virtual Stream *selectStream(int index) override {
121 LOGI("selectStream SDFAT: %d", index);
122 if (index > -1) { // avoid invalid position
123 idx_pos = index;
124 }
125 return selectStream(idx[idx_pos]);
126 }
127
128 virtual Stream *selectStream(const char *path) override {
129 file.close();
130 if (path == nullptr) {
131 LOGE("Filename is null")
132 return nullptr;
133 }
134
135 // AudioFile new_file;
136 if (!file.open(path, O_RDONLY)) {
137 LOGE("Open error: '%s'", path);
138 }
139
140 LOGI("-> selectStream: %s", path);
141 strncpy(file_name, path, MAX_FILE_LEN);
142 // file = new_file;
143 return &file;
144 }
145
148 void setFileFilter(const char *filter) { file_name_pattern = filter; }
149
151 int index() { return idx_pos; }
152
154 const char *toStr() { return file_name; }
155
156 // provides default setting go to the next
157 virtual bool isAutoNext() { return true; }
158
160 virtual void setPath(const char *p) { start_path = p; }
161
164 long size() { return idx.size(); }
165
167 AudioFs &getAudioFs() { return sd; }
168
169 protected:
170 SdSpiConfig *p_cfg = nullptr;
171 AudioFs sd;
172 AudioFile file;
174 size_t idx_pos = 0;
175 char file_name[MAX_FILE_LEN];
176 const char *exension = nullptr;
177 const char *start_path = nullptr;
178 const char *file_name_pattern = "*";
179 int cs;
180 bool setup_index = true;
181 bool owns_cfg = false;
182 bool is_sd_setup = false;
183 bool is_close_sd = true;
184
185 const char *getFileName(AudioFile &file) {
186 static char name[MAX_FILE_LEN];
187 file.getName(name, MAX_FILE_LEN);
188 return name;
189 }
190};
191
192} // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:15
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:151
AudioSourceSDFAT(const char *startFilePath, const char *ext, SdSpiConfig &config, bool setupIndex=true)
Costructor with SdSpiConfig.
Definition AudioSourceSDFAT.h:63
long size()
Definition AudioSourceSDFAT.h:164
virtual Stream * selectStream(int index) override
Definition AudioSourceSDFAT.h:120
AudioSourceSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex=true)
Constructor for providing an open FS.
Definition AudioSourceSDFAT.h:74
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
AudioFs & getAudioFs()
provides access to the AudioFs object
Definition AudioSourceSDFAT.h:167
virtual void begin() override
Reset actual stream and move to root.
Definition AudioSourceSDFAT.h:92
const char * toStr()
provides the actual file name
Definition AudioSourceSDFAT.h:154
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSDFAT.h:160
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSDFAT.h:157
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSDFAT.h:115
void setFileFilter(const char *filter)
Definition AudioSourceSDFAT.h:148
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition AudioSourceSDFAT.h:128
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition SDDirect.h:22
long size()
Provides the number of files.
Definition SDDirect.h:50
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10