arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceSD.h
1#pragma once
2#include "SD.h"
3#include "SPI.h"
4#include "AudioLogger.h"
5#include "AudioTools/Disk/AudioSource.h"
6#include "SDDirect.h"
7
8namespace audio_tools {
9
10
32class AudioSourceSD : public AudioSource {
33public:
35 AudioSourceSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
36 start_path = startFilePath;
37 extension = ext;
38 setup_index = setupIndex;
39 p_spi = &SPI;
40 cs = chipSelect;
41 }
42
43#ifdef USE_SD_SUPPORTS_SPI
44
45 // Pass your own spi instance, in case you need a dedicated one
46 AudioSourceSD(const char *startFilePath, const char *ext, int chipSelect, SPIClass &spiInstance, bool setupIndex=true) {
47 start_path = startFilePath;
48 extension = ext;
49 setup_index = setupIndex;
50 p_spi = &spiInstance;
51 cs = chipSelect;
52 }
53
54#endif
55
56 virtual bool begin() override {
57 TRACED();
58 if (!is_sd_setup) {
59 int retry = 10;
60 while (!start_sd()) {
61 LOGE("SD.begin cs=%d failed", cs);
62 delay(500);
63 if (--retry <= 0) {
64 return false;
65 }
66 }
67 is_sd_setup = true;
68 }
69 idx.begin(start_path, extension, file_name_pattern);
70 idx_pos = 0;
71 return is_sd_setup;
72 }
73
74 void end() {
75 SD.end();
76 is_sd_setup = false;
77 }
78
79 virtual Stream *nextStream(int offset = 1) override {
80 LOGI("nextStream: %d", offset);
81 return selectStream(idx_pos + offset);
82 }
83
84 virtual Stream *selectStream(int index) override {
85 LOGI("selectStream: %d", index);
86 idx_pos = index;
87 file_name = idx[index];
88 if (file_name==nullptr) return nullptr;
89 LOGI("Using file %s", file_name);
90 file = SD.open(file_name);
91 return file ? &file : nullptr;
92 }
93
94 virtual Stream *selectStream(const char *path) override {
95 file.close();
96 file = SD.open(path);
97 file_name = file.name();
98 LOGI("-> selectStream: %s", path);
99 return file ? &file : nullptr;
100 }
101
104 void setFileFilter(const char *filter) { file_name_pattern = filter; }
105
107 int index() { return idx_pos; }
108
110 const char *toStr() { return file_name; }
111
112 // provides default setting go to the next
113 virtual bool isAutoNext() { return true; }
114
116 virtual void setPath(const char *p) { start_path = p; }
117
119 long size() { return idx.size();}
120
121protected:
122#if defined(USE_SD_NO_NS)
124#else
125 SDDirect<fs::SDFS,fs::File> idx{SD};
126#endif
127 File file;
128 size_t idx_pos = 0;
129 const char *file_name;
130 const char *extension = nullptr;
131 const char *start_path = nullptr;
132 const char *file_name_pattern = "*";
133 bool setup_index = true;
134 bool is_sd_setup = false;
135 int cs;
136 SPIClass *p_spi = nullptr;
137
138 bool start_sd(){
139#ifdef USE_SD_SUPPORTS_SPI
140 return SD.begin(cs, *p_spi);
141#else
142 return SD.begin(cs);
143#endif
144 }
145
146};
147
148} // 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 AudioSourceSD.h:32
int index()
Provides the current index position.
Definition AudioSourceSD.h:107
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition AudioSourceSD.h:119
virtual Stream * selectStream(int index) override
Definition AudioSourceSD.h:84
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSD.h:56
const char * toStr()
provides the actual file name
Definition AudioSourceSD.h:110
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSD.h:116
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSD.h:113
AudioSourceSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition AudioSourceSD.h:35
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSD.h:79
void setFileFilter(const char *filter)
Definition AudioSourceSD.h:104
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSD.h:94
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