arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceIdxSDFAT.h
Go to the documentation of this file.
1#pragma once
2
3#include <SPI.h>
4#include <SdFat.h>
5
6#include "AudioToolsConfig.h"
7#include "AudioLogger.h"
9
10#define USE_SDFAT
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 AudioSourceIdxSDFAT(const char *startFilePath = "/", const char *ext = ".mp3",
32 int chipSelect = PIN_CS, int speedMHz = 10,
33 bool setupIndex = true) {
34 TRACED();
35 LOGI("SD chipSelect: %d", chipSelect);
36 LOGI("SD speedMHz: %d", speedMHz);
37 LOGI("ext: %s", ext);
39 owns_cfg = true;
41 exension = ext;
43 }
44
46 AudioSourceIdxSDFAT(const char *startFilePath, const char *ext,
47 SdSpiConfig &config, bool setupIndex = true) {
48 TRACED();
49 p_cfg = &config;
50 owns_cfg = false;
52 exension = ext;
54 }
55
57 AudioSourceIdxSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex = true){
58 TRACED();
59 sd = fs;
60 p_cfg = nullptr;
61 owns_cfg = false;
63 exension = ext;
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 ~AudioSourceIdxSDFAT() { end(); }
71
72 virtual bool begin() override {
73 TRACED();
74 if (!is_sd_setup) {
75 if (!sd.begin(*p_cfg)) {
76 LOGE("sd.begin failed");
77 return false;
78 }
79 is_sd_setup = true;
80 }
82 idx_pos = 0;
83 return is_sd_setup;
84 }
85
86 void end() {
87 if (is_sd_setup) {
88 file.close();
89#ifdef ESP32
90 if (is_close_sd) sd.end();
91#endif
92 if (owns_cfg) delete (p_cfg);
93 is_sd_setup = false;
94 }
95 }
96
97 virtual Stream *nextStream(int offset = 1) override {
98 LOGI("nextStream: %d", offset);
99 return selectStream(idx_pos + offset);
100 }
101
102 virtual Stream *selectStream(int index) override {
103 LOGI("selectStream: %d", index);
104 idx_pos = index;
105 return selectStream(idx[index]);
106 }
107
108 virtual Stream *selectStream(const char *path) override {
109 file.close();
110 if (path == nullptr) {
111 LOGE("Filename is null")
112 return nullptr;
113 }
114
115 // AudioFile new_file;
116 if (!file.open(path, O_RDONLY)) {
117 LOGE("Open error: '%s'", path);
118 }
119
120 LOGI("-> selectStream: %s", path);
121 // file = new_file;
122 return &file;
123 }
124
127 void setFileFilter(const char *filter) { file_name_pattern = filter; }
128
130 int index() { return idx_pos; }
131
133 const char *toStr() { return file_name; }
134
135 // provides default setting go to the next
136 virtual bool isAutoNext() { return true; }
137
139 virtual void setPath(const char *p) { start_path = p; }
140
142 long size() { return idx.size(); }
143
145 int indexOf(const char* filename) {
146 return idx.indexOf(filename);
147 }
148
150 const char* name(int pos) {
151 return idx[pos];
152 }
153
157 }
158
159 protected:
160 SdSpiConfig *p_cfg = nullptr;
164 size_t idx_pos = 0;
166 const char *exension = nullptr;
167 const char *start_path = nullptr;
168 const char *file_name_pattern = "*";
169 bool setup_index = true;
170 bool is_close_sd = true;
171 bool is_sd_setup = false;
172 int cs;
173 bool owns_cfg = false;
174
175 const char *getFileName(AudioFile &file) {
176 static char name[MAX_FILE_LEN];
177 file.getName(name, MAX_FILE_LEN);
178 return name;
179 }
180};
181
182} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define MAX_FILE_LEN
Definition LegacyAudioSourceSDFAT.h:18
#define PIN_CS
Definition avr.h:14
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. An index file is used to speed up ...
Definition AudioSourceIdxSDFAT.h:28
int index()
Provides the current index position.
Definition AudioSourceIdxSDFAT.h:130
AudioSourceIdxSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex=true)
Constructor for providing an open FS.
Definition AudioSourceIdxSDFAT.h:57
const char * exension
Definition AudioSourceIdxSDFAT.h:166
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSDFAT.h:142
bool owns_cfg
Definition AudioSourceIdxSDFAT.h:173
SDIndex< AudioFs, AudioFile > idx
Definition AudioSourceIdxSDFAT.h:163
bool is_sd_setup
Definition AudioSourceIdxSDFAT.h:171
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSDFAT.h:102
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSDFAT.h:72
SdSpiConfig * p_cfg
Definition AudioSourceIdxSDFAT.h:160
bool is_close_sd
Definition AudioSourceIdxSDFAT.h:170
size_t idx_pos
Definition AudioSourceIdxSDFAT.h:164
const char * getFileName(AudioFile &file)
Definition AudioSourceIdxSDFAT.h:175
AudioFs sd
Definition AudioSourceIdxSDFAT.h:161
const char * start_path
Definition AudioSourceIdxSDFAT.h:167
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSDFAT.h:133
AudioFile file
Definition AudioSourceIdxSDFAT.h:162
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSDFAT.h:139
char file_name[MAX_FILE_LEN]
Definition AudioSourceIdxSDFAT.h:165
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSDFAT.h:136
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSDFAT.h:145
AudioSourceIdxSDFAT(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, int speedMHz=10, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSDFAT.h:31
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSDFAT.h:97
void end()
Definition AudioSourceIdxSDFAT.h:86
virtual ~AudioSourceIdxSDFAT()
Definition AudioSourceIdxSDFAT.h:70
bool setup_index
Definition AudioSourceIdxSDFAT.h:169
void setFileFilter(const char *filter)
Definition AudioSourceIdxSDFAT.h:127
int cs
Definition AudioSourceIdxSDFAT.h:172
const char * file_name_pattern
Definition AudioSourceIdxSDFAT.h:168
void setCreateIndex(bool rebuild)
Defines whether the index should be rebuild on begin.
Definition AudioSourceIdxSDFAT.h:155
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSDFAT.h:150
AudioSourceIdxSDFAT(const char *startFilePath, const char *ext, SdSpiConfig &config, bool setupIndex=true)
Costructor with SdSpiConfig.
Definition AudioSourceIdxSDFAT.h:46
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSDFAT.h:108
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition SDIndex.h:20
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
sdfat::SdSpiConfig SdSpiConfig
Definition LegacyAudioSourceSDFAT.h:22
sdfat::SdFs AudioFs
Definition LegacyAudioSourceSDFAT.h:23
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512