Skip to content

Commit 80acc8d

Browse files
committed
FileLoop: distinguish between start and rewind pos
1 parent 3859a9a commit 80acc8d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/AudioTools/Disk/FileLoop.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,25 @@ namespace audio_tools {
2424
template <class FileType> class FileLoopT : public BaseStream {
2525
public:
2626
FileLoopT() = default;
27-
FileLoopT(FileType file, int count = -1, int rewindPos = -1) {
27+
FileLoopT(FileType file, int count = -1, int rewindPos = -1, int startPos = 0) {
2828
setFile(file);
2929
setLoopCount(count);
30-
setStartPos(rewindPos);
30+
setRewindPos(rewindPos);
31+
setStartPos(startPos);
3132
}
3233

3334
// restarts the file from the beginning
3435
bool begin() {
3536
TRACEI();
36-
// automatic determination of start pos
37-
if (start_pos <= 0){
37+
// automatic determination of rewind pos
38+
if (rewind_pos <= 0){
3839
current_file.seek(0);
3940
char tmp[5] = {0};
4041
current_file.readBytes(tmp, 4);
4142
// for wav files remove header
42-
start_pos = StrView(tmp).equals("RIFF") ? 44 : 0;
43-
current_file.seek(0);
44-
} else {
45-
current_file.seek(start_pos);
43+
rewind_pos = StrView(tmp).equals("RIFF") ? 44 : 0;
4644
}
45+
current_file.seek(start_pos);
4746
size_open = total_size;
4847
return current_file;
4948
}
@@ -63,6 +62,9 @@ template <class FileType> class FileLoopT : public BaseStream {
6362
}
6463

6564
/// defines the start position after the rewind. E.g. for wav files this should be 44
65+
void setRewindPos(int pos) { rewind_pos = pos; }
66+
67+
/// defines the start position at the beginning
6668
void setStartPos(int pos) { start_pos = pos; }
6769

6870
/// optionally defines the requested playing size in bytes
@@ -107,10 +109,10 @@ template <class FileType> class FileLoopT : public BaseStream {
107109
int result2 = 0;
108110
int open = copy_len - result1;
109111
if (isLoopActive() && open > 0) {
110-
if (start_pos < 0) start_pos = 0;
111-
LOGI("seek %d", start_pos);
112+
if (rewind_pos < 0) rewind_pos = 0;
113+
LOGI("seek %d", rewind_pos);
112114
// looping logic -> rewind to beginning: read step 2
113-
current_file.seek(start_pos);
115+
current_file.seek(rewind_pos);
114116
// notify user
115117
if (callback!=nullptr){
116118
callback(*this);
@@ -139,7 +141,8 @@ template <class FileType> class FileLoopT : public BaseStream {
139141
size_t write(const uint8_t* data, size_t len) { return current_file.write(data, len);}
140142

141143
protected:
142-
int start_pos = -1;
144+
int rewind_pos = -1;
145+
int start_pos = 0;
143146
int loop_count = -1;
144147
int size_open = -1;
145148
int total_size = -1;

0 commit comments

Comments
 (0)