arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSourceVFS.h
1#pragma once
2
3#include <filesystem>
4
5#include "AudioLogger.h"
6#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7#include "AudioTools/Disk/AudioSource.h"
8#include "AudioTools/Disk/VFS.h"
9#include "AudioTools/Disk/VFSFile.h"
10
11namespace audio_tools {
12
13namespace fs = std::filesystem;
14
23 public:
25 AudioSourceVFS(VFS &vfs, const char *startFilePath = "/",
26 const char *ext = "") {
27 start_path = startFilePath;
28 exension = ext;
29 setVFS(vfs);
30 }
31
33 AudioSourceVFS(const char *startFilePath = "/",
34 const char *ext = "") {
35 start_path = startFilePath;
36 exension = ext;
37 }
38
39 virtual void begin() override {
40 TRACED();
41 idx_pos = 0;
42 if (p_vfs) p_vfs->begin();
43 }
44
45 virtual void end() {
46 if (p_vfs) p_vfs->end();
47 }
48
49 virtual Stream *nextStream(int offset = 1) override {
50 LOGI("nextStream: %d", offset);
51 return selectStream(idx_pos + offset);
52 }
53
54 virtual Stream *selectStream(int index) override {
55 LOGI("selectStream: %d", index);
56 idx_pos = index;
57 file_name = get(index);
58 if (file_name == nullptr) return nullptr;
59 LOGI("Using file %s", file_name);
60 assert(p_vfs != nullptr);
61 file = p_vfs->open(file_name);
62 return file ? &file : nullptr;
63 }
64
65 virtual Stream *selectStream(const char *path) override {
66 file.close();
67 assert(p_vfs != nullptr);
68 file = p_vfs->open(path);
69 file_name = file.name();
70 LOGI("-> selectStream: %s", path);
71 return file ? &file : nullptr;
72 }
73
76 void setFileFilter(const char *filter) { file_name_pattern = filter; }
77
79 int index() { return idx_pos; }
80
82 const char *toStr() { return file_name; }
83
84 // provides default setting go to the next
85 virtual bool isAutoNext() { return true; }
86
88 virtual void setPath(const char *p) { start_path = p; }
89
92 long size() {
93 long count = 0;
94 for (auto const &dir_entry : fs::recursive_directory_iterator(start_path)) {
95 if (isValidAudioFile(dir_entry)) count++;
96 }
97 return count;
98 }
99
101 void setVFS(VFS &vfs){
102 p_vfs = &vfs;
103 }
104
105 protected:
106 VFSFile file;
107 size_t idx_pos = 0;
108 const char *file_name;
109 const char *exension = "";
110 const char *start_path = "/";
111 const char *file_name_pattern = "*";
112 fs::directory_entry entry;
113 VFS *p_vfs = nullptr;
114
115 const char *get(int idx) {
116 int count = 0;
117 const char *result = nullptr;
118 for (auto const &dir_entry : fs::recursive_directory_iterator(start_path)) {
119 if (isValidAudioFile(dir_entry)) {
120 if (count++ == idx) {
121 entry = dir_entry;
122 result = entry.path().c_str();
123 break;
124 }
125 }
126 }
127 return result;
128 }
129
131 bool isValidAudioFile(fs::directory_entry file) {
132 const std::filesystem::path &path = file.path();
133
134 const char *file_name = path.filename().c_str();
135 if (file.is_directory()) {
136 LOGD("-> isValidAudioFile: '%s': %d", file_name, false);
137 return false;
138 }
139 StrView strFileTName(file_name);
140 bool result = strFileTName.endsWithIgnoreCase(exension) &&
141 strFileTName.matches(file_name_pattern);
142 LOGD("-> isValidAudioFile: '%s': %d", file_name, result);
143 return result;
144 }
145};
146
147} // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:15
AudioSource using the standard C++ api. In order to make this work you need to configure and provide ...
Definition AudioSourceVFS.h:22
int index()
Provides the current index position.
Definition AudioSourceVFS.h:79
long size()
Definition AudioSourceVFS.h:92
virtual Stream * selectStream(int index) override
Definition AudioSourceVFS.h:54
AudioSourceVFS(VFS &vfs, const char *startFilePath="/", const char *ext="")
Default constructor with VFS.
Definition AudioSourceVFS.h:25
virtual void begin() override
Reset actual stream and move to root.
Definition AudioSourceVFS.h:39
const char * toStr()
provides the actual file name
Definition AudioSourceVFS.h:82
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceVFS.h:88
AudioSourceVFS(const char *startFilePath="/", const char *ext="")
Default constructor w/o VFS.
Definition AudioSourceVFS.h:33
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceVFS.h:85
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceVFS.h:49
void setFileFilter(const char *filter)
Definition AudioSourceVFS.h:76
bool isValidAudioFile(fs::directory_entry file)
checks if the file is a valid audio file
Definition AudioSourceVFS.h:131
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition AudioSourceVFS.h:65
void setVFS(VFS &vfs)
Assign the VFS: to be used before calling begin.
Definition AudioSourceVFS.h:101
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
virtual bool matches(const char *pattern)
Definition StrView.h:193
virtual bool endsWithIgnoreCase(const char *str)
checks if the string ends with the indicated substring
Definition StrView.h:185
Definition NoArduino.h:142
Arduino File support using std::fstream.
Definition VFSFile.h:33
Definition VFS.h:16
virtual bool begin()
mount the file systems
Definition VFS.h:19
virtual void end()
unmount the file system
Definition VFS.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10