arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends 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
31class AudioSourceIdxSD : public AudioSource {
32public:
34 AudioSourceIdxSD(const char *startFilePath = "/", const char *ext = ".mp3", int chipSelect = PIN_CS, bool setupIndex=true) {
35 start_path = startFilePath;
36 exension = 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 AudioSourceSD(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, exension, 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)
115 SDIndex<SDClass, File> idx{SD};
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 *exension = 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
static const char * toStr(MetaDataType t)
Converts the MetaDataType to a string.
Definition AbstractMetaData.h:17
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10