Skip to content

Commit 647b966

Browse files
committed
Create FATFS FileSource for ESP8266Audio
1 parent 24b28ab commit 647b966

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

AudioFileSourceFatFs.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
AudioFileSourceSPIFFS
3+
Input SD card "file" to be used by AudioGenerator
4+
5+
Copyright (C) 2017 Earle F. Philhower, III
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#include "AudioFileSourceFatFs.h"
22+
23+
AudioFileSourceFatFs::AudioFileSourceFatFs()
24+
{
25+
_isOpen = false;
26+
}
27+
28+
AudioFileSourceFatFs::AudioFileSourceFatFs(const char *filename)
29+
{
30+
_isOpen = false;
31+
open(filename);
32+
}
33+
34+
bool AudioFileSourceFatFs::open(const char *filename)
35+
{
36+
if (_isOpen) file.close();
37+
_isOpen = file.open((char*)filename, FA_OPEN_EXISTING | FA_READ);
38+
return _isOpen;
39+
}
40+
41+
AudioFileSourceFatFs::~AudioFileSourceFatFs()
42+
{
43+
if (_isOpen) file.close();
44+
}
45+
46+
uint32_t AudioFileSourceFatFs::read(void *data, uint32_t len)
47+
{
48+
return file.read(data, len);
49+
}
50+
51+
bool AudioFileSourceFatFs::seek(int32_t pos, int dir)
52+
{
53+
if (!_isOpen) return false;
54+
if (dir==SEEK_SET) return file.seekSet(pos);
55+
else if (dir==SEEK_CUR) return file.seekSet(file.curPosition() + pos);
56+
else if (dir==SEEK_END) return file.seekSet(file.fileSize() + pos);
57+
return false;
58+
}
59+
60+
bool AudioFileSourceFatFs::close()
61+
{
62+
_isOpen = false;
63+
return file.close();
64+
}
65+
66+
bool AudioFileSourceFatFs::isOpen()
67+
{
68+
return _isOpen;
69+
}
70+
71+
uint32_t AudioFileSourceFatFs::getSize()
72+
{
73+
if (!_isOpen) return 0;
74+
return file.fileSize();
75+
}
76+
77+
uint32_t AudioFileSourceFatFs::getPos()
78+
{
79+
if (!_isOpen) return 0;
80+
return file.curPosition();
81+
}

AudioFileSourceFatFs.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
AudioFileSourceSPIFFS
3+
Input SD card "file" to be used by AudioGenerator
4+
5+
Copyright (C) 2017 Earle F. Philhower, III
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef _AUDIOFILESOURCEFATFS_H
22+
#define _AUDIOFILESOURCEFATFS_H
23+
24+
#include "AudioFileSource.h"
25+
#include <FatFs.h>
26+
27+
class AudioFileSourceFatFs : public AudioFileSource
28+
{
29+
public:
30+
AudioFileSourceFatFs();
31+
AudioFileSourceFatFs(const char *filename);
32+
virtual ~AudioFileSourceFatFs() override;
33+
34+
virtual bool open(const char *filename) override;
35+
virtual uint32_t read(void *data, uint32_t len) override;
36+
virtual bool seek(int32_t pos, int dir) override;
37+
virtual bool close() override;
38+
virtual bool isOpen() override;
39+
virtual uint32_t getSize() override;
40+
virtual uint32_t getPos() override;
41+
42+
private:
43+
FileFs file;
44+
bool _isOpen;
45+
};
46+
47+
48+
#endif
49+

0 commit comments

Comments
 (0)