arduino-audio-tools
Loading...
Searching...
No Matches
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// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. (40?)
14#define SPI_CLOCK SD_SCK_MHZ(50)
15
16namespace audio_tools {
27template <typename AudioFs = SdFat32, typename AudioFile = File32>
29 public:
31 AudioSourceSDFAT(const char *startFilePath = "/", const char *ext = ".mp3",
32 int chipSelect = PIN_CS, int speedMHz = 10,
33 int spi_mode = DEDICATED_SPI, bool setupIndex = true) {
34 TRACED();
35 LOGI("SD chipSelect: %d", chipSelect);
36 LOGI("SD speedMHz: %d", speedMHz);
37 LOGI("ext: %s", ext);
38 p_cfg = new SdSpiConfig(chipSelect, spi_mode, SD_SCK_MHZ(speedMHz));
39 owns_cfg = true;
40 start_path = startFilePath;
41 exension = ext;
42 setup_index = setupIndex;
43 }
44
46 AudioSourceSDFAT(const char *startFilePath, const char *ext,
47 SdSpiConfig &config, bool setupIndex = true) {
48 TRACED();
49 p_cfg = &config;
50 owns_cfg = false;
51 start_path = startFilePath;
52 exension = ext;
53 setup_index = setupIndex;
54 }
55
57 AudioSourceSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex = true){
58 TRACED();
59 sd = fs;
60 p_cfg = nullptr;
61 owns_cfg = false;
62 start_path = startFilePath;
63 exension = ext;
64 setup_index = setupIndex;
65 is_sd_setup = true;
66 // since we expect an open fs we do not close it
67 is_close_sd = false;
68 }
69
70 virtual ~AudioSourceSDFAT() {
71 end();
72 if (owns_cfg) delete (p_cfg);
73 }
74
75 virtual bool begin() override {
76 TRACED();
77 if (!is_sd_setup) {
78 if (!sd.begin(*p_cfg)) {
79 LOGE("sd.begin failed");
80 return false;
81 }
82 is_sd_setup = true;
83 }
84 idx.begin(start_path, exension, file_name_pattern);
85 idx_pos = 0;
86 return is_sd_setup;
87 }
88
89 void end() {
90 file.close();
91
92 if (is_sd_setup) {
93 TRACEI();
94#ifdef ESP32
95 if (is_close_sd) sd.end();
96#endif
97 is_sd_setup = false;
98 }
99 }
100
101 virtual Stream *nextStream(int offset = 1) override {
102 LOGI("nextStream: %d", offset);
103 return selectStream(idx_pos + offset);
104 }
105
106 virtual Stream *selectStream(int index) override {
107 LOGI("selectStream SDFAT: %d", index);
108 if (index > -1) { // avoid invalid position
109 idx_pos = index;
110 }
111 return selectStream(idx[idx_pos]);
112 }
113
114 virtual Stream *selectStream(const char *path) override {
115 file.close();
116 if (path == nullptr) {
117 LOGE("Filename is null")
118 return nullptr;
119 }
120
121 // AudioFile new_file;
122 if (!file.open(path, O_RDONLY)) {
123 LOGE("Open error: '%s'", path);
124 }
125
126 LOGI("-> selectStream: %s", path);
127 strncpy(file_name, path, MAX_FILE_LEN);
128 // file = new_file;
129 return &file;
130 }
131
134 void setFileFilter(const char *filter) { file_name_pattern = filter; }
135
137 int index() { return idx_pos; }
138
140 const char *toStr() { return file_name; }
141
142 // provides default setting go to the next
143 virtual bool isAutoNext() { return true; }
144
146 virtual void setPath(const char *p) { start_path = p; }
147
150 long size() { return idx.size(); }
151
153 AudioFs &getAudioFs() { return sd; }
154
155 protected:
156 SdSpiConfig *p_cfg = nullptr;
157 AudioFs sd;
158 AudioFile file;
160 size_t idx_pos = 0;
161 char file_name[MAX_FILE_LEN];
162 const char *exension = nullptr;
163 const char *start_path = nullptr;
164 const char *file_name_pattern = "*";
165 int cs;
166 bool setup_index = true;
167 bool owns_cfg = false;
168 bool is_sd_setup = false;
169 bool is_close_sd = true;
170
171 const char *getFileName(AudioFile &file) {
172 static char name[MAX_FILE_LEN];
173 file.getName(name, MAX_FILE_LEN);
174 return name;
175 }
176};
177
178} // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:16
ESP32 AudioSource for AudioPlayer using an SD card as data source. This class is based on the Arduino...
Definition AudioSourceSDFAT.h:28
int index()
Provides the current index position.
Definition AudioSourceSDFAT.h:137
AudioSourceSDFAT(const char *startFilePath, const char *ext, SdSpiConfig &config, bool setupIndex=true)
Costructor with SdSpiConfig.
Definition AudioSourceSDFAT.h:46
long size()
Definition AudioSourceSDFAT.h:150
virtual Stream * selectStream(int index) override
Definition AudioSourceSDFAT.h:106
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSDFAT.h:75
AudioSourceSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex=true)
Constructor for providing an open FS.
Definition AudioSourceSDFAT.h:57
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:31
AudioFs & getAudioFs()
provides access to the AudioFs object
Definition AudioSourceSDFAT.h:153
const char * toStr()
provides the actual file name
Definition AudioSourceSDFAT.h:140
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSDFAT.h:146
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSDFAT.h:143
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSDFAT.h:101
void setFileFilter(const char *filter)
Definition AudioSourceSDFAT.h:134
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSDFAT.h:114
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition SDDirect.h:22
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10