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 void begin() override {
55 TRACED();
56 if (!is_sd_setup) {
57 while (!start_sd()) {
58 LOGW("SD.begin cs=%d failed", cs);
59 delay(500);
60 }
61 is_sd_setup = true;
62 }
63 idx.begin(start_path, extension, file_name_pattern, setup_index);
64 idx_pos = 0;
65 }
66
67 void end() {
68 SD.end();
69 is_sd_setup = false;
70 }
71
72 virtual Stream *nextStream(int offset = 1) override {
73 LOGI("nextStream: %d", offset);
74 return selectStream(idx_pos + offset);
75 }
76
77 virtual Stream *selectStream(int index) override {
78 LOGI("selectStream: %d", index);
79 idx_pos = index;
80 file_name = idx[index];
81 if (file_name==nullptr) return nullptr;
82 LOGI("Using file %s", file_name);
83 file = SD.open(file_name);
84 return file ? &file : nullptr;
85 }
86
87 virtual Stream *selectStream(const char *path) override {
88 file.close();
89 file = SD.open(path);
90 file_name = file.name();
91 LOGI("-> selectStream: %s", path);
92 return file ? &file : nullptr;
93 }
94
97 void setFileFilter(const char *filter) { file_name_pattern = filter; }
98
100 int index() { return idx_pos; }
101
103 const char *toStr() { return file_name; }
104
105 // provides default setting go to the next
106 virtual bool isAutoNext() { return true; }
107
109 virtual void setPath(const char *p) { start_path = p; }
110
112 long size() { return idx.size();}
113
115 int indexOf(const char* filename) {
116 return idx.indexOf(filename);
117 }
118
120 const char* name(int pos) {
121 return idx[pos];
122 }
123
124protected:
125#if defined(USE_SD_NO_NS)
127#else
128 SDIndex<fs::SDFS,fs::File> idx{SD};
129#endif
130 File file;
131 size_t idx_pos = 0;
132 const char *file_name;
133 const char *extension = nullptr;
134 const char *start_path = nullptr;
135 const char *file_name_pattern = "*";
136 bool setup_index = true;
137 bool is_sd_setup = false;
138 SPIClass *p_spi = nullptr;
139 int cs;
140
141 bool start_sd(){
142#ifdef USE_SD_SUPPORTS_SPI
143 return SD.begin(cs, *p_spi);
144#else
145 return SD.begin(cs);
146#endif
147 }
148};
149
150} // 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:100
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSD.h:112
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:77
virtual void begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSD.h:54
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSD.h:103
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSD.h:109
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSD.h:106
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSD.h:115
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSD.h:72
void setFileFilter(const char *filter)
Definition AudioSourceIdxSD.h:97
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSD.h:120
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSD.h:87
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:99
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10