arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceSDMMC.h
1#pragma once
2
3#include "AudioLogger.h"
4#include "AudioTools/Disk/AudioSource.h"
5#include "FS.h"
6#include "SD_MMC.h"
7#include "AudioTools/Disk/SDDirect.h"
8
9namespace audio_tools {
10
33public:
35 AudioSourceSDMMC(const char *startFilePath = "/", const char *ext = ".mp3", bool setupIndex=true) {
36 start_path = startFilePath;
37 exension = ext;
38 setup_index = setupIndex;
39 }
40
41 virtual ~AudioSourceSDMMC() {
42 end();
43 }
44
45 virtual bool begin() override {
46 TRACED();
47 if (!is_sd_setup) {
48 if (!SD_MMC.begin("/sdcard", true)) {
49 LOGE("SD_MMC.begin failed");
50 return false;
51 }
52 is_sd_setup = true;
53 }
54 idx.begin(start_path, exension, file_name_pattern);
55 idx_pos = 0;
56 return is_sd_setup;
57 }
58
59 void end() {
60 file.close();
61 SD_MMC.end();
62 is_sd_setup = false;
63 }
64
65
66 virtual Stream *nextStream(int offset = 1) override {
67 LOGI("nextStream: %d", offset);
68 return selectStream(idx_pos + offset);
69 }
70
71 virtual Stream *selectStream(int index) override {
72 LOGI("selectStream: %d", index);
73 file.close();
74 idx_pos = index;
75 file_name = idx[index];
76 if (file_name==nullptr) return nullptr;
77 LOGI("Using file %s", file_name);
78 file = SD_MMC.open(file_name);
79 return file ? &file : nullptr;
80 }
81
82 virtual Stream *selectStream(const char *path) override {
83 file.close();
84 file = SD_MMC.open(path);
85 file_name = file.name();
86 LOGI("-> selectStream: %s", path);
87 return file ? &file : nullptr;
88 }
89
92 void setFileFilter(const char *filter) { file_name_pattern = filter; }
93
95 int index() { return idx_pos; }
96
98 const char *toStr() { return file_name; }
99
100 // provides default setting go to the next
101 virtual bool isAutoNext() { return true; }
102
104 virtual void setPath(const char *p) { start_path = p; }
105
107 long size() { return idx.size();}
108
109protected:
111 File file;
112 size_t idx_pos = 0;
113 const char *file_name;
114 const char *exension = nullptr;
115 const char *start_path = nullptr;
116 const char *file_name_pattern = "*";
117 bool setup_index = true;
118 bool is_sd_setup = false;
119};
120
121} // 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. This class is based on the Arduino...
Definition AudioSourceSDMMC.h:32
int index()
Provides the current index position.
Definition AudioSourceSDMMC.h:95
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition AudioSourceSDMMC.h:107
virtual Stream * selectStream(int index) override
Definition AudioSourceSDMMC.h:71
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSDMMC.h:45
const char * toStr()
provides the actual file name
Definition AudioSourceSDMMC.h:98
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSDMMC.h:104
AudioSourceSDMMC(const char *startFilePath="/", const char *ext=".mp3", bool setupIndex=true)
Default constructor.
Definition AudioSourceSDMMC.h:35
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSDMMC.h:101
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSDMMC.h:66
void setFileFilter(const char *filter)
Definition AudioSourceSDMMC.h:92
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSDMMC.h:82
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition SDDirect.h:22
long size()
Provides the number of files.
Definition SDDirect.h:50
Definition NoArduino.h:142
VFSFile File
Desktop file system compatibility alias.
Definition File.h:12
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10