arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Modules Pages
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
32public:
34 AudioSourceIdxSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
35 start_path = startFilePath;
36 extension = ext;
37 setup_index = setupIndex;
38 p_spi = &SPI;
39 cs = chipSelect;
40 }
41
42#ifdef USE_SD_SUPPORTS_SPI
43 // Pass your own spi instance, in case you need a dedicated one
44 AudioSourceIdxSD(const char *startFilePath, const char *ext, int chipSelect, SPIClass &spiInstance, bool setupIndex=true) {
45 start_path = startFilePath;
46 extension = ext;
47 setup_index = setupIndex;
48 p_spi = &spiInstance;
49 cs = chipSelect;
50 }
51#endif
52
53 virtual void begin() override {
54 TRACED();
55 if (!is_sd_setup) {
56 while (!start_sd()) {
57 LOGW("SD.begin cs=%d failed", cs);
58 delay(500);
59 }
60 is_sd_setup = true;
61 }
62 idx.begin(start_path, extension, file_name_pattern, setup_index);
63 idx_pos = 0;
64 }
65
66 void end() {
67 SD.end();
68 is_sd_setup = false;
69 }
70
71 virtual Stream *nextStream(int offset = 1) override {
72 LOGI("nextStream: %d", offset);
73 return selectStream(idx_pos + offset);
74 }
75
76 virtual Stream *selectStream(int index) override {
77 LOGI("selectStream: %d", index);
78 idx_pos = index;
79 file_name = idx[index];
80 if (file_name==nullptr) return nullptr;
81 LOGI("Using file %s", file_name);
82 file = SD.open(file_name);
83 return file ? &file : nullptr;
84 }
85
86 virtual Stream *selectStream(const char *path) override {
87 file.close();
88 file = SD.open(path);
89 file_name = file.name();
90 LOGI("-> selectStream: %s", path);
91 return file ? &file : nullptr;
92 }
93
96 void setFileFilter(const char *filter) { file_name_pattern = filter; }
97
99 int index() { return idx_pos; }
100
102 const char *toStr() { return file_name; }
103
104 // provides default setting go to the next
105 virtual bool isAutoNext() { return true; }
106
108 virtual void setPath(const char *p) { start_path = p; }
109
111 long size() { return idx.size();}
112
113protected:
114#if defined(USE_SD_NO_NS)
116#else
117 SDIndex<fs::SDFS,fs::File> idx{SD};
118#endif
119 File file;
120 size_t idx_pos = 0;
121 const char *file_name;
122 const char *extension = nullptr;
123 const char *start_path = nullptr;
124 const char *file_name_pattern = "*";
125 bool setup_index = true;
126 bool is_sd_setup = false;
127 SPIClass *p_spi = nullptr;
128 int cs;
129
130 bool start_sd(){
131#ifdef USE_SD_SUPPORTS_SPI
132 return SD.begin(cs, *p_spi);
133#else
134 return SD.begin(cs);
135#endif
136 }
137};
138
139} // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:15
ESP32 AudioSource for AudioPlayer using an SD card as data source. This class is based on the Arduino...
Definition AudioSourceIdxSD.h:31
int index()
Provides the current index position.
Definition AudioSourceIdxSD.h:99
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSD.h:111
AudioSourceIdxSD(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSD.h:34
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSD.h:76
virtual void begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSD.h:53
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSD.h:102
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSD.h:108
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSD.h:105
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSD.h:71
void setFileFilter(const char *filter)
Definition AudioSourceIdxSD.h:96
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition AudioSourceIdxSD.h:86
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition SDIndex.h:20
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10