arduino-audio-tools
Loading...
Searching...
No Matches
FileLoop.h
Go to the documentation of this file.
1#pragma once
2
5#ifdef ARDUINO
6# include "FS.h"
7# define READTYPE char
8#else
9# define READTYPE uint8_t
10#endif
11
12namespace audio_tools {
13
25template <class FileType> class FileLoopT : public BaseStream {
26public:
27 FileLoopT() = default;
28 FileLoopT(FileType file, int count = -1, int rewindPos = -1, int startPos = 0) {
30 setLoopCount(count);
31 setRewindPos(rewindPos);
32 setStartPos(startPos);
33 }
34
35 // restarts the file from the beginning
36 bool begin() {
37 TRACEI();
38 // automatic determination of rewind pos
39 if (rewind_pos <= 0){
40 current_file.seek(0);
41 char tmp[5] = {0};
42 current_file.readBytes(tmp, 4);
43 // for wav files remove header
44 rewind_pos = StrView(tmp).equals("RIFF") ? 44 : 0;
45 }
48 return current_file;
49 }
50
51 // closes the file
52 void end() {
53 TRACEI();
54 current_file.close();
55 }
56
58 void setFile(FileType file) { this->current_file = file; }
59
61 FileType &file(){
62 return current_file;
63 }
64
66 void setRewindPos(int pos) { rewind_pos = pos; }
67
69 void setStartPos(int pos) { start_pos = pos; }
70
72 void setSize(size_t len) {
73 total_size = len;
74 }
75
77 size_t size() {
78 return total_size == -1 ? current_file.size() : total_size;
79 }
80
82 void setCallback(void (*cb)(FileLoopT &loop)){
83 callback = cb;
84 }
85
88 void setLoopCount(int count) { loop_count = count; }
89
90 int available() override {
91 // if we manage the size, we return the open amount
92 if (total_size!=-1) return size_open;
93 // otherwise we return DEFAULT_BUFFER_SIZE if looping is active
94 return isLoopActive() ? DEFAULT_BUFFER_SIZE : current_file.available();
95 }
96
97 size_t readBytes(uint8_t *data, size_t len) override {
98 LOGD("FileLoopT::readBytes %d at %d", (int)len, (int)current_file.position());
99 if (!current_file)
100 return 0;
101
102 // limit the copy size if necessary
103 int copy_len = len;
104 if (total_size!=-1){
105 copy_len = min((int)len, size_open);
106 }
107
108 // read step 1;
109 int result1 = current_file.readBytes((READTYPE *)data, copy_len);
110 int result2 = 0;
111 int open = copy_len - result1;
112 if (isLoopActive() && open > 0) {
113 if (rewind_pos < 0) rewind_pos = 0;
114 LOGI("seek %d", rewind_pos);
115 // looping logic -> rewind to beginning: read step 2
117 // notify user
118 if (callback!=nullptr){
119 callback(*this);
120 }
121 result1 = current_file.readBytes((READTYPE*)data + result1, open);
122 if (loop_count>0)
123 loop_count--;
124 }
125 // determine the result size
126 int result = result1 + result2;
127 // calculate the size_open if necessary
128 if (total_size!=-1){
129 size_open -= result;
130 }
131 return result;
132 }
133
134 // Returns the bool of the current file
135 operator bool() {
136 return current_file;
137 }
138
140 bool isLoopActive() { return loop_count > 0 || loop_count == -1; }
141
142 size_t write(const uint8_t* data, size_t len) { return current_file.write(data, len);}
143
144protected:
145 int rewind_pos = -1;
146 int start_pos = 0;
147 int loop_count = -1;
148 int size_open = -1;
149 int total_size = -1;
150 void (*callback)(FileLoopT &loop) = nullptr;
151 FileType current_file;
152};
153
165class FileLoop : public FileLoopT<File> {
166public:
167 FileLoop() = default;
168 FileLoop(File file, int count = -1, int rewindPos = 0)
169 : FileLoopT<File>(file, count, rewindPos) {}
170};
171
172} // 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:9
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:165
FileLoop(File file, int count=-1, int rewindPos=0)
Definition FileLoop.h:168
A simple class which implements a automatic looping file. In order to support different file implemen...
Definition FileLoop.h:25
size_t size()
Returns the (requested) file size.
Definition FileLoop.h:77
void setLoopCount(int count)
Definition FileLoop.h:88
int total_size
Definition FileLoop.h:149
void setRewindPos(int pos)
defines the start position after the rewind. E.g. for wav files this should be 44
Definition FileLoop.h:66
int loop_count
Definition FileLoop.h:147
void setFile(FileType file)
defines the file that is used for looping
Definition FileLoop.h:58
void setStartPos(int pos)
defines the start position at the beginning
Definition FileLoop.h:69
FileType & file()
Returns the file.
Definition FileLoop.h:61
void setCallback(void(*cb)(FileLoopT &loop))
You can be notified about a rewind.
Definition FileLoop.h:82
size_t readBytes(uint8_t *data, size_t len) override
Definition FileLoop.h:97
bool begin()
Definition FileLoop.h:36
bool isLoopActive()
Definition FileLoop.h:140
int available() override
Definition FileLoop.h:90
FileLoopT(FileType file, int count=-1, int rewindPos=-1, int startPos=0)
Definition FileLoop.h:28
void setSize(size_t len)
optionally defines the requested playing size in bytes
Definition FileLoop.h:72
int start_pos
Definition FileLoop.h:146
void end()
Definition FileLoop.h:52
int rewind_pos
Definition FileLoop.h:145
FileType current_file
Definition FileLoop.h:151
void(* callback)(FileLoopT &loop)
Definition FileLoop.h:150
size_t write(const uint8_t *data, size_t len)
Definition FileLoop.h:142
int size_open
Definition FileLoop.h:148
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition StrView.h:165
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