arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceIdxSD.h
Go to the documentation of this file.
1#pragma once
2#include <SPI.h>
3#include <SD.h>
4#include "AudioLogger.h"
7
8namespace audio_tools {
9
37public:
39 AudioSourceIdxSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
41 extension = ext;
43 p_spi = &SPI;
44 cs = chipSelect;
45 }
46
47#ifdef USE_SD_SUPPORTS_SPI
48 // Pass your own spi instance, in case you need a dedicated one
49 AudioSourceIdxSD(const char *startFilePath, const char *ext, int chipSelect, SPIClass &spiInstance, bool setupIndex=true) {
51 extension = ext;
54 cs = chipSelect;
55 }
56#endif
57
58 virtual bool begin() override {
59 TRACED();
60 if (!is_sd_setup) {
61 int retry = 10;
62 while (!start_sd()) {
63 LOGW("SD.begin cs=%d failed", cs);
64 delay(500);
65 if (--retry <= 0) {
66 return false;
67 }
68 }
69 is_sd_setup = true;
70 }
72 idx_pos = 0;
73 return is_sd_setup;
74 }
75
76 void end() {
77 file.close();
78 SD.end();
79 is_sd_setup = false;
80 }
81
82 virtual Stream *nextStream(int offset = 1) override {
83 LOGI("nextStream: %d", offset);
84 return selectStream(idx_pos + offset);
85 }
86
87 virtual Stream *selectStream(int index) override {
88 LOGI("selectStream: %d", index);
89 idx_pos = index;
91 if (file_name==nullptr) return nullptr;
92 LOGI("Using file %s", file_name);
93 file.close();
95 return file ? &file : nullptr;
96 }
97
98 virtual Stream *selectStream(const char *path) override {
99 file.close();
100 file = SD.open(path);
101 file_name = file.name();
102 LOGI("-> selectStream: %s", path);
103 return file ? &file : nullptr;
104 }
105
108 void setFileFilter(const char *filter) { file_name_pattern = filter; }
109
111 int index() { return idx_pos; }
112
114 const char *toStr() { return file_name; }
115
116 // provides default setting go to the next
117 virtual bool isAutoNext() { return true; }
118
120 virtual void setPath(const char *p) { start_path = p; }
121
123 long size() { return idx.size();}
124
126 int indexOf(const char* filename) {
127 return idx.indexOf(filename);
128 }
129
131 const char* name(int pos) {
132 return idx[pos];
133 }
134
138 }
139
140protected:
141#if defined(USE_SD_NO_NS)
143#else
145#endif
147 size_t idx_pos = 0;
148 const char *file_name;
149 const char *extension = nullptr;
150 const char *start_path = nullptr;
151 const char *file_name_pattern = "*";
152 bool setup_index = true;
153 bool is_sd_setup = false;
154 SPIClass *p_spi = nullptr;
155 int cs;
156
157 bool start_sd(){
158#ifdef USE_SD_SUPPORTS_SPI
159 return SD.begin(cs, *p_spi);
160#else
161 return SD.begin(cs);
162#endif
163 }
164};
165
166} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#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 AudioSourceIdxSD.h:36
int index()
Provides the current index position.
Definition AudioSourceIdxSD.h:111
bool start_sd()
Definition AudioSourceIdxSD.h:157
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSD.h:123
bool is_sd_setup
Definition AudioSourceIdxSD.h:153
AudioSourceIdxSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSD.h:39
const char * extension
Definition AudioSourceIdxSD.h:149
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSD.h:87
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSD.h:58
File file
Definition AudioSourceIdxSD.h:146
size_t idx_pos
Definition AudioSourceIdxSD.h:147
SPIClass * p_spi
Definition AudioSourceIdxSD.h:154
const char * start_path
Definition AudioSourceIdxSD.h:150
SDIndex< fs::SDFS, fs::File > idx
Definition AudioSourceIdxSD.h:144
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSD.h:114
const char * file_name
Definition AudioSourceIdxSD.h:148
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSD.h:120
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSD.h:117
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSD.h:126
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSD.h:82
void end()
Definition AudioSourceIdxSD.h:76
bool setup_index
Definition AudioSourceIdxSD.h:152
void setFileFilter(const char *filter)
Definition AudioSourceIdxSD.h:108
int cs
Definition AudioSourceIdxSD.h:155
const char * file_name_pattern
Definition AudioSourceIdxSD.h:151
void setCreateIndex(bool rebuild)
Defines whether the index should be rebuild on begin.
Definition AudioSourceIdxSD.h:136
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSD.h:131
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSD.h:98
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition SDIndex.h:20
void begin(const char *startDir, const char *extension, const char *file_name_pattern, bool setupIndex=true)
Definition SDIndex.h:23
long size()
Definition SDIndex.h:167
int indexOf(const char *filename)
Find the index of a filename.
Definition SDIndex.h:115
Arduino File API for Zephyr.
Definition ZephyrFile.h:23
void close()
Definition ZephyrFile.h:67
const char * name() const
Definition ZephyrFile.h:200
ZephyrFile open(const char *path, const char *mode="r", bool create=false)
Definition ZephyrSD.h:51
void end()
Definition ZephyrSD.h:43
bool begin(const char *diskName="SD", const char *mountPoint="/SD")
Definition ZephyrSD.h:32
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(uint32_t ms)
Definition Arduino.h:255
static ZephyrSDClass SD
Access to SD object.
Definition ZephyrSD.h:215
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508