arduino-audio-tools
Loading...
Searching...
No Matches
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 ~AudioSourceVFS() {
40 end();
41 }
42
43
44 virtual bool begin() override {
45 TRACED();
46 idx_pos = 0;
47 return (p_vfs) ? p_vfs->begin() : false;
48 }
49
50 virtual void end() {
51 file.close();
52 if (p_vfs) p_vfs->end();
53 }
54
55 virtual Stream *nextStream(int offset = 1) override {
56 LOGI("nextStream: %d", offset);
57 return selectStream(idx_pos + offset);
58 }
59
60 virtual Stream *selectStream(int index) override {
61 LOGI("selectStream: %d", index);
62 idx_pos = index;
63 file_name = get(index);
64 if (file_name == nullptr) return nullptr;
65 LOGI("Using file %s", file_name);
66 assert(p_vfs != nullptr);
67 file = p_vfs->open(file_name);
68 return file ? &file : nullptr;
69 }
70
71 virtual Stream *selectStream(const char *path) override {
72 file.close();
73 if (path == nullptr) {
74 LOGE("Filename is null")
75 return nullptr;
76 }
77 assert(p_vfs != nullptr);
78 file = p_vfs->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
102 long size() {
103 long count = 0;
104 for (auto const &dir_entry : fs::recursive_directory_iterator(start_path)) {
105 if (isValidAudioFile(dir_entry)) count++;
106 }
107 return count;
108 }
109
111 void setVFS(VFS &vfs){
112 p_vfs = &vfs;
113 }
114
115 protected:
116 VFSFile file;
117 size_t idx_pos = 0;
118 const char *file_name;
119 const char *exension = "";
120 const char *start_path = "/";
121 const char *file_name_pattern = "*";
122 fs::directory_entry entry;
123 VFS *p_vfs = nullptr;
124
125 const char *get(int idx) {
126 int count = 0;
127 const char *result = nullptr;
128 for (auto const &dir_entry : fs::recursive_directory_iterator(start_path)) {
129 if (isValidAudioFile(dir_entry)) {
130 if (count++ == idx) {
131 entry = dir_entry;
132 result = entry.path().c_str();
133 break;
134 }
135 }
136 }
137 return result;
138 }
139
141 bool isValidAudioFile(fs::directory_entry file) {
142 const std::filesystem::path &path = file.path();
143
144 const char *file_name = path.filename().c_str();
145 if (file.is_directory()) {
146 LOGD("-> isValidAudioFile: '%s': %d", file_name, false);
147 return false;
148 }
149 StrView strFileTName(file_name);
150 bool result = strFileTName.endsWithIgnoreCase(exension) &&
151 strFileTName.matches(file_name_pattern);
152 LOGD("-> isValidAudioFile: '%s': %d", file_name, result);
153 return result;
154 }
155};
156
157} // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:16
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:89
long size()
Definition AudioSourceVFS.h:102
virtual Stream * selectStream(int index) override
Definition AudioSourceVFS.h:60
AudioSourceVFS(VFS &vfs, const char *startFilePath="/", const char *ext="")
Default constructor with VFS.
Definition AudioSourceVFS.h:25
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceVFS.h:44
const char * toStr()
provides the actual file name
Definition AudioSourceVFS.h:92
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceVFS.h:98
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:95
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceVFS.h:55
void setFileFilter(const char *filter)
Definition AudioSourceVFS.h:86
bool isValidAudioFile(fs::directory_entry file)
checks if the file is a valid audio file
Definition AudioSourceVFS.h:141
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceVFS.h:71
void setVFS(VFS &vfs)
Assign the VFS: to be used before calling begin.
Definition AudioSourceVFS.h:111
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
Base class which uses c++ file functions. It is also used as base class for an ESP32 Virtual File Sys...
Definition VFS.h:15
virtual bool begin()
mount the file systems
Definition VFS.h:18
virtual void end()
unmount the file system
Definition VFS.h:20
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10