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