arduino-audio-tools
Loading...
Searching...
No Matches
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 bool begin() override {
29 TRACED();
30 idx_pos = 0;
31 return true;
32 }
33
34 virtual void end() {
35 }
36
37 virtual Stream *nextStream(int offset = 1) override {
38 LOGI("nextStream: %d", offset);
39 Stream* s = selectStream(idx_pos + offset);
40 if (s == nullptr && offset > 0) {
41 LOGI("Wrapping to start of directory");
42 idx_pos = 0;
43 s = selectStream(idx_pos);
44 }
45 return s;
46 }
47
48 virtual Stream *selectStream(int index) override {
49 // Determine total mp3 file count to normalize index
50 long count = size();
51 LOGI("selectStream: %d of %d", index, (int) count);
52 if (count <= 0) {
53 LOGW("No audio files found under: %s", start_path ? start_path : "<null>");
54 return nullptr;
55 }
56 int norm = index % count;
57 if (norm < 0) norm += count;
58 idx_pos = norm;
59 file_name = get(norm);
60 if (file_name==nullptr) {
61 LOGW("Could not resolve index %d (normalized %d)", index, norm);
62 return nullptr;
63 }
64 LOGI("Using file %s", file_name);
65 file.close();
66 file = SD.open(file_name);
67 return file ? &file : nullptr;
68 }
69
70 virtual Stream *selectStream(const char *path) override {
71 file.close();
72 file = SD.open(path);
73 file_name = file.name();
74 LOGI("-> selectStream: %s", path);
75 return file ? &file : nullptr;
76 }
77
80 void setFileFilter(const char *filter) { file_name_pattern = filter; }
81
83 int index() { return idx_pos; }
84
86 const char *toStr() { return file_name; }
87
88 // provides default setting go to the next
89 virtual bool isAutoNext() { return true; }
90
92 virtual void setPath(const char *p) { start_path = p; }
93
95 long size() {
96 if (count == 0){
97 for (auto const& dir_entry : fs::recursive_directory_iterator(start_path)){
98 if (isValidAudioFile(dir_entry))
99 count++;
100 }
101 }
102 return count;
103 }
104
105protected:
106 File file;
107 size_t idx_pos = 0;
108 const char *file_name;
109 std::string current_path; // holds persistent path string
110 const char *exension = "";
111 const char *start_path = nullptr;
112 const char *file_name_pattern = "*";
113 fs::directory_entry entry;
114 long count = 0;
115
116 const char* get(int idx){
117 int count = 0;
118 const char* result = nullptr;
119 for (auto const& dir_entry : fs::recursive_directory_iterator(start_path)){
120 if (isValidAudioFile(dir_entry)){
121 if (count++==idx){
122 entry = dir_entry;
123 current_path = entry.path().string();
124 result = current_path.c_str();
125 break;
126 }
127 }
128 }
129 return result;
130 }
131
133 bool isValidAudioFile(fs::directory_entry file) {
134 const std::filesystem::path path = file.path();
135 const std::string filename = path.filename().string();
136 const char *file_name_c = filename.c_str();
137 if (file.is_directory()) {
138 LOGD("-> isValidAudioFile: '%s': %d", file_name_c, false);
139 return false;
140 }
141 StrView strFileTName(file_name_c);
142 bool result = strFileTName.endsWithIgnoreCase(exension)
143 && strFileTName.matches(file_name_pattern);
144 LOGD("-> isValidAudioFile: '%s': %d", file_name_c, result);
145 return result;
146 }
147
148};
149
150} // 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.
Definition AudioSourceSTD.h:19
int index()
Provides the current index position.
Definition AudioSourceSTD.h:83
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:95
AudioSourceSTD(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition AudioSourceSTD.h:22
virtual Stream * selectStream(int index) override
Definition AudioSourceSTD.h:48
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSTD.h:28
const char * toStr()
provides the actual file name
Definition AudioSourceSTD.h:86
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSTD.h:92
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSTD.h:89
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSTD.h:37
void setFileFilter(const char *filter)
Definition AudioSourceSTD.h:80
bool isValidAudioFile(fs::directory_entry file)
checks if the file is a valid audio file
Definition AudioSourceSTD.h:133
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSTD.h:70
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