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 {
31template <typename AudioFs = SdFat32, typename AudioFile = File32>
33 public:
35 AudioSourceIdxSDFAT(const char *startFilePath = "/", const char *ext = ".mp3",
36 int chipSelect = PIN_CS, int speedMHz = 10,
37 bool setupIndex = true) {
38 TRACED();
39 LOGI("SD chipSelect: %d", chipSelect);
40 LOGI("SD speedMHz: %d", speedMHz);
41 LOGI("ext: %s", ext);
42 p_cfg = new SdSpiConfig(chipSelect, DEDICATED_SPI, SD_SCK_MHZ(speedMHz));
43 owns_cfg = true;
44 start_path = startFilePath;
45 exension = ext;
46 setup_index = setupIndex;
47 }
48
50 AudioSourceIdxSDFAT(const char *startFilePath, const char *ext,
51 SdSpiConfig &config, bool setupIndex = true) {
52 TRACED();
53 p_cfg = &config;
54 owns_cfg = false;
55 start_path = startFilePath;
56 exension = ext;
57 setup_index = setupIndex;
58 }
59
61 AudioSourceIdxSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex = true){
62 TRACED();
63 sd = fs;
64 p_cfg = nullptr;
65 owns_cfg = false;
66 start_path = startFilePath;
67 exension = ext;
68 setup_index = setupIndex;
69 is_sd_setup = true;
70 // since we expect an open fs we do not close it
71 is_close_sd = false;
72 }
73
74 virtual ~AudioSourceIdxSDFAT() { end(); }
75
76 virtual bool begin() override {
77 TRACED();
78 if (!is_sd_setup) {
79 if (!sd.begin(*p_cfg)) {
80 LOGE("sd.begin failed");
81 return false;
82 }
83 is_sd_setup = true;
84 }
86 idx_pos = 0;
87 return is_sd_setup;
88 }
89
90 void end() {
91 if (is_sd_setup) {
92 file.close();
93#ifdef ESP32
94 if (is_close_sd) sd.end();
95#endif
96 if (owns_cfg) delete (p_cfg);
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: %d", index);
108 idx_pos = index;
109 return selectStream(idx[index]);
110 }
111
112 virtual Stream *selectStream(const char *path) override {
113 file.close();
114 if (path == nullptr) {
115 LOGE("Filename is null")
116 return nullptr;
117 }
118
119 // AudioFile new_file;
120 if (!file.open(path, O_RDONLY)) {
121 LOGE("Open error: '%s'", path);
122 }
123
124 LOGI("-> selectStream: %s", path);
125 // file = new_file;
126 return &file;
127 }
128
131 void setFileFilter(const char *filter) { file_name_pattern = filter; }
132
134 int index() { return idx_pos; }
135
137 const char *toStr() { return file_name; }
138
139 // provides default setting go to the next
140 virtual bool isAutoNext() { return true; }
141
143 virtual void setPath(const char *p) { start_path = p; }
144
146 long size() { return idx.size(); }
147
149 int indexOf(const char* filename) {
150 return idx.indexOf(filename);
151 }
152
154 const char* name(int pos) {
155 return idx[pos];
156 }
157
159 void setCreateIndex(bool rebuild) {
160 setup_index = rebuild;
161 }
162
163 protected:
164 SdSpiConfig *p_cfg = nullptr;
165 AudioFs sd;
166 AudioFile file;
168 size_t idx_pos = 0;
170 const char *exension = nullptr;
171 const char *start_path = nullptr;
172 const char *file_name_pattern = "*";
173 bool setup_index = true;
174 bool is_close_sd = true;
175 bool is_sd_setup = false;
176 int cs;
177 bool owns_cfg = false;
178
179 const char *getFileName(AudioFile &file) {
180 static char name[MAX_FILE_LEN];
181 file.getName(name, MAX_FILE_LEN);
182 return name;
183 }
184};
185
186} // 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 SDDirect.h:6
#define PIN_CS
Definition avr.h:14
Definition Arduino.h:136
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:17
ESP32 AudioSource for AudioPlayer using an SD card as data source. An index file is used to speed up ...
Definition AudioSourceIdxSDFAT.h:32
int index()
Provides the current index position.
Definition AudioSourceIdxSDFAT.h:134
AudioSourceIdxSDFAT(AudioFs fs, const char *startFilePath="/", const char *ext="", bool setupIndex=true)
Constructor for providing an open FS.
Definition AudioSourceIdxSDFAT.h:61
const char * exension
Definition AudioSourceIdxSDFAT.h:170
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSDFAT.h:146
bool owns_cfg
Definition AudioSourceIdxSDFAT.h:177
SDIndex< AudioFs, AudioFile > idx
Definition AudioSourceIdxSDFAT.h:167
bool is_sd_setup
Definition AudioSourceIdxSDFAT.h:175
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSDFAT.h:106
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSDFAT.h:76
SdSpiConfig * p_cfg
Definition AudioSourceIdxSDFAT.h:164
bool is_close_sd
Definition AudioSourceIdxSDFAT.h:174
size_t idx_pos
Definition AudioSourceIdxSDFAT.h:168
const char * getFileName(AudioFile &file)
Definition AudioSourceIdxSDFAT.h:179
AudioFs sd
Definition AudioSourceIdxSDFAT.h:165
const char * start_path
Definition AudioSourceIdxSDFAT.h:171
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSDFAT.h:137
AudioFile file
Definition AudioSourceIdxSDFAT.h:166
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSDFAT.h:143
char file_name[MAX_FILE_LEN]
Definition AudioSourceIdxSDFAT.h:169
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSDFAT.h:140
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSDFAT.h:149
AudioSourceIdxSDFAT(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, int speedMHz=10, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSDFAT.h:35
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSDFAT.h:101
void end()
Definition AudioSourceIdxSDFAT.h:90
virtual ~AudioSourceIdxSDFAT()
Definition AudioSourceIdxSDFAT.h:74
bool setup_index
Definition AudioSourceIdxSDFAT.h:173
void setFileFilter(const char *filter)
Definition AudioSourceIdxSDFAT.h:131
int cs
Definition AudioSourceIdxSDFAT.h:176
const char * file_name_pattern
Definition AudioSourceIdxSDFAT.h:172
void setCreateIndex(bool rebuild)
Defines whether the index should be rebuild on begin.
Definition AudioSourceIdxSDFAT.h:159
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSDFAT.h:154
AudioSourceIdxSDFAT(const char *startFilePath, const char *ext, SdSpiConfig &config, bool setupIndex=true)
Costructor with SdSpiConfig.
Definition AudioSourceIdxSDFAT.h:50
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSDFAT.h:112
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition SDIndex.h:20
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6