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
33public:
35 AudioSourceIdxSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
37 extension = ext;
39 p_spi = &SPI;
40 cs = chipSelect;
41 }
42
43#ifdef USE_SD_SUPPORTS_SPI
44 // Pass your own spi instance, in case you need a dedicated one
45 AudioSourceIdxSD(const char *startFilePath, const char *ext, int chipSelect, SPIClass &spiInstance, bool setupIndex=true) {
47 extension = ext;
50 cs = chipSelect;
51 }
52#endif
53
54 virtual bool begin() override {
55 TRACED();
56 if (!is_sd_setup) {
57 int retry = 10;
58 while (!start_sd()) {
59 LOGW("SD.begin cs=%d failed", cs);
60 delay(500);
61 if (--retry <= 0) {
62 return false;
63 }
64 }
65 is_sd_setup = true;
66 }
68 idx_pos = 0;
69 return is_sd_setup;
70 }
71
72 void end() {
73 file.close();
74 SD.end();
75 is_sd_setup = false;
76 }
77
78 virtual Stream *nextStream(int offset = 1) override {
79 LOGI("nextStream: %d", offset);
80 return selectStream(idx_pos + offset);
81 }
82
83 virtual Stream *selectStream(int index) override {
84 LOGI("selectStream: %d", index);
85 idx_pos = index;
87 if (file_name==nullptr) return nullptr;
88 LOGI("Using file %s", file_name);
89 file.close();
91 return file ? &file : nullptr;
92 }
93
94 virtual Stream *selectStream(const char *path) override {
95 file.close();
96 file = SD.open(path);
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
122 int indexOf(const char* filename) {
123 return idx.indexOf(filename);
124 }
125
127 const char* name(int pos) {
128 return idx[pos];
129 }
130
134 }
135
136protected:
137#if defined(USE_SD_NO_NS)
139#else
141#endif
143 size_t idx_pos = 0;
144 const char *file_name;
145 const char *extension = nullptr;
146 const char *start_path = nullptr;
147 const char *file_name_pattern = "*";
148 bool setup_index = true;
149 bool is_sd_setup = false;
150 SPIClass *p_spi = nullptr;
151 int cs;
152
153 bool start_sd(){
154#ifdef USE_SD_SUPPORTS_SPI
155 return SD.begin(cs, *p_spi);
156#else
157 return SD.begin(cs);
158#endif
159 }
160};
161
162} // 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
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 AudioSourceIdxSD.h:32
int index()
Provides the current index position.
Definition AudioSourceIdxSD.h:107
bool start_sd()
Definition AudioSourceIdxSD.h:153
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSD.h:119
bool is_sd_setup
Definition AudioSourceIdxSD.h:149
AudioSourceIdxSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSD.h:35
const char * extension
Definition AudioSourceIdxSD.h:145
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSD.h:83
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSD.h:54
File file
Definition AudioSourceIdxSD.h:142
size_t idx_pos
Definition AudioSourceIdxSD.h:143
SPIClass * p_spi
Definition AudioSourceIdxSD.h:150
const char * start_path
Definition AudioSourceIdxSD.h:146
SDIndex< fs::SDFS, fs::File > idx
Definition AudioSourceIdxSD.h:140
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSD.h:110
const char * file_name
Definition AudioSourceIdxSD.h:144
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSD.h:116
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSD.h:113
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSD.h:122
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSD.h:78
void end()
Definition AudioSourceIdxSD.h:72
bool setup_index
Definition AudioSourceIdxSD.h:148
void setFileFilter(const char *filter)
Definition AudioSourceIdxSD.h:104
int cs
Definition AudioSourceIdxSD.h:151
const char * file_name_pattern
Definition AudioSourceIdxSD.h:147
void setCreateIndex(bool rebuild)
Defines whether the index should be rebuild on begin.
Definition AudioSourceIdxSD.h:132
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSD.h:127
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSD.h:94
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
Definition NoArduino.h:142
Arduino File support using std::fstream.
Definition VFSFile.h:33
void close()
Definition VFSFile.h:180
const char * name() const
Definition VFSFile.h:182
virtual bool begin()
mount the file systems
Definition VFS.h:18
VFSFile open(const char *file, FileMode mode=VFS_FILE_READ)
Definition VFS.h:24
virtual void end()
unmount the file system
Definition VFS.h:20
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(unsigned long ms)
Definition Time.h:23
static FS SD
Definition File.h:18
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512