arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSourceIdxSDFAT.h
1#pragma once
2
3#include <SPI.h>
4#include <SdFat.h>
5
6#include "AudioToolsConfig.h"
7#include "AudioLogger.h"
8#include "AudioTools/Disk/AudioSource.h"
9
10#define USE_SDFAT
11#include "AudioTools/Disk/SDIndex.h"
12
13// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
14// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
15#ifndef SD_FAT_TYPE
16#define SD_FAT_TYPE 1
17#endif
18// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. (40?)
19#define SPI_CLOCK SD_SCK_MHZ(50)
20#if SD_FAT_TYPE == 0
21typedef SdFat AudioFs;
22typedef File AudioFile;
23#elif SD_FAT_TYPE == 1
24typedef SdFat32 AudioFs;
25typedef File32 AudioFile;
26#elif SD_FAT_TYPE == 2
27typedef SdExFat AudioFs;
28typedef ExFile AudioFile;
29#elif SD_FAT_TYPE == 3
30typedef SdFs AudioFs;
31typedef FsFile AudioFile;
32#endif
33
34namespace audio_tools {
44 public:
46 AudioSourceIdxSDFAT(const char *startFilePath = "/", const char *ext = ".mp3",
47 int chipSelect = PIN_CS, int speedMHz = 10,
48 bool setupIndex = true) {
49 TRACED();
50 LOGI("SD chipSelect: %d", chipSelect);
51 LOGI("SD speedMHz: %d", speedMHz);
52 LOGI("ext: %s", ext);
53 p_cfg = new SdSpiConfig(chipSelect, DEDICATED_SPI, SD_SCK_MHZ(speedMHz));
54 owns_cfg = true;
55 start_path = startFilePath;
56 exension = ext;
57 setup_index = setupIndex;
58 }
59
61 AudioSourceIdxSDFAT(const char *startFilePath, const char *ext,
62 SdSpiConfig &config, bool setupIndex = true) {
63 TRACED();
64 p_cfg = &config;
65 owns_cfg = false;
66 start_path = startFilePath;
67 exension = ext;
68 setup_index = setupIndex;
69 }
70
71 virtual ~AudioSourceIdxSDFAT() { end(); }
72
73 virtual void begin() override {
74 TRACED();
75 if (!is_sd_setup) {
76 if (!sd.begin(*p_cfg)) {
77 LOGE("sd.begin failed");
78 return;
79 }
80 is_sd_setup = true;
81 }
82 idx.begin(start_path, exension, file_name_pattern, setup_index);
83 idx_pos = 0;
84 }
85
86 void end() {
87 if (is_sd_setup) {
88#ifdef ESP32
89 sd.end();
90#endif
91 if (owns_cfg) delete (p_cfg);
92 is_sd_setup = false;
93 }
94 }
95
96 virtual Stream *nextStream(int offset = 1) override {
97 LOGI("nextStream: %d", offset);
98 return selectStream(idx_pos + offset);
99 }
100
101 virtual Stream *selectStream(int index) override {
102 LOGI("selectStream: %d", index);
103 idx_pos = index;
104 return selectStream(idx[index]);
105 }
106
107 virtual Stream *selectStream(const char *path) override {
108 file.close();
109 if (path == nullptr) {
110 LOGE("Filename is null")
111 return nullptr;
112 }
113
114 // AudioFile new_file;
115 if (!file.open(path, O_RDONLY)) {
116 LOGE("Open error: '%s'", path);
117 }
118
119 LOGI("-> selectStream: %s", path);
120 // file = new_file;
121 return &file;
122 }
123
126 void setFileFilter(const char *filter) { file_name_pattern = filter; }
127
129 int index() { return idx_pos; }
130
132 const char *toStr() { return file_name; }
133
134 // provides default setting go to the next
135 virtual bool isAutoNext() { return true; }
136
138 virtual void setPath(const char *p) { start_path = p; }
139
141 long size() { return idx.size(); }
142
143 protected:
144 SdSpiConfig *p_cfg = nullptr;
145 AudioFs sd;
146 AudioFile file;
148 size_t idx_pos = 0;
149 char file_name[MAX_FILE_LEN];
150 const char *exension = nullptr;
151 const char *start_path = nullptr;
152 const char *file_name_pattern = "*";
153 bool setup_index = true;
154 bool is_sd_setup = false;
155 int cs;
156 bool owns_cfg = false;
157
158 const char *getFileName(AudioFile &file) {
159 static char name[MAX_FILE_LEN];
160 file.getName(name, MAX_FILE_LEN);
161 return name;
162 }
163};
164
165} // 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 AudioSourceIdxSDFAT.h:43
int index()
Provides the current index position.
Definition AudioSourceIdxSDFAT.h:129
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSDFAT.h:141
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSDFAT.h:101
virtual void begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSDFAT.h:73
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSDFAT.h:132
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSDFAT.h:138
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSDFAT.h:135
AudioSourceIdxSDFAT(const char *startFilePath="/", const char *ext=".mp3", int chipSelect=PIN_CS, int speedMHz=10, bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSDFAT.h:46
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSDFAT.h:96
void setFileFilter(const char *filter)
Definition AudioSourceIdxSDFAT.h:126
AudioSourceIdxSDFAT(const char *startFilePath, const char *ext, SdSpiConfig &config, bool setupIndex=true)
Costructor with SdSpiConfig.
Definition AudioSourceIdxSDFAT.h:61
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition AudioSourceIdxSDFAT.h:107
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