arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSourceSTD.h
1#pragma once
2
3#include "AudioLogger.h"
4#include "AudioTools/Disk/AudioSource.h"
5#include "AudioTools/AudioLibs/Desktop/File.h"
6#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7#include <filesystem>
8
9namespace audio_tools {
10
11namespace fs = std::filesystem;
12
20public:
22 AudioSourceSTD(const char *startFilePath = "/", const char *ext = ".mp3") {
23 start_path = startFilePath;
24 exension = ext;
25 timeout_auto_next_value = 600000;
26 }
27
28 virtual void begin() override {
29 TRACED();
30 idx_pos = 0;
31 }
32
33 virtual void end() {
34 }
35
36 virtual Stream *nextStream(int offset = 1) override {
37 LOGI("nextStream: %d", offset);
38 return selectStream(idx_pos + offset);
39 }
40
41 virtual Stream *selectStream(int index) override {
42 LOGI("selectStream: %d", index);
43 idx_pos = index;
44 file_name = get(index);
45 if (file_name==nullptr) return nullptr;
46 LOGI("Using file %s", file_name);
47 file = SD.open(file_name);
48 return file ? &file : nullptr;
49 }
50
51 virtual Stream *selectStream(const char *path) override {
52 file.close();
53 file = SD.open(path);
54 file_name = file.name();
55 LOGI("-> selectStream: %s", path);
56 return file ? &file : nullptr;
57 }
58
61 void setFileFilter(const char *filter) { file_name_pattern = filter; }
62
64 int index() { return idx_pos; }
65
67 const char *toStr() { return file_name; }
68
69 // provides default setting go to the next
70 virtual bool isAutoNext() { return true; }
71
73 virtual void setPath(const char *p) { start_path = p; }
74
76 long size() {
77 long count = 0;
78 for (auto const& dir_entry : fs::recursive_directory_iterator(start_path)){
79 if (isValidAudioFile(dir_entry))
80 count++;
81 }
82 return count;
83 }
84
85protected:
86 File file;
87 size_t idx_pos = 0;
88 const char *file_name;
89 const char *exension = "";
90 const char *start_path = nullptr;
91 const char *file_name_pattern = "*";
92 fs::directory_entry entry;
93
94 const char* get(int idx){
95 int count = 0;
96 const char* result = nullptr;
97 for (auto const& dir_entry : fs::recursive_directory_iterator(start_path)){
98 if (isValidAudioFile(dir_entry)){
99 if (count++==idx){
100 entry = dir_entry;
101 result = entry.path().c_str();
102 break;
103 }
104 }
105 }
106 return result;
107 }
108
110 bool isValidAudioFile(fs::directory_entry file) {
111 const std::filesystem::path& path = file.path();
112
113 const char *file_name = path.filename().c_str();
114 if (file.is_directory()) {
115 LOGD("-> isValidAudioFile: '%s': %d", file_name, false);
116 return false;
117 }
118 StrView strFileTName(file_name);
119 bool result = strFileTName.endsWithIgnoreCase(exension)
120 && strFileTName.matches(file_name_pattern);
121 LOGD("-> isValidAudioFile: '%s': %d", file_name, result);
122 return result;
123 }
124
125};
126
127} // 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.
Definition AudioSourceSTD.h:19
int index()
Provides the current index position.
Definition AudioSourceSTD.h:64
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition AudioSourceSTD.h:76
AudioSourceSTD(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition AudioSourceSTD.h:22
virtual Stream * selectStream(int index) override
Definition AudioSourceSTD.h:41
virtual void begin() override
Reset actual stream and move to root.
Definition AudioSourceSTD.h:28
const char * toStr()
provides the actual file name
Definition AudioSourceSTD.h:67
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSTD.h:73
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSTD.h:70
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSTD.h:36
void setFileFilter(const char *filter)
Definition AudioSourceSTD.h:61
bool isValidAudioFile(fs::directory_entry file)
checks if the file is a valid audio file
Definition AudioSourceSTD.h:110
virtual Stream * selectStream(const char *path) override
Returns audio stream by path.
Definition AudioSourceSTD.h:51
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
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10