arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceSTD.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioLogger.h"
7#include <filesystem>
8
9namespace audio_tools {
10
11namespace fs = std::filesystem;
12
23public:
25 AudioSourceSTD(const char *startFilePath = "/", const char *ext = ".mp3") {
27 exension = ext;
29 }
30
31 virtual ~AudioSourceSTD() {
32 end();
33 }
34
35 virtual bool begin() override {
36 TRACED();
37 idx_pos = 0;
38 return true;
39 }
40
41 virtual void end() {
42 file.close();
43 }
44
45 virtual Stream *nextStream(int offset = 1) override {
46 LOGI("nextStream: %d", offset);
47 Stream* s = selectStream(idx_pos + offset);
48 if (s == nullptr && offset > 0) {
49 LOGI("Wrapping to start of directory");
50 idx_pos = 0;
52 }
53 return s;
54 }
55
56 virtual Stream *selectStream(int index) override {
57 // Determine total mp3 file count to normalize index
58 long count = size();
59 LOGI("selectStream: %d of %d", index, (int) count);
60 if (count <= 0) {
61 LOGW("No audio files found under: %s", start_path ? start_path : "<null>");
62 return nullptr;
63 }
64 int norm = index % count;
65 if (norm < 0) norm += count;
66 idx_pos = norm;
68 if (file_name==nullptr) {
69 LOGW("Could not resolve index %d (normalized %d)", index, norm);
70 return nullptr;
71 }
72 LOGI("Using file %s", file_name);
73 file.close();
75 return file ? &file : nullptr;
76 }
77
78 virtual Stream *selectStream(const char *path) override {
79 file.close();
80 file = SD.open(path);
82 LOGI("-> selectStream: %s", path);
83 return file ? &file : nullptr;
84 }
85
88 void setFileFilter(const char *filter) { file_name_pattern = filter; }
89
91 int index() { return idx_pos; }
92
94 const char *toStr() { return file_name; }
95
96 // provides default setting go to the next
97 virtual bool isAutoNext() { return true; }
98
100 virtual void setPath(const char *p) { start_path = p; }
101
103 long size() {
104 if (count == 0){
105 for (auto const& dir_entry : fs::recursive_directory_iterator(start_path)){
107 count++;
108 }
109 }
110 return count;
111 }
112
113protected:
115 size_t idx_pos = 0;
116 const char *file_name;
117 std::string current_path; // holds persistent path string
118 const char *exension = "";
119 const char *start_path = nullptr;
120 const char *file_name_pattern = "*";
121 fs::directory_entry entry;
122 long count = 0;
123
124 const char* get(int idx){
125 int count = 0;
126 const char* result = nullptr;
127 for (auto const& dir_entry : fs::recursive_directory_iterator(start_path)){
129 if (count++==idx){
131 current_path = entry.path().string();
132 result = current_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 const std::string filename = path.filename().string();
144 const char *file_name_c = filename.c_str();
145 if (file.is_directory()) {
146 LOGD("-> isValidAudioFile: '%s': %d", file_name_c, false);
147 return false;
148 }
150 bool result = strFileTName.endsWithIgnoreCase(exension)
152 LOGD("-> isValidAudioFile: '%s': %d", file_name_c, result);
153 return result;
154 }
155
156};
157
158} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
Definition Arduino.h:136
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:17
int timeout_auto_next_value
Definition AudioSource.h:76
AudioSource using the standard C++ api.
Definition AudioSourceSTD.h:22
int index()
Provides the current index position.
Definition AudioSourceSTD.h:91
const char * get(int idx)
Definition AudioSourceSTD.h:124
const char * exension
Definition AudioSourceSTD.h:118
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:103
AudioSourceSTD(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition AudioSourceSTD.h:25
virtual ~AudioSourceSTD()
Definition AudioSourceSTD.h:31
virtual Stream * selectStream(int index) override
Definition AudioSourceSTD.h:56
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSTD.h:35
fs::directory_entry entry
Definition AudioSourceSTD.h:121
File file
Definition AudioSourceSTD.h:114
long count
Definition AudioSourceSTD.h:122
size_t idx_pos
Definition AudioSourceSTD.h:115
const char * start_path
Definition AudioSourceSTD.h:119
const char * toStr()
provides the actual file name
Definition AudioSourceSTD.h:94
const char * file_name
Definition AudioSourceSTD.h:116
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSTD.h:100
std::string current_path
Definition AudioSourceSTD.h:117
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSTD.h:97
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSTD.h:45
void setFileFilter(const char *filter)
Definition AudioSourceSTD.h:88
virtual void end()
Definition AudioSourceSTD.h:41
const char * file_name_pattern
Definition AudioSourceSTD.h:120
bool isValidAudioFile(fs::directory_entry file)
checks if the file is a valid audio file
Definition AudioSourceSTD.h:141
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSTD.h:78
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
Arduino File API for Zephyr.
Definition ZephyrFile.h:23
void close()
Definition ZephyrFile.h:67
const char * name() const
Definition ZephyrFile.h:200
ZephyrFile open(const char *path, const char *mode="r", bool create=false)
Definition ZephyrSD.h:51
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
static ZephyrSDClass SD
Access to SD object.
Definition ZephyrSD.h:215
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508