arduino-audio-tools
Loading...
Searching...
No Matches
FileLoop.h
Go to the documentation of this file.
1#pragma once
2
5// IS_DESKTOP (PlatformConfig/desktop.h) #defines ARDUINO for compatibility
6// even though it has no FS.h - File there comes from SD.h instead (already
7// included by whoever uses File/FileLoop on desktop), but its readBytes()
8// still wants char* like every other Arduino-style File, so READTYPE
9// stays char for it too.
10#if defined(ARDUINO) && !defined(IS_DESKTOP)
11# include "FS.h"
12#endif
13#if defined(ARDUINO)
14# define READTYPE char
15#else
16# define READTYPE uint8_t
17#endif
18
19namespace audio_tools {
20
32template <class FileType> class FileLoopT : public BaseStream {
33public:
34 FileLoopT() = default;
35 FileLoopT(FileType file, int count = -1, int rewindPos = -1, int startPos = 0) {
37 setLoopCount(count);
38 setRewindPos(rewindPos);
39 setStartPos(startPos);
40 }
41
42 // restarts the file from the beginning
43 bool begin() {
44 TRACEI();
45 // automatic determination of rewind pos
46 if (rewind_pos <= 0){
47 current_file.seek(0);
48 char tmp[5] = {0};
49 current_file.readBytes(tmp, 4);
50 // for wav files remove header
51 rewind_pos = StrView(tmp).equals("RIFF") ? 44 : 0;
52 }
55 return current_file;
56 }
57
58 // closes the file
59 void end() {
60 TRACEI();
61 current_file.close();
62 }
63
65 void setFile(FileType file) { this->current_file = file; }
66
68 FileType &file(){
69 return current_file;
70 }
71
73 void setRewindPos(int pos) { rewind_pos = pos; }
74
76 void setStartPos(int pos) { start_pos = pos; }
77
79 void setSize(size_t len) {
80 total_size = len;
81 }
82
84 size_t size() {
85 return total_size == -1 ? current_file.size() : total_size;
86 }
87
89 void setCallback(void (*cb)(FileLoopT &loop)){
90 callback = cb;
91 }
92
95 void setLoopCount(int count) { loop_count = count; }
96
97 int available() override {
98 // if we manage the size, we return the open amount
99 if (total_size!=-1) return size_open;
100 // otherwise we return DEFAULT_BUFFER_SIZE if looping is active
101 return isLoopActive() ? DEFAULT_BUFFER_SIZE : current_file.available();
102 }
103
104 size_t readBytes(uint8_t *data, size_t len) override {
105 LOGD("FileLoopT::readBytes %d at %d", (int)len, (int)current_file.position());
106 if (!current_file)
107 return 0;
108
109 // limit the copy size if necessary
110 int copy_len = len;
111 if (total_size!=-1){
112 copy_len = min((int)len, size_open);
113 }
114
115 // read step 1;
116 int result1 = current_file.readBytes((READTYPE *)data, copy_len);
117 int result2 = 0;
118 int open = copy_len - result1;
119 if (isLoopActive() && open > 0) {
120 if (rewind_pos < 0) rewind_pos = 0;
121 LOGI("seek %d", rewind_pos);
122 // looping logic -> rewind to beginning: read step 2
124 // notify user
125 if (callback!=nullptr){
126 callback(*this);
127 }
128 result1 = current_file.readBytes((READTYPE*)data + result1, open);
129 if (loop_count>0)
130 loop_count--;
131 }
132 // determine the result size
133 int result = result1 + result2;
134 // calculate the size_open if necessary
135 if (total_size!=-1){
136 size_open -= result;
137 }
138 return result;
139 }
140
141 // Returns the bool of the current file
142 operator bool() {
143 return current_file;
144 }
145
147 bool isLoopActive() { return loop_count > 0 || loop_count == -1; }
148
149 size_t write(const uint8_t* data, size_t len) { return current_file.write(data, len);}
150
151protected:
152 int rewind_pos = -1;
153 int start_pos = 0;
154 int loop_count = -1;
155 int size_open = -1;
156 int total_size = -1;
157 void (*callback)(FileLoopT &loop) = nullptr;
158 FileType current_file;
159};
160
172class FileLoop : public FileLoopT<File> {
173public:
174 FileLoop() = default;
175 FileLoop(File file, int count = -1, int rewindPos = 0)
176 : FileLoopT<File>(file, count, rewindPos) {}
177};
178
179} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define READTYPE
Definition FileLoop.h:16
void loop()
#define DEFAULT_BUFFER_SIZE
Definition avr.h:20
Base class for all Streams. It relies on write(const uint8_t *buffer, size_t size) and readBytes(uint...
Definition BaseStream.h:33
A simple class which implements a automatic looping file. The file needs to be of the class File from...
Definition FileLoop.h:172
FileLoop(File file, int count=-1, int rewindPos=0)
Definition FileLoop.h:175
A simple class which implements a automatic looping file. In order to support different file implemen...
Definition FileLoop.h:32
size_t size()
Returns the (requested) file size.
Definition FileLoop.h:84
void setLoopCount(int count)
Definition FileLoop.h:95
int total_size
Definition FileLoop.h:156
void setRewindPos(int pos)
defines the start position after the rewind. E.g. for wav files this should be 44
Definition FileLoop.h:73
int loop_count
Definition FileLoop.h:154
void setFile(FileType file)
defines the file that is used for looping
Definition FileLoop.h:65
void setStartPos(int pos)
defines the start position at the beginning
Definition FileLoop.h:76
FileType & file()
Returns the file.
Definition FileLoop.h:68
void setCallback(void(*cb)(FileLoopT &loop))
You can be notified about a rewind.
Definition FileLoop.h:89
size_t readBytes(uint8_t *data, size_t len) override
Definition FileLoop.h:104
bool begin()
Definition FileLoop.h:43
bool isLoopActive()
Definition FileLoop.h:147
int available() override
Definition FileLoop.h:97
FileLoopT(FileType file, int count=-1, int rewindPos=-1, int startPos=0)
Definition FileLoop.h:35
void setSize(size_t len)
optionally defines the requested playing size in bytes
Definition FileLoop.h:79
int start_pos
Definition FileLoop.h:153
void end()
Definition FileLoop.h:59
int rewind_pos
Definition FileLoop.h:152
FileType current_file
Definition FileLoop.h:158
void(* callback)(FileLoopT &loop)
Definition FileLoop.h:157
size_t write(const uint8_t *data, size_t len)
Definition FileLoop.h:149
int size_open
Definition FileLoop.h:155
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:29
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition StrView.h:197
Arduino File API for Zephyr.
Definition ZephyrFile.h:23
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6