arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceIdxSD.h
1#pragma once
2#include <SPI.h>
3#include <SD.h>
4#include "AudioLogger.h"
5#include "AudioTools/Disk/AudioSource.h"
6#include "AudioTools/Disk/SDIndex.h"
7
8namespace audio_tools {
9
33public:
35 AudioSourceIdxSD(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 // 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) {
46 start_path = startFilePath;
47 extension = ext;
48 setup_index = setupIndex;
49 p_spi = &spiInstance;
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 }
67 idx.begin(start_path, extension, file_name_pattern, setup_index);
68 idx_pos = 0;
69 return is_sd_setup;
70 }
71
72 void end() {
73 SD.end();
74 is_sd_setup = false;
75 }
76
77 virtual Stream *nextStream(int offset = 1) override {
78 LOGI("nextStream: %d", offset);
79 return selectStream(idx_pos + offset);
80 }
81
82 virtual Stream *selectStream(int index) override {
83 LOGI("selectStream: %d", index);
84 idx_pos = index;
85 file_name = idx[index];
86 if (file_name==nullptr) return nullptr;
87 LOGI("Using file %s", file_name);
88 file = SD.open(file_name);
89 return file ? &file : nullptr;
90 }
91
92 virtual Stream *selectStream(const char *path) override {
93 file.close();
94 file = SD.open(path);
95 file_name = file.name();
96 LOGI("-> selectStream: %s", path);
97 return file ? &file : nullptr;
98 }
99
102 void setFileFilter(const char *filter) { file_name_pattern = filter; }
103
105 int index() { return idx_pos; }
106
108 const char *toStr() { return file_name; }
109
110 // provides default setting go to the next
111 virtual bool isAutoNext() { return true; }
112
114 virtual void setPath(const char *p) { start_path = p; }
115
117 long size() { return idx.size();}
118
120 int indexOf(const char* filename) {
121 return idx.indexOf(filename);
122 }
123
125 const char* name(int pos) {
126 return idx[pos];
127 }
128
129protected:
130#if defined(USE_SD_NO_NS)
132#else
133 SDIndex<fs::SDFS,fs::File> idx{SD};
134#endif
135 File file;
136 size_t idx_pos = 0;
137 const char *file_name;
138 const char *extension = nullptr;
139 const char *start_path = nullptr;
140 const char *file_name_pattern = "*";
141 bool setup_index = true;
142 bool is_sd_setup = false;
143 SPIClass *p_spi = nullptr;
144 int cs;
145
146 bool start_sd(){
147#ifdef USE_SD_SUPPORTS_SPI
148 return SD.begin(cs, *p_spi);
149#else
150 return SD.begin(cs);
151#endif
152 }
153};
154
155} // 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. An index file is used to speed up ...
Definition AudioSourceIdxSD.h:32
int index()
Provides the current index position.
Definition AudioSourceIdxSD.h:105
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSD.h:117
AudioSourceIdxSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSD.h:35
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSD.h:82
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSD.h:54
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSD.h:108
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSD.h:114
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSD.h:111
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSD.h:120
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSD.h:77
void setFileFilter(const char *filter)
Definition AudioSourceIdxSD.h:102
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSD.h:125
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSD.h:92
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition SDIndex.h:20
int indexOf(const char *filename)
Find the index of a filename.
Definition SDIndex.h:115
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10