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 bool begin() override {
42 TRACED();
43 if (!is_sd_setup) {
44 if (!SD_MMC.begin("/sdcard", true)) {
45 LOGE("SD_MMC.begin failed");
46 return false;
47 }
48 is_sd_setup = true;
49 }
50 idx.begin(start_path, exension, file_name_pattern);
51 idx_pos = 0;
52 return is_sd_setup;
53 }
54
55 void end() {
56 SD_MMC.end();
57 is_sd_setup = false;
58 }
59
60
61 virtual Stream *nextStream(int offset = 1) override {
62 LOGI("nextStream: %d", offset);
63 return selectStream(idx_pos + offset);
64 }
65
66 virtual Stream *selectStream(int index) override {
67 LOGI("selectStream: %d", index);
68 idx_pos = index;
69 file_name = idx[index];
70 if (file_name==nullptr) return nullptr;
71 LOGI("Using file %s", file_name);
72 file = SD_MMC.open(file_name);
73 return file ? &file : nullptr;
74 }
75
76 virtual Stream *selectStream(const char *path) override {
77 file.close();
78 file = SD_MMC.open(path);
79 file_name = file.name();
80 LOGI("-> selectStream: %s", path);
81 return file ? &file : nullptr;
82 }
83
86 void setFileFilter(const char *filter) { file_name_pattern = filter; }
87
89 int index() { return idx_pos; }
90
92 const char *toStr() { return file_name; }
93
94 // provides default setting go to the next
95 virtual bool isAutoNext() { return true; }
96
98 virtual void setPath(const char *p) { start_path = p; }
99
101 long size() { return idx.size();}
102
103protected:
105 File file;
106 size_t idx_pos = 0;
107 const char *file_name;
108 const char *exension = nullptr;
109 const char *start_path = nullptr;
110 const char *file_name_pattern = "*";
111 bool setup_index = true;
112 bool is_sd_setup = false;
113};
114
115} // 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:89
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:101
virtual Stream * selectStream(int index) override
Definition AudioSourceSDMMC.h:66
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSDMMC.h:41
const char * toStr()
provides the actual file name
Definition AudioSourceSDMMC.h:92
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSDMMC.h:98
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:95
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSDMMC.h:61
void setFileFilter(const char *filter)
Definition AudioSourceSDMMC.h:86
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSDMMC.h:76
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
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10